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