-
Notifications
You must be signed in to change notification settings - Fork 64
/
trickle-overload.c
1244 lines (1002 loc) · 25.3 KB
/
trickle-overload.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
/*
* trickle-overload.c
*
* Copyright (c) 2002, 2003 Marius Aamodt Eriksen <[email protected]>
* All rights reserved.
*
* $Id: trickle-overload.c,v 1.38 2004/02/13 06:11:21 marius Exp $
*/
/* Ick. linux sucks. */
#define _GNU_SOURCE
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/un.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#include <netinet/in.h>
#ifdef HAVE_ERR_H
#include <err.h>
#endif /* HAVE_ERR_H */
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <limits.h>
#include <math.h>
#if defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME)
#include <time.h>
#endif /* defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME) */
#include <syslog.h>
#include <pwd.h>
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include "bwstat.h"
#include "trickle.h"
#include "message.h"
#include "util.h"
#include "trickledu.h"
#ifndef INFTIM
#define INFTIM -1
#endif /* INFTIM */
#define SD_INSELECT 0x01
struct sockdesc {
int sock;
int flags;
struct bwstat *stat;
struct {
int flags;
size_t lastlen;
size_t selectlen;
} data[2];
TAILQ_ENTRY(sockdesc) next;
};
struct delay {
struct sockdesc *sd;
struct timeval delaytv;
struct timeval abstv;
short which;
short pollevents;
int pollidx;
TAILQ_ENTRY(delay) next;
};
TAILQ_HEAD(delayhead, delay);
struct _pollfd {
struct pollfd *pfd;
int idx;
TAILQ_ENTRY(_pollfd) next;
};
TAILQ_HEAD(_pollfdhead, _pollfd);
static TAILQ_HEAD(sockdeschead, sockdesc) sdhead;
static uint32_t winsz;
static int verbose;
static uint lim[2];
static char *argv0;
static double tsmooth;
static uint lsmooth/* , latency */;
static int trickled, initialized, initializing;
/* XXX initializing - volatile? */
#ifdef HAVE_PTHREAD
static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
static void trickle_lock()
{
pthread_mutex_lock(&global_lock);
}
static void trickle_unlock()
{
pthread_mutex_unlock(&global_lock);
}
#else
static void trickle_lock() {}
static void trickle_unlock() {}
#endif
#define DECLARE(name, ret, args) static ret (*libc_##name) args
DECLARE(socket, int, (int, int, int));
DECLARE(close, int, (int));
/* DECLARE(setsockopt, int, (int, int, int, const void *, socklen_t)); */
DECLARE(read, ssize_t, (int, void *, size_t));
DECLARE(recv, ssize_t, (int, void *, size_t, int));
DECLARE(readv, ssize_t, (int, const struct iovec *, int));
#ifdef __sun__
DECLARE(recvfrom, ssize_t, (int, void *, size_t, int, struct sockaddr *,
Psocklen_t));
#else
DECLARE(recvfrom, ssize_t, (int, void *, size_t, int, struct sockaddr *,
socklen_t *));
#endif /* __sun__ */
DECLARE(write, ssize_t, (int, const void *, size_t));
DECLARE(send, ssize_t, (int, const void *, size_t, int));
DECLARE(writev, ssize_t, (int, const struct iovec *, int));
DECLARE(sendto, ssize_t, (int, const void *, size_t, int,
const struct sockaddr *, socklen_t));
DECLARE(select, int, (int, fd_set *, fd_set *, fd_set *, struct timeval *));
DECLARE(poll, int, (struct pollfd *, int, int));
#ifdef __sun__
DECLARE(accept, int, (int, struct sockaddr *, Psocklen_t));
#else
DECLARE(accept, int, (int, struct sockaddr *, socklen_t *));
#endif /* __sun__ */
DECLARE(dup, int, (int));
DECLARE(dup2, int, (int, int));
#ifdef HAVE_SENDFILE
DECLARE(sendfile, ssize_t, (int, int, off_t *, size_t));
#endif
static int delay(int, ssize_t *, short);
static struct timeval *getdelay(struct sockdesc *, ssize_t *, short);
static void update(int, ssize_t, short);
static void updatesd(struct sockdesc *, ssize_t, short);
static void trickle_init(void);
void safe_printv(int, const char *, ...);
#define errx(l, fmt, arg...) do { \
safe_printv(0, fmt, ##arg); \
exit(l); \
} while (0)
#ifdef DL_NEED_UNDERSCORE
#define UNDERSCORE "_"
#else
#define UNDERSCORE ""
#endif /* DL_NEED_UNDERSCORE */
#define INIT do { \
if (!initialized && !initializing) \
trickle_init(); \
} while (0)
#define GETADDR(x) do { \
if ((libc_##x = dlsym(dh, UNDERSCORE #x)) == NULL) \
errx(0, "[trickle] Failed to get " #x "() address"); \
} while (0)
static void
trickle_init(void)
{
void *dh;
char *winszstr, *verbosestr,
*recvlimstr, *sendlimstr, *sockname, *tsmoothstr, *lsmoothstr;
/* *latencystr; */
initializing = 1;
#ifdef NODLOPEN
dh = (void *) -1L;
#else
if ((dh = dlopen(DLOPENLIBC, RTLD_LAZY)) == NULL)
errx(1, "[trickle] Failed to open libc");
#endif /* DLOPEN */
/*
* We get write first, so that we have a bigger chance of
* exiting gracefully with safe_printv.
*/
GETADDR(write);
GETADDR(socket);
/* GETADDR(setsockopt); */
GETADDR(close);
GETADDR(read);
GETADDR(readv);
#ifndef __FreeBSD__
GETADDR(recv);
#endif /* !__FreeBSD__ */
GETADDR(recvfrom);
GETADDR(writev);
#ifndef __FreeBSD__
GETADDR(send);
#endif /* !__FreeBSD__ */
GETADDR(sendto);
GETADDR(select);
// GETADDR(poll);
GETADDR(dup);
GETADDR(dup2);
GETADDR(accept);
#ifdef HAVE_SENDFILE
GETADDR(sendfile);
#endif
/* XXX pthread test */
/* if ((dh = dlopen("/usr/lib/libpthread.so.1.0", RTLD_LAZY)) == NULL) */
/* errx(1, "[trickle] Failed to open libpthread"); */
GETADDR(poll);
if ((winszstr = getenv("TRICKLE_WINDOW_SIZE")) == NULL)
errx(1, "[trickle] Failed to get window size");
if ((recvlimstr = getenv("TRICKLE_DOWNLOAD_LIMIT")) == NULL)
errx(1, "[trickle] Failed to get limit");
if ((sendlimstr = getenv("TRICKLE_UPLOAD_LIMIT")) == NULL)
errx(1, "[trickle] Failed to get limit");
if ((verbosestr = getenv("TRICKLE_VERBOSE")) == NULL)
errx(1, "[trickle] Failed to get verbosity level");
if ((argv0 = getenv("TRICKLE_ARGV")) == NULL)
errx(1, "[trickle] Failed to get argv");
if ((sockname = getenv("TRICKLE_SOCKNAME")) == NULL)
errx(1, "[trickle] Failed to get socket name");
if ((tsmoothstr = getenv("TRICKLE_TSMOOTH")) == NULL)
errx(1, "[trickle] Failed to get time smoothing parameter");
if ((lsmoothstr = getenv("TRICKLE_LSMOOTH")) == NULL)
errx(1, "[trickle] Failed to get length smoothing parameter");
/*
if ((latencystr = getenv("TRICKLE_LATENCY")) == NULL)
errx(1, "[trickle] Failed to get length latency parameter");
*/
winsz = atoi(winszstr) * 1024;
lim[TRICKLE_RECV] = atoi(recvlimstr) * 1024;
lim[TRICKLE_SEND] = atoi(sendlimstr) * 1024;
/* latency = atoi(latencystr);*/
verbose = atoi(verbosestr);
// verbose = -1;
if ((tsmooth = strtod(tsmoothstr, (char **)NULL)) <= 0.0)
errx(1, "[trickle] Invalid time smoothing parameter");
lsmooth = atoi(lsmoothstr) * 1024;
TAILQ_INIT(&sdhead);
/*
* Open controlling socket
*/
trickled_configure(sockname, libc_socket, libc_read,
libc_write, libc_close, argv0);
trickled_open(&trickled);
bwstat_init(winsz);
safe_printv(1, "[trickle] Initialized");
initialized = 1;
}
int
socket(int domain, int type, int protocol)
{
int sock;
struct sockdesc *sd;
trickle_lock();
INIT;
trickle_unlock();
sock = (*libc_socket)(domain, type, protocol);
#ifdef DEBUG
safe_printv(0, "[DEBUG] socket(%d, %d, %d) = %d",
domain, type, protocol, sock);
#endif /* DEBUG */
#ifdef SOCK_NONBLOCK
type &= ~SOCK_NONBLOCK;
#endif
#ifdef SOCK_CLOEXEC
type &= ~SOCK_CLOEXEC;
#endif
if (sock != -1 && domain == AF_INET && type == SOCK_STREAM) {
if ((sd = calloc(1, sizeof(*sd))) == NULL)
return (-1);
trickle_lock();
if ((sd->stat = bwstat_new()) == NULL) {
trickle_unlock();
free(sd);
return (-1);
}
/* All sockets are equals. */
sd->stat->lsmooth = lsmooth;
sd->stat->tsmooth = tsmooth;
sd->sock = sock;
TAILQ_INSERT_TAIL(&sdhead, sd, next);
trickle_unlock();
}
return (sock);
}
int
close(int fd)
{
struct sockdesc *sd, *next;
trickle_lock();
INIT;
#ifdef DEBUG
safe_printv(0, "[DEBUG] close(%d)", fd);
#endif /* DEBUG */
for (sd = TAILQ_FIRST(&sdhead); sd != NULL; sd = next) {
next = TAILQ_NEXT(sd, next);
if (sd->sock == fd) {
TAILQ_REMOVE(&sdhead, sd, next);
bwstat_free(sd->stat);
free(sd);
break;
}
}
if (fd == trickled) {
trickled_close(&trickled);
trickled_open(&trickled);
}
trickle_unlock();
return ((*libc_close)(fd));
}
static struct delay *
select_delay(struct delayhead *dhead, struct sockdesc *sd, short which)
{
ssize_t len = -1;
struct timeval *delaytv;
struct delay *d, *_d;
updatesd(sd, 0, which);
if ((delaytv = getdelay(sd, &len, which)) == NULL)
return (NULL);
safe_printv(3, "[trickle] Delaying socket (%s) %d "
"by %ld seconds %ld microseconds",
which == 0 ? "write" : "read", sd->sock,
delaytv->tv_sec, delaytv->tv_usec);
if ((d = calloc(1, sizeof(*d))) == NULL)
return (NULL);
gettimeofday(&d->abstv, NULL);
d->delaytv = *delaytv;
d->which = which;
d->sd = sd;
sd->data[which].selectlen = len;
if (TAILQ_EMPTY(dhead))
TAILQ_INSERT_HEAD(dhead, d, next);
else {
TAILQ_FOREACH(_d, dhead, next)
if (timercmp(&d->delaytv, &_d->delaytv, <)) {
TAILQ_INSERT_BEFORE(_d, d, next);
break;
}
if (_d == NULL)
TAILQ_INSERT_TAIL(dhead, d, next);
}
return (d);
}
static struct delay *
select_shift(struct delayhead *dhead, struct timeval *difftv,
struct timeval **delaytv)
{
struct delay *d;
struct sockdesc *sd;
TAILQ_FOREACH(d, dhead, next) {
if (timercmp(&d->delaytv, difftv, >))
break;
sd = d->sd;
updatesd(sd, 0, d->which);
SET(sd->data[d->which].flags, SD_INSELECT);
}
if (d != NULL)
timersub(&d->delaytv, difftv, *delaytv);
else
*delaytv = NULL;
/* XXX this should be impossible ... */
if (*delaytv != NULL &&
((*delaytv)->tv_sec < 0 || (*delaytv)->tv_usec < 0))
timerclear(*delaytv);
return (d);
}
int
select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
struct timeval *__timeout)
{
struct sockdesc *sd;
fd_set *fdsets[] = { wfds, rfds }, *fds;
struct timeval *delaytv, _delaytv, *selecttv = NULL, *timeout = NULL,
_timeout, inittv, curtv, difftv;
short which;
struct delayhead dhead;
struct delay *d, *_d;
int ret;
#ifdef DEBUG
safe_printv(0, "[DEBUG] select(%d)", nfds);
#endif /* DEBUG */
TAILQ_INIT(&dhead);
/* We don't want to modify the user's timeout */
if (__timeout != NULL) {
_timeout = *__timeout;
timeout = &_timeout;
}
trickle_lock();
INIT;
/*
* Sockets that require delaying get added to the delay list.
* delaytv is always assigned to the head of the list.
*/
for (which = 0; which < 2; which++)
TAILQ_FOREACH(sd, &sdhead, next)
if ((fds = fdsets[which]) != NULL &&
FD_ISSET(sd->sock, fds) &&
select_delay(&dhead, sd, which)) {
FD_CLR(sd->sock, fds);
}
gettimeofday(&inittv, NULL);
curtv = inittv;
d = TAILQ_FIRST(&dhead);
if (d != NULL) {
_delaytv = d->delaytv;
delaytv = &_delaytv;
} else
delaytv = NULL;
timersub(&curtv, &inittv, &difftv);
again:
selecttv = NULL;
if (delaytv != NULL)
selecttv = delaytv;
if (timeout != NULL) {
timersub(timeout, &difftv, timeout);
if (timeout->tv_sec < 0 || timeout->tv_usec < 0)
timerclear(timeout);
if (delaytv != NULL && timercmp(timeout, delaytv, <))
selecttv = timeout;
else if (delaytv == NULL)
selecttv = timeout;
}
#ifdef DEBUG
safe_printv(0, "[DEBUG] IN select(%d)", nfds);
#endif /* DEBUG */
trickle_unlock();
ret = (*libc_select)(nfds, rfds, wfds, efds, selecttv);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] OUT select(%d) = %d", nfds, ret);
#endif /* DEBUG */
if (ret == 0 && delaytv != NULL && selecttv == delaytv) {
gettimeofday(&curtv, NULL);
timersub(&curtv, &inittv, &difftv);
_d = select_shift(&dhead, &difftv, &delaytv);
while ((d = TAILQ_FIRST(&dhead)) != _d) {
FD_SET(d->sd->sock, fdsets[d->which]);
TAILQ_REMOVE(&dhead, d, next);
free(d);
}
goto again;
}
while ((d = TAILQ_FIRST(&dhead)) != NULL) {
CLR(d->sd->data[d->which].flags, SD_INSELECT);
TAILQ_REMOVE(&dhead, d, next);
free(d);
}
trickle_unlock();
return (ret);
}
#define POLL_WRMASK (POLLOUT | POLLWRNORM | POLLWRBAND)
#define POLL_RDMASK (POLLIN | /* POLLNORM | */ POLLPRI | POLLRDNORM | POLLRDBAND)
#if defined(__linux__) || (defined(__svr4__) && defined(__sun__)) || defined(__OpenBSD__)
int
poll(struct pollfd *fds, nfds_t nfds, int __timeout)
#elif defined(__FreeBSD__)
int
poll(struct pollfd *fds, unsigned int nfds, int __timeout)
#else
int
poll(struct pollfd *fds, int nfds, int __timeout)
#endif /* __linux__ */
{
struct pollfd *pfd;
int i, polltimeout, ret;
struct sockdesc *sd;
struct delay *d, *_d;
struct timeval inittv, curtv, _timeout, *timeout = NULL, *delaytv,
*polltv, difftv;
struct delayhead dhead;
#if defined(DEBUG) || defined(DEBUG_POLL)
safe_printv(0, "[DEBUG] poll(*, %d, %d)", nfds, __timeout);
#endif /* DEBUG */
if (__timeout != INFTIM) {
_timeout.tv_sec = __timeout / 1000;
_timeout.tv_usec = (__timeout % 1000) * 100;
timeout = &_timeout;
}
TAILQ_INIT(&dhead);
trickle_lock();
INIT;
for (i = 0; i < nfds; i++) {
pfd = &fds[i];
TAILQ_FOREACH(sd, &sdhead, next)
if (sd->sock == pfd->fd)
break;
if (sd == NULL)
continue;
/* For each event */
if (pfd->events & POLL_RDMASK &&
(d = select_delay(&dhead, sd, TRICKLE_RECV)) != NULL) {
d->pollevents = pfd->events & POLL_RDMASK;
d->pollidx = i;
pfd->events &= ~POLL_RDMASK;
}
if (pfd->events & POLL_WRMASK &&
(d = select_delay(&dhead, sd, TRICKLE_SEND)) != NULL) {
d->pollevents = pfd->events & POLL_WRMASK;
d->pollidx = i;
pfd->events &= ~POLL_WRMASK;
}
}
gettimeofday(&inittv, NULL);
curtv = inittv;
d = TAILQ_FIRST(&dhead);
delaytv = d != NULL ? &d->delaytv : NULL;
again:
timersub(&inittv, &curtv, &difftv);
polltv = NULL;
if (delaytv != NULL)
polltv = delaytv;
if (timeout != NULL) {
timersub(timeout, &difftv, timeout);
if (delaytv != NULL && timercmp(timeout, delaytv, <))
polltv = timeout;
else if (delaytv == NULL)
polltv = timeout;
}
/* Calculate polltimeout here based on polltv */
if (polltv != NULL)
polltimeout = polltv->tv_sec * 1000 +
polltv->tv_usec / 100;
else
polltimeout = INFTIM;
#if defined(DEBUG) || defined(DEBUG_POLL)
safe_printv(0, "[DEBUG] IN poll(*, %d, %d)", nfds, polltimeout);
#endif /* DEBUG */
trickle_unlock();
ret = (*libc_poll)((struct pollfd *)fds, (int)nfds, (int)polltimeout);
#if defined(DEBUG) || defined(DEBUG_POLL)
safe_printv(0, "[DEBUG] OUT poll(%d) = %d", nfds, ret);
#endif /* DEBUG */
trickle_lock();
if (ret == 0 && delaytv != NULL && polltv == delaytv) {
_d = select_shift(&dhead, &inittv, &delaytv);
while ((d = TAILQ_FIRST(&dhead)) != NULL && d != _d) {
fds[d->pollidx].events |= d->pollevents;
TAILQ_REMOVE(&dhead, d, next);
free(d);
}
gettimeofday(&curtv, NULL);
goto again;
}
while ((d = TAILQ_FIRST(&dhead)) != NULL) {
CLR(d->sd->data[d->which].flags, SD_INSELECT);
TAILQ_REMOVE(&dhead, d, next);
free(d);
}
trickle_unlock();
return (ret);
}
ssize_t
read(int fd, void *buf, size_t nbytes)
{
ssize_t ret = -1;
size_t xnbytes = nbytes;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(fd, &xnbytes, TRICKLE_RECV) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_read)(fd, buf, xnbytes);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] read(%d, *, %d) = %d", fd, xnbytes, ret);
} else {
safe_printv(0, "[DEBUG] delaying read(%d, *, %d) = %d", fd, xnbytes, ret);
#endif /* DEBUG */
}
update(fd, ret, TRICKLE_RECV);
trickle_unlock();
if (eagain) {
ret = -1;
errno = EAGAIN;
}
return (ret);
}
/*
* XXX defunct for smoothing ... for now
*/
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{
size_t len = 0;
ssize_t ret = -1;
int i, eagain;
for (i = 0; i < iovcnt; i++)
len += iov[i].iov_len;
trickle_lock();
INIT;
if (!(eagain = delay(fd, &len, TRICKLE_RECV) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_readv)(fd, iov, iovcnt);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] readv(%d, *, %d) = %d", fd, iovcnt, ret);
} else {
safe_printv(0, "[DEBUG] delaying readv(%d, *, %d)", fd, iovcnt);
#endif /* DEBUG */
}
update(fd, ret, TRICKLE_RECV);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
#ifndef __FreeBSD__
ssize_t
recv(int sock, void *buf, size_t len, int flags)
{
ssize_t ret = -1;
size_t xlen = len;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(sock, &xlen, TRICKLE_RECV) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_recv)(sock, buf, xlen, flags);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] recv(%d, *, %d, %d) = %d",
sock, len, flags, ret);
} else {
safe_printv(0, "[DEBUG] delaying recv(%d, *, %d, %d)", sock, len, flags);
#endif /* DEBUG */
}
update(sock, ret, TRICKLE_RECV);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
#endif /* !__FreeBSD__ */
#ifdef __sun__
ssize_t
recvfrom(int sock, void *buf, size_t len, int flags, struct sockaddr *from,
Psocklen_t fromlen)
#else
ssize_t
recvfrom(int sock, void *buf, size_t len, int flags, struct sockaddr *from,
socklen_t *fromlen)
#endif /* __sun__ */
{
ssize_t ret = -1;
size_t xlen = len;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(sock, &xlen, TRICKLE_RECV) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_recvfrom)(sock, buf, xlen, flags, from, fromlen);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] recvfrom(%d, *, %d, %d) = %d",
sock, len, flags, ret);
} else {
safe_printv(0, "[DEBUG] delaying recvfrom(%d, *, %d, %d)", sock,
len, flags);
#endif /* DEBUG */
}
update(sock, ret, TRICKLE_RECV);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
ssize_t
write(int fd, const void *buf, size_t len)
{
ssize_t ret = -1;
size_t xlen = len;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(fd, &xlen, TRICKLE_SEND) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_write)(fd, buf, xlen);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] write(%d, *, %d) = %d", fd, len, ret);
} else {
safe_printv(0, "[DEBUG] delaying write(%d, *, %d)", fd, len);
#endif /* DEBUG */
}
update(fd, ret, TRICKLE_SEND);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
/*
* XXX defunct for smoothing ... for now
*/
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{
ssize_t ret = -1;
size_t len = 0;
int i, eagain;
for (i = 0; i < iovcnt; i++)
len += iov[i].iov_len;
trickle_lock();
INIT;
if (!(eagain = delay(fd, &len, TRICKLE_SEND) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_writev)(fd, iov, iovcnt);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] writev(%d, *, %d) = %d",
fd, iovcnt, ret);
} else {
safe_printv(0, "[DEBUG] delaying writev(%d, *, %d)", fd, iovcnt);
#endif /* DEBUG */
}
update(fd, ret, TRICKLE_SEND);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
#ifndef __FreeBSD__
ssize_t
send(int sock, const void *buf, size_t len, int flags)
{
ssize_t ret = -1;
size_t xlen = len;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(sock, &xlen, TRICKLE_SEND) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_send)(sock, buf, xlen, flags);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] send(%d, *, %d, %d) = %d",
sock, len, flags, ret);
} else {
safe_printv(0, "[DEBUG] delaying send(%d, *, %d, %d)", sock,
len, flags);
#endif /* DEBUG */
}
update(sock, ret, TRICKLE_SEND);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
#endif /* !__FreeBSD__ */
ssize_t
sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *to,
socklen_t tolen)
{
ssize_t ret = -1;
size_t xlen = len;
int eagain;
trickle_lock();
INIT;
if (!(eagain = delay(sock, &xlen, TRICKLE_SEND) == TRICKLE_WOULDBLOCK)) {
trickle_unlock();
ret = (*libc_sendto)(sock, buf, xlen, flags, to, tolen);
trickle_lock();
#ifdef DEBUG
safe_printv(0, "[DEBUG] sendto(%d, *, %d) = %d", sock, len, ret);
} else {
safe_printv(0, "[DEBUG] delaying sendto(%d, *, %d)", sock, len);
#endif /* DEBUG */
}
update(sock, ret, TRICKLE_SEND);
trickle_unlock();
if (eagain) {
errno = EAGAIN;
ret = -1;
}
return (ret);
}
#if 0
int
setsockopt(int sock, int level, int optname, const void *optval,
socklen_t option)
{
INIT;
/* blocking, etc. */
return ((*libc_setsockopt)(sock, level, optname, optval, option));
}
#endif /* 0 */
int
dup(int oldfd)
{
int newfd;
struct sockdesc *sd, *nsd;
trickle_lock();
INIT;
trickle_unlock();
newfd = (*libc_dup)(oldfd);
#ifdef DEBUG
safe_printv(0, "[DEBUG] dup(%d) = %d", oldfd, newfd);
#endif /* DEBUG */
trickle_lock();
TAILQ_FOREACH(sd, &sdhead, next)
if (oldfd == sd->sock)
break;
if (sd != NULL && newfd != -1) {
if ((nsd = malloc(sizeof(*nsd))) == NULL) {
trickle_unlock();
(*libc_close)(newfd);