more header include cleanups
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef WIN32
26 #include "win32_compat.h"
27 #endif
28 #define SERVER "10.1.1.27"
29 #define EXPORT "/shared"
30
31 #ifdef HAVE_POLL_H
32 #include <poll.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <netinet/in.h>
39 #include "libnfs-zdr.h"
40 #include "libnfs.h"
41 #include "libnfs-raw.h"
42 #include "libnfs-raw-mount.h"
43 #include "libnfs-raw-nfs.h"
44 #include "libnfs-raw-rquota.h"
45
46 struct client {
47 char *server;
48 char *export;
49 uint32_t mount_port;
50 uint32_t rquota_port;
51 int is_finished;
52 struct nfs_fh3 rootfh;
53 };
54
55 void rquota_getquota_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
56 {
57 struct client *client = private_data;
58 // GETQUOTA1res *res = data;
59
60 if (status == RPC_STATUS_ERROR) {
61 printf("rquota/getquota call failed with \"%s\"\n", (char *)data);
62 exit(10);
63 }
64 if (status != RPC_STATUS_SUCCESS) {
65 printf("rquota/getquota call to server %s failed, status:%d\n", client->server, status);
66 exit(10);
67 }
68
69 printf("rquota responded ok\n");
70 client->is_finished = 1;
71 }
72
73 void rquota_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
74 {
75 struct client *client = private_data;
76
77 if (status != RPC_STATUS_SUCCESS) {
78 printf("connection to RPC.RQUOTAD on server %s failed\n", client->server);
79 exit(10);
80 }
81
82 printf("Connected to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port);
83 printf("Send GETQUOTA request for uid 100\n");
84 if (rpc_rquota1_getquota_async(rpc, rquota_getquota_cb, EXPORT, 100, client) != 0) {
85 printf("Failed to send fsinfo request\n");
86 exit(10);
87 }
88 }
89
90 void acl_getacl_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
91 {
92 struct client *client = private_data;
93 GETACL3res *res = data;
94
95 printf("Got NFSACL/GETACL reply\n");
96
97 if (status == RPC_STATUS_SUCCESS) {
98 printf("Got an ACL : ACL status:%d\n", res->status);
99 if (res->status == NFS3_OK) {
100 int i;
101 printf("ACL MASK 0x%08x\n", res->GETACL3res_u.resok.mask);
102 printf("NUM ACE %d\n", res->GETACL3res_u.resok.ace_count);
103 for (i=0; i<res->GETACL3res_u.resok.ace.ace_len; i++) {
104 printf("Type:0x%08x\n", res->GETACL3res_u.resok.ace.ace_val[i].type);
105 printf("ID:%d\n", res->GETACL3res_u.resok.ace.ace_val[i].id);
106 printf("Perm:0x%08x\n", res->GETACL3res_u.resok.ace.ace_val[i].perm);
107 }
108 }
109 }
110
111 printf("Disconnect socket from nfs server\n");
112 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
113 printf("Failed to disconnect socket to nfs\n");
114 exit(10);
115 }
116
117 printf("Connect to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port);
118 if (rpc_connect_async(rpc, client->server, client->rquota_port, rquota_connect_cb, client) != 0) {
119 printf("Failed to start connection\n");
120 exit(10);
121 }
122 }
123
124 void acl_null_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
125 {
126 struct client *client = private_data;
127 GETACL3args args;
128
129 printf("Got NFSACL/NULL reply\n");
130 printf("Get ACL for root handle\n");
131
132 args.dir = client->rootfh;
133 args.mask = NFSACL_MASK_ACL_ENTRY|NFSACL_MASK_ACL_COUNT|NFSACL_MASK_ACL_DEFAULT_ENTRY|NFSACL_MASK_ACL_DEFAULT_COUNT;
134 if (rpc_nfsacl_getacl_async(rpc, acl_getacl_cb, &args, client) != 0) {
135 printf("Failed to send getacl request\n");
136 exit(10);
137 }
138
139 }
140
141 void nfs_fsinfo_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data)
142 {
143 struct client *client = private_data;
144 FSINFO3res *res = data;
145
146 if (status == RPC_STATUS_ERROR) {
147 printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data);
148 exit(10);
149 }
150 if (status != RPC_STATUS_SUCCESS) {
151 printf("nfs/fsinfo call to server %s failed, status:%d\n", client->server, status);
152 exit(10);
153 }
154
155 printf("Got reply from server for NFS/FSINFO procedure.\n");
156 printf("Read Max:%d\n", (int)res->FSINFO3res_u.resok.rtmax);
157 printf("Write Max:%d\n", (int)res->FSINFO3res_u.resok.wtmax);
158
159 printf("Send NFSACL/NULL request\n");
160 if (rpc_nfsacl_null_async(rpc, acl_null_cb, client) != 0) {
161 printf("Failed to send acl/null request\n");
162 exit(10);
163 }
164 }
165
166
167 void nfs_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
168 {
169 struct client *client = private_data;
170
171 if (status != RPC_STATUS_SUCCESS) {
172 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
173 exit(10);
174 }
175
176 printf("Connected to RPC.NFSD on %s:%d\n", client->server, client->mount_port);
177 printf("Send FSINFO request\n");
178 if (rpc_nfs_fsinfo_async(rpc, nfs_fsinfo_cb, &client->rootfh, client) != 0) {
179 printf("Failed to send fsinfo request\n");
180 exit(10);
181 }
182 }
183
184 void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
185 {
186 struct client *client = private_data;
187 mountres3 *mnt = data;
188
189 if (status == RPC_STATUS_ERROR) {
190 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
191 exit(10);
192 }
193 if (status != RPC_STATUS_SUCCESS) {
194 printf("mount/mnt call to server %s failed, status:%d\n", client->server, status);
195 exit(10);
196 }
197
198 printf("Got reply from server for MOUNT/MNT procedure.\n");
199 client->rootfh.data.data_len = mnt->mountres3_u.mountinfo.fhandle.fhandle3_len;
200 client->rootfh.data.data_val = malloc(client->rootfh.data.data_len);
201 memcpy(client->rootfh.data.data_val, mnt->mountres3_u.mountinfo.fhandle.fhandle3_val, client->rootfh.data.data_len);
202
203 printf("Disconnect socket from mountd server\n");
204 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
205 printf("Failed to disconnect socket to mountd\n");
206 exit(10);
207 }
208
209 printf("Connect to RPC.NFSD on %s:%d\n", client->server, 2049);
210 if (rpc_connect_async(rpc, client->server, 2049, nfs_connect_cb, client) != 0) {
211 printf("Failed to start connection\n");
212 exit(10);
213 }
214 }
215
216
217
218 void mount_export_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
219 {
220 struct client *client = private_data;
221 exports export = *(exports *)data;
222
223 if (status == RPC_STATUS_ERROR) {
224 printf("mount null call failed with \"%s\"\n", (char *)data);
225 exit(10);
226 }
227 if (status != RPC_STATUS_SUCCESS) {
228 printf("mount null call to server %s failed, status:%d\n", client->server, status);
229 exit(10);
230 }
231
232 printf("Got reply from server for MOUNT/EXPORT procedure.\n");
233 while (export != NULL) {
234 printf("Export: %s\n", export->ex_dir);
235 export = export->ex_next;
236 }
237 printf("Send MOUNT/MNT command for %s\n", client->export);
238 if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) {
239 printf("Failed to send mnt request\n");
240 exit(10);
241 }
242 }
243
244 void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
245 {
246 struct client *client = private_data;
247
248 if (status == RPC_STATUS_ERROR) {
249 printf("mount null call failed with \"%s\"\n", (char *)data);
250 exit(10);
251 }
252 if (status != RPC_STATUS_SUCCESS) {
253 printf("mount null call to server %s failed, status:%d\n", client->server, status);
254 exit(10);
255 }
256
257 printf("Got reply from server for MOUNT/NULL procedure.\n");
258 printf("Send MOUNT/EXPORT command\n");
259 if (rpc_mount_export_async(rpc, mount_export_cb, client) != 0) {
260 printf("Failed to send export request\n");
261 exit(10);
262 }
263 }
264
265 void mount_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
266 {
267 struct client *client = private_data;
268
269 if (status != RPC_STATUS_SUCCESS) {
270 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
271 exit(10);
272 }
273
274 printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
275 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
276 if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) {
277 printf("Failed to send null request\n");
278 exit(10);
279 }
280 }
281
282
283 void pmap_getport2_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
284 {
285 struct client *client = private_data;
286
287 if (status == RPC_STATUS_ERROR) {
288 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
289 exit(10);
290 }
291 if (status != RPC_STATUS_SUCCESS) {
292 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
293 exit(10);
294 }
295
296 client->mount_port = *(uint32_t *)data;
297 printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client->mount_port);
298 if (client->mount_port == 0) {
299 printf("RPC.MOUNTD is not available on server : %s:%d\n", client->server, client->mount_port);
300 exit(10);
301 }
302
303 printf("Disconnect socket from portmap server\n");
304 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
305 printf("Failed to disconnect socket to portmapper\n");
306 exit(10);
307 }
308
309 printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
310 if (rpc_connect_async(rpc, client->server, client->mount_port, mount_connect_cb, client) != 0) {
311 printf("Failed to start connection\n");
312 exit(10);
313 }
314 }
315
316 void pmap_getport1_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
317 {
318 struct client *client = private_data;
319
320 if (status == RPC_STATUS_ERROR) {
321 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
322 exit(10);
323 }
324 if (status != RPC_STATUS_SUCCESS) {
325 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
326 exit(10);
327 }
328
329 client->rquota_port = *(uint32_t *)data;
330 printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client->rquota_port);
331 if (client->rquota_port == 0) {
332 printf("RPC.RQUOTAD is not available on server : %s:%d\n", client->server, client->rquota_port);
333 // exit(10);
334 }
335
336 printf("Send getport request asking for MOUNT port\n");
337 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, pmap_getport2_cb, client) != 0) {
338 printf("Failed to send getport request\n");
339 exit(10);
340 }
341 }
342
343 void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
344 {
345 struct client *client = private_data;
346
347 if (status == RPC_STATUS_ERROR) {
348 printf("portmapper null call failed with \"%s\"\n", (char *)data);
349 exit(10);
350 }
351 if (status != RPC_STATUS_SUCCESS) {
352 printf("portmapper null call to server %s failed, status:%d\n", client->server, status);
353 exit(10);
354 }
355
356 printf("Got reply from server for PORTMAP/NULL procedure.\n");
357 printf("Send getport request asking for MOUNT port\n");
358 if (rpc_pmap_getport_async(rpc, RQUOTA_PROGRAM, RQUOTA_V1, IPPROTO_TCP, pmap_getport1_cb, client) != 0) {
359 printf("Failed to send getport request\n");
360 exit(10);
361 }
362 }
363
364 void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
365 {
366 struct client *client = private_data;
367
368 printf("pmap_connect_cb status:%d.\n", status);
369 if (status != RPC_STATUS_SUCCESS) {
370 printf("connection to portmapper on server %s failed\n", client->server);
371 exit(10);
372 }
373
374 printf("Send NULL request to check if portmapper is actually running\n");
375 if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) {
376 printf("Failed to send null request\n");
377 exit(10);
378 }
379 }
380
381
382 int main(int argc _U_, char *argv[] _U_)
383 {
384 struct rpc_context *rpc;
385 struct pollfd pfd;
386 struct client client;
387
388 rpc = rpc_init_context();
389 if (rpc == NULL) {
390 printf("failed to init context\n");
391 exit(10);
392 }
393
394 client.server = SERVER;
395 client.export = EXPORT;
396 client.is_finished = 0;
397 if (rpc_connect_async(rpc, client.server, 111, pmap_connect_cb, &client) != 0) {
398 printf("Failed to start connection\n");
399 exit(10);
400 }
401
402 for (;;) {
403 pfd.fd = rpc_get_fd(rpc);
404 pfd.events = rpc_which_events(rpc);
405
406 if (poll(&pfd, 1, -1) < 0) {
407 printf("Poll failed");
408 exit(10);
409 }
410 if (rpc_service(rpc, pfd.revents) < 0) {
411 printf("rpc_service failed\n");
412 break;
413 }
414 if (client.is_finished) {
415 break;
416 }
417 }
418
419 rpc_destroy_context(rpc);
420 rpc=NULL;
421 printf("nfsclient finished\n");
422 return 0;
423 }