1c3b240a39c509cbbfdf84e04513721e63cc93aa
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_set_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
130 struct client
*client
= private_data
;
131 uint32_t res
= *(uint32_t *)data
;
133 if (status
== RPC_STATUS_ERROR
) {
134 printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data
);
137 if (status
!= RPC_STATUS_SUCCESS
) {
138 printf("PORTMAP3/SET call failed, status:%d\n", status
);
142 printf("PORTMAP3/SET:\n");
143 printf(" Res:%d\n", res
);
145 client
->is_finished
= 1;
148 void pmap3_unset_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
150 struct client
*client
= private_data
;
151 uint32_t res
= *(uint32_t *)data
;
153 if (status
== RPC_STATUS_ERROR
) {
154 printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data
);
157 if (status
!= RPC_STATUS_SUCCESS
) {
158 printf("PORTMAP3/UNSET call failed, status:%d\n", status
);
162 printf("PORTMAP3/UNSET:\n");
163 printf(" Res:%d\n", res
);
165 client
->is_finished
= 1;
168 void pmap3_gettime_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
170 struct client
*client
= private_data
;
171 time_t t
= *(uint32_t *)data
;
173 if (status
== RPC_STATUS_ERROR
) {
174 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data
);
177 if (status
!= RPC_STATUS_SUCCESS
) {
178 printf("PORTMAP3/GETTIME call failed, status:%d\n", status
);
182 printf("PORTMAP3/GETTIME:\n");
183 printf(" Time:%d %s\n", (int)t
, ctime(&t
));
185 client
->is_finished
= 1;
188 void pmap3_uaddr2taddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
190 struct client
*client
= private_data
;
191 struct pmap3_netbuf
*nb
= data
;
194 if (status
== RPC_STATUS_ERROR
) {
195 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data
);
198 if (status
!= RPC_STATUS_SUCCESS
) {
199 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status
);
203 printf("PORTMAP3/UADDR2TADDR:\n");
204 printf(" MaxLen:%d\n", nb
->maxlen
);
206 for (i
= 0; i
< nb
->maxlen
; i
++) {
207 printf("%02x ", nb
->buf
.buf_val
[i
]);
213 client
->is_finished
= 1;
216 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
218 struct client
*client
= private_data
;
220 if (status
== RPC_STATUS_ERROR
) {
221 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
224 if (status
!= RPC_STATUS_SUCCESS
) {
225 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
229 printf("PORTMAP2/NULL responded and server is alive\n");
230 client
->is_finished
= 1;
233 void pmap3_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("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
241 if (status
!= RPC_STATUS_SUCCESS
) {
242 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
246 printf("PORTMAP3/NULL responded and server is alive\n");
247 client
->is_finished
= 1;
250 void pmap_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("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
258 if (status
!= RPC_STATUS_SUCCESS
) {
259 printf("PORTMAP/NULL call failed, status:%d\n", status
);
263 client
->is_finished
= 1;
266 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
268 struct client
*client
= private_data
;
270 if (status
!= RPC_STATUS_SUCCESS
) {
271 printf("connection to portmapper failed\n");
275 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
276 printf("Failed to send null request\n");
282 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
286 client
->is_finished
= 0;
288 pfd
.fd
= rpc_get_fd(rpc
);
289 pfd
.events
= rpc_which_events(rpc
);
291 if (poll(&pfd
, 1, -1) < 0) {
292 printf("Poll failed");
295 if (rpc_service(rpc
, pfd
.revents
) < 0) {
296 printf("rpc_service failed\n");
299 if (client
->is_finished
) {
305 int main(int argc _U_
, char *argv
[] _U_
)
307 struct rpc_context
*rpc
;
308 struct client client
;
320 int command_found
= 0;
322 int set3prog
, set3vers
;
323 char *set3netid
, *set3addr
, *set3owner
;
324 int unset3prog
, unset3vers
;
325 char *unset3netid
, *unset3addr
, *unset3owner
;
326 int getaddr3prog
, getaddr3vers
;
327 char *getaddr3netid
, *getaddr3addr
, *getaddr3owner
;
330 rpc
= rpc_init_context();
332 printf("failed to init context\n");
336 for (i
= 1; i
< argc
; i
++) {
337 if (!strcmp(argv
[i
], "dump2")) {
340 } else if (!strcmp(argv
[i
], "null2")) {
343 } else if (!strcmp(argv
[i
], "dump3")) {
346 } else if (!strcmp(argv
[i
], "gettime3")) {
349 } else if (!strcmp(argv
[i
], "u2t3")) {
351 u2t3string
= argv
[++i
];
353 } else if (!strcmp(argv
[i
], "getaddr3")) {
355 getaddr3prog
= atoi(argv
[++i
]);
356 getaddr3vers
= atoi(argv
[++i
]);
357 getaddr3netid
= argv
[++i
];
358 getaddr3addr
= argv
[++i
];
359 getaddr3owner
= argv
[++i
];
361 } else if (!strcmp(argv
[i
], "set3")) {
363 set3prog
= atoi(argv
[++i
]);
364 set3vers
= atoi(argv
[++i
]);
365 set3netid
= argv
[++i
];
366 set3addr
= argv
[++i
];
367 set3owner
= argv
[++i
];
369 } else if (!strcmp(argv
[i
], "null3")) {
376 if (command_found
== 0 || server
== NULL
) {
377 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
381 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
382 printf("Failed to start connection\n");
385 wait_until_finished(rpc
, &client
);
388 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
389 printf("Failed to send NULL2 request\n");
392 wait_until_finished(rpc
, &client
);
395 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
396 printf("Failed to send DUMP2 request\n");
399 wait_until_finished(rpc
, &client
);
402 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
403 printf("Failed to send NULL3 request\n");
406 wait_until_finished(rpc
, &client
);
409 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
410 printf("Failed to send DUMP3 request\n");
413 wait_until_finished(rpc
, &client
);
416 if (rpc_pmap3_gettime_async(rpc
, pmap3_gettime_cb
, &client
) != 0) {
417 printf("Failed to send GETTIME3 request\n");
420 wait_until_finished(rpc
, &client
);
423 if (rpc_pmap3_uaddr2taddr_async(rpc
, u2t3string
, pmap3_uaddr2taddr_cb
, &client
) != 0) {
424 printf("Failed to send UADDR2TADDR3 request\n");
427 wait_until_finished(rpc
, &client
);
430 struct pmap3_mapping map
;
432 map
.prog
= getaddr3prog
;
433 map
.vers
= getaddr3vers
;
434 map
.netid
= getaddr3netid
;
435 map
.addr
= getaddr3addr
;
436 map
.owner
= getaddr3owner
;
437 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
438 printf("Failed to send GETADDR3 request\n");
441 wait_until_finished(rpc
, &client
);
444 struct pmap3_mapping map
;
448 map
.netid
= set3netid
;
450 map
.owner
= set3owner
;
451 if (rpc_pmap3_set_async(rpc
, &map
, pmap3_set_cb
, &client
) != 0) {
452 printf("Failed to send SET3 request\n");
455 wait_until_finished(rpc
, &client
);
458 struct pmap3_mapping map
;
460 map
.prog
= unset3prog
;
461 map
.vers
= unset3vers
;
462 map
.netid
= unset3netid
;
463 map
.addr
= unset3addr
;
464 map
.owner
= unset3owner
;
465 if (rpc_pmap3_unset_async(rpc
, &map
, pmap3_unset_cb
, &client
) != 0) {
466 printf("Failed to send UNSET3 request\n");
469 wait_until_finished(rpc
, &client
);
473 rpc_destroy_context(rpc
);