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_getaddr_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_gettime_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
130 struct client
*client
= private_data
;
131 time_t t
= *(uint32_t *)data
;
133 if (status
== RPC_STATUS_ERROR
) {
134 printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data
);
137 if (status
!= RPC_STATUS_SUCCESS
) {
138 printf("PORTMAP3/GETTIME call failed, status:%d\n", status
);
142 printf("PORTMAP3/GETTIME:\n");
143 printf(" Time:%d %s\n", (int)t
, ctime(&t
));
145 client
->is_finished
= 1;
148 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
150 struct client
*client
= private_data
;
152 if (status
== RPC_STATUS_ERROR
) {
153 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
156 if (status
!= RPC_STATUS_SUCCESS
) {
157 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
161 printf("PORTMAP2/NULL responded and server is alive\n");
162 client
->is_finished
= 1;
165 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
167 struct client
*client
= private_data
;
169 if (status
== RPC_STATUS_ERROR
) {
170 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
173 if (status
!= RPC_STATUS_SUCCESS
) {
174 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
178 printf("PORTMAP3/NULL responded and server is alive\n");
179 client
->is_finished
= 1;
182 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
184 struct client
*client
= private_data
;
186 if (status
== RPC_STATUS_ERROR
) {
187 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
190 if (status
!= RPC_STATUS_SUCCESS
) {
191 printf("PORTMAP/NULL call failed, status:%d\n", status
);
195 client
->is_finished
= 1;
198 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
200 struct client
*client
= private_data
;
202 if (status
!= RPC_STATUS_SUCCESS
) {
203 printf("connection to portmapper failed\n");
207 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
208 printf("Failed to send null request\n");
214 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
218 client
->is_finished
= 0;
220 pfd
.fd
= rpc_get_fd(rpc
);
221 pfd
.events
= rpc_which_events(rpc
);
223 if (poll(&pfd
, 1, -1) < 0) {
224 printf("Poll failed");
227 if (rpc_service(rpc
, pfd
.revents
) < 0) {
228 printf("rpc_service failed\n");
231 if (client
->is_finished
) {
237 int main(int argc _U_
, char *argv
[] _U_
)
239 struct rpc_context
*rpc
;
240 struct client client
;
249 int command_found
= 0;
251 int getaddr3prog
, getaddr3vers
;
254 rpc
= rpc_init_context();
256 printf("failed to init context\n");
260 for (i
= 1; i
< argc
; i
++) {
261 if (!strcmp(argv
[i
], "dump2")) {
264 } else if (!strcmp(argv
[i
], "null2")) {
267 } else if (!strcmp(argv
[i
], "dump3")) {
270 } else if (!strcmp(argv
[i
], "gettime3")) {
273 } else if (!strcmp(argv
[i
], "getaddr3")) {
275 getaddr3prog
= atoi(argv
[++i
]);
276 getaddr3vers
= atoi(argv
[++i
]);
277 getaddr3netid
= argv
[++i
];
279 } else if (!strcmp(argv
[i
], "null3")) {
286 if (command_found
== 0 || server
== NULL
) {
287 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
291 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
292 printf("Failed to start connection\n");
295 wait_until_finished(rpc
, &client
);
298 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
299 printf("Failed to send NULL2 request\n");
302 wait_until_finished(rpc
, &client
);
305 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
306 printf("Failed to send DUMP2 request\n");
309 wait_until_finished(rpc
, &client
);
312 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
313 printf("Failed to send NULL3 request\n");
316 wait_until_finished(rpc
, &client
);
319 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
320 printf("Failed to send DUMP3 request\n");
323 wait_until_finished(rpc
, &client
);
326 if (rpc_pmap3_gettime_async(rpc
, pmap3_gettime_cb
, &client
) != 0) {
327 printf("Failed to send GETTIME3 request\n");
330 wait_until_finished(rpc
, &client
);
333 struct pmap3_mapping map
;
335 map
.prog
=getaddr3prog
;
336 map
.vers
=getaddr3vers
;
337 map
.netid
=getaddr3netid
;
340 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
341 printf("Failed to send GETADDR3 request\n");
344 wait_until_finished(rpc
, &client
);
348 rpc_destroy_context(rpc
);