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.
22 #define SERVER "10.1.1.27"
23 #define EXPORT "/shared"
30 #include "libnfs-raw.h"
31 #include "libnfs-raw-mount.h"
32 #include "libnfs-raw-nfs.h"
33 #include "libnfs-raw-rquota.h"
41 struct nfs_fh3 rootfh
;
44 void rquota_getquota_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
46 struct client
*client
= private_data
;
47 // GETQUOTA1res *res = data;
49 if (status
== RPC_STATUS_ERROR
) {
50 printf("rquota/getquota call failed with \"%s\"\n", (char *)data
);
53 if (status
!= RPC_STATUS_SUCCESS
) {
54 printf("rquota/getquota call to server %s failed, status:%d\n", client
->server
, status
);
58 printf("rquota responded ok\n");
59 client
->is_finished
= 1;
62 void rquota_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
64 struct client
*client
= private_data
;
66 if (status
!= RPC_STATUS_SUCCESS
) {
67 printf("connection to RPC.RQUOTAD on server %s failed\n", client
->server
);
71 printf("Connected to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
72 printf("Send GETQUOTA request for uid 100\n");
73 if (rpc_rquota1_getquota_async(rpc
, rquota_getquota_cb
, EXPORT
, 100, client
) != 0) {
74 printf("Failed to send fsinfo request\n");
79 void nfs_fsinfo_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
81 struct client
*client
= private_data
;
82 FSINFO3res
*res
= data
;
84 if (status
== RPC_STATUS_ERROR
) {
85 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data
);
88 if (status
!= RPC_STATUS_SUCCESS
) {
89 printf("nfs/fsinfo call to server %s failed, status:%d\n", client
->server
, status
);
93 printf("Got reply from server for NFS/FSINFO procedure.\n");
94 printf("Read Max:%d\n", (int)res
->FSINFO3res_u
.resok
.rtmax
);
95 printf("Write Max:%d\n", (int)res
->FSINFO3res_u
.resok
.wtmax
);
97 printf("Disconnect socket from nfs server\n");
98 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
99 printf("Failed to disconnect socket to nfs\n");
103 printf("Connect to RPC.RQUOTAD on %s:%d\n", client
->server
, client
->rquota_port
);
104 if (rpc_connect_async(rpc
, client
->server
, client
->rquota_port
, rquota_connect_cb
, client
) != 0) {
105 printf("Failed to start connection\n");
111 void nfs_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
113 struct client
*client
= private_data
;
115 if (status
!= RPC_STATUS_SUCCESS
) {
116 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
120 printf("Connected to RPC.NFSD on %s:%d\n", client
->server
, client
->mount_port
);
121 printf("Send FSINFO request\n");
122 if (rpc_nfs_fsinfo_async(rpc
, nfs_fsinfo_cb
, &client
->rootfh
, client
) != 0) {
123 printf("Failed to send fsinfo request\n");
128 void mount_mnt_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
130 struct client
*client
= private_data
;
131 mountres3
*mnt
= data
;
133 if (status
== RPC_STATUS_ERROR
) {
134 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
137 if (status
!= RPC_STATUS_SUCCESS
) {
138 printf("mount/mnt call to server %s failed, status:%d\n", client
->server
, status
);
142 printf("Got reply from server for MOUNT/MNT procedure.\n");
143 client
->rootfh
.data
.data_len
= mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
144 client
->rootfh
.data
.data_val
= malloc(client
->rootfh
.data
.data_len
);
145 memcpy(client
->rootfh
.data
.data_val
, mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, client
->rootfh
.data
.data_len
);
147 printf("Disconnect socket from mountd server\n");
148 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
149 printf("Failed to disconnect socket to mountd\n");
153 printf("Connect to RPC.NFSD on %s:%d\n", client
->server
, 2049);
154 if (rpc_connect_async(rpc
, client
->server
, 2049, nfs_connect_cb
, client
) != 0) {
155 printf("Failed to start connection\n");
162 void mount_export_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
164 struct client
*client
= private_data
;
165 exports export
= *(exports
*)data
;
167 if (status
== RPC_STATUS_ERROR
) {
168 printf("mount null call failed with \"%s\"\n", (char *)data
);
171 if (status
!= RPC_STATUS_SUCCESS
) {
172 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
176 printf("Got reply from server for MOUNT/EXPORT procedure.\n");
177 while (export
!= NULL
) {
178 printf("Export: %s\n", export
->ex_dir
);
179 export
= export
->ex_next
;
181 printf("Send MOUNT/MNT command for %s\n", client
->export
);
182 if (rpc_mount_mnt_async(rpc
, mount_mnt_cb
, client
->export
, client
) != 0) {
183 printf("Failed to send mnt request\n");
188 void mount_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
190 struct client
*client
= private_data
;
192 if (status
== RPC_STATUS_ERROR
) {
193 printf("mount null call failed with \"%s\"\n", (char *)data
);
196 if (status
!= RPC_STATUS_SUCCESS
) {
197 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
201 printf("Got reply from server for MOUNT/NULL procedure.\n");
202 printf("Send MOUNT/EXPORT command\n");
203 if (rpc_mount_export_async(rpc
, mount_export_cb
, client
) != 0) {
204 printf("Failed to send export request\n");
209 void mount_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
211 struct client
*client
= private_data
;
213 if (status
!= RPC_STATUS_SUCCESS
) {
214 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
218 printf("Connected to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
219 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
220 if (rpc_mount_null_async(rpc
, mount_null_cb
, client
) != 0) {
221 printf("Failed to send null request\n");
227 void pmap_getport2_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
229 struct client
*client
= private_data
;
231 if (status
== RPC_STATUS_ERROR
) {
232 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
235 if (status
!= RPC_STATUS_SUCCESS
) {
236 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
240 client
->mount_port
= *(uint32_t *)data
;
241 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client
->mount_port
);
242 if (client
->mount_port
== 0) {
243 printf("RPC.MOUNTD is not available on server : %s:%d\n", client
->server
, client
->mount_port
);
247 printf("Disconnect socket from portmap server\n");
248 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
249 printf("Failed to disconnect socket to portmapper\n");
253 printf("Connect to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
254 if (rpc_connect_async(rpc
, client
->server
, client
->mount_port
, mount_connect_cb
, client
) != 0) {
255 printf("Failed to start connection\n");
260 void pmap_getport1_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
262 struct client
*client
= private_data
;
264 if (status
== RPC_STATUS_ERROR
) {
265 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
268 if (status
!= RPC_STATUS_SUCCESS
) {
269 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
273 client
->rquota_port
= *(uint32_t *)data
;
274 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client
->rquota_port
);
275 if (client
->rquota_port
== 0) {
276 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client
->server
, client
->rquota_port
);
280 printf("Send getport request asking for MOUNT port\n");
281 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, pmap_getport2_cb
, client
) != 0) {
282 printf("Failed to send getport request\n");
287 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
289 struct client
*client
= private_data
;
291 if (status
== RPC_STATUS_ERROR
) {
292 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
295 if (status
!= RPC_STATUS_SUCCESS
) {
296 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
300 printf("Got reply from server for PORTMAP/NULL procedure.\n");
301 printf("Send getport request asking for MOUNT port\n");
302 if (rpc_pmap_getport_async(rpc
, RQUOTA_PROGRAM
, RQUOTA_V1
, pmap_getport1_cb
, client
) != 0) {
303 printf("Failed to send getport request\n");
308 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
310 struct client
*client
= private_data
;
312 printf("pmap_connect_cb status:%d.\n", status
);
313 if (status
!= RPC_STATUS_SUCCESS
) {
314 printf("connection to portmapper on server %s failed\n", client
->server
);
318 printf("Send NULL request to check if portmapper is actually running\n");
319 if (rpc_pmap_null_async(rpc
, pmap_null_cb
, client
) != 0) {
320 printf("Failed to send null request\n");
326 int main(int argc _U_
, char *argv
[] _U_
)
328 struct rpc_context
*rpc
;
330 struct client client
;
332 rpc
= rpc_init_context();
334 printf("failed to init context\n");
338 client
.server
= SERVER
;
339 client
.export
= EXPORT
;
340 client
.is_finished
= 0;
341 if (rpc_connect_async(rpc
, client
.server
, 111, pmap_connect_cb
, &client
) != 0) {
342 printf("Failed to start connection\n");
347 pfd
.fd
= rpc_get_fd(rpc
);
348 pfd
.events
= rpc_which_events(rpc
);
350 if (poll(&pfd
, 1, -1) < 0) {
351 printf("Poll failed");
354 if (rpc_service(rpc
, pfd
.revents
) < 0) {
355 printf("rpc_service failed\n");
358 if (client
.is_finished
) {
363 rpc_destroy_context(rpc
);
365 printf("nfsclient finished\n");