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