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