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"
29 #include "libnfs-raw.h"
30 #include "libnfs-raw-mount.h"
31 #include "libnfs-raw-nfs.h"
38 struct nfs_fh3 rootfh
;
41 void nfs_fsinfo_cb(struct rpc_context
*rpc _U_
, int status
, void *data
, void *private_data
)
43 struct client
*client
= private_data
;
44 FSINFO3res
*res
= data
;
46 if (status
== RPC_STATUS_ERROR
) {
47 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data
);
50 if (status
!= RPC_STATUS_SUCCESS
) {
51 printf("nfs/fsinfo call to server %s failed, status:%d\n", client
->server
, status
);
55 printf("Got reply from server for NFS/FSINFO procedure.\n");
56 printf("Read Max:%d\n", (int)res
->FSINFO3res_u
.resok
.rtmax
);
57 printf("Write Max:%d\n", (int)res
->FSINFO3res_u
.resok
.wtmax
);
58 client
->is_finished
= 1;
62 void nfs_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.MOUNTD on server %s failed\n", client
->server
);
71 printf("Connected to RPC.NFSDD on %s:%d\n", client
->server
, client
->mount_port
);
72 printf("Send FSINFO request\n");
73 if (rpc_nfs_fsinfo_async(rpc
, nfs_fsinfo_cb
, &client
->rootfh
, client
) != 0) {
74 printf("Failed to send fsinfo request\n");
79 void mount_mnt_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
81 struct client
*client
= private_data
;
82 mountres3
*mnt
= data
;
84 if (status
== RPC_STATUS_ERROR
) {
85 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
88 if (status
!= RPC_STATUS_SUCCESS
) {
89 printf("mount/mnt call to server %s failed, status:%d\n", client
->server
, status
);
93 printf("Got reply from server for MOUNT/MNT procedure.\n");
94 client
->rootfh
.data
.data_len
= mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_len
;
95 client
->rootfh
.data
.data_val
= malloc(client
->rootfh
.data
.data_len
);
96 memcpy(client
->rootfh
.data
.data_val
, mnt
->mountres3_u
.mountinfo
.fhandle
.fhandle3_val
, client
->rootfh
.data
.data_len
);
98 printf("Disconnect socket from mountd server\n");
99 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
100 printf("Failed to disconnect socket to mountd\n");
104 printf("Connect to RPC.NFSDD on %s:%d\n", client
->server
, 2049);
105 if (rpc_connect_async(rpc
, client
->server
, 2049, nfs_connect_cb
, client
) != 0) {
106 printf("Failed to start connection\n");
112 void mount_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
114 struct client
*client
= private_data
;
116 if (status
== RPC_STATUS_ERROR
) {
117 printf("mount null call failed with \"%s\"\n", (char *)data
);
120 if (status
!= RPC_STATUS_SUCCESS
) {
121 printf("mount null call to server %s failed, status:%d\n", client
->server
, status
);
125 printf("Got reply from server for MOUNT/NULL procedure.\n");
126 printf("Send MOUNT/MNT command for %s\n", client
->export
);
127 if (rpc_mount_mnt_async(rpc
, mount_mnt_cb
, client
->export
, client
) != 0) {
128 printf("Failed to send mnt request\n");
133 void mount_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
135 struct client
*client
= private_data
;
137 if (status
!= RPC_STATUS_SUCCESS
) {
138 printf("connection to RPC.MOUNTD on server %s failed\n", client
->server
);
142 printf("Connected to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
143 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
144 if (rpc_mount_null_async(rpc
, mount_null_cb
, client
) != 0) {
145 printf("Failed to send null request\n");
151 void pmap_getport_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
153 struct client
*client
= private_data
;
155 if (status
== RPC_STATUS_ERROR
) {
156 printf("portmapper getport call failed with \"%s\"\n", (char *)data
);
159 if (status
!= RPC_STATUS_SUCCESS
) {
160 printf("portmapper getport call to server %s failed, status:%d\n", client
->server
, status
);
164 client
->mount_port
= *(uint32_t *)data
;
165 printf("GETPORT returned Port:%d\n", client
->mount_port
);
166 if (client
->mount_port
== 0) {
167 printf("RPC.MOUNTD is not available on server : %s:%d\n", client
->server
, client
->mount_port
);
171 printf("Disconnect socket from portmap server\n");
172 if (rpc_disconnect(rpc
, "normal disconnect") != 0) {
173 printf("Failed to disconnect socket to portmapper\n");
177 printf("Connect to RPC.MOUNTD on %s:%d\n", client
->server
, client
->mount_port
);
178 if (rpc_connect_async(rpc
, client
->server
, client
->mount_port
, mount_connect_cb
, client
) != 0) {
179 printf("Failed to start connection\n");
185 void pmap_null_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data
)
187 struct client
*client
= private_data
;
189 if (status
== RPC_STATUS_ERROR
) {
190 printf("portmapper null call failed with \"%s\"\n", (char *)data
);
193 if (status
!= RPC_STATUS_SUCCESS
) {
194 printf("portmapper null call to server %s failed, status:%d\n", client
->server
, status
);
198 printf("Got reply from server for PORTMAP/NULL procedure.\n");
199 printf("Send getport request asking for MOUNT port\n");
200 if (rpc_pmap_getport_async(rpc
, MOUNT_PROGRAM
, MOUNT_V3
, pmap_getport_cb
, client
) != 0) {
201 printf("Failed to send getport request\n");
206 void pmap_connect_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
208 struct client
*client
= private_data
;
210 printf("pmap_connect_cb status:%d.\n", status
);
211 if (status
!= RPC_STATUS_SUCCESS
) {
212 printf("connection to portmapper on server %s failed\n", client
->server
);
216 printf("Send NULL request to check if portmapper is actually running\n");
217 if (rpc_pmap_null_async(rpc
, pmap_null_cb
, client
) != 0) {
218 printf("Failed to send null request\n");
224 int main(int argc _U_
, char *argv
[] _U_
)
226 struct rpc_context
*rpc
;
228 struct client client
;
230 rpc
= rpc_init_context();
232 printf("failed to init context\n");
236 client
.server
= SERVER
;
237 client
.export
= EXPORT
;
238 client
.is_finished
= 0;
239 if (rpc_connect_async(rpc
, client
.server
, 111, pmap_connect_cb
, &client
) != 0) {
240 printf("Failed to start connection\n");
245 pfd
.fd
= rpc_get_fd(rpc
);
246 pfd
.events
= rpc_which_events(rpc
);
248 if (poll(&pfd
, 1, -1) < 0) {
249 printf("Poll failed");
252 if (rpc_service(rpc
, pfd
.revents
) < 0) {
253 printf("rpc_service failed\n");
256 if (client
.is_finished
) {
261 rpc_destroy_context(rpc
);
263 printf("nfsclient finished\n");