1 /*
2 ** Copyright (C) 2004-2020 by Carnegie Mellon University.
3 **
4 ** @OPENSOURCE_LICENSE_START@
5 ** See license information in ../../LICENSE.txt
6 ** @OPENSOURCE_LICENSE_END@
7 */
8 
9 /*
10 **
11 **  Exports a method to portably set socket send/receive buffer sizes.
12 **
13 */
14 
15 
16 #include <silk/silk.h>
17 
18 RCSIDENT("$SiLK: sku-bigsockbuf.c ef14e54179be 2020-04-14 21:57:45Z mthomas $");
19 
20 #include <silk/utils.h>
21 
22 /*
23  * function: skGrowSocketBuffer
24  *
25  * There is no portable way to determine the max send and receive buffers
26  * that can be set for a socket, so guess then decrement that guess by
27  * 2K until the call succeeds.  If n > 1MB then the decrement by .5MB
28  * instead.
29  *
30  * returns size or -1 for error
31  */
32 int
skGrowSocketBuffer(int fd,int dir,int size)33 skGrowSocketBuffer(
34     int                 fd,
35     int                 dir,
36     int                 size)
37 {
38     int n, tries;
39 
40     /* initial size */
41     n = size;
42     tries = 0;
43 
44     while (n > 4096) {
45         if (setsockopt(fd, SOL_SOCKET, dir, (char*)&n, sizeof (n)) < 0) {
46             /* anything other than no buffers available is fatal */
47             if (errno != ENOBUFS) {
48                 return -1;
49             }
50             /* try a smaller value */
51             if (n > 1024*1024) { /*most systems not > 256K bytes w/o tweaking*/
52                 n -= 1024*1024;
53             } else {
54                 n -= 2048;
55             }
56             ++tries;
57         } else {
58             return n;
59         }
60     } /* while */
61 
62     /* no increase in buffer size */
63     return 0;
64 }
65 
66 
67 /*
68 ** Local Variables:
69 ** mode:c
70 ** indent-tabs-mode:nil
71 ** c-basic-offset:4
72 ** End:
73 */
74