-
Notifications
You must be signed in to change notification settings - Fork 1
/
astftpd.c
1358 lines (1281 loc) · 35.4 KB
/
astftpd.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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <sys/signalfd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sched.h> // CPU_SET
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include "util.h"
enum tftp_commands {
OP_RRQ = 1,
OP_WRQ = 2,
OP_DATA = 3,
OP_ACK = 4,
OP_ERROR = 5,
OP_OACK = 6,
};
struct tftp_hdr {
uint16_t opcode;
} __attribute__((packed));
struct tftp_rrq {
uint16_t opcode;
char filename[0];
} __attribute__((packed));
struct tftp_data {
uint16_t opcode;
uint16_t block;
char data[0];
} __attribute__((packed));
struct tftp_ack {
uint16_t opcode;
uint16_t block;
} __attribute__((packed));
struct tftp_oack {
uint16_t opcode;
char opts[0];
} __attribute__((packed));
struct tftp_error {
uint16_t opcode;
uint16_t errcode;
char data[0];
} __attribute__((packed));
enum {
TFTP_PORT = 69,
};
enum {
MAX_EVENTS = 128,
CLIENTS_AT_ONCE = 32,
};
enum {
TFTP_BUFSZ = 1024,
TFTP_CLIENT_BUFSZ = 128,
};
enum {
BLKSIZE_DFLT = 512,
};
enum tftp_client_state {
S_RRQ_RECIEVED = 1,
S_SEND_DATA = 2,
S_WAIT_ACK = 3,
S_WAIT_FINAL_ACK = 4,
S_TIMEOUT = 5,
};
/*
* stores the whole file cached in the RAM
* The data gets read in before starting server threads, after that both
* the list and its entries are immutable (hence no locks).
* TODO: use a lock free list instead
*/
struct tftpd_file {
char *filename;
char *cache;
size_t size;
int fd;
int refcnt;
struct tftpd_file *next;
};
ssize_t cache_tftpd_file(struct tftpd_file *tf);
void close_tftpd_file(struct tftpd_file *tf);
int populate_file_cache(struct tftpd_file *head);
struct tftpd_file *find_tftpd_file(struct tftpd_file *head, const char *filename);
struct tftpd_file *add_tftpd_file(struct tftpd_file **headp, const char *filename);
/* No locks - each thread maintains its own clients list */
struct tftpd_client {
int sock;
int fd;
struct tftpd_file *file; // owned by the main thread
char *data; // points into file->cache and is owned by file
size_t data_len;
uint16_t block_num;
uint16_t block_size;
uint16_t tftp_tid; // local port number, in network byte order
int pending;
int state;
int has_options;
struct sockaddr_storage client_addr;
char buf[TFTP_CLIENT_BUFSZ];
size_t buf_len;
size_t reply_len;
char str_addr[INET_ADDRSTRLEN + sizeof("12345")];
time_t sent_ts; /* measured in milliseconds since the server start */
time_t acked_ts;
struct tftpd_client *next;
};
struct tftpd_ctx;
void tftpd_send_error(struct tftpd_ctx *ctx, struct tftpd_client *client,
uint16_t code, const char *msg);
void tftpd_close_connection(struct tftpd_ctx *ctx, struct tftpd_client *client);
void tftpd_client_reset_timestamps(struct tftpd_ctx *ctx, struct tftpd_client *client);
struct tftpd_conf {
unsigned int client_timeout;
uint16_t port;
int check_tid;
};
struct tftpd_ctx {
int sock;
int epoll_fd;
int timer_fd;
int signal_fd;
struct tftpd_client *clients;
struct tftpd_file *files;
char buf[TFTP_BUFSZ];
char cbuf[TFTP_BUFSZ];
size_t buf_len;
size_t cbuf_len;
struct sockaddr_storage curr_client;
struct sockaddr_storage curr_dest;
time_t last_ts;
struct tftpd_client *dead_clients;
struct tftpd_conf conf;
};
/*
* @param optsp pointer to TFTP options start, set by this function
* @param optlenp length of TFTP options, set by this function
* @return requested file name, can be NULL if the message is invalid
*/
char *tftp_parse_rrq(struct tftp_rrq *rrq, size_t len, char **optsp, size_t *optlenp);
/*
* @param optstart pointer to TFTP options start
* @param optslen total length of TFTP options
* @param optname the name of the option to look for
* @param optlenp the length of the option in question, set by this function
* @return pointer to the option value, located in [optstart, optstart + optlen)
*/
char *tftp_find_option(char *optstart, size_t optslen, const char *optname, size_t *optlenp);
/*
* @param optstart pointer to TFTP options start
* @param blksize TFTP block size, set by this function
* @return 0 if block size is not specified or is invalid, 1 otherwize
*/
int tftp_get_blksize(char *optstart, size_t optslen, int *blksize);
static int sprintf_addr(char *buf, size_t buf_len, struct sockaddr_storage const *addr);
/* @return 1 if the daemon should exit, otherwise 0 */
int tftpd_handle_signals(struct tftpd_ctx *ctx);
static ssize_t tftpd_recv_pkt(struct tftpd_ctx *ctx, int sock)
{
struct msghdr msg;
struct iovec iov;
ssize_t bytes_read = 0;
int have_orig_dest = 0;
iov.iov_base = ctx->buf;
iov.iov_len = ctx->buf_len;
msg.msg_name = &ctx->curr_client;
msg.msg_namelen = sizeof(ctx->curr_client);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ctx->cbuf;
msg.msg_controllen = ctx->cbuf_len;
if ((bytes_read = recvmsg(sock, &msg, 0)) < 0) {
bzero(&ctx->curr_dest, sizeof(ctx->curr_dest));
bzero(&ctx->curr_client, sizeof(ctx->curr_client));
if (EWOULDBLOCK == errno || EAGAIN == errno) {
return 0;
} else {
perror("recvmsg");
return -1;
}
}
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg && cmsg->cmsg_len >= sizeof(*cmsg);
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (SOL_IP != cmsg->cmsg_level || IP_ORIGDSTADDR != cmsg->cmsg_type) {
continue;
}
memcpy(&ctx->curr_dest, CMSG_DATA(cmsg), sizeof(struct sockaddr_in));
have_orig_dest = 1;
break;
}
if (unlikely(!have_orig_dest)) {
fprintf(stderr, "%s: WARN: unknown orig dest\n", __func__);
bzero(&ctx->curr_dest, sizeof(ctx->curr_dest));
return -1;
}
if (unlikely(bytes_read <= sizeof(struct tftp_hdr))) {
fprintf(stderr, "%s: datagram is too short\n", __func__);
return -1;
}
return bytes_read;
}
struct tftpd_client *tftpd_new_client(struct tftpd_ctx *ctx)
{
struct sockaddr_storage local_addr;
socklen_t local_addr_size = sizeof(local_addr);
int one = 1;
struct tftpd_client *client;
struct epoll_event ev;
struct itimerspec its;
client = calloc(1, sizeof(struct tftpd_client));
if (!client) {
fprintf(stderr, "%s: ERR: buy more RAM\n", __func__);
return NULL;
}
client->block_size = BLKSIZE_DFLT;
client->block_num = 1;
client->buf_len = TFTP_CLIENT_BUFSZ;
memcpy(&client->client_addr, &ctx->curr_client, sizeof(client->client_addr));
if (sprintf_addr(client->str_addr, sizeof(client->str_addr), &ctx->curr_client) < 0) {
bzero(client->str_addr, sizeof(client->str_addr));
fprintf(stderr, "%s: WARN: failed to print the client address\n", __func__);
}
if ((client->sock = socket(ctx->curr_client.ss_family, SOCK_DGRAM | SOCK_NONBLOCK, 0)) < 0) {
perror("client socket");
goto err_socket;
}
if (ctx->curr_dest.ss_family != AF_INET) {
fprintf(stderr, "%s: only IPv4 is supported\n", __func__);
goto err_socket;
}
/* bind the client socket to local IP */
memcpy(&local_addr, &ctx->curr_dest, sizeof(local_addr));
((struct sockaddr_in *)&local_addr)->sin_port = 0;
if (bind(client->sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
perror("client bind");
goto err_socket;
}
/* read back the assigned port, will be used in the TID check */
if (getsockname(client->sock, (struct sockaddr *)&local_addr, &local_addr_size) < 0) {
perror("getsockname");
goto err_socket;
}
client->tftp_tid = ((struct sockaddr_in *)&local_addr)->sin_port;
if (connect(client->sock, (struct sockaddr *)&(client->client_addr), sizeof(client->client_addr)) < 0) {
perror("client connect");
goto err_socket;
}
/* recieve the original dst address to check the TID */
if (setsockopt(client->sock, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)) != 0) {
perror("setsockopt IP_RECVORIGDSTADDR");
goto err_socket;
}
bzero(&ev, sizeof(ev));
ev.data.ptr = client;
ev.events = EPOLLIN;
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, client->sock, &ev) < 0) {
perror("client epoll_ctl");
goto err_socket;
}
tftpd_client_reset_timestamps(ctx, client);
if (!ctx->clients) {
/* rearm the timer */
bzero(&its, sizeof(its));
its.it_value.tv_sec = ctx->conf.client_timeout;
its.it_interval.tv_sec = ctx->conf.client_timeout;
if (timerfd_settime(ctx->timer_fd, 0, &its, NULL) < 0) {
fprintf(stderr, "%s: failed to rearm the timer\n", __func__);
}
}
list_append(&ctx->clients, client);
return client;
err_socket:
if (client->sock >= 0) {
close(client->sock);
client->sock = -1;
}
if (client) {
free(client);
}
return NULL;
}
ssize_t tftpd_read_data_block(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
struct tftp_data *reply = (struct tftp_data *)client->buf;
size_t offset = 0, chunk_len = 0;
if (client->pending) {
return 0;
}
reply->opcode = htons(OP_DATA);
reply->block = htons(client->block_num);
offset = (client->block_num - 1)*client->block_size;
if (offset + client->block_size > client->file->size) {
client->state = S_WAIT_FINAL_ACK;
chunk_len = client->file->size - offset;
} else {
chunk_len = client->block_size;
}
client->data = client->file->cache + offset;
client->data_len = chunk_len;
client->reply_len = sizeof(*reply);
return client->reply_len + chunk_len;
}
int subscribe_sock_is_writable(struct tftpd_ctx *ctx, struct tftpd_client *client, int val)
{
struct epoll_event ev;
bzero(&ev, sizeof(ev));
ev.data.ptr = client;
ev.events = EPOLLIN;
if (val) {
ev.events |= EPOLLOUT;
}
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) < 0) {
perror("subscribe_sock_is_writable");
return -1;
}
return 0;
}
ssize_t tftpd_send_reply_pkt(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
ssize_t bytes_sent = 0;
struct msghdr msg;
struct iovec iov[2];
bzero(&msg, sizeof(msg));
iov[0].iov_base = client->buf;
iov[0].iov_len = client->reply_len;
msg.msg_iov = iov;
if (client->data_len) {
iov[1].iov_base = client->data;
iov[1].iov_len = client->data_len;
msg.msg_iovlen = 2;
} else {
msg.msg_iovlen = 1;
}
bytes_sent = sendmsg(client->sock, &msg, 0);
if (likely(bytes_sent > 0)) {
if (unlikely(client->pending)) {
/* write readiness notification is not necessary any more,
* unsubscribe to prevent epoll_wait from awaking every cycle */
if (subscribe_sock_is_writable(ctx, client, 0) < 0) {
fprintf(stderr, "%s: client %s: failed to unsubscribe from "
"socket write readiness notifications\n",
__func__, client->str_addr);
return -1;
}
client->pending = 0;
}
/* Not quite accurate but saves a number of clock_gettime syscalls */
client->sent_ts = ctx->last_ts;
return bytes_sent;
}
if (EAGAIN == errno || EWOULDBLOCK == errno) {
if (client->pending) {
return 0;
}
/* ask the kernel to notify us when it's possible to send data */
if (subscribe_sock_is_writable(ctx, client, 1) < 0) {
fprintf(stderr, "%s: client %s: failed to subscribe to "
"socket write readiness notifications\n",
__func__, client->str_addr);
return -1;
}
client->pending = 1;
return 0;
} else {
fprintf(stderr, "%s: client %s: send failed: %s\n",
__func__, client->str_addr,
strerror(errno));
return -1;
}
}
ssize_t tftpd_send_data(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
ssize_t bytes_read = 0;
if (likely(!client->pending)) {
bytes_read = tftpd_read_data_block(ctx, client);
}
if (unlikely(bytes_read < 0)) {
fprintf(stderr, "%s: client %s: failed to read block %d\n",
__func__, client->str_addr, client->block_num);
return -1;
}
return tftpd_send_reply_pkt(ctx, client);
}
ssize_t tftpd_send_oack(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
struct tftp_oack *reply = (struct tftp_oack *)client->buf;
char *optp = reply->opts;
char *optname = "blksize";
size_t buf_len = client->buf_len;
size_t reply_len = sizeof(*reply);
int bytes_required = 0;
if (unlikely(buf_len < sizeof(*reply))) {
fprintf(stderr, "%s: client send buffer too short\n", __func__);
goto err_close;
}
buf_len -= sizeof(*reply);
bzero(reply, sizeof(*reply));
reply->opcode = htons(OP_OACK);
if (unlikely(buf_len <= strlen(optname) + 1)) {
fprintf(stderr, "%s: ERR: send buffer too short: need %ld, got %ld\n",
__func__, strlen(optname) + 1, buf_len);
goto err_close;
}
strcpy(optp, optname);
reply_len += strlen(optname) + 1;
buf_len -= strlen(optname) + 1;
optp += strlen(optname) + 1;
bytes_required = snprintf(optp, buf_len, "%d", (int)client->block_size);
if (unlikely(bytes_required >= buf_len)) {
fprintf(stderr, "%s: ERR: send buffer too short: need: %d, got %ld\n",
__func__, bytes_required, buf_len);
goto err_close;
}
reply_len += bytes_required + 1;
buf_len -= bytes_required + 1;
client->reply_len = reply_len;
return tftpd_send_reply_pkt(ctx, client);
err_close:
tftpd_send_error(ctx, client, ENOMEM, "internal error");
tftpd_close_connection(ctx, client);
return -1;
}
void tftpd_send_error(struct tftpd_ctx *ctx, struct tftpd_client *client,
uint16_t code, const char *msg)
{
struct tftp_error *err = (struct tftp_error *)client->buf;
client->reply_len = sizeof(*err) + strlen(msg) + 1;
if (client->reply_len > client->buf_len) {
client->reply_len = 0;
fprintf(stderr, "%s: error message too long\n", __func__);
return;
}
err->opcode = htons(OP_ERROR);
err->errcode = htons(code);
strcpy(err->data, msg);
if (send(client->sock, client->buf, client->reply_len, 0) < 0) {
fprintf(stderr, "%s: WARN: failed to send error pkt to client %s\n",
__func__, client->str_addr);
/* Don't bother to retransmit the ERROR packet */
}
}
ssize_t tftpd_new_connection(struct tftpd_ctx *ctx) {
ssize_t msg_len;
int blksize = BLKSIZE_DFLT;
char *optstart = NULL;
size_t optslen = 0;
int has_options = 0;
if ((msg_len = tftpd_recv_pkt(ctx, ctx->sock)) < 0) {
fprintf(stderr, "%s: failed to read datagram\n", __func__);
return -1;
}
if (msg_len == 0) {
return 0;
}
struct tftp_rrq *rrq = (struct tftp_rrq *)ctx->buf;
if (ntohs(rrq->opcode) != OP_RRQ) {
fprintf(stderr, "%s: ERR: expected RRQ, got %d\n", __func__, ntohs(rrq->opcode));
return 0;
}
char *filename = tftp_parse_rrq(rrq, msg_len, &optstart, &optslen);
if (!filename) {
fprintf(stderr, "%s: ERR: RRQ without a filename\n", __func__);
return 0;
}
if (tftp_get_blksize(optstart, optslen, &blksize)) {
has_options = 1;
// fprintf(stderr, "%s: DBG: block size %d\n", __func__, blksize);
}
struct tftpd_file *file = find_tftpd_file(ctx->files, filename);
struct tftpd_client *client;
for (client = ctx->clients; client; client = client->next) {
if (!memcmp(&client->client_addr, &ctx->curr_client, sizeof(ctx->curr_client))) {
break;
}
}
if (!client) {
if (!(client = tftpd_new_client(ctx))) {
fprintf(stderr, "%s: failed to initialize client socket\n", __func__);
return -1;
}
}
if (!file) {
fprintf(stderr, "%s: no such file: %s (requested by %s)\n",
__func__, filename, client->str_addr);
tftpd_send_error(ctx, client, ENOENT, filename);
tftpd_close_connection(ctx, client);
return 0;
}
__sync_add_and_fetch(&file->refcnt, 1);
if (client->file != file) {
if (client->file) {
client->file->refcnt--;
}
client->file = file;
}
client->block_size = blksize;
if (has_options) {
client->block_num = 0;
client->has_options = 1;
}
if (client->has_options) {
msg_len = tftpd_send_oack(ctx, client);
} else {
msg_len = tftpd_send_data(ctx, client);
}
return msg_len;
}
void tftpd_close_connection(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) < 0) {
perror("close_connection: epoll_ctl DEL");
}
if (close(client->sock) < 0) {
perror("close_connection: close sock");
}
list_remove(&ctx->clients, client);
list_append(&ctx->dead_clients, client);
if (client->file && __sync_sub_and_fetch(&client->file->refcnt, 1) == 0) {
/*
* TODO: make the file list non-blocking
* TODO: implement reading in files in a dedicated thread
* TODO: implement the control socket and commands controlling the cache
*/
}
client->file = NULL;
if (!ctx->clients) {
struct itimerspec its;
bzero(&its, sizeof(its));
/* don't tick if there are no clients. Makes greenpeace happy */
timerfd_settime(ctx->timer_fd, 0, &its, NULL);
}
}
int tftpd_handle_ack(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
struct tftp_ack *reply = (struct tftp_ack *)ctx->buf;
uint16_t block_num = ntohs(reply->block);
if (block_num < client->block_num - 1 || block_num > client->block_num) {
fprintf(stderr, "%s: WARN: bogus ACK, expected block %d, got %d\n",
__func__,
(int)client->block_num,
(int)block_num);
return 0;
}
client->acked_ts = ctx->last_ts;
if (client->state != S_WAIT_FINAL_ACK) {
client->block_num = block_num + 1;
tftpd_send_data(ctx, client);
} else {
tftpd_close_connection(ctx, client);
}
return 0;
}
int tftpd_handle_client(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
ssize_t bytes_read;
if ((bytes_read = tftpd_recv_pkt(ctx, client->sock)) < 0) {
fprintf(stderr, "%s: failed to receive datagram\n", __func__);
return -1;
}
if (unlikely(bytes_read == 0)) {
if (client->pending) {
if (tftpd_send_data(ctx, client) < 0) {
/* TODO: send an error message */
return -1;
}
}
return 0;
}
if (unlikely(AF_INET != ctx->curr_dest.ss_family)) {
fprintf(stderr, "%s: ERR: only IPv4 is supported\n", __func__);
return -1;
}
uint16_t tid = ((struct sockaddr_in *)&ctx->curr_dest)->sin_port;
if (ctx->conf.check_tid && tid != client->tftp_tid) {
fprintf(stderr, "%s: DBG: wrong tid: %u (expected: %u) => drop packet\n",
__func__,
(unsigned)ntohs(tid),
(unsigned)ntohs(client->tftp_tid));
return 0;
}
struct tftp_hdr *hdr = (struct tftp_hdr *)ctx->buf;
uint16_t opcode = ntohs(hdr->opcode);
switch (opcode) {
case OP_ACK:
if (unlikely(bytes_read < sizeof(struct tftp_ack))) {
fprintf(stderr, "%s: ACK should be at least %d bytes long\n",
__func__,
(int)sizeof(struct tftp_ack));
return -1;
}
return tftpd_handle_ack(ctx, client);
break;
case OP_ERROR:
fprintf(stderr, "%s: client %s: ERR, closing\n", __func__, client->str_addr);
tftpd_close_connection(ctx, client);
return -1;
break;
default:
fprintf(stderr, "%s: unknown opcode: %d\n", __func__, (int)opcode);
return -1;
break;
}
return 0;
}
void tftpd_kick_stuck_clients(struct tftpd_ctx *ctx)
{
time_t client_ts = 0;
uint64_t tick_count;
/* read in the tick count so epoll won't report timer_fd as ready */
if (read(ctx->timer_fd, &tick_count, sizeof(tick_count)) < 0) {
/* ignore the error */
}
for (struct tftpd_client *c = ctx->clients, *next = c->next; c; c = next) {
next = c->next;
client_ts = c->sent_ts;
if (c->acked_ts > c->sent_ts) {
client_ts = c->acked_ts;
}
if (client_ts > ctx->last_ts) {
fprintf(stderr, "%s: client %s: timestamp in future\n",
__func__, c->str_addr);
continue;
}
if (ctx->last_ts - client_ts >= ctx->conf.client_timeout*1000) {
next = c->next;
tftpd_send_error(ctx, c, ETIMEDOUT, "timed out");
tftpd_close_connection(ctx, c);
}
}
}
int is_dead_client(struct tftpd_ctx *ctx, struct tftpd_client *client)
{
for (struct tftpd_client *c = ctx->dead_clients; c; c = c->next) {
if (c == client) {
return 1;
}
}
return 0;
}
void wipe_dead_clients(struct tftpd_ctx *ctx)
{
for (struct tftpd_client *c = ctx->dead_clients, *next = NULL; c; c = next) {
next = c->next;
/* tftpd_clse_connection has released all resources except the object itself */
free(c);
}
ctx->dead_clients = NULL;
}
void sort_clients_by_deadline(struct tftpd_client **clients, size_t count)
{
/* shaker sort is often faster than qsort on small datasets */
int swapped;
if (count <= 1) {
return;
}
do {
swapped = 0;
for (size_t i = 0; i < count - 1; ++i) {
if (clients[i+1]->sent_ts < clients[i+1]->sent_ts) {
array_elt_swap(clients, i, i + 1);
swapped = 1;
}
}
if (!swapped) {
return;
}
swapped = 0;
for (size_t i = count - 1; i--; ) {
if (clients[i+1]->sent_ts < clients[i]->sent_ts) {
array_elt_swap(clients, i, i + 1);
swapped = 1;
}
}
} while (swapped);
}
/* can be used for sorting with qsort
int compare_clients_by_deadline(const void *cv1, const void *cv2)
{
struct tftpd_client const * const *pp1 = cv1;
struct tftpd_client const * const *pp2 = cv1;
struct tftpd_client const *c1 = *pp1;
struct tftpd_client const *c2 = *pp2;
if (c1->sent_ts < c2->sent_ts) {
return -1;
}
if (c1->sent_ts > c2->sent_ts) {
return 1;
}
return 0;
} */
int tftpd_run(struct tftpd_ctx *ctx) {
int nfds = 0;
struct tftpd_client *client = NULL;
struct epoll_event events[MAX_EVENTS];
struct tftpd_client *pending_clients[MAX_EVENTS];
struct timespec timestamp;
time_t start_time;
if (clock_gettime(CLOCK_MONOTONIC, ×tamp) < 0) {
perror("clock_gettime");
return -1;
}
start_time = timestamp.tv_sec;
bzero(events, sizeof(events));
bzero(pending_clients, sizeof(pending_clients));
for (;;) {
/* pick many events to find out which clients are ready and
* serve the clients with nearest deadlines
*/
if ((nfds = epoll_wait(ctx->epoll_fd, events, MAX_EVENTS, -1)) < 0) {
perror("epoll_wait");
return -1;
}
if (clock_gettime(CLOCK_MONOTONIC, ×tamp) < 0) {
perror("clock_gettime");
} else {
/* measure time from the server start so time_t has enough bits
* to represent the time stamp
*/
ctx->last_ts = (timestamp.tv_sec - start_time)*1000 + (timestamp.tv_nsec >> 20);
}
int pending_clients_count = 0, served_client_count = 0;
for (int n = 0; n < nfds; ++n) {
uint64_t pval = events[n].data.u64;
if (unlikely(is_ptr_fd(pval))) {
int fd = unpack_fd_from_ptr(pval);
/* this is ugly, but a lookup table for 2 fds is even more so */
if (ctx->timer_fd == fd) {
tftpd_kick_stuck_clients(ctx);
} else if (ctx->signal_fd == fd) {
if (tftpd_handle_signals(ctx)) {
goto out;
}
} else {
fprintf(stderr, "%s: bogus fd: %d\n", __func__, fd);
}
continue;
}
client = events[n].data.ptr;
/* XXX: in theory the kernel should squash events which occured
* between epoll_wait calls. Perhaps this check can be skipped.
*/
if (unlikely(is_dead_client(ctx, client))) {
fprintf(stderr, "%s: INFO: client %s is dead\n",
__func__, client->str_addr);
continue;
}
if (client) {
pending_clients[pending_clients_count] = client;
pending_clients_count++;
} else {
/* serve new clients immediately */
tftpd_new_connection(ctx);
}
}
sort_clients_by_deadline(pending_clients, pending_clients_count);
/* qsort can be slower on small data sets.
* qsort can't inline the comparison function
qsort(pending_clients, pending_clients_count, sizeof(struct tftpd_client *),
compare_clients_by_deadline);
*/
for (int n = 0; served_client_count < CLIENTS_AT_ONCE && n < pending_clients_count; ++n) {
client = pending_clients[n];
if (unlikely(is_dead_client(ctx, client))) {
fprintf(stderr, "%s: DBG: client %s is dead\n",
__func__, client->str_addr);
continue;
}
tftpd_handle_client(ctx, client);
served_client_count++;
}
wipe_dead_clients(ctx);
}
out:
return 0;
}
int tftpd_start(struct tftpd_ctx *ctx) {
int one = 1;
struct epoll_event ev;
struct sockaddr_in server_addr;
sigset_t sigmask;
ctx->timer_fd = -1;
ctx->epoll_fd = -1;
ctx->sock = -1;
ctx->signal_fd = -1;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(ctx->conf.port);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((ctx->epoll_fd = epoll_create(MAX_EVENTS)) < 0) {
perror("epoll_create");
goto out;
}
if ((ctx->timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0) {
perror("timerfd_create");
goto out;
}
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGTERM);
sigaddset(&sigmask, SIGINT);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGHUP);
if (sigprocmask(SIG_BLOCK, &sigmask, NULL)) {
perror("sigprocmask");
goto out;
}
if ((ctx->signal_fd = signalfd(-1, &sigmask, SFD_NONBLOCK)) < 0) {
perror("signalfd");
goto out;
}
if ((ctx->sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0)) < 0) {
perror("socket");
goto out;
}
if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
perror("setsockopt SO_REUSEADDR");
goto out;
}
/* Allow more than one process/thread to bind to the address:port.
* The kernel will distribute incoming datagrams between the server threads.
* Therefore all threads are equal (no special thread accepting the connections)
* and clients list and all that are per thread and don't need any locks
*/
if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) != 0) {
perror("setsockopt SO_REUSEPORT");
goto out;
}
if (bind(ctx->sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
goto out;
}
if (setsockopt(ctx->sock, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)) != 0) {
perror("setsockopt IP_RECVORIGDSTADDR");
goto out;
}
bzero(&ev, sizeof(ev));
ev.events = EPOLLIN;
/* pointers to heap allocated memory are aligned at the word boundary.
* Therefore lower 2 or 3 bits can be used to encode some data. Here
* we use the lowest bit to distinguish between the file descriptors
* and pointers.
*/
ev.data.u64 = pack_fd_as_ptr(ctx->signal_fd);
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->signal_fd, &ev) < 0) {
perror("epoll_ctl: signalfd");
goto out;
}
bzero(&ev, sizeof(ev));
ev.events = EPOLLIN;
ev.data.u64 = pack_fd_as_ptr(ctx->timer_fd);
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->timer_fd, &ev) < 0) {
perror("epoll_ctl: timerfd");
goto out;
}
bzero(&ev, sizeof(ev));
ev.events = EPOLLIN;
ev.data.ptr = NULL;
if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->sock, &ev) < 0) {
perror("epoll_ctl: sock");
goto out;
}
return 0;
out:
if (ctx->epoll_fd >= 0) {
close(ctx->epoll_fd);
ctx->epoll_fd = -1;
}
if (ctx->sock >= 0) {
close(ctx->sock);
ctx->sock = -1;
}
if (ctx->timer_fd >= 0) {
close(ctx->timer_fd);
ctx->timer_fd = -1;
}
if (ctx->signal_fd >= 0) {
close(ctx->signal_fd);
ctx->signal_fd = -1;
}
return -1;
}
ssize_t cache_tftpd_file(struct tftpd_file *tf) {
struct stat stbuf;
bzero(&stbuf, sizeof(stbuf));
if (tf->fd <= 0) {
tf->fd = open(tf->filename, O_RDONLY);
}
if (tf->fd < 0) {
fprintf(stderr, "%s: failed to open %s: %s\n",
__func__,
tf->filename,
strerror(errno));
goto out;
}
if (fstat(tf->fd, &stbuf) < 0) {
fprintf(stderr, "%s: failed to stat %s: %s\n",
__func__,
tf->filename,
strerror(errno));
goto out;
}
if (!(tf->cache = mmap(NULL, stbuf.st_size, PROT_READ, MAP_SHARED, tf->fd, 0))) {
fprintf(stderr, "%s: failed to mmap %s: %s (%d)\n",
__func__,
tf->filename,
strerror(errno),
errno);
goto out;
}
if (mlock(tf->cache, stbuf.st_size) < 0) {
fprintf(stderr, "%s: failed to lock %ld bytes\n",
__func__,
(long)stbuf.st_size);
}
tf->size = stbuf.st_size;
if (readahead(tf->fd, 0, tf->size) < 0) {
fprintf(stderr, "%s: failed to readahead file %s: %s (%d)\n",
__func__,
tf->filename,
strerror(errno),
errno);
}
fprintf(stderr, "%s: cached file %s (%ld bytes)\n",
__func__, tf->filename, tf->size);
if (close(tf->fd) < 0) {
fprintf(stderr, "%s: failed to close file %s (fd %d): %s (%d)\n",