IPv6: If we use IPv6 then we need to use PMAP v3 GETADDR
[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>
40#include "libnfs-zdr.h"
41#include "libnfs.h"
42#include "libnfs-raw.h"
43#include "libnfs-raw-mount.h"
44#include "libnfs-raw-nfs.h"
45#include "libnfs-raw-portmap.h"
46#include "libnfs-raw-rquota.h"
47
48struct client {
49 int is_finished;
50};
51
52void pmap2_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
53{
54 struct client *client = private_data;
55 struct pmap2_dump_result *dr = data;
56 struct pmap2_mapping_list *list = dr->list;
57
58 if (status == RPC_STATUS_ERROR) {
59 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data);
60 exit(10);
61 }
62 if (status != RPC_STATUS_SUCCESS) {
63 printf("PORTMAP2/DUMP call failed, status:%d\n", status);
64 exit(10);
65 }
66
67 printf("PORTMAP2/DUMP:\n");
68 while (list) {
69 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
70 list->map.prog,
71 list->map.vers,
72 list->map.prot,
73 list->map.port);
74 list = list->next;
75 }
76 client->is_finished = 1;
77}
78
79void pmap3_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
80{
81 struct client *client = private_data;
82 struct pmap3_dump_result *dr = data;
83 struct pmap3_mapping_list *list = dr->list;
84
85 if (status == RPC_STATUS_ERROR) {
86 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data);
87 exit(10);
88 }
89 if (status != RPC_STATUS_SUCCESS) {
90 printf("PORTMAP3/DUMP call failed, status:%d\n", status);
91 exit(10);
92 }
93
94 printf("PORTMAP3/DUMP:\n");
95 while (list) {
96 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
97 list->map.prog,
98 list->map.vers,
99 list->map.netid,
100 list->map.addr,
101 list->map.owner);
102 list = list->next;
103 }
104 client->is_finished = 1;
105}
106
7fbedfde
RS
107void pmap3_getaddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
108{
109 struct client *client = private_data;
110 struct pmap3_getaddr_result *gar = data;
111
112 if (status == RPC_STATUS_ERROR) {
113 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data);
114 exit(10);
115 }
116 if (status != RPC_STATUS_SUCCESS) {
117 printf("PORTMAP3/GETADDR call failed, status:%d\n", status);
118 exit(10);
119 }
120
121 printf("PORTMAP3/GETADDR:\n");
122 printf(" Addr:%s\n", gar->addr);
123
124 client->is_finished = 1;
125}
126
4edd7830
RS
127void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
128{
129 struct client *client = private_data;
130
131 if (status == RPC_STATUS_ERROR) {
132 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data);
133 exit(10);
134 }
135 if (status != RPC_STATUS_SUCCESS) {
136 printf("PORTMAP2/NULL call failed, status:%d\n", status);
137 exit(10);
138 }
139
140 printf("PORTMAP2/NULL responded and server is alive\n");
141 client->is_finished = 1;
142}
143
144void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
145{
146 struct client *client = private_data;
147
148 if (status == RPC_STATUS_ERROR) {
149 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data);
150 exit(10);
151 }
152 if (status != RPC_STATUS_SUCCESS) {
153 printf("PORTMAP3/NULL call failed, status:%d\n", status);
154 exit(10);
155 }
156
157 printf("PORTMAP3/NULL responded and server is alive\n");
158 client->is_finished = 1;
159}
160
161void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
162{
163 struct client *client = private_data;
164
165 if (status == RPC_STATUS_ERROR) {
166 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data);
167 exit(10);
168 }
169 if (status != RPC_STATUS_SUCCESS) {
170 printf("PORTMAP/NULL call failed, status:%d\n", status);
171 exit(10);
172 }
173
174 client->is_finished = 1;
175}
176
177void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
178{
179 struct client *client = private_data;
180
181 if (status != RPC_STATUS_SUCCESS) {
182 printf("connection to portmapper failed\n");
183 exit(10);
184 }
185
186 if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) {
187 printf("Failed to send null request\n");
188 exit(10);
189 }
190}
191
192
193static void wait_until_finished(struct rpc_context *rpc, struct client *client)
194{
195 struct pollfd pfd;
196
197 client->is_finished = 0;
198 for (;;) {
199 pfd.fd = rpc_get_fd(rpc);
200 pfd.events = rpc_which_events(rpc);
201
202 if (poll(&pfd, 1, -1) < 0) {
203 printf("Poll failed");
204 exit(10);
205 }
206 if (rpc_service(rpc, pfd.revents) < 0) {
207 printf("rpc_service failed\n");
208 break;
209 }
210 if (client->is_finished) {
211 break;
212 }
213 }
214}
215
216int main(int argc _U_, char *argv[] _U_)
217{
218 struct rpc_context *rpc;
219 struct client client;
220 char *server = NULL;
221 int i;
222 int null2 = 0;
223 int dump2 = 0;
224 int null3 = 0;
7fbedfde 225 int getaddr3 = 0;
4edd7830
RS
226 int dump3 = 0;
227 int command_found = 0;
228
7fbedfde
RS
229 int getaddr3prog, getaddr3vers;
230 char *getaddr3netid;
231
4edd7830
RS
232 rpc = rpc_init_context();
233 if (rpc == NULL) {
234 printf("failed to init context\n");
235 exit(10);
236 }
237
238 for (i = 1; i < argc; i++) {
239 if (!strcmp(argv[i], "dump2")) {
240 dump2 = 1;
241 command_found++;
242 } else if (!strcmp(argv[i], "null2")) {
243 null2 = 1;
244 command_found++;
245 } else if (!strcmp(argv[i], "dump3")) {
246 dump3 = 1;
247 command_found++;
7fbedfde
RS
248 } else if (!strcmp(argv[i], "getaddr3")) {
249 getaddr3 = 1;
250 getaddr3prog = atoi(argv[++i]);
251 getaddr3vers = atoi(argv[++i]);
252 getaddr3netid = argv[++i];
253 command_found++;
4edd7830
RS
254 } else if (!strcmp(argv[i], "null3")) {
255 null3 = 1;
256 command_found++;
257 } else {
258 server = argv[i];
259 }
260 }
261 if (command_found == 0 || server == NULL) {
262 fprintf(stderr, "Usage: portmap-client <command*> <server>\n");
263 exit(10);
264 }
265
266 if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) {
267 printf("Failed to start connection\n");
268 exit(10);
269 }
270 wait_until_finished(rpc, &client);
271
272 if (null2) {
273 if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) {
274 printf("Failed to send NULL2 request\n");
275 exit(10);
276 }
277 wait_until_finished(rpc, &client);
278 }
279 if (dump2) {
280 if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) {
281 printf("Failed to send DUMP2 request\n");
282 exit(10);
283 }
284 wait_until_finished(rpc, &client);
285 }
286 if (null3) {
287 if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) {
288 printf("Failed to send NULL3 request\n");
289 exit(10);
290 }
291 wait_until_finished(rpc, &client);
292 }
293 if (dump3) {
294 if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) {
295 printf("Failed to send DUMP3 request\n");
296 exit(10);
297 }
298 wait_until_finished(rpc, &client);
299 }
7fbedfde
RS
300 if (getaddr3) {
301 struct pmap3_mapping map;
302
303 map.prog=getaddr3prog;
304 map.vers=getaddr3vers;
305 map.netid=getaddr3netid;
306 map.addr="";
307 map.owner="";
308 if (rpc_pmap3_getaddr_async(rpc, &map, pmap3_getaddr_cb, &client) != 0) {
309 printf("Failed to send GETADDR3 request\n");
310 exit(10);
311 }
312 wait_until_finished(rpc, &client);
313 }
4edd7830
RS
314
315
316 rpc_destroy_context(rpc);
317 rpc=NULL;
318 return 0;
319}