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 <sys/socket.h>
43 #include "libnfs-zdr.h"
45 #include "libnfs-raw.h"
46 #include "libnfs-raw-mount.h"
47 #include "libnfs-raw-nfs.h"
48 #include "libnfs-raw-portmap.h"
49 #include "libnfs-raw-rquota.h"
55 void pmap2_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
57 struct client
*client
= private_data
;
58 struct pmap2_dump_result
*dr
= data
;
59 struct pmap2_mapping_list
*list
= dr
->list
;
61 if (status
== RPC_STATUS_ERROR
) {
62 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data
);
65 if (status
!= RPC_STATUS_SUCCESS
) {
66 printf("PORTMAP2/DUMP call failed, status:%d\n", status
);
70 printf("PORTMAP2/DUMP:\n");
72 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
79 client
->is_finished
= 1;
82 void pmap3_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
84 struct client
*client
= private_data
;
85 struct pmap3_dump_result
*dr
= data
;
86 struct pmap3_mapping_list
*list
= dr
->list
;
88 if (status
== RPC_STATUS_ERROR
) {
89 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data
);
92 if (status
!= RPC_STATUS_SUCCESS
) {
93 printf("PORTMAP3/DUMP call failed, status:%d\n", status
);
97 printf("PORTMAP3/DUMP:\n");
99 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
107 client
->is_finished
= 1;
110 void pmap3_getaddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
112 struct client
*client
= private_data
;
113 struct pmap3_string_result
*gar
= data
;
115 if (status
== RPC_STATUS_ERROR
) {
116 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data
);
119 if (status
!= RPC_STATUS_SUCCESS
) {
120 printf("PORTMAP3/GETADDR call failed, status:%d\n", status
);
124 printf("PORTMAP3/GETADDR:\n");
125 printf(" Addr:%s\n", gar
->addr
);
127 client
->is_finished
= 1;
130 void pmap3_set_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
132 struct client
*client
= private_data
;
133 uint32_t res
= *(uint32_t *)data
;
135 if (status
== RPC_STATUS_ERROR
) {
136 printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data
);
139 if (status
!= RPC_STATUS_SUCCESS
) {
140 printf("PORTMAP3/SET call failed, status:%d\n", status
);
144 printf("PORTMAP3/SET:\n");
145 printf(" Res:%d\n", res
);
147 client
->is_finished
= 1;
150 void pmap3_unset_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
152 struct client
*client
= private_data
;
153 uint32_t res
= *(uint32_t *)data
;
155 if (status
== RPC_STATUS_ERROR
) {
156 printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data
);
159 if (status
!= RPC_STATUS_SUCCESS
) {
160 printf("PORTMAP3/UNSET call failed, status:%d\n", status
);
164 printf("PORTMAP3/UNSET:\n");
165 printf(" Res:%d\n", res
);
167 client
->is_finished
= 1;
170 void pmap3_gettime_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
172 struct client
*client
= private_data
;
173 time_t t
= *(uint32_t *)data
;
175 if (status
== RPC_STATUS_ERROR
) {
176 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data
);
179 if (status
!= RPC_STATUS_SUCCESS
) {
180 printf("PORTMAP3/GETTIME call failed, status:%d\n", status
);
184 printf("PORTMAP3/GETTIME:\n");
185 printf(" Time:%d %s\n", (int)t
, ctime(&t
));
187 client
->is_finished
= 1;
190 void pmap3_uaddr2taddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
192 struct client
*client
= private_data
;
193 struct pmap3_netbuf
*nb
= data
;
194 struct sockaddr_storage
*ss
;
195 char host
[256], port
[6];
198 if (status
== RPC_STATUS_ERROR
) {
199 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data
);
202 if (status
!= RPC_STATUS_SUCCESS
) {
203 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status
);
207 printf("PORTMAP3/UADDR2TADDR:\n");
208 printf(" MaxLen:%d\n", nb
->maxlen
);
210 for (i
= 0; i
< nb
->maxlen
; i
++) {
211 printf("%02x ", nb
->buf
.buf_val
[i
]);
218 ss
= (struct sockaddr_storage
*)&nb
->buf
.buf_val
[0];
219 getnameinfo((struct sockaddr
*)ss
, sizeof(struct sockaddr_storage
),
220 &host
[0], sizeof(host
), &port
[0], sizeof(port
),
221 NI_NUMERICHOST
|NI_NUMERICSERV
);
222 switch (ss
->ss_family
) {
224 printf(" IPv4: %s:%s\n", &host
[0], &port
[0]);
227 printf(" IPv6: %s:%s\n", &host
[0], &port
[0]);
230 client
->is_finished
= 1;
233 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
235 struct client
*client
= private_data
;
237 if (status
== RPC_STATUS_ERROR
) {
238 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
241 if (status
!= RPC_STATUS_SUCCESS
) {
242 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
246 printf("PORTMAP2/NULL responded and server is alive\n");
247 client
->is_finished
= 1;
250 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
252 struct client
*client
= private_data
;
254 if (status
== RPC_STATUS_ERROR
) {
255 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
258 if (status
!= RPC_STATUS_SUCCESS
) {
259 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
263 printf("PORTMAP3/NULL responded and server is alive\n");
264 client
->is_finished
= 1;
267 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
269 struct client
*client
= private_data
;
271 if (status
== RPC_STATUS_ERROR
) {
272 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
275 if (status
!= RPC_STATUS_SUCCESS
) {
276 printf("PORTMAP/NULL call failed, status:%d\n", status
);
280 client
->is_finished
= 1;
283 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
285 struct client
*client
= private_data
;
287 if (status
!= RPC_STATUS_SUCCESS
) {
288 printf("connection to portmapper failed\n");
292 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
293 printf("Failed to send null request\n");
299 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
303 client
->is_finished
= 0;
305 pfd
.fd
= rpc_get_fd(rpc
);
306 pfd
.events
= rpc_which_events(rpc
);
308 if (poll(&pfd
, 1, -1) < 0) {
309 printf("Poll failed");
312 if (rpc_service(rpc
, pfd
.revents
) < 0) {
313 printf("rpc_service failed\n");
316 if (client
->is_finished
) {
322 int main(int argc _U_
, char *argv
[] _U_
)
324 struct rpc_context
*rpc
;
325 struct client client
;
337 int command_found
= 0;
339 int set3prog
, set3vers
;
340 char *set3netid
, *set3addr
, *set3owner
;
341 int unset3prog
, unset3vers
;
342 char *unset3netid
, *unset3addr
, *unset3owner
;
343 int getaddr3prog
, getaddr3vers
;
344 char *getaddr3netid
, *getaddr3addr
, *getaddr3owner
;
347 rpc
= rpc_init_context();
349 printf("failed to init context\n");
353 for (i
= 1; i
< argc
; i
++) {
354 if (!strcmp(argv
[i
], "dump2")) {
357 } else if (!strcmp(argv
[i
], "null2")) {
360 } else if (!strcmp(argv
[i
], "dump3")) {
363 } else if (!strcmp(argv
[i
], "gettime3")) {
366 } else if (!strcmp(argv
[i
], "u2t3")) {
368 u2t3string
= argv
[++i
];
370 } else if (!strcmp(argv
[i
], "getaddr3")) {
372 getaddr3prog
= atoi(argv
[++i
]);
373 getaddr3vers
= atoi(argv
[++i
]);
374 getaddr3netid
= argv
[++i
];
375 getaddr3addr
= argv
[++i
];
376 getaddr3owner
= argv
[++i
];
378 } else if (!strcmp(argv
[i
], "set3")) {
380 set3prog
= atoi(argv
[++i
]);
381 set3vers
= atoi(argv
[++i
]);
382 set3netid
= argv
[++i
];
383 set3addr
= argv
[++i
];
384 set3owner
= argv
[++i
];
386 } else if (!strcmp(argv
[i
], "null3")) {
393 if (command_found
== 0 || server
== NULL
) {
394 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
398 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
399 printf("Failed to start connection\n");
402 wait_until_finished(rpc
, &client
);
405 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
406 printf("Failed to send NULL2 request\n");
409 wait_until_finished(rpc
, &client
);
412 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
413 printf("Failed to send DUMP2 request\n");
416 wait_until_finished(rpc
, &client
);
419 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
420 printf("Failed to send NULL3 request\n");
423 wait_until_finished(rpc
, &client
);
426 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
427 printf("Failed to send DUMP3 request\n");
430 wait_until_finished(rpc
, &client
);
433 if (rpc_pmap3_gettime_async(rpc
, pmap3_gettime_cb
, &client
) != 0) {
434 printf("Failed to send GETTIME3 request\n");
437 wait_until_finished(rpc
, &client
);
440 if (rpc_pmap3_uaddr2taddr_async(rpc
, u2t3string
, pmap3_uaddr2taddr_cb
, &client
) != 0) {
441 printf("Failed to send UADDR2TADDR3 request\n");
444 wait_until_finished(rpc
, &client
);
447 struct pmap3_mapping map
;
449 map
.prog
= getaddr3prog
;
450 map
.vers
= getaddr3vers
;
451 map
.netid
= getaddr3netid
;
452 map
.addr
= getaddr3addr
;
453 map
.owner
= getaddr3owner
;
454 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
455 printf("Failed to send GETADDR3 request\n");
458 wait_until_finished(rpc
, &client
);
461 struct pmap3_mapping map
;
465 map
.netid
= set3netid
;
467 map
.owner
= set3owner
;
468 if (rpc_pmap3_set_async(rpc
, &map
, pmap3_set_cb
, &client
) != 0) {
469 printf("Failed to send SET3 request\n");
472 wait_until_finished(rpc
, &client
);
475 struct pmap3_mapping map
;
477 map
.prog
= unset3prog
;
478 map
.vers
= unset3vers
;
479 map
.netid
= unset3netid
;
480 map
.addr
= unset3addr
;
481 map
.owner
= unset3owner
;
482 if (rpc_pmap3_unset_async(rpc
, &map
, pmap3_unset_cb
, &client
) != 0) {
483 printf("Failed to send UNSET3 request\n");
486 wait_until_finished(rpc
, &client
);
490 rpc_destroy_context(rpc
);