PORTMAP: Rename pmap3_getaddr_result to pmap3_string_result
[deb_libnfs.git] / examples / portmap-client.c
CommitLineData
4edd7830
RS
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>
5245608a 40#include <time.h>
4edd7830
RS
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
49struct client {
50 int is_finished;
51};
52
53void 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
80void 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
7fbedfde
RS
108void pmap3_getaddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
109{
110 struct client *client = private_data;
24f45c54 111 struct pmap3_string_result *gar = data;
7fbedfde
RS
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
5245608a
RS
128void pmap3_gettime_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
129{
130 struct client *client = private_data;
131 time_t t = *(uint32_t *)data;
132
133 if (status == RPC_STATUS_ERROR) {
134 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data);
135 exit(10);
136 }
137 if (status != RPC_STATUS_SUCCESS) {
138 printf("PORTMAP3/GETTIME call failed, status:%d\n", status);
139 exit(10);
140 }
141
142 printf("PORTMAP3/GETTIME:\n");
143 printf(" Time:%d %s\n", (int)t, ctime(&t));
144
145 client->is_finished = 1;
146}
147
729266a7
RS
148void pmap3_uaddr2taddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
149{
150 struct client *client = private_data;
151 struct pmap3_netbuf *nb = data;
152 int i;
153
154 if (status == RPC_STATUS_ERROR) {
155 printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data);
156 exit(10);
157 }
158 if (status != RPC_STATUS_SUCCESS) {
159 printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status);
160 exit(10);
161 }
162
163 printf("PORTMAP3/UADDR2TADDR:\n");
164 printf(" MaxLen:%d\n", nb->maxlen);
165 printf(" ");
166 for (i = 0; i < nb->maxlen; i++) {
167 printf("%02x ", nb->buf.buf_val[i]);
168 if (i %16 == 15) {
169 printf("\n ");
170 }
171 }
172 printf("\n");
173 client->is_finished = 1;
174}
175
4edd7830
RS
176void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
177{
178 struct client *client = private_data;
179
180 if (status == RPC_STATUS_ERROR) {
181 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data);
182 exit(10);
183 }
184 if (status != RPC_STATUS_SUCCESS) {
185 printf("PORTMAP2/NULL call failed, status:%d\n", status);
186 exit(10);
187 }
188
189 printf("PORTMAP2/NULL responded and server is alive\n");
190 client->is_finished = 1;
191}
192
193void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
194{
195 struct client *client = private_data;
196
197 if (status == RPC_STATUS_ERROR) {
198 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data);
199 exit(10);
200 }
201 if (status != RPC_STATUS_SUCCESS) {
202 printf("PORTMAP3/NULL call failed, status:%d\n", status);
203 exit(10);
204 }
205
206 printf("PORTMAP3/NULL responded and server is alive\n");
207 client->is_finished = 1;
208}
209
210void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
211{
212 struct client *client = private_data;
213
214 if (status == RPC_STATUS_ERROR) {
215 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data);
216 exit(10);
217 }
218 if (status != RPC_STATUS_SUCCESS) {
219 printf("PORTMAP/NULL call failed, status:%d\n", status);
220 exit(10);
221 }
222
223 client->is_finished = 1;
224}
225
226void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
227{
228 struct client *client = private_data;
229
230 if (status != RPC_STATUS_SUCCESS) {
231 printf("connection to portmapper failed\n");
232 exit(10);
233 }
234
235 if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) {
236 printf("Failed to send null request\n");
237 exit(10);
238 }
239}
240
241
242static void wait_until_finished(struct rpc_context *rpc, struct client *client)
243{
244 struct pollfd pfd;
245
246 client->is_finished = 0;
247 for (;;) {
248 pfd.fd = rpc_get_fd(rpc);
249 pfd.events = rpc_which_events(rpc);
250
251 if (poll(&pfd, 1, -1) < 0) {
252 printf("Poll failed");
253 exit(10);
254 }
255 if (rpc_service(rpc, pfd.revents) < 0) {
256 printf("rpc_service failed\n");
257 break;
258 }
259 if (client->is_finished) {
260 break;
261 }
262 }
263}
264
265int main(int argc _U_, char *argv[] _U_)
266{
267 struct rpc_context *rpc;
268 struct client client;
269 char *server = NULL;
270 int i;
271 int null2 = 0;
272 int dump2 = 0;
273 int null3 = 0;
7fbedfde 274 int getaddr3 = 0;
4edd7830 275 int dump3 = 0;
5245608a 276 int gettime3 = 0;
729266a7 277 int u2t3 = 0;
4edd7830
RS
278 int command_found = 0;
279
7fbedfde
RS
280 int getaddr3prog, getaddr3vers;
281 char *getaddr3netid;
729266a7 282 char *u2t3string;
7fbedfde 283
4edd7830
RS
284 rpc = rpc_init_context();
285 if (rpc == NULL) {
286 printf("failed to init context\n");
287 exit(10);
288 }
289
290 for (i = 1; i < argc; i++) {
291 if (!strcmp(argv[i], "dump2")) {
292 dump2 = 1;
293 command_found++;
294 } else if (!strcmp(argv[i], "null2")) {
295 null2 = 1;
296 command_found++;
297 } else if (!strcmp(argv[i], "dump3")) {
298 dump3 = 1;
299 command_found++;
5245608a
RS
300 } else if (!strcmp(argv[i], "gettime3")) {
301 gettime3 = 1;
302 command_found++;
729266a7
RS
303 } else if (!strcmp(argv[i], "u2t3")) {
304 u2t3 = 1;
305 u2t3string = argv[++i];
306 command_found++;
7fbedfde
RS
307 } else if (!strcmp(argv[i], "getaddr3")) {
308 getaddr3 = 1;
309 getaddr3prog = atoi(argv[++i]);
310 getaddr3vers = atoi(argv[++i]);
311 getaddr3netid = argv[++i];
312 command_found++;
4edd7830
RS
313 } else if (!strcmp(argv[i], "null3")) {
314 null3 = 1;
315 command_found++;
316 } else {
317 server = argv[i];
318 }
319 }
320 if (command_found == 0 || server == NULL) {
321 fprintf(stderr, "Usage: portmap-client <command*> <server>\n");
322 exit(10);
323 }
324
325 if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) {
326 printf("Failed to start connection\n");
327 exit(10);
328 }
329 wait_until_finished(rpc, &client);
330
331 if (null2) {
332 if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) {
333 printf("Failed to send NULL2 request\n");
334 exit(10);
335 }
336 wait_until_finished(rpc, &client);
337 }
338 if (dump2) {
339 if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) {
340 printf("Failed to send DUMP2 request\n");
341 exit(10);
342 }
343 wait_until_finished(rpc, &client);
344 }
345 if (null3) {
346 if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) {
347 printf("Failed to send NULL3 request\n");
348 exit(10);
349 }
350 wait_until_finished(rpc, &client);
351 }
352 if (dump3) {
353 if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) {
354 printf("Failed to send DUMP3 request\n");
355 exit(10);
356 }
357 wait_until_finished(rpc, &client);
358 }
5245608a
RS
359 if (gettime3) {
360 if (rpc_pmap3_gettime_async(rpc, pmap3_gettime_cb, &client) != 0) {
361 printf("Failed to send GETTIME3 request\n");
362 exit(10);
363 }
364 wait_until_finished(rpc, &client);
365 }
729266a7
RS
366 if (u2t3) {
367 if (rpc_pmap3_uaddr2taddr_async(rpc, u2t3string, pmap3_uaddr2taddr_cb, &client) != 0) {
368 printf("Failed to send UADDR2TADDR3 request\n");
369 exit(10);
370 }
371 wait_until_finished(rpc, &client);
372 }
7fbedfde
RS
373 if (getaddr3) {
374 struct pmap3_mapping map;
375
376 map.prog=getaddr3prog;
377 map.vers=getaddr3vers;
378 map.netid=getaddr3netid;
379 map.addr="";
380 map.owner="";
381 if (rpc_pmap3_getaddr_async(rpc, &map, pmap3_getaddr_cb, &client) != 0) {
382 printf("Failed to send GETADDR3 request\n");
383 exit(10);
384 }
385 wait_until_finished(rpc, &client);
386 }
4edd7830
RS
387
388
389 rpc_destroy_context(rpc);
390 rpc=NULL;
391 return 0;
392}