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>
40 #include "libnfs-zdr.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"
52 void pmap2_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
54 struct client
*client
= private_data
;
55 struct pmap2_dump_result
*dr
= data
;
56 struct pmap2_mapping_list
*list
= dr
->list
;
58 if (status
== RPC_STATUS_ERROR
) {
59 printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data
);
62 if (status
!= RPC_STATUS_SUCCESS
) {
63 printf("PORTMAP2/DUMP call failed, status:%d\n", status
);
67 printf("PORTMAP2/DUMP:\n");
69 printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n",
76 client
->is_finished
= 1;
79 void pmap3_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
81 struct client
*client
= private_data
;
82 struct pmap3_dump_result
*dr
= data
;
83 struct pmap3_mapping_list
*list
= dr
->list
;
85 if (status
== RPC_STATUS_ERROR
) {
86 printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data
);
89 if (status
!= RPC_STATUS_SUCCESS
) {
90 printf("PORTMAP3/DUMP call failed, status:%d\n", status
);
94 printf("PORTMAP3/DUMP:\n");
96 printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
104 client
->is_finished
= 1;
107 void pmap3_getaddr_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
109 struct client
*client
= private_data
;
110 struct pmap3_getaddr_result
*gar
= data
;
112 if (status
== RPC_STATUS_ERROR
) {
113 printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data
);
116 if (status
!= RPC_STATUS_SUCCESS
) {
117 printf("PORTMAP3/GETADDR call failed, status:%d\n", status
);
121 printf("PORTMAP3/GETADDR:\n");
122 printf(" Addr:%s\n", gar
->addr
);
124 client
->is_finished
= 1;
127 void pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
129 struct client
*client
= private_data
;
131 if (status
== RPC_STATUS_ERROR
) {
132 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
135 if (status
!= RPC_STATUS_SUCCESS
) {
136 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
140 printf("PORTMAP2/NULL responded and server is alive\n");
141 client
->is_finished
= 1;
144 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
146 struct client
*client
= private_data
;
148 if (status
== RPC_STATUS_ERROR
) {
149 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
152 if (status
!= RPC_STATUS_SUCCESS
) {
153 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
157 printf("PORTMAP3/NULL responded and server is alive\n");
158 client
->is_finished
= 1;
161 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
163 struct client
*client
= private_data
;
165 if (status
== RPC_STATUS_ERROR
) {
166 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
169 if (status
!= RPC_STATUS_SUCCESS
) {
170 printf("PORTMAP/NULL call failed, status:%d\n", status
);
174 client
->is_finished
= 1;
177 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
179 struct client
*client
= private_data
;
181 if (status
!= RPC_STATUS_SUCCESS
) {
182 printf("connection to portmapper failed\n");
186 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
187 printf("Failed to send null request\n");
193 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
197 client
->is_finished
= 0;
199 pfd
.fd
= rpc_get_fd(rpc
);
200 pfd
.events
= rpc_which_events(rpc
);
202 if (poll(&pfd
, 1, -1) < 0) {
203 printf("Poll failed");
206 if (rpc_service(rpc
, pfd
.revents
) < 0) {
207 printf("rpc_service failed\n");
210 if (client
->is_finished
) {
216 int main(int argc _U_
, char *argv
[] _U_
)
218 struct rpc_context
*rpc
;
219 struct client client
;
227 int command_found
= 0;
229 int getaddr3prog
, getaddr3vers
;
232 rpc
= rpc_init_context();
234 printf("failed to init context\n");
238 for (i
= 1; i
< argc
; i
++) {
239 if (!strcmp(argv
[i
], "dump2")) {
242 } else if (!strcmp(argv
[i
], "null2")) {
245 } else if (!strcmp(argv
[i
], "dump3")) {
248 } else if (!strcmp(argv
[i
], "getaddr3")) {
250 getaddr3prog
= atoi(argv
[++i
]);
251 getaddr3vers
= atoi(argv
[++i
]);
252 getaddr3netid
= argv
[++i
];
254 } else if (!strcmp(argv
[i
], "null3")) {
261 if (command_found
== 0 || server
== NULL
) {
262 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
266 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
267 printf("Failed to start connection\n");
270 wait_until_finished(rpc
, &client
);
273 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
274 printf("Failed to send NULL2 request\n");
277 wait_until_finished(rpc
, &client
);
280 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
281 printf("Failed to send DUMP2 request\n");
284 wait_until_finished(rpc
, &client
);
287 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
288 printf("Failed to send NULL3 request\n");
291 wait_until_finished(rpc
, &client
);
294 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
295 printf("Failed to send DUMP3 request\n");
298 wait_until_finished(rpc
, &client
);
301 struct pmap3_mapping map
;
303 map
.prog
=getaddr3prog
;
304 map
.vers
=getaddr3vers
;
305 map
.netid
=getaddr3netid
;
308 if (rpc_pmap3_getaddr_async(rpc
, &map
, pmap3_getaddr_cb
, &client
) != 0) {
309 printf("Failed to send GETADDR3 request\n");
312 wait_until_finished(rpc
, &client
);
316 rpc_destroy_context(rpc
);