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>
44 #include "libnfs-raw.h"
45 #include "libnfs-raw-mount.h"
46 #include "libnfs-raw-nfs.h"
47 #include "libnfs-raw-portmap.h"
48 #include "libnfs-raw-rquota.h"
54 void pmap2_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
56 struct client
*client
= private_data
;
57 struct pmap2_dump_result
*dr
= data
;
58 struct pmap2_mapping_list
*list
= dr
->list
;
60 if (status
== RPC_STATUS_ERROR
) {
61 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data
);
64 if (status
!= RPC_STATUS_SUCCESS
) {
65 printf("PORTMAP2/DUMP call failed, status:%d\n", status
);
69 printf("PORTMAP2/DUMP:\n");
71 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
78 client
->is_finished
= 1;
81 void pmap3_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
83 struct client
*client
= private_data
;
84 struct pmap3_dump_result
*dr
= data
;
85 struct pmap3_mapping_list
*list
= dr
->list
;
87 if (status
== RPC_STATUS_ERROR
) {
88 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data
);
91 if (status
!= RPC_STATUS_SUCCESS
) {
92 printf("PORTMAP3/DUMP call failed, status:%d\n", status
);
96 printf("PORTMAP3/DUMP:\n");
98 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
106 client
->is_finished
= 1;
109 void pmap3_getaddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
111 struct client
*client
= private_data
;
112 struct pmap3_string_result
*gar
= data
;
114 if (status
== RPC_STATUS_ERROR
) {
115 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data
);
118 if (status
!= RPC_STATUS_SUCCESS
) {
119 printf("PORTMAP3/GETADDR call failed, status:%d\n", status
);
123 printf("PORTMAP3/GETADDR:\n");
124 printf(" Addr:%s\n", gar
->addr
);
126 client
->is_finished
= 1;
129 void pmap3_set_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
131 struct client
*client
= private_data
;
132 uint32_t res
= *(uint32_t *)data
;
134 if (status
== RPC_STATUS_ERROR
) {
135 printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data
);
138 if (status
!= RPC_STATUS_SUCCESS
) {
139 printf("PORTMAP3/SET call failed, status:%d\n", status
);
143 printf("PORTMAP3/SET:\n");
144 printf(" Res:%d\n", res
);
146 client
->is_finished
= 1;
149 void pmap3_unset_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
151 struct client
*client
= private_data
;
152 uint32_t res
= *(uint32_t *)data
;
154 if (status
== RPC_STATUS_ERROR
) {
155 printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data
);
158 if (status
!= RPC_STATUS_SUCCESS
) {
159 printf("PORTMAP3/UNSET call failed, status:%d\n", status
);
163 printf("PORTMAP3/UNSET:\n");
164 printf(" Res:%d\n", res
);
166 client
->is_finished
= 1;
169 void pmap3_gettime_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
171 struct client
*client
= private_data
;
172 time_t t
= *(uint32_t *)data
;
174 if (status
== RPC_STATUS_ERROR
) {
175 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data
);
178 if (status
!= RPC_STATUS_SUCCESS
) {
179 printf("PORTMAP3/GETTIME call failed, status:%d\n", status
);
183 printf("PORTMAP3/GETTIME:\n");
184 printf(" Time:%d %s\n", (int)t
, ctime(&t
));
186 client
->is_finished
= 1;
189 void pmap3_uaddr2taddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
191 struct client
*client
= private_data
;
192 struct pmap3_netbuf
*nb
= data
;
193 struct sockaddr_storage
*ss
;
194 char host
[256], port
[6];
197 if (status
== RPC_STATUS_ERROR
) {
198 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data
);
201 if (status
!= RPC_STATUS_SUCCESS
) {
202 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status
);
206 printf("PORTMAP3/UADDR2TADDR:\n");
207 printf(" MaxLen:%d\n", nb
->maxlen
);
209 for (i
= 0; i
< nb
->maxlen
; i
++) {
210 printf("%02x ", nb
->buf
.buf_val
[i
]);
217 ss
= (struct sockaddr_storage
*)&nb
->buf
.buf_val
[0];
218 getnameinfo((struct sockaddr
*)ss
, sizeof(struct sockaddr_storage
),
219 &host
[0], sizeof(host
), &port
[0], sizeof(port
),
220 NI_NUMERICHOST
|NI_NUMERICSERV
);
221 switch (ss
->ss_family
) {
223 printf(" IPv4: %s:%s\n", &host
[0], &port
[0]);
226 printf(" IPv6: %s:%s\n", &host
[0], &port
[0]);
229 client
->is_finished
= 1;
232 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
234 struct client
*client
= private_data
;
236 if (status
== RPC_STATUS_ERROR
) {
237 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
240 if (status
!= RPC_STATUS_SUCCESS
) {
241 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
245 printf("PORTMAP2/NULL responded and server is alive\n");
246 client
->is_finished
= 1;
249 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
251 struct client
*client
= private_data
;
253 if (status
== RPC_STATUS_ERROR
) {
254 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
257 if (status
!= RPC_STATUS_SUCCESS
) {
258 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
262 printf("PORTMAP3/NULL responded and server is alive\n");
263 client
->is_finished
= 1;
266 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
268 struct client
*client
= private_data
;
270 if (status
== RPC_STATUS_ERROR
) {
271 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
274 if (status
!= RPC_STATUS_SUCCESS
) {
275 printf("PORTMAP/NULL call failed, status:%d\n", status
);
279 client
->is_finished
= 1;
282 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
284 struct client
*client
= private_data
;
286 if (status
!= RPC_STATUS_SUCCESS
) {
287 printf("connection to portmapper failed\n");
291 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
292 printf("Failed to send null request\n");
298 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
302 client
->is_finished
= 0;
304 pfd
.fd
= rpc_get_fd(rpc
);
305 pfd
.events
= rpc_which_events(rpc
);
307 if (poll(&pfd
, 1, -1) < 0) {
308 printf("Poll failed");
311 if (rpc_service(rpc
, pfd
.revents
) < 0) {
312 printf("rpc_service failed\n");
315 if (client
->is_finished
) {
321 int main(int argc _U_
, char *argv
[] _U_
)
323 struct rpc_context
*rpc
;
324 struct client client
;
336 int command_found
= 0;
338 int set3prog
, set3vers
;
339 char *set3netid
, *set3addr
, *set3owner
;
340 int unset3prog
, unset3vers
;
341 char *unset3netid
, *unset3addr
, *unset3owner
;
342 int getaddr3prog
, getaddr3vers
;
343 char *getaddr3netid
, *getaddr3addr
, *getaddr3owner
;
346 rpc
= rpc_init_context();
348 printf("failed to init context\n");
352 for (i
= 1; i
< argc
; i
++) {
353 if (!strcmp(argv
[i
], "dump2")) {
356 } else if (!strcmp(argv
[i
], "null2")) {
359 } else if (!strcmp(argv
[i
], "dump3")) {
362 } else if (!strcmp(argv
[i
], "gettime3")) {
365 } else if (!strcmp(argv
[i
], "u2t3")) {
367 u2t3string
= argv
[++i
];
369 } else if (!strcmp(argv
[i
], "getaddr3")) {
371 getaddr3prog
= atoi(argv
[++i
]);
372 getaddr3vers
= atoi(argv
[++i
]);
373 getaddr3netid
= argv
[++i
];
374 getaddr3addr
= argv
[++i
];
375 getaddr3owner
= argv
[++i
];
377 } else if (!strcmp(argv
[i
], "set3")) {
379 set3prog
= atoi(argv
[++i
]);
380 set3vers
= atoi(argv
[++i
]);
381 set3netid
= argv
[++i
];
382 set3addr
= argv
[++i
];
383 set3owner
= argv
[++i
];
385 } else if (!strcmp(argv
[i
], "null3")) {
392 if (command_found
== 0 || server
== NULL
) {
393 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
397 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
398 printf("Failed to start connection\n");
401 wait_until_finished(rpc
, &client
);
404 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
405 printf("Failed to send NULL2 request\n");
408 wait_until_finished(rpc
, &client
);
411 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
412 printf("Failed to send DUMP2 request\n");
415 wait_until_finished(rpc
, &client
);
418 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
419 printf("Failed to send NULL3 request\n");
422 wait_until_finished(rpc
, &client
);
425 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
426 printf("Failed to send DUMP3 request\n");
429 wait_until_finished(rpc
, &client
);
432 if (rpc_pmap3_gettime_async(rpc
, pmap3_gettime_cb
, &client
) != 0) {
433 printf("Failed to send GETTIME3 request\n");
436 wait_until_finished(rpc
, &client
);
439 if (rpc_pmap3_uaddr2taddr_async(rpc
, u2t3string
, pmap3_uaddr2taddr_cb
, &client
) != 0) {
440 printf("Failed to send UADDR2TADDR3 request\n");
443 wait_until_finished(rpc
, &client
);
446 struct pmap3_mapping map
;
448 map
.prog
= getaddr3prog
;
449 map
.vers
= getaddr3vers
;
450 map
.netid
= getaddr3netid
;
451 map
.addr
= getaddr3addr
;
452 map
.owner
= getaddr3owner
;
453 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
454 printf("Failed to send GETADDR3 request\n");
457 wait_until_finished(rpc
, &client
);
460 struct pmap3_mapping map
;
464 map
.netid
= set3netid
;
466 map
.owner
= set3owner
;
467 if (rpc_pmap3_set_async(rpc
, &map
, pmap3_set_cb
, &client
) != 0) {
468 printf("Failed to send SET3 request\n");
471 wait_until_finished(rpc
, &client
);
474 struct pmap3_mapping map
;
476 map
.prog
= unset3prog
;
477 map
.vers
= unset3vers
;
478 map
.netid
= unset3netid
;
479 map
.addr
= unset3addr
;
480 map
.owner
= unset3owner
;
481 if (rpc_pmap3_unset_async(rpc
, &map
, pmap3_unset_cb
, &client
) != 0) {
482 printf("Failed to send UNSET3 request\n");
485 wait_until_finished(rpc
, &client
);
489 rpc_destroy_context(rpc
);