Merge pull request #1 from Memphiz/originalnfs
[deb_libnfs.git] / examples / nfsclient-raw.c
1 /*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
3
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.
8
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.
13
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/>.
16 */
17
18 /* Example program using the lowlevel raw interface.
19 * This allow accurate control of the exact commands that are being used.
20 */
21
22 #define SERVER "10.1.1.27"
23 #define EXPORT "/shared"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <poll.h>
29 #include "libnfs.h"
30 #include "libnfs-raw.h"
31 #include "libnfs-raw-mount.h"
32 #include "libnfs-raw-nfs.h"
33 #include "libnfs-raw-rquota.h"
34
35 struct client {
36 char *server;
37 char *export;
38 uint32_t mount_port;
39 uint32_t rquota_port;
40 int is_finished;
41 struct nfs_fh3 rootfh;
42 };
43
44 void rquota_getquota_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
45 {
46 struct client *client = private_data;
47 GETQUOTA1res *res = data;
48
49 if (status == RPC_STATUS_ERROR) {
50 printf("rquota/getquota call failed with \"%s\"\n", (char *)data);
51 exit(10);
52 }
53 if (status != RPC_STATUS_SUCCESS) {
54 printf("rquota/getquota call to server %s failed, status:%d\n", client->server, status);
55 exit(10);
56 }
57
58 printf("rquota responded ok\n");
59 client->is_finished = 1;
60 }
61
62 void rquota_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
63 {
64 struct client *client = private_data;
65
66 if (status != RPC_STATUS_SUCCESS) {
67 printf("connection to RPC.RQUOTAD on server %s failed\n", client->server);
68 exit(10);
69 }
70
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");
75 exit(10);
76 }
77 }
78
79 void nfs_fsinfo_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
80 {
81 struct client *client = private_data;
82 FSINFO3res *res = data;
83
84 if (status == RPC_STATUS_ERROR) {
85 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data);
86 exit(10);
87 }
88 if (status != RPC_STATUS_SUCCESS) {
89 printf("nfs/fsinfo call to server %s failed, status:%d\n", client->server, status);
90 exit(10);
91 }
92
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);
96
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");
100 exit(10);
101 }
102
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");
106 exit(10);
107 }
108 }
109
110
111 void nfs_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
112 {
113 struct client *client = private_data;
114
115 if (status != RPC_STATUS_SUCCESS) {
116 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
117 exit(10);
118 }
119
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");
124 exit(10);
125 }
126 }
127
128 void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
129 {
130 struct client *client = private_data;
131 mountres3 *mnt = data;
132
133 if (status == RPC_STATUS_ERROR) {
134 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
135 exit(10);
136 }
137 if (status != RPC_STATUS_SUCCESS) {
138 printf("mount/mnt call to server %s failed, status:%d\n", client->server, status);
139 exit(10);
140 }
141
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);
146
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");
150 exit(10);
151 }
152
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");
156 exit(10);
157 }
158 }
159
160
161 void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
162 {
163 struct client *client = private_data;
164
165 if (status == RPC_STATUS_ERROR) {
166 printf("mount null call failed with \"%s\"\n", (char *)data);
167 exit(10);
168 }
169 if (status != RPC_STATUS_SUCCESS) {
170 printf("mount null call to server %s failed, status:%d\n", client->server, status);
171 exit(10);
172 }
173
174 printf("Got reply from server for MOUNT/NULL procedure.\n");
175 printf("Send MOUNT/MNT command for %s\n", client->export);
176 if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) {
177 printf("Failed to send mnt request\n");
178 exit(10);
179 }
180 }
181
182 void mount_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
183 {
184 struct client *client = private_data;
185
186 if (status != RPC_STATUS_SUCCESS) {
187 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
188 exit(10);
189 }
190
191 printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
192 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
193 if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) {
194 printf("Failed to send null request\n");
195 exit(10);
196 }
197 }
198
199
200 void pmap_getport2_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
201 {
202 struct client *client = private_data;
203
204 if (status == RPC_STATUS_ERROR) {
205 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
206 exit(10);
207 }
208 if (status != RPC_STATUS_SUCCESS) {
209 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
210 exit(10);
211 }
212
213 client->mount_port = *(uint32_t *)data;
214 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client->mount_port);
215 if (client->mount_port == 0) {
216 printf("RPC.MOUNTD is not available on server : %s:%d\n", client->server, client->mount_port);
217 exit(10);
218 }
219
220 printf("Disconnect socket from portmap server\n");
221 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
222 printf("Failed to disconnect socket to portmapper\n");
223 exit(10);
224 }
225
226 printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
227 if (rpc_connect_async(rpc, client->server, client->mount_port, mount_connect_cb, client) != 0) {
228 printf("Failed to start connection\n");
229 exit(10);
230 }
231 }
232
233 void pmap_getport1_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
234 {
235 struct client *client = private_data;
236
237 if (status == RPC_STATUS_ERROR) {
238 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
239 exit(10);
240 }
241 if (status != RPC_STATUS_SUCCESS) {
242 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
243 exit(10);
244 }
245
246 client->rquota_port = *(uint32_t *)data;
247 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client->rquota_port);
248 if (client->rquota_port == 0) {
249 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client->server, client->rquota_port);
250 exit(10);
251 }
252
253 printf("Send getport request asking for MOUNT port\n");
254 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport2_cb, client) != 0) {
255 printf("Failed to send getport request\n");
256 exit(10);
257 }
258 }
259
260 void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
261 {
262 struct client *client = private_data;
263
264 if (status == RPC_STATUS_ERROR) {
265 printf("portmapper null call failed with \"%s\"\n", (char *)data);
266 exit(10);
267 }
268 if (status != RPC_STATUS_SUCCESS) {
269 printf("portmapper null call to server %s failed, status:%d\n", client->server, status);
270 exit(10);
271 }
272
273 printf("Got reply from server for PORTMAP/NULL procedure.\n");
274 printf("Send getport request asking for MOUNT port\n");
275 if (rpc_pmap_getport_async(rpc, RQUOTA_PROGRAM, RQUOTA_V1, pmap_getport1_cb, client) != 0) {
276 printf("Failed to send getport request\n");
277 exit(10);
278 }
279 }
280
281 void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
282 {
283 struct client *client = private_data;
284
285 printf("pmap_connect_cb status:%d.\n", status);
286 if (status != RPC_STATUS_SUCCESS) {
287 printf("connection to portmapper on server %s failed\n", client->server);
288 exit(10);
289 }
290
291 printf("Send NULL request to check if portmapper is actually running\n");
292 if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) {
293 printf("Failed to send null request\n");
294 exit(10);
295 }
296 }
297
298
299 int main(int argc _U_, char *argv[] _U_)
300 {
301 struct rpc_context *rpc;
302 struct pollfd pfd;
303 struct client client;
304
305 rpc = rpc_init_context();
306 if (rpc == NULL) {
307 printf("failed to init context\n");
308 exit(10);
309 }
310
311 client.server = SERVER;
312 client.export = EXPORT;
313 client.is_finished = 0;
314 if (rpc_connect_async(rpc, client.server, 111, pmap_connect_cb, &client) != 0) {
315 printf("Failed to start connection\n");
316 exit(10);
317 }
318
319 for (;;) {
320 pfd.fd = rpc_get_fd(rpc);
321 pfd.events = rpc_which_events(rpc);
322
323 if (poll(&pfd, 1, -1) < 0) {
324 printf("Poll failed");
325 exit(10);
326 }
327 if (rpc_service(rpc, pfd.revents) < 0) {
328 printf("rpc_service failed\n");
329 break;
330 }
331 if (client.is_finished) {
332 break;
333 }
334 }
335
336 rpc_destroy_context(rpc);
337 rpc=NULL;
338 printf("nfsclient finished\n");
339 return 0;
340 }