-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathnfs4-cat-talloc.c
968 lines (814 loc) · 29 KB
/
nfs4-cat-talloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/* THIS IS NOT A PROPER NFS4 CLIENT!
* This software is only meant to illustrate how to plug libnfs into
* an eventsystem like libevent and then use the raw rpc api
* connect to an nfsv4 server and read a file.
* If any kind of error occurs it will immediately terminate by calling
* exit() without even attempting to cleanup.
* If the access to the server is successful it should however run valgrind
* clean.
*
* NFSv4 access is done through the raw async interface and is cumbersome
* to use for NFSv4 due to the richness of the protocol.
* A future aim will be to build better helper functions to make ease
* of use better.
*/
/*
Copyright (C) by Ronnie Sahlberg <[email protected]> 2015
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef AROS
#include "aros_compat.h"
#endif
#ifdef WIN32
#include <win32/win32_compat.h>
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#else
#include <sys/stat.h>
#include <string.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-raw-nfs4.h"
#include <tevent.h>
#include <talloc.h>
struct tevent_context *ev;
struct client {
struct nfs_context *nfs;
struct rpc_context *rpc;
char *server;
char *path;
struct tevent_fd *fde;
int callback_fd;
/* For SETCLIENTID */
verifier4 verifier;
char *id;
clientid4 clientid;
verifier4 setclientid_confirm;
/* For OPEN */
char *owner;
int op_len;
/* filehandle and state for the open file */
nfs_fh4 fh;
uint32_t seqid;
stateid4 stateid;
/* offset when reading */
uint64_t offset;
int is_finished;
};
static void set_nonblocking(int fd)
{
int v = 0;
#if defined(WIN32)
long nonblocking=1;
v = ioctl(fd, FIONBIO, &nonblocking);
#else
v = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, v | O_NONBLOCK);
#endif
}
static int client_destructor(struct client *c)
{
if (c->nfs) {
nfs_destroy_context(c->nfs);
}
if (c->callback_fd != -1) {
close(c->callback_fd);
}
};
void usage(void)
{
fprintf(stderr, "Usage: nfs4-cat-talloc <file>\n");
fprintf(stderr, " <file> is an nfs url.\n");
exit(0);
}
static void update_events(struct rpc_context *rpc,
struct tevent_fd *fde)
{
int events = rpc_which_events(rpc);
int flags = 0;
if (events & POLLIN) {
flags |= TEVENT_FD_READ;
}
if (events & POLLOUT) {
flags |= TEVENT_FD_WRITE;
}
tevent_fd_set_flags(fde, flags);
}
struct server {
struct rpc_context *rpc;
struct tevent_fd *fde;
};
/*
* Helper functions to send client RPC requests.
*/
static void send_setclientid(struct rpc_context *rpc,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
SETCLIENTID4args *sc4args;
nfs_argop4 op[1];
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
struct sockaddr_in *in;
struct sockaddr_in6 *in6;
char *netid;
char str[240], addr[256];
unsigned short port;
if (getsockname(client->callback_fd, (struct sockaddr *)&ss, &len) < 0) {
fprintf(stderr, "getsockaddr failed\n");
talloc_free(client);
exit(10);
}
switch (ss.ss_family) {
case AF_INET:
netid = "tcp";
in = (struct sockaddr_in *)&ss;
inet_ntop(AF_INET, &in->sin_addr, str, sizeof(str));
port = ntohs(in->sin_port);
break;
case AF_INET6:
netid = "tcp6";
in6 = (struct sockaddr_in6 *)&ss;
inet_ntop(AF_INET6, &in6->sin6_addr, str, sizeof(str));
port = ntohs(in6->sin6_port);
break;
}
sprintf(addr, "%s.%d.%d", str, port >> 8, port & 0xff);
memset(op, 0, sizeof(op));
op[0].argop = OP_SETCLIENTID;
sc4args = &op[0].nfs_argop4_u.opsetclientid;
memcpy(sc4args->client.verifier, client->verifier, sizeof(verifier4));
sc4args->client.id.id_len = strlen(client->id);
sc4args->client.id.id_val = client->id;
sc4args->callback.cb_program = NFS4_CALLBACK;
sc4args->callback.cb_location.r_netid = netid;
sc4args->callback.cb_location.r_addr = addr;
sc4args->callback_ident = 0x00000001;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 SETCLIENTID request\n");
talloc_free(client);
exit(10);
}
}
static void send_setclientid_confirm(struct rpc_context *rpc,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
SETCLIENTID_CONFIRM4args *scc4args;
nfs_argop4 op[1];
memset(op, 0, sizeof(op));
op[0].argop = OP_SETCLIENTID_CONFIRM;
scc4args = &op[0].nfs_argop4_u.opsetclientid_confirm;
scc4args->clientid = client->clientid;
memcpy(scc4args->setclientid_confirm, client->setclientid_confirm,
NFS4_VERIFIER_SIZE);
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 SETCLIENTID_CONFIRM request\n");
talloc_free(client);
exit(10);
}
}
static void send_getrootfh(struct rpc_context *rpc,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
nfs_argop4 op[2];
memset(op, 0, sizeof(op));
op[0].argop = OP_PUTROOTFH;
op[1].argop = OP_GETFH;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 GETROOTFH request\n");
talloc_free(client);
exit(10);
}
}
static void send_open(struct rpc_context *rpc, nfs_fh4 dir, char *path,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
ACCESS4args *a4args;
LOOKUP4args *l4args;
OPEN4args *o4args;
nfs_argop4 *op;
int i = 0, idx = 0;
char *tmp;
/*
* Count how many directories we have in the path.
*/
tmp = path;
while (tmp = strchr(tmp, '/')) {
i++;
tmp++;
}
op = talloc_zero_array(client, nfs_argop4, 4 + i);
op[idx].argop = OP_PUTFH;
op[idx].nfs_argop4_u.opputfh.object = dir;
idx++;
while (i-- > 0) {
tmp = strchr(path, '/');
*tmp++ = '\0';
op[idx].argop = OP_LOOKUP;
l4args = &op[idx].nfs_argop4_u.oplookup;
l4args->objname.utf8string_len = strlen(path);
l4args->objname.utf8string_val = path;
idx++;
path = tmp;
}
op[idx].argop = OP_OPEN;
o4args = &op[idx].nfs_argop4_u.opopen;
o4args->seqid = client->seqid;
o4args->share_access = OPEN4_SHARE_ACCESS_READ;
o4args->share_deny = OPEN4_SHARE_DENY_NONE;
o4args->owner.clientid = client->clientid;
o4args->owner.owner.owner_len = strlen(client->owner);
o4args->owner.owner.owner_val = client->owner;
o4args->openhow.opentype = OPEN4_NOCREATE;
o4args->claim.claim = CLAIM_NULL;
o4args->claim.open_claim4_u.file.utf8string_len = strlen(path);
o4args->claim.open_claim4_u.file.utf8string_val = path;
idx++;
op[idx].argop = OP_GETFH;
idx++;
op[idx].argop = OP_ACCESS;
a4args = &op[idx].nfs_argop4_u.opaccess;
a4args->access = ACCESS4_READ;
client->seqid++;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = idx;
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 OPEN request\n");
talloc_free(client);
exit(10);
}
talloc_free(op);
}
static void send_open_confirm(struct rpc_context *rpc, nfs_fh4 object, rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
OPEN_CONFIRM4args *oc4args;
PUTFH4args *pfh4args;
nfs_argop4 op[2];
memset(op, 0, sizeof(op));
op[0].argop = OP_PUTFH;
pfh4args = &op[0].nfs_argop4_u.opputfh;
pfh4args->object = object;
op[1].argop = OP_OPEN_CONFIRM;
oc4args = &op[1].nfs_argop4_u.opopen_confirm;
oc4args->open_stateid.seqid = client->seqid;
memcpy(oc4args->open_stateid.other,
client->stateid.other, 12);
oc4args->seqid = client->seqid;
client->seqid++;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 CLOSE request\n");
talloc_free(client);
exit(10);
}
}
static void send_read(struct rpc_context *rpc, nfs_fh4 object,
uint64_t offset, uint32_t count,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
PUTFH4args *pfh4args;
READ4args *r4args;
nfs_argop4 op[3];
memset(op, 0, sizeof(op));
op[0].argop = OP_PUTFH;
pfh4args = &op[0].nfs_argop4_u.opputfh;
pfh4args->object = object;
op[1].argop = OP_READ;
r4args = &op[1].nfs_argop4_u.opread;
r4args->stateid.seqid = client->seqid;
memcpy(r4args->stateid.other, client->stateid.other, 12);
r4args->offset = offset;
r4args->count = count;
op[2].argop = OP_GETATTR;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 READ request\n");
talloc_free(client);
exit(10);
}
}
static void send_close(struct rpc_context *rpc, nfs_fh4 object,
rpc_cb cb, void *private_data)
{
struct client *client = private_data;
COMPOUND4args args;
CLOSE4args *c4args;
PUTFH4args *pfh4args;
nfs_argop4 op[2];
memset(op, 0, sizeof(op));
op[0].argop = OP_PUTFH;
pfh4args = &op[0].nfs_argop4_u.opputfh;
pfh4args->object = object;
op[1].argop = OP_CLOSE;
c4args = &op[1].nfs_argop4_u.opclose;
c4args->seqid = client->seqid;
c4args->open_stateid.seqid = client->seqid;
memcpy(c4args->open_stateid.other, client->stateid.other, 12);
client->seqid++;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = sizeof(op) / sizeof(nfs_argop4);
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_task(rpc, cb, &args, private_data) == NULL) {
fprintf(stderr, "Failed to send nfs4 CLOSE request\n");
talloc_free(client);
exit(10);
}
}
/*
* Callbacks for completed requests.
*/
void close_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
/*
* FINISHED
*/
/*
* Note that we can not start tearing down and destroying the contexts
* right here as we are still in a callback from ... rpc_service().
* Instead flag that we should abort and start doing the teardown
* in client_io once we return from libnfs.
*/
client->is_finished = 1;
}
void read_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
struct READ4res *r4res;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to read file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to read file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
r4res = &res->resarray.resarray_val[1].nfs_resop4_u.opread;
write(1,
r4res->READ4res_u.resok4.data.data_val,
r4res->READ4res_u.resok4.data.data_len);
/*
* Are we at end-of-file? If so we can close the file and exit.
*/
if (r4res->READ4res_u.resok4.eof) {
send_close(rpc, client->fh, close_cb, client);
return;
}
/*
* We still have more data to read.
*/
client->offset += r4res->READ4res_u.resok4.data.data_len;
send_read(rpc, client->fh, client->offset, 4096, read_cb, client);
}
void open_confirm_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to confirm open file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to confirm open file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
send_read(rpc, client->fh, client->offset, 4096, read_cb, client);
}
void open_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
GETFH4res *gfh4res;
OPEN4res *o4res;
int idx;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to open file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to open file on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
/* Find the index for the OPEN opcode */
for (idx = 1; idx < res->resarray.resarray_len - 1; idx++) {
if ((res->resarray.resarray_val[idx].resop == OP_OPEN) &&
(res->resarray.resarray_val[idx + 1].resop == OP_GETFH)) {
break;
}
}
if (idx >= res->resarray.resarray_len - 1) {
fprintf(stderr, "No OP_OPEN in server response\n");
talloc_free(client);
exit(10);
}
/* Store the open handle in the client structure */
gfh4res = &res->resarray.resarray_val[idx+1].nfs_resop4_u.opgetfh;
client->fh.nfs_fh4_len = gfh4res->GETFH4res_u.resok4.object.nfs_fh4_len;
client->fh.nfs_fh4_val = talloc_size(client, client->fh.nfs_fh4_len);
if (client->fh.nfs_fh4_val == NULL) {
fprintf(stderr, "Failed to allocate data for nfs_fh4\n");
talloc_free(client);
exit(10);
}
memcpy(client->fh.nfs_fh4_val,
gfh4res->GETFH4res_u.resok4.object.nfs_fh4_val,
client->fh.nfs_fh4_len);
/* Store stateid for the open handle in the client structure */
o4res = &res->resarray.resarray_val[idx].nfs_resop4_u.opopen;
client->stateid.seqid = o4res->OPEN4res_u.resok4.stateid.seqid;
memcpy(client->stateid.other,
o4res->OPEN4res_u.resok4.stateid.other, 12);
/* Check if server wants us to confirm the open */
if (o4res->OPEN4res_u.resok4.rflags & OPEN4_RESULT_CONFIRM) {
send_open_confirm(rpc, client->fh, open_confirm_cb, client);
return;
}
send_read(rpc, client->fh, client->offset, 4096, read_cb, client);
}
void getrootfh_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
GETFH4res *gfh4res;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to get root filehandle of server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to get root filehandle of server %s\n",
client->server);
talloc_free(client);
exit(10);
}
gfh4res = &res->resarray.resarray_val[1].nfs_resop4_u.opgetfh;
send_open(rpc, gfh4res->GETFH4res_u.resok4.object,
client->path, open_cb, client);
}
void setclientid_confirm_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
char *path;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to set client id of server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to set client id of server %s\n",
client->server);
talloc_free(client);
exit(10);
}
send_getrootfh(rpc, getrootfh_cb, client);
}
void setclientid_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
COMPOUND4res *res = data;
SETCLIENTID4res *sc4res;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "Failed to set client id on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
if (res->status != NFS4_OK) {
fprintf(stderr, "Failed to set client id on server %s\n",
client->server);
talloc_free(client);
exit(10);
}
sc4res = &res->resarray.resarray_val[0].nfs_resop4_u.opsetclientid;
client->clientid = sc4res->SETCLIENTID4res_u.resok4.clientid;
memcpy(client->setclientid_confirm,
sc4res->SETCLIENTID4res_u.resok4.setclientid_confirm,
NFS4_VERIFIER_SIZE);
send_setclientid_confirm(rpc, setclientid_confirm_cb, client);
}
/*
* NULL procedure for the callback protocol.
*/
static int cb_null_proc(struct rpc_context *rpc, struct rpc_msg *call, void *opaque)
{
rpc_send_reply(rpc, call, NULL, (zdrproc_t)zdr_void, 0);
return 0;
}
/*
* CB_COMPOUND procedure for the callback protocol.
* This is where the server will inform us about lease breaks and similar.
*/
static int cb_compound_proc(struct rpc_context *rpc, struct rpc_msg *call, void *opaque)
{
CB_COMPOUND4args *args = call->body.cbody.args;
fprintf(stderr, "cb_compund_cb. Do something here.\n");
return 0;
}
struct service_proc pt[] = {
{CB_NULL, cb_null_proc,
(zdrproc_t)zdr_void, 0},
{CB_COMPOUND, cb_compound_proc,
(zdrproc_t)zdr_CB_COMPOUND4args, sizeof(CB_COMPOUND4args)},
};
static void server_io(struct tevent_context *ev, struct tevent_fd *fde,
uint16_t events, void *private_data)
{
struct server *server = private_data;
int revents = 0;
if (events & TEVENT_FD_READ) {
revents |= POLLIN;
}
if (events & TEVENT_FD_WRITE) {
revents |= POLLOUT;
}
if (rpc_service(server->rpc, revents) < 0) {
talloc_free(server);
return;
}
update_events(server->rpc, server->fde);
}
static int server_destructor(struct server *s)
{
if (s->rpc) {
rpc_destroy_context(s->rpc);
}
}
/*
* This callback is invoked when others (the nfsv4 server) initiates a
* NFSv4 CALLBACK sessions to us.
* We accept() the connection and create a local rpc server context
* for the callback protocol.
*/
static void client_accept(struct tevent_context *ev, struct tevent_fd *fde,
uint16_t events, void *private_data)
{
struct client *client = private_data;
struct server *server;
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
int fd;
server = talloc_zero(client, struct server);
talloc_set_destructor(server, server_destructor);
if ((fd = accept(client->callback_fd, (struct sockaddr *)&ss, &len)) < 0) {
fprintf(stderr, "accept failed\n");
talloc_free(server);
return;
}
set_nonblocking(fd);
server->rpc = rpc_init_server_context(fd);
if (server->rpc == NULL) {
fprintf(stderr, "Failed to create server rpc context\n");
talloc_free(server);
return;
}
rpc_register_service(server->rpc, NFS4_CALLBACK, NFS_CB,
pt, sizeof(pt) / sizeof(pt[0]));
server->fde = tevent_add_fd(ev, server, fd, TEVENT_FD_READ,
server_io, server);
tevent_fd_set_auto_close(server->fde);
update_events(server->rpc, server->fde);
}
/*
* This callback is invoked when our async connect() to the server has
* completed. At this point we know which IP address was used locally for
* the connection and can bind our nfsv4 callback server instance to it.
*/
void connect_cb(struct rpc_context *rpc, int status, void *data,
void *private_data)
{
struct client *client = private_data;
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
struct sockaddr_in *in;
struct sockaddr_in6 *in6;
struct tevent_fd *fde;
if (status != RPC_STATUS_SUCCESS) {
fprintf(stderr, "connection to NFSv4 server %s failed\n",
client->server);
talloc_free(client);
exit(10);
}
/*
* NFSv4 CALLBACK
* Now that we have a client connection we can register a callback
* server port on the same IP address as was used to the outgoing
* client connection. That way we know that the address used by the
* server is routable, and uses the same version of ip, that the client
* supports and can route to.
*/
if (getsockname(rpc_get_fd(rpc), (struct sockaddr *)&ss, &len) < 0) {
fprintf(stderr, "getsockaddr failed\n");
talloc_free(client);
exit(10);
}
switch (ss.ss_family) {
case AF_INET:
in = (struct sockaddr_in *)&ss;
in->sin_port=0;
break;
case AF_INET6:
in6 = (struct sockaddr_in6 *)&ss;
in6->sin6_port=0;
break;
default:
fprintf(stderr, "Can not handle AF_FAMILY:%d", ss.ss_family);
talloc_free(client);
exit(10);
}
client->callback_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client->callback_fd == -1) {
fprintf(stderr, "Failed to create callback socket\n");
talloc_free(client);
exit(10);
}
set_nonblocking(client->callback_fd);
if (bind(client->callback_fd, (struct sockaddr *)&ss, sizeof(ss)) < 0) {
fprintf(stderr, "Failed to bind callback socket\n");
talloc_free(client);
exit(10);
}
if (listen(client->callback_fd, 16) < 0) {
fprintf(stderr, "failed to listen to callback socket\n");
talloc_free(client);
exit(10);
}
fde = tevent_add_fd(ev, client, client->callback_fd, TEVENT_FD_READ,
client_accept, private_data);
tevent_fd_set_auto_close(fde);
update_events(client->rpc, client->fde);
/*
* Now that we are finished setting up the callback server port
* we can proceed and negotiate the nfsv4 client id.
*/
send_setclientid(rpc, setclientid_cb, client);
}
static void client_io(struct tevent_context *ev, struct tevent_fd *fde,
uint16_t events, void *private_data)
{
struct client *client = private_data;
int revents = 0;
if (events & TEVENT_FD_READ) {
revents |= POLLIN;
}
if (events & TEVENT_FD_WRITE) {
revents |= POLLOUT;
}
if (rpc_service(client->rpc, revents) < 0) {
fprintf(stderr, "rpc_service failed\n");
talloc_free(client);
exit(10);
}
update_events(client->rpc, client->fde);
if (client->is_finished) {
talloc_free(client);
}
}
int main(int argc, char *argv[])
{
TALLOC_CTX *ctx = talloc_new(NULL);
struct client *client;
struct nfs_url *url;
int i, fd;
#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
fprintf(stderr, "Failed to start Winsock2\n");
exit(10);
}
#endif
#ifdef AROS
aros_init_socket();
#endif
srandom(time(NULL) ^ getpid());
if (argc < 2) {
usage();
}
ev = tevent_context_init(ctx);
client = talloc_zero(ctx, struct client);
talloc_set_destructor(client, client_destructor);
client->callback_fd = -1;
client->nfs = nfs_init_context();
if (client->nfs == NULL) {
fprintf(stderr, "failed to init nfs context\n");
talloc_free(ctx);
exit(10);
}
url = nfs_parse_url_dir(client->nfs, argv[1]);
if (url == NULL) {
fprintf(stderr, "failed to parse url\n");
talloc_free(ctx);
exit(10);
}
client->server = talloc_strdup(client, url->server);
client->path = talloc_strdup(client, &url->path[1]); // skip leading '/'
nfs_destroy_url(url);
for (i = 0; i < NFS4_VERIFIER_SIZE; i++) {
client->verifier[i] = random() & 0xff;
}
client->id = talloc_asprintf(client, "Libnfs %s tcp pid:%d",
argv[1], getpid());
client->owner = talloc_asprintf(client, "open id:libnfs pid:%d",
getpid());
/*
* From here on we will mainly use the rpc context directly
* and not the nfs context so lets store the rpc context here
* for easy access.
*/
client->rpc = nfs_get_rpc_context(client->nfs);
if (rpc_connect_program_async(client->rpc, client->server,
NFS4_PROGRAM, NFS_V4,
connect_cb, client) != 0) {
fprintf(stderr, "Failed to start connection: %s\n",
rpc_get_error(client->rpc));
talloc_free(ctx);
exit(10);
}
/*
* Set up the events we need for the outgoing client RPC channel.
*/
fd = rpc_get_fd(client->rpc);
client->fde = tevent_add_fd(ev, client, fd, TEVENT_FD_READ,
client_io, (void *)client);
update_events(client->rpc, client->fde);
tevent_loop_wait(ev);
talloc_free(ctx);
return 0;
}