2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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"
28 #define SERVER "10.1.1.27"
29 #define EXPORT "/shared"
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.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"
55 struct nfs_fh3 rootfh
;
58 void rquota_getquota_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
60 struct client
*client
= private_data
;
61 // GETQUOTA1res *res = data;
63 if (status
== RPC_STATUS_ERROR
) {
64 printf("rquota/getquota call failed with \"%s\"\n", (char *)data
);
67 if (status
!= RPC_STATUS_SUCCESS
) {
68 printf("rquota/getquota call to server %s failed, status:%d\n", client
->server
, status
);
72 printf("rquota responded ok\n");
73 client
->is_finished
= 1;
76 void rquota_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
78 struct client
*client
= private_data
;
80 if (status
!= RPC_STATUS_SUCCESS
) {
81 printf("connection to RPC.RQUOTAD on server %s failed\n", client
->server
);
85 printf("Connected to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
86 printf("Send GETQUOTA request for uid 100\n");
87 if (rpc_rquota1_getquota_async(rpc
, rquota_getquota_cb
, EXPORT
, 100, client
) != 0) {
88 printf("Failed to send getquota request\n");
93 void acl_getacl_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
95 struct client
*client
= private_data
;
96 GETACL3res
*res
= data
;
98 printf("Got NFSACL/GETACL reply\n");
100 if (status
== RPC_STATUS_SUCCESS
) {
101 printf("Got an ACL : ACL status:%d\n", res
->status
);
102 if (res
->status
== NFS3_OK
) {
104 printf("ACL MASK 0x%08x\n", res
->GETACL3res_u
.resok
.mask
);
105 printf("NUM ACE %d\n", res
->GETACL3res_u
.resok
.ace_count
);
106 for (i
=0; i
<res
->GETACL3res_u
.resok
.ace
.ace_len
; i
++) {
107 printf("Type:0x%08x\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].type
);
108 printf("ID:%d\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].id
);
109 printf("Perm:0x%08x\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].perm
);
114 printf("Disconnect socket from nfs server\n");
115 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
116 printf("Failed to disconnect socket to nfs\n");
120 printf("Connect to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
121 if (rpc_connect_async(rpc
, client
->server
, client
->rquota_port
, rquota_connect_cb
, client
) != 0) {
122 printf("Failed to start connection\n");
127 void acl_null_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
129 struct client
*client
= private_data
;
132 printf("Got NFSACL/NULL reply\n");
133 printf("Get ACL for root handle\n");
135 args
.dir
= client
->rootfh
;
136 args
.mask
= NFSACL_MASK_ACL_ENTRY
|NFSACL_MASK_ACL_COUNT
|NFSACL_MASK_ACL_DEFAULT_ENTRY
|NFSACL_MASK_ACL_DEFAULT_COUNT
;
137 if (rpc_nfsacl_getacl_async(rpc
, acl_getacl_cb
, &args
, client
) != 0) {
138 printf("Failed to send getacl request\n");
144 void nfs_fsinfo_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
146 struct client
*client
= private_data
;
147 FSINFO3res
*res
= data
;
149 if (status
== RPC_STATUS_ERROR
) {
150 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data
);
153 if (status
!= RPC_STATUS_SUCCESS
) {
154 printf("nfs/fsinfo call to server %s failed, status:%d\n", client
->server
, status
);
158 printf("Got reply from server for NFS/FSINFO procedure.\n");
159 printf("Read Max:%d\n", (int)res
->FSINFO3res_u
.resok
.rtmax
);
160 printf("Write Max:%d\n", (int)res
->FSINFO3res_u
.resok
.wtmax
);
162 printf("Send NFSACL/NULL request\n");
163 if (rpc_nfsacl_null_async(rpc
, acl_null_cb
, client
) != 0) {
164 printf("Failed to send acl/null request\n");
170 void nfs_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
172 struct client
*client
= private_data
;
173 struct FSINFO3args args
;
175 if (status
!= RPC_STATUS_SUCCESS
) {
176 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
180 printf("Connected to RPC.NFSD on %s:%d\n", client
->server
, client
->mount_port
);
181 printf("Send FSINFO request\n");
182 args
.fsroot
= client
->rootfh
;
183 if (rpc_nfs3_fsinfo_async(rpc
, nfs_fsinfo_cb
, &args
, client
) != 0) {
184 printf("Failed to send fsinfo request\n");
189 void mount_mnt_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
191 struct client
*client
= private_data
;
192 mountres3
*mnt
= data
;
194 if (status
== RPC_STATUS_ERROR
) {
195 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
198 if (status
!= RPC_STATUS_SUCCESS
) {
199 printf("mount/mnt call to server %s failed, status:%d\n", client
->server
, status
);
203 printf("Got reply from server for MOUNT/MNT procedure.\n");
204 client
->rootfh
.data
.data_len
= mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
205 client
->rootfh
.data
.data_val
= malloc(client
->rootfh
.data
.data_len
);
206 memcpy(client
->rootfh
.data
.data_val
, mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, client
->rootfh
.data
.data_len
);
208 printf("Disconnect socket from mountd server\n");
209 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
210 printf("Failed to disconnect socket to mountd\n");
214 printf("Connect to RPC.NFSD on %s:%d\n", client
->server
, 2049);
215 if (rpc_connect_async(rpc
, client
->server
, 2049, nfs_connect_cb
, client
) != 0) {
216 printf("Failed to start connection\n");
223 void mount_export_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
225 struct client
*client
= private_data
;
226 exports export
= *(exports
*)data
;
228 if (status
== RPC_STATUS_ERROR
) {
229 printf("mount null call failed with \"%s\"\n", (char *)data
);
232 if (status
!= RPC_STATUS_SUCCESS
) {
233 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
237 printf("Got reply from server for MOUNT/EXPORT procedure.\n");
238 while (export
!= NULL
) {
239 printf("Export: %s\n", export
->ex_dir
);
240 export
= export
->ex_next
;
242 printf("Send MOUNT/MNT command for %s\n", client
->export
);
243 if (rpc_mount_mnt_async(rpc
, mount_mnt_cb
, client
->export
, client
) != 0) {
244 printf("Failed to send mnt request\n");
249 void mount_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
251 struct client
*client
= private_data
;
253 if (status
== RPC_STATUS_ERROR
) {
254 printf("mount null call failed with \"%s\"\n", (char *)data
);
257 if (status
!= RPC_STATUS_SUCCESS
) {
258 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
262 printf("Got reply from server for MOUNT/NULL procedure.\n");
263 printf("Send MOUNT/EXPORT command\n");
264 if (rpc_mount_export_async(rpc
, mount_export_cb
, client
) != 0) {
265 printf("Failed to send export request\n");
270 void mount_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
272 struct client
*client
= private_data
;
274 if (status
!= RPC_STATUS_SUCCESS
) {
275 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
279 printf("Connected to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
280 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
281 if (rpc_mount_null_async(rpc
, mount_null_cb
, client
) != 0) {
282 printf("Failed to send null request\n");
288 void pmap_getport2_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
290 struct client
*client
= private_data
;
292 if (status
== RPC_STATUS_ERROR
) {
293 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
296 if (status
!= RPC_STATUS_SUCCESS
) {
297 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
301 client
->mount_port
= *(uint32_t *)data
;
302 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client
->mount_port
);
303 if (client
->mount_port
== 0) {
304 printf("RPC.MOUNTD is not available on server : %s:%d\n", client
->server
, client
->mount_port
);
308 printf("Disconnect socket from portmap server\n");
309 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
310 printf("Failed to disconnect socket to portmapper\n");
314 printf("Connect to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
315 if (rpc_connect_async(rpc
, client
->server
, client
->mount_port
, mount_connect_cb
, client
) != 0) {
316 printf("Failed to start connection\n");
321 void pmap_getport1_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
323 struct client
*client
= private_data
;
325 if (status
== RPC_STATUS_ERROR
) {
326 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
329 if (status
!= RPC_STATUS_SUCCESS
) {
330 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
334 client
->rquota_port
= *(uint32_t *)data
;
335 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client
->rquota_port
);
336 if (client
->rquota_port
== 0) {
337 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client
->server
, client
->rquota_port
);
341 printf("Send getport request asking for MOUNT port\n");
342 if (rpc_pmap2_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, pmap_getport2_cb
, client
) != 0) {
343 printf("Failed to send getport request\n");
348 void pmap_dump_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
350 struct client
*client
= private_data
;
351 struct pmap2_dump_result
*dr
= data
;
352 struct pmap2_mapping_list
*list
= dr
->list
;
354 if (status
== RPC_STATUS_ERROR
) {
355 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
358 if (status
!= RPC_STATUS_SUCCESS
) {
359 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
363 printf("Got reply from server for PORTMAP/DUMP procedure.\n");
365 printf("Prog:%d Vers:%d Protocol:%d Port:%d\n",
373 printf("Send getport request asking for MOUNT port\n");
374 if (rpc_pmap2_getport_async(rpc
, RQUOTA_PROGRAM
, RQUOTA_V1
, IPPROTO_TCP
, pmap_getport1_cb
, client
) != 0) {
375 printf("Failed to send getport request\n");
380 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
382 struct client
*client
= private_data
;
384 if (status
== RPC_STATUS_ERROR
) {
385 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
388 if (status
!= RPC_STATUS_SUCCESS
) {
389 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
393 printf("Got reply from server for PORTMAP/NULL procedure.\n");
394 printf("Send PMAP/DUMP command\n");
395 if (rpc_pmap2_dump_async(rpc
, pmap_dump_cb
, client
) != 0) {
396 printf("Failed to send getport request\n");
401 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
403 struct client
*client
= private_data
;
405 printf("pmap_connect_cb status:%d.\n", status
);
406 if (status
!= RPC_STATUS_SUCCESS
) {
407 printf("connection to portmapper on server %s failed\n", client
->server
);
411 printf("Send NULL request to check if portmapper is actually running\n");
412 if (rpc_pmap2_null_async(rpc
, pmap_null_cb
, client
) != 0) {
413 printf("Failed to send null request\n");
419 int main(int argc _U_
, char *argv
[] _U_
)
421 struct rpc_context
*rpc
;
423 struct client client
;
425 rpc
= rpc_init_context();
427 printf("failed to init context\n");
431 client
.server
= SERVER
;
432 client
.export
= EXPORT
;
433 client
.is_finished
= 0;
434 if (rpc_connect_async(rpc
, client
.server
, 111, pmap_connect_cb
, &client
) != 0) {
435 printf("Failed to start connection\n");
440 pfd
.fd
= rpc_get_fd(rpc
);
441 pfd
.events
= rpc_which_events(rpc
);
443 if (poll(&pfd
, 1, -1) < 0) {
444 printf("Poll failed");
447 if (rpc_service(rpc
, pfd
.revents
) < 0) {
448 printf("rpc_service failed\n");
451 if (client
.is_finished
) {
456 rpc_destroy_context(rpc
);
458 printf("nfsclient finished\n");