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 pmap2_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
109 struct client
*client
= private_data
;
111 if (status
== RPC_STATUS_ERROR
) {
112 printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data
);
115 if (status
!= RPC_STATUS_SUCCESS
) {
116 printf("PORTMAP2/NULL call failed, status:%d\n", status
);
120 printf("PORTMAP2/NULL responded and server is alive\n");
121 client
->is_finished
= 1;
124 void pmap3_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
126 struct client
*client
= private_data
;
128 if (status
== RPC_STATUS_ERROR
) {
129 printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data
);
132 if (status
!= RPC_STATUS_SUCCESS
) {
133 printf("PORTMAP3/NULL call failed, status:%d\n", status
);
137 printf("PORTMAP3/NULL responded and server is alive\n");
138 client
->is_finished
= 1;
141 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
143 struct client
*client
= private_data
;
145 if (status
== RPC_STATUS_ERROR
) {
146 printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data
);
149 if (status
!= RPC_STATUS_SUCCESS
) {
150 printf("PORTMAP/NULL call failed, status:%d\n", status
);
154 client
->is_finished
= 1;
157 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
159 struct client
*client
= private_data
;
161 if (status
!= RPC_STATUS_SUCCESS
) {
162 printf("connection to portmapper failed\n");
166 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
167 printf("Failed to send null request\n");
173 static void wait_until_finished(struct rpc_context
*rpc
, struct client
*client
)
177 client
->is_finished
= 0;
179 pfd
.fd
= rpc_get_fd(rpc
);
180 pfd
.events
= rpc_which_events(rpc
);
182 if (poll(&pfd
, 1, -1) < 0) {
183 printf("Poll failed");
186 if (rpc_service(rpc
, pfd
.revents
) < 0) {
187 printf("rpc_service failed\n");
190 if (client
->is_finished
) {
196 int main(int argc _U_
, char *argv
[] _U_
)
198 struct rpc_context
*rpc
;
199 struct client client
;
206 int command_found
= 0;
208 rpc
= rpc_init_context();
210 printf("failed to init context\n");
214 for (i
= 1; i
< argc
; i
++) {
215 if (!strcmp(argv
[i
], "dump2")) {
218 } else if (!strcmp(argv
[i
], "null2")) {
221 } else if (!strcmp(argv
[i
], "dump3")) {
224 } else if (!strcmp(argv
[i
], "null3")) {
231 if (command_found
== 0 || server
== NULL
) {
232 fprintf(stderr
, "Usage: portmap-client <command*> <server>\n");
236 if (rpc_connect_async(rpc
, server
, 111, pmap_connect_cb
, &client
) != 0) {
237 printf("Failed to start connection\n");
240 wait_until_finished(rpc
, &client
);
243 if (rpc_pmap2_null_async(rpc
, pmap2_null_cb
, &client
) != 0) {
244 printf("Failed to send NULL2 request\n");
247 wait_until_finished(rpc
, &client
);
250 if (rpc_pmap2_dump_async(rpc
, pmap2_dump_cb
, &client
) != 0) {
251 printf("Failed to send DUMP2 request\n");
254 wait_until_finished(rpc
, &client
);
257 if (rpc_pmap3_null_async(rpc
, pmap3_null_cb
, &client
) != 0) {
258 printf("Failed to send NULL3 request\n");
261 wait_until_finished(rpc
, &client
);
264 if (rpc_pmap3_dump_async(rpc
, pmap3_dump_cb
, &client
) != 0) {
265 printf("Failed to send DUMP3 request\n");
268 wait_until_finished(rpc
, &client
);
272 rpc_destroy_context(rpc
);