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.
23 #include "win32_compat.h"
27 #define SERVER "10.1.1.27"
28 #define EXPORT "/shared"
34 #include "libnfs-raw.h"
35 #include "libnfs-raw-mount.h"
36 #include "libnfs-raw-nfs.h"
37 #include "libnfs-raw-rquota.h"
45 struct nfs_fh3 rootfh
;
48 void rquota_getquota_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
50 struct client
*client
= private_data
;
51 // GETQUOTA1res *res = data;
53 if (status
== RPC_STATUS_ERROR
) {
54 printf("rquota/getquota call failed with \"%s\"\n", (char *)data
);
57 if (status
!= RPC_STATUS_SUCCESS
) {
58 printf("rquota/getquota call to server %s failed, status:%d\n", client
->server
, status
);
62 printf("rquota responded ok\n");
63 client
->is_finished
= 1;
66 void rquota_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
68 struct client
*client
= private_data
;
70 if (status
!= RPC_STATUS_SUCCESS
) {
71 printf("connection to RPC.RQUOTAD on server %s failed\n", client
->server
);
75 printf("Connected to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
76 printf("Send GETQUOTA request for uid 100\n");
77 if (rpc_rquota1_getquota_async(rpc
, rquota_getquota_cb
, EXPORT
, 100, client
) != 0) {
78 printf("Failed to send fsinfo request\n");
83 void acl_getacl_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
85 struct client
*client
= private_data
;
86 GETACL3res
*res
= data
;
88 printf("Got NFSACL/GETACL reply\n");
90 if (status
== RPC_STATUS_SUCCESS
) {
91 printf("Got an ACL : ACL status:%d\n", res
->status
);
92 if (res
->status
== NFS3_OK
) {
94 printf("ACL MASK 0x%08x\n", res
->GETACL3res_u
.resok
.mask
);
95 printf("NUM ACE %d\n", res
->GETACL3res_u
.resok
.ace_count
);
96 for (i
=0; i
<res
->GETACL3res_u
.resok
.ace
.ace_len
; i
++) {
97 printf("Type:0x%08x\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].type
);
98 printf("ID:%d\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].id
);
99 printf("Perm:0x%08x\n", res
->GETACL3res_u
.resok
.ace
.ace_val
[i
].perm
);
104 printf("Disconnect socket from nfs server\n");
105 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
106 printf("Failed to disconnect socket to nfs\n");
110 printf("Connect to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
111 if (rpc_connect_async(rpc
, client
->server
, client
->rquota_port
, rquota_connect_cb
, client
) != 0) {
112 printf("Failed to start connection\n");
117 void acl_null_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
119 struct client
*client
= private_data
;
121 printf("Got NFSACL/NULL reply\n");
122 printf("Get ACL for root handle\n");
124 if (rpc_nfsacl_getacl_async(rpc
, acl_getacl_cb
, &client
->rootfh
, NFSACL_MASK_ACL_ENTRY
|NFSACL_MASK_ACL_COUNT
|NFSACL_MASK_ACL_DEFAULT_ENTRY
|NFSACL_MASK_ACL_DEFAULT_COUNT
, client
) != 0) {
125 printf("Failed to send getacl request\n");
131 void nfs_fsinfo_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
133 struct client
*client
= private_data
;
134 FSINFO3res
*res
= data
;
136 if (status
== RPC_STATUS_ERROR
) {
137 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data
);
140 if (status
!= RPC_STATUS_SUCCESS
) {
141 printf("nfs/fsinfo call to server %s failed, status:%d\n", client
->server
, status
);
145 printf("Got reply from server for NFS/FSINFO procedure.\n");
146 printf("Read Max:%d\n", (int)res
->FSINFO3res_u
.resok
.rtmax
);
147 printf("Write Max:%d\n", (int)res
->FSINFO3res_u
.resok
.wtmax
);
149 printf("Send NFSACL/NULL request\n");
150 if (rpc_nfsacl_null_async(rpc
, acl_null_cb
, client
) != 0) {
151 printf("Failed to send acl/null request\n");
157 void nfs_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 RPC.MOUNTD on server %s failed\n", client
->server
);
166 printf("Connected to RPC.NFSD on %s:%d\n", client
->server
, client
->mount_port
);
167 printf("Send FSINFO request\n");
168 if (rpc_nfs_fsinfo_async(rpc
, nfs_fsinfo_cb
, &client
->rootfh
, client
) != 0) {
169 printf("Failed to send fsinfo request\n");
174 void mount_mnt_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
176 struct client
*client
= private_data
;
177 mountres3
*mnt
= data
;
179 if (status
== RPC_STATUS_ERROR
) {
180 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
183 if (status
!= RPC_STATUS_SUCCESS
) {
184 printf("mount/mnt call to server %s failed, status:%d\n", client
->server
, status
);
188 printf("Got reply from server for MOUNT/MNT procedure.\n");
189 client
->rootfh
.data
.data_len
= mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
190 client
->rootfh
.data
.data_val
= malloc(client
->rootfh
.data
.data_len
);
191 memcpy(client
->rootfh
.data
.data_val
, mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, client
->rootfh
.data
.data_len
);
193 printf("Disconnect socket from mountd server\n");
194 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
195 printf("Failed to disconnect socket to mountd\n");
199 printf("Connect to RPC.NFSD on %s:%d\n", client
->server
, 2049);
200 if (rpc_connect_async(rpc
, client
->server
, 2049, nfs_connect_cb
, client
) != 0) {
201 printf("Failed to start connection\n");
208 void mount_export_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
210 struct client
*client
= private_data
;
211 exports export
= *(exports
*)data
;
213 if (status
== RPC_STATUS_ERROR
) {
214 printf("mount null call failed with \"%s\"\n", (char *)data
);
217 if (status
!= RPC_STATUS_SUCCESS
) {
218 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
222 printf("Got reply from server for MOUNT/EXPORT procedure.\n");
223 while (export
!= NULL
) {
224 printf("Export: %s\n", export
->ex_dir
);
225 export
= export
->ex_next
;
227 printf("Send MOUNT/MNT command for %s\n", client
->export
);
228 if (rpc_mount_mnt_async(rpc
, mount_mnt_cb
, client
->export
, client
) != 0) {
229 printf("Failed to send mnt request\n");
234 void mount_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
236 struct client
*client
= private_data
;
238 if (status
== RPC_STATUS_ERROR
) {
239 printf("mount null call failed with \"%s\"\n", (char *)data
);
242 if (status
!= RPC_STATUS_SUCCESS
) {
243 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
247 printf("Got reply from server for MOUNT/NULL procedure.\n");
248 printf("Send MOUNT/EXPORT command\n");
249 if (rpc_mount_export_async(rpc
, mount_export_cb
, client
) != 0) {
250 printf("Failed to send export request\n");
255 void mount_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
257 struct client
*client
= private_data
;
259 if (status
!= RPC_STATUS_SUCCESS
) {
260 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
264 printf("Connected to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
265 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
266 if (rpc_mount_null_async(rpc
, mount_null_cb
, client
) != 0) {
267 printf("Failed to send null request\n");
273 void pmap_getport2_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
275 struct client
*client
= private_data
;
277 if (status
== RPC_STATUS_ERROR
) {
278 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
281 if (status
!= RPC_STATUS_SUCCESS
) {
282 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
286 client
->mount_port
= *(uint32_t *)data
;
287 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client
->mount_port
);
288 if (client
->mount_port
== 0) {
289 printf("RPC.MOUNTD is not available on server : %s:%d\n", client
->server
, client
->mount_port
);
293 printf("Disconnect socket from portmap server\n");
294 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
295 printf("Failed to disconnect socket to portmapper\n");
299 printf("Connect to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
300 if (rpc_connect_async(rpc
, client
->server
, client
->mount_port
, mount_connect_cb
, client
) != 0) {
301 printf("Failed to start connection\n");
306 void pmap_getport1_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
308 struct client
*client
= private_data
;
310 if (status
== RPC_STATUS_ERROR
) {
311 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
314 if (status
!= RPC_STATUS_SUCCESS
) {
315 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
319 client
->rquota_port
= *(uint32_t *)data
;
320 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client
->rquota_port
);
321 if (client
->rquota_port
== 0) {
322 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client
->server
, client
->rquota_port
);
326 printf("Send getport request asking for MOUNT port\n");
327 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, pmap_getport2_cb
, client
) != 0) {
328 printf("Failed to send getport request\n");
333 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
335 struct client
*client
= private_data
;
337 if (status
== RPC_STATUS_ERROR
) {
338 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
341 if (status
!= RPC_STATUS_SUCCESS
) {
342 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
346 printf("Got reply from server for PORTMAP/NULL procedure.\n");
347 printf("Send getport request asking for MOUNT port\n");
348 if (rpc_pmap_getport_async(rpc
, RQUOTA_PROGRAM
, RQUOTA_V1
, pmap_getport1_cb
, client
) != 0) {
349 printf("Failed to send getport request\n");
354 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
356 struct client
*client
= private_data
;
358 printf("pmap_connect_cb status:%d.\n", status
);
359 if (status
!= RPC_STATUS_SUCCESS
) {
360 printf("connection to portmapper on server %s failed\n", client
->server
);
364 printf("Send NULL request to check if portmapper is actually running\n");
365 if (rpc_pmap_null_async(rpc
, pmap_null_cb
, client
) != 0) {
366 printf("Failed to send null request\n");
372 int main(int argc _U_
, char *argv
[] _U_
)
374 struct rpc_context
*rpc
;
376 struct client client
;
378 rpc
= rpc_init_context();
380 printf("failed to init context\n");
384 client
.server
= SERVER
;
385 client
.export
= EXPORT
;
386 client
.is_finished
= 0;
387 if (rpc_connect_async(rpc
, client
.server
, 111, pmap_connect_cb
, &client
) != 0) {
388 printf("Failed to start connection\n");
393 pfd
.fd
= rpc_get_fd(rpc
);
394 pfd
.events
= rpc_which_events(rpc
);
396 if (poll(&pfd
, 1, -1) < 0) {
397 printf("Poll failed");
400 if (rpc_service(rpc
, pfd
.revents
) < 0) {
401 printf("rpc_service failed\n");
404 if (client
.is_finished
) {
409 rpc_destroy_context(rpc
);
411 printf("nfsclient finished\n");