types: remove the [u_]quad type and replace with [u]int64
[deb_libnfs.git] / examples / portmap-client.c
1 /*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2014
3
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.
8
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.
13
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/>.
16 */
17
18 /* Example program using the lowlevel raw interface.
19 * This allow accurate control of the exact commands that are being used.
20 */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef WIN32
26 #include "win32_compat.h"
27 #endif
28
29 #ifdef HAVE_POLL_H
30 #include <poll.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36
37 #include <netdb.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/socket.h>
42 #include <time.h>
43 #include "libnfs.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"
49
50 struct client {
51 int is_finished;
52 };
53
54 void pmap2_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
55 {
56 struct client *client = private_data;
57 struct pmap2_dump_result *dr = data;
58 struct pmap2_mapping_list *list = dr->list;
59
60 if (status == RPC_STATUS_ERROR) {
61 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data);
62 exit(10);
63 }
64 if (status != RPC_STATUS_SUCCESS) {
65 printf("PORTMAP2/DUMP call failed, status:%d\n", status);
66 exit(10);
67 }
68
69 printf("PORTMAP2/DUMP:\n");
70 while (list) {
71 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
72 list->map.prog,
73 list->map.vers,
74 list->map.prot,
75 list->map.port);
76 list = list->next;
77 }
78 client->is_finished = 1;
79 }
80
81 void pmap3_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
82 {
83 struct client *client = private_data;
84 struct pmap3_dump_result *dr = data;
85 struct pmap3_mapping_list *list = dr->list;
86
87 if (status == RPC_STATUS_ERROR) {
88 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data);
89 exit(10);
90 }
91 if (status != RPC_STATUS_SUCCESS) {
92 printf("PORTMAP3/DUMP call failed, status:%d\n", status);
93 exit(10);
94 }
95
96 printf("PORTMAP3/DUMP:\n");
97 while (list) {
98 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
99 list->map.prog,
100 list->map.vers,
101 list->map.netid,
102 list->map.addr,
103 list->map.owner);
104 list = list->next;
105 }
106 client->is_finished = 1;
107 }
108
109 void pmap3_getaddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
110 {
111 struct client *client = private_data;
112 struct pmap3_string_result *gar = data;
113
114 if (status == RPC_STATUS_ERROR) {
115 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data);
116 exit(10);
117 }
118 if (status != RPC_STATUS_SUCCESS) {
119 printf("PORTMAP3/GETADDR call failed, status:%d\n", status);
120 exit(10);
121 }
122
123 printf("PORTMAP3/GETADDR:\n");
124 printf(" Addr:%s\n", gar->addr);
125
126 client->is_finished = 1;
127 }
128
129 void pmap3_set_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
130 {
131 struct client *client = private_data;
132 uint32_t res = *(uint32_t *)data;
133
134 if (status == RPC_STATUS_ERROR) {
135 printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data);
136 exit(10);
137 }
138 if (status != RPC_STATUS_SUCCESS) {
139 printf("PORTMAP3/SET call failed, status:%d\n", status);
140 exit(10);
141 }
142
143 printf("PORTMAP3/SET:\n");
144 printf(" Res:%d\n", res);
145
146 client->is_finished = 1;
147 }
148
149 void pmap3_unset_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
150 {
151 struct client *client = private_data;
152 uint32_t res = *(uint32_t *)data;
153
154 if (status == RPC_STATUS_ERROR) {
155 printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data);
156 exit(10);
157 }
158 if (status != RPC_STATUS_SUCCESS) {
159 printf("PORTMAP3/UNSET call failed, status:%d\n", status);
160 exit(10);
161 }
162
163 printf("PORTMAP3/UNSET:\n");
164 printf(" Res:%d\n", res);
165
166 client->is_finished = 1;
167 }
168
169 void pmap3_gettime_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
170 {
171 struct client *client = private_data;
172 time_t t = *(uint32_t *)data;
173
174 if (status == RPC_STATUS_ERROR) {
175 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data);
176 exit(10);
177 }
178 if (status != RPC_STATUS_SUCCESS) {
179 printf("PORTMAP3/GETTIME call failed, status:%d\n", status);
180 exit(10);
181 }
182
183 printf("PORTMAP3/GETTIME:\n");
184 printf(" Time:%d %s\n", (int)t, ctime(&t));
185
186 client->is_finished = 1;
187 }
188
189 void pmap3_uaddr2taddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
190 {
191 struct client *client = private_data;
192 struct pmap3_netbuf *nb = data;
193 struct sockaddr_storage *ss;
194 char host[256], port[6];
195 int i;
196
197 if (status == RPC_STATUS_ERROR) {
198 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data);
199 exit(10);
200 }
201 if (status != RPC_STATUS_SUCCESS) {
202 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status);
203 exit(10);
204 }
205
206 printf("PORTMAP3/UADDR2TADDR:\n");
207 printf(" MaxLen:%d\n", nb->maxlen);
208 printf(" ");
209 for (i = 0; i < nb->maxlen; i++) {
210 printf("%02x ", nb->buf.buf_val[i]);
211 if (i %16 == 15) {
212 printf("\n ");
213 }
214 }
215 printf("\n");
216 printf(" ---\n");
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) {
222 case AF_INET:
223 printf(" IPv4: %s:%s\n", &host[0], &port[0]);
224 break;
225 case AF_INET6:
226 printf(" IPv6: %s:%s\n", &host[0], &port[0]);
227 break;
228 }
229 client->is_finished = 1;
230 }
231
232 void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
233 {
234 struct client *client = private_data;
235
236 if (status == RPC_STATUS_ERROR) {
237 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data);
238 exit(10);
239 }
240 if (status != RPC_STATUS_SUCCESS) {
241 printf("PORTMAP2/NULL call failed, status:%d\n", status);
242 exit(10);
243 }
244
245 printf("PORTMAP2/NULL responded and server is alive\n");
246 client->is_finished = 1;
247 }
248
249 void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
250 {
251 struct client *client = private_data;
252
253 if (status == RPC_STATUS_ERROR) {
254 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data);
255 exit(10);
256 }
257 if (status != RPC_STATUS_SUCCESS) {
258 printf("PORTMAP3/NULL call failed, status:%d\n", status);
259 exit(10);
260 }
261
262 printf("PORTMAP3/NULL responded and server is alive\n");
263 client->is_finished = 1;
264 }
265
266 void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
267 {
268 struct client *client = private_data;
269
270 if (status == RPC_STATUS_ERROR) {
271 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data);
272 exit(10);
273 }
274 if (status != RPC_STATUS_SUCCESS) {
275 printf("PORTMAP/NULL call failed, status:%d\n", status);
276 exit(10);
277 }
278
279 client->is_finished = 1;
280 }
281
282 void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
283 {
284 struct client *client = private_data;
285
286 if (status != RPC_STATUS_SUCCESS) {
287 printf("connection to portmapper failed\n");
288 exit(10);
289 }
290
291 if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) {
292 printf("Failed to send null request\n");
293 exit(10);
294 }
295 }
296
297
298 static void wait_until_finished(struct rpc_context *rpc, struct client *client)
299 {
300 struct pollfd pfd;
301
302 client->is_finished = 0;
303 for (;;) {
304 pfd.fd = rpc_get_fd(rpc);
305 pfd.events = rpc_which_events(rpc);
306
307 if (poll(&pfd, 1, -1) < 0) {
308 printf("Poll failed");
309 exit(10);
310 }
311 if (rpc_service(rpc, pfd.revents) < 0) {
312 printf("rpc_service failed\n");
313 break;
314 }
315 if (client->is_finished) {
316 break;
317 }
318 }
319 }
320
321 int main(int argc _U_, char *argv[] _U_)
322 {
323 struct rpc_context *rpc;
324 struct client client;
325 char *server = NULL;
326 int i;
327 int null2 = 0;
328 int dump2 = 0;
329 int null3 = 0;
330 int set3 = 0;
331 int unset3 = 0;
332 int getaddr3 = 0;
333 int dump3 = 0;
334 int gettime3 = 0;
335 int u2t3 = 0;
336 int command_found = 0;
337
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;
344 char *u2t3string;
345
346 rpc = rpc_init_context();
347 if (rpc == NULL) {
348 printf("failed to init context\n");
349 exit(10);
350 }
351
352 for (i = 1; i < argc; i++) {
353 if (!strcmp(argv[i], "dump2")) {
354 dump2 = 1;
355 command_found++;
356 } else if (!strcmp(argv[i], "null2")) {
357 null2 = 1;
358 command_found++;
359 } else if (!strcmp(argv[i], "dump3")) {
360 dump3 = 1;
361 command_found++;
362 } else if (!strcmp(argv[i], "gettime3")) {
363 gettime3 = 1;
364 command_found++;
365 } else if (!strcmp(argv[i], "u2t3")) {
366 u2t3 = 1;
367 u2t3string = argv[++i];
368 command_found++;
369 } else if (!strcmp(argv[i], "getaddr3")) {
370 getaddr3 = 1;
371 getaddr3prog = atoi(argv[++i]);
372 getaddr3vers = atoi(argv[++i]);
373 getaddr3netid = argv[++i];
374 getaddr3addr = argv[++i];
375 getaddr3owner = argv[++i];
376 command_found++;
377 } else if (!strcmp(argv[i], "set3")) {
378 set3 = 1;
379 set3prog = atoi(argv[++i]);
380 set3vers = atoi(argv[++i]);
381 set3netid = argv[++i];
382 set3addr = argv[++i];
383 set3owner = argv[++i];
384 command_found++;
385 } else if (!strcmp(argv[i], "null3")) {
386 null3 = 1;
387 command_found++;
388 } else {
389 server = argv[i];
390 }
391 }
392 if (command_found == 0 || server == NULL) {
393 fprintf(stderr, "Usage: portmap-client <command*> <server>\n");
394 exit(10);
395 }
396
397 if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) {
398 printf("Failed to start connection\n");
399 exit(10);
400 }
401 wait_until_finished(rpc, &client);
402
403 if (null2) {
404 if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) {
405 printf("Failed to send NULL2 request\n");
406 exit(10);
407 }
408 wait_until_finished(rpc, &client);
409 }
410 if (dump2) {
411 if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) {
412 printf("Failed to send DUMP2 request\n");
413 exit(10);
414 }
415 wait_until_finished(rpc, &client);
416 }
417 if (null3) {
418 if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) {
419 printf("Failed to send NULL3 request\n");
420 exit(10);
421 }
422 wait_until_finished(rpc, &client);
423 }
424 if (dump3) {
425 if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) {
426 printf("Failed to send DUMP3 request\n");
427 exit(10);
428 }
429 wait_until_finished(rpc, &client);
430 }
431 if (gettime3) {
432 if (rpc_pmap3_gettime_async(rpc, pmap3_gettime_cb, &client) != 0) {
433 printf("Failed to send GETTIME3 request\n");
434 exit(10);
435 }
436 wait_until_finished(rpc, &client);
437 }
438 if (u2t3) {
439 if (rpc_pmap3_uaddr2taddr_async(rpc, u2t3string, pmap3_uaddr2taddr_cb, &client) != 0) {
440 printf("Failed to send UADDR2TADDR3 request\n");
441 exit(10);
442 }
443 wait_until_finished(rpc, &client);
444 }
445 if (getaddr3) {
446 struct pmap3_mapping map;
447
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");
455 exit(10);
456 }
457 wait_until_finished(rpc, &client);
458 }
459 if (set3) {
460 struct pmap3_mapping map;
461
462 map.prog = set3prog;
463 map.vers = set3vers;
464 map.netid = set3netid;
465 map.addr = set3addr;
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");
469 exit(10);
470 }
471 wait_until_finished(rpc, &client);
472 }
473 if (unset3) {
474 struct pmap3_mapping map;
475
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");
483 exit(10);
484 }
485 wait_until_finished(rpc, &client);
486 }
487
488
489 rpc_destroy_context(rpc);
490 rpc=NULL;
491 return 0;
492 }