487cbed043cbc13f452bfa4e43d76b0bc3f533f1
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2014
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 /* Example program using the lowlevel raw interface.
19 * This allow accurate control of the exact commands that are being used.
26 #include "win32_compat.h"
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
41 #include "libnfs-zdr.h"
43 #include "libnfs-raw.h"
44 #include "libnfs-raw-mount.h"
45 #include "libnfs-raw-nfs.h"
46 #include "libnfs-raw-portmap.h"
47 #include "libnfs-raw-rquota.h"
53 void pmap2_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
55 struct client
*client
= private_data
;
56 struct pmap2_dump_result
*dr
= data
;
57 struct pmap2_mapping_list
*list
= dr
->list
;
59 if (status
== RPC_STATUS_ERROR
) {
60 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data
);
63 if (status
!= RPC_STATUS_SUCCESS
) {
64 printf("PORTMAP2/DUMP call failed, status:%d\n", status
);
68 printf("PORTMAP2/DUMP:\n");
70 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
77 client
->is_finished
= 1;
80 void pmap3_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
82 struct client
*client
= private_data
;
83 struct pmap3_dump_result
*dr
= data
;
84 struct pmap3_mapping_list
*list
= dr
->list
;
86 if (status
== RPC_STATUS_ERROR
) {
87 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data
);
90 if (status
!= RPC_STATUS_SUCCESS
) {
91 printf("PORTMAP3/DUMP call failed, status:%d\n", status
);
95 printf("PORTMAP3/DUMP:\n");
97 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
105 client
->is_finished
= 1;
108 void pmap3_getaddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
110 struct client
*client
= private_data
;
111 struct pmap3_string_result
*gar
= data
;
113 if (status
== RPC_STATUS_ERROR
) {
114 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data
);
117 if (status
!= RPC_STATUS_SUCCESS
) {
118 printf("PORTMAP3/GETADDR call failed, status:%d\n", status
);
122 printf("PORTMAP3/GETADDR:\n");
123 printf(" Addr:%s\n", gar
->addr
);
125 client
->is_finished
= 1;
128 void pmap3_gettime_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
130 struct client
*client
= private_data
;
131 time_t t
= *(uint32_t *)data
;
133 if (status
== RPC_STATUS_ERROR
) {
134 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data
);
137 if (status
!= RPC_STATUS_SUCCESS
) {
138 printf("PORTMAP3/GETTIME call failed, status:%d\n", status
);
142 printf("PORTMAP3/GETTIME:\n");
143 printf(" Time:%d %s\n", (int)t
, ctime(&t
));
145 client
->is_finished
= 1;
148 void pmap3_uaddr2taddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
150 struct client
*client
= private_data
;
151 struct pmap3_netbuf
*nb
= data
;
154 if (status
== RPC_STATUS_ERROR
) {
155 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data
);
158 if (status
!= RPC_STATUS_SUCCESS
) {
159 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status
);
163 printf("PORTMAP3/UADDR2TADDR:\n");
164 printf(" MaxLen:%d\n", nb
->maxlen
);
166 for (i
= 0; i
< nb
->maxlen
; i
++) {
167 printf("%02x ", nb
->buf
.buf_val
[i
]);
173 client
->is_finished
= 1;
176 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
178 struct client
*client
= private_data
;
180 if (status
== RPC_STATUS_ERROR
) {
181 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
184 if (status
!= RPC_STATUS_SUCCESS
) {
185 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
189 printf("PORTMAP2/NULL responded and server is alive\n");
190 client
->is_finished
= 1;
193 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
195 struct client
*client
= private_data
;
197 if (status
== RPC_STATUS_ERROR
) {
198 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
201 if (status
!= RPC_STATUS_SUCCESS
) {
202 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
206 printf("PORTMAP3/NULL responded and server is alive\n");
207 client
->is_finished
= 1;
210 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
212 struct client
*client
= private_data
;
214 if (status
== RPC_STATUS_ERROR
) {
215 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
218 if (status
!= RPC_STATUS_SUCCESS
) {
219 printf("PORTMAP/NULL call failed, status:%d\n", status
);
223 client
->is_finished
= 1;
226 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
228 struct client
*client
= private_data
;
230 if (status
!= RPC_STATUS_SUCCESS
) {
231 printf("connection to portmapper failed\n");
235 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
236 printf("Failed to send null request\n");
242 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
246 client
->is_finished
= 0;
248 pfd
.fd
= rpc_get_fd(rpc
);
249 pfd
.events
= rpc_which_events(rpc
);
251 if (poll(&pfd
, 1, -1) < 0) {
252 printf("Poll failed");
255 if (rpc_service(rpc
, pfd
.revents
) < 0) {
256 printf("rpc_service failed\n");
259 if (client
->is_finished
) {
265 int main(int argc _U_
, char *argv
[] _U_
)
267 struct rpc_context
*rpc
;
268 struct client client
;
278 int command_found
= 0;
280 int getaddr3prog
, getaddr3vers
;
284 rpc
= rpc_init_context();
286 printf("failed to init context\n");
290 for (i
= 1; i
< argc
; i
++) {
291 if (!strcmp(argv
[i
], "dump2")) {
294 } else if (!strcmp(argv
[i
], "null2")) {
297 } else if (!strcmp(argv
[i
], "dump3")) {
300 } else if (!strcmp(argv
[i
], "gettime3")) {
303 } else if (!strcmp(argv
[i
], "u2t3")) {
305 u2t3string
= argv
[++i
];
307 } else if (!strcmp(argv
[i
], "getaddr3")) {
309 getaddr3prog
= atoi(argv
[++i
]);
310 getaddr3vers
= atoi(argv
[++i
]);
311 getaddr3netid
= argv
[++i
];
313 } else if (!strcmp(argv
[i
], "null3")) {
320 if (command_found
== 0 || server
== NULL
) {
321 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
325 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
326 printf("Failed to start connection\n");
329 wait_until_finished(rpc
, &client
);
332 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
333 printf("Failed to send NULL2 request\n");
336 wait_until_finished(rpc
, &client
);
339 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
340 printf("Failed to send DUMP2 request\n");
343 wait_until_finished(rpc
, &client
);
346 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
347 printf("Failed to send NULL3 request\n");
350 wait_until_finished(rpc
, &client
);
353 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
354 printf("Failed to send DUMP3 request\n");
357 wait_until_finished(rpc
, &client
);
360 if (rpc_pmap3_gettime_async(rpc
, pmap3_gettime_cb
, &client
) != 0) {
361 printf("Failed to send GETTIME3 request\n");
364 wait_until_finished(rpc
, &client
);
367 if (rpc_pmap3_uaddr2taddr_async(rpc
, u2t3string
, pmap3_uaddr2taddr_cb
, &client
) != 0) {
368 printf("Failed to send UADDR2TADDR3 request\n");
371 wait_until_finished(rpc
, &client
);
374 struct pmap3_mapping map
;
376 map
.prog
=getaddr3prog
;
377 map
.vers
=getaddr3vers
;
378 map
.netid
=getaddr3netid
;
381 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
382 printf("Failed to send GETADDR3 request\n");
385 wait_until_finished(rpc
, &client
);
389 rpc_destroy_context(rpc
);