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
;
122 printf("Got NFSACL/NULL reply\n");
123 printf("Get ACL for root handle\n");
125 args
.dir
= client
->rootfh
;
126 args
.mask
= NFSACL_MASK_ACL_ENTRY
|NFSACL_MASK_ACL_COUNT
|NFSACL_MASK_ACL_DEFAULT_ENTRY
|NFSACL_MASK_ACL_DEFAULT_COUNT
;
127 if (rpc_nfsacl_getacl_async(rpc
, acl_getacl_cb
, &args
, client
) != 0) {
128 printf("Failed to send getacl request\n");
134 void nfs_fsinfo_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
136 struct client
*client
= private_data
;
137 FSINFO3res
*res
= data
;
139 if (status
== RPC_STATUS_ERROR
) {
140 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data
);
143 if (status
!= RPC_STATUS_SUCCESS
) {
144 printf("nfs/fsinfo call to server %s failed, status:%d\n", client
->server
, status
);
148 printf("Got reply from server for NFS/FSINFO procedure.\n");
149 printf("Read Max:%d\n", (int)res
->FSINFO3res_u
.resok
.rtmax
);
150 printf("Write Max:%d\n", (int)res
->FSINFO3res_u
.resok
.wtmax
);
152 printf("Send NFSACL/NULL request\n");
153 if (rpc_nfsacl_null_async(rpc
, acl_null_cb
, client
) != 0) {
154 printf("Failed to send acl/null request\n");
160 void nfs_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
162 struct client
*client
= private_data
;
164 if (status
!= RPC_STATUS_SUCCESS
) {
165 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
169 printf("Connected to RPC.NFSD on %s:%d\n", client
->server
, client
->mount_port
);
170 printf("Send FSINFO request\n");
171 if (rpc_nfs_fsinfo_async(rpc
, nfs_fsinfo_cb
, &client
->rootfh
, client
) != 0) {
172 printf("Failed to send fsinfo request\n");
177 void mount_mnt_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
179 struct client
*client
= private_data
;
180 mountres3
*mnt
= data
;
182 if (status
== RPC_STATUS_ERROR
) {
183 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
186 if (status
!= RPC_STATUS_SUCCESS
) {
187 printf("mount/mnt call to server %s failed, status:%d\n", client
->server
, status
);
191 printf("Got reply from server for MOUNT/MNT procedure.\n");
192 client
->rootfh
.data
.data_len
= mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
193 client
->rootfh
.data
.data_val
= malloc(client
->rootfh
.data
.data_len
);
194 memcpy(client
->rootfh
.data
.data_val
, mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, client
->rootfh
.data
.data_len
);
196 printf("Disconnect socket from mountd server\n");
197 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
198 printf("Failed to disconnect socket to mountd\n");
202 printf("Connect to RPC.NFSD on %s:%d\n", client
->server
, 2049);
203 if (rpc_connect_async(rpc
, client
->server
, 2049, nfs_connect_cb
, client
) != 0) {
204 printf("Failed to start connection\n");
211 void mount_export_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
213 struct client
*client
= private_data
;
214 exports export
= *(exports
*)data
;
216 if (status
== RPC_STATUS_ERROR
) {
217 printf("mount null call failed with \"%s\"\n", (char *)data
);
220 if (status
!= RPC_STATUS_SUCCESS
) {
221 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
225 printf("Got reply from server for MOUNT/EXPORT procedure.\n");
226 while (export
!= NULL
) {
227 printf("Export: %s\n", export
->ex_dir
);
228 export
= export
->ex_next
;
230 printf("Send MOUNT/MNT command for %s\n", client
->export
);
231 if (rpc_mount_mnt_async(rpc
, mount_mnt_cb
, client
->export
, client
) != 0) {
232 printf("Failed to send mnt request\n");
237 void mount_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
239 struct client
*client
= private_data
;
241 if (status
== RPC_STATUS_ERROR
) {
242 printf("mount null call failed with \"%s\"\n", (char *)data
);
245 if (status
!= RPC_STATUS_SUCCESS
) {
246 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
250 printf("Got reply from server for MOUNT/NULL procedure.\n");
251 printf("Send MOUNT/EXPORT command\n");
252 if (rpc_mount_export_async(rpc
, mount_export_cb
, client
) != 0) {
253 printf("Failed to send export request\n");
258 void mount_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
260 struct client
*client
= private_data
;
262 if (status
!= RPC_STATUS_SUCCESS
) {
263 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
267 printf("Connected to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
268 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
269 if (rpc_mount_null_async(rpc
, mount_null_cb
, client
) != 0) {
270 printf("Failed to send null request\n");
276 void pmap_getport2_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
278 struct client
*client
= private_data
;
280 if (status
== RPC_STATUS_ERROR
) {
281 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
284 if (status
!= RPC_STATUS_SUCCESS
) {
285 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
289 client
->mount_port
= *(uint32_t *)data
;
290 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client
->mount_port
);
291 if (client
->mount_port
== 0) {
292 printf("RPC.MOUNTD is not available on server : %s:%d\n", client
->server
, client
->mount_port
);
296 printf("Disconnect socket from portmap server\n");
297 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
298 printf("Failed to disconnect socket to portmapper\n");
302 printf("Connect to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
303 if (rpc_connect_async(rpc
, client
->server
, client
->mount_port
, mount_connect_cb
, client
) != 0) {
304 printf("Failed to start connection\n");
309 void pmap_getport1_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
311 struct client
*client
= private_data
;
313 if (status
== RPC_STATUS_ERROR
) {
314 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
317 if (status
!= RPC_STATUS_SUCCESS
) {
318 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
322 client
->rquota_port
= *(uint32_t *)data
;
323 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client
->rquota_port
);
324 if (client
->rquota_port
== 0) {
325 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client
->server
, client
->rquota_port
);
329 printf("Send getport request asking for MOUNT port\n");
330 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, IPPROTO_TCP
, pmap_getport2_cb
, client
) != 0) {
331 printf("Failed to send getport request\n");
336 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
338 struct client
*client
= private_data
;
340 if (status
== RPC_STATUS_ERROR
) {
341 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
344 if (status
!= RPC_STATUS_SUCCESS
) {
345 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
349 printf("Got reply from server for PORTMAP/NULL procedure.\n");
350 printf("Send getport request asking for MOUNT port\n");
351 if (rpc_pmap_getport_async(rpc
, RQUOTA_PROGRAM
, RQUOTA_V1
, IPPROTO_TCP
, pmap_getport1_cb
, client
) != 0) {
352 printf("Failed to send getport request\n");
357 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
359 struct client
*client
= private_data
;
361 printf("pmap_connect_cb status:%d.\n", status
);
362 if (status
!= RPC_STATUS_SUCCESS
) {
363 printf("connection to portmapper on server %s failed\n", client
->server
);
367 printf("Send NULL request to check if portmapper is actually running\n");
368 if (rpc_pmap_null_async(rpc
, pmap_null_cb
, client
) != 0) {
369 printf("Failed to send null request\n");
375 int main(int argc _U_
, char *argv
[] _U_
)
377 struct rpc_context
*rpc
;
379 struct client client
;
381 rpc
= rpc_init_context();
383 printf("failed to init context\n");
387 client
.server
= SERVER
;
388 client
.export
= EXPORT
;
389 client
.is_finished
= 0;
390 if (rpc_connect_async(rpc
, client
.server
, 111, pmap_connect_cb
, &client
) != 0) {
391 printf("Failed to start connection\n");
396 pfd
.fd
= rpc_get_fd(rpc
);
397 pfd
.events
= rpc_which_events(rpc
);
399 if (poll(&pfd
, 1, -1) < 0) {
400 printf("Poll failed");
403 if (rpc_service(rpc
, pfd
.revents
) < 0) {
404 printf("rpc_service failed\n");
407 if (client
.is_finished
) {
412 rpc_destroy_context(rpc
);
414 printf("nfsclient finished\n");