summaryrefslogtreecommitdiffstats
path: root/src/tcp_window_size.c
blob: 4b5d79b387ad5123b2e6650cf0ff26e2060a8336 (plain)
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
/*---------------------------------------------------------------
 * Copyright (c) 1999,2000,2001,2002,2003
 * The Board of Trustees of the University of Illinois
 * All Rights Reserved.
 *---------------------------------------------------------------
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software (Iperf) and associated
 * documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 *
 * Redistributions of source code must retain the above
 * copyright notice, this list of conditions and
 * the following disclaimers.
 *
 *
 * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimers in the documentation and/or other materials
 * provided with the distribution.
 *
 *
 * Neither the names of the University of Illinois, NCSA,
 * nor the names of its contributors may be used to endorse
 * or promote products derived from this Software without
 * specific prior written permission.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ________________________________________________________________
 * National Laboratory for Applied Network Research
 * National Center for Supercomputing Applications
 * University of Illinois at Urbana-Champaign
 * http://www.ncsa.uiuc.edu
 * ________________________________________________________________
 *
 * tcp_window_size.c
 * by Mark Gates <mgates@nlanr.net>
 * -------------------------------------------------------------------
 * set/getsockopt
 * ------------------------------------------------------------------- */

#include "headers.h"

#include "util.h"

#ifdef __cplusplus
extern "C" {
#endif

/* -------------------------------------------------------------------
 * If inTCPWin > 0, set the TCP window size (via the socket buffer
 * sizes) for inSock. Otherwise leave it as the system default.
 *
 * This must be called prior to calling listen() or connect() on
 * the socket, for TCP window sizes > 64 KB to be effective.
 *
 * This now works on UNICOS also, by setting TCP_WINSHIFT.
 * This now works on AIX, by enabling RFC1323.
 * returns -1 on error, 0 on no error.
 * ------------------------------------------------------------------- */

int setsock_tcp_windowsize (int inSock, int inTCPWin, int inSend) {
#ifdef SO_SNDBUF
    int rc;
    int newTCPWin;

    assert(inSock >= 0);
    if (inTCPWin > 0) {
#ifdef TCP_WINSHIFT
        /* UNICOS requires setting the winshift explicitly */
        if (inTCPWin > 65535) {
            int winShift = 0;
            int scaledWin = inTCPWin >> 16;
            while (scaledWin > 0) {
                scaledWin >>= 1;
                winShift++;
            }

            /* set TCP window shift */
            rc = setsockopt(inSock, IPPROTO_TCP, TCP_WINSHIFT,
                             (char*) &winShift, sizeof(winShift));
            if (rc < 0) {
                return rc;
            }

            /* Note: you cannot verify TCP window shift, since it returns
             * a structure and not the same integer we use to set it. (ugh) */
        }
#endif /* TCP_WINSHIFT  */

#ifdef TCP_RFC1323
        /* On AIX, RFC 1323 extensions can be set system-wide,
         * using the 'no' network options command. But we can also set them
         * per-socket, so let's try just in case. */
        if (inTCPWin > 65535) {
            /* enable RFC 1323 */
            int on = 1;
            rc = setsockopt(inSock, IPPROTO_TCP, TCP_RFC1323,
                             (char*) &on, sizeof(on));
            if (rc < 0) {
                return rc;
            }
        }
#endif /* TCP_RFC1323 */

        if (!inSend) {
            /* receive buffer -- set
             * note: results are verified after connect() or listen(),
             * since some OS's don't show the corrected value until then. */
            newTCPWin = inTCPWin;
            rc = setsockopt(inSock, SOL_SOCKET, SO_RCVBUF,
                             (char*) &newTCPWin, sizeof(newTCPWin));
        } else {
            /* send buffer -- set
             * note: results are verified after connect() or listen(),
             * since some OS's don't show the corrected value until then. */
            newTCPWin = inTCPWin;
            rc = setsockopt(inSock, SOL_SOCKET, SO_SNDBUF,
                             (char*) &newTCPWin, sizeof(newTCPWin));
        }
        if (rc < 0) {
            return rc;
        }
    }
#endif /* SO_SNDBUF */
    return 0;
} /* end setsock_tcp_windowsize */

/* -------------------------------------------------------------------
 * returns the TCP window size (on the sending buffer, SO_SNDBUF),
 * or -1 on error.
 * ------------------------------------------------------------------- */

int getsock_tcp_windowsize (int inSock, int inSend) {
    int rc = -1;
#ifdef SO_SNDBUF
    Socklen_t len;
    int theTCPWin = 0;
    /* send buffer -- query for buffer size */
    len = sizeof(theTCPWin);
    if (inSend) {
        rc = getsockopt(inSock, SOL_SOCKET, SO_SNDBUF,
                         (char*) &theTCPWin, &len);
    } else {
        rc = getsockopt(inSock, SOL_SOCKET, SO_RCVBUF,
                         (char*) &theTCPWin, &len);
    }
    if (rc == 0) {
        rc = theTCPWin;
    }
#endif
    return rc;
} /* end getsock_tcp_windowsize */

#if HAVE_DECL_TCP_WINDOW_CLAMP
int setsock_tcp_windowclamp (int inSock, int clampsize) {
    assert(inSock >= 0);
    assert(clampsize > 0);
    int rc;
    Socklen_t len;

    len = sizeof(clampsize);
    rc = setsockopt(inSock, IPPROTO_TCP, TCP_WINDOW_CLAMP, (char*)(&clampsize), len);
    WARN_errno(rc == SOCKET_ERROR, "setsockopt TCP_WINDOW_CLAMP");
    return rc;
}

int getsock_tcp_windowclamp (int inSock) {
    assert(inSock >= 0);
    int clamp = 0;
    int rc;
    Socklen_t len;

    /* send buffer -- query for buffer size */
    len = sizeof(clamp);
    rc = getsockopt(inSock, IPPROTO_TCP, TCP_WINDOW_CLAMP, (char*)(&clamp), &len);
    WARN_errno(rc == SOCKET_ERROR, "getsockopt TCP_WINDOW_CLAMP");
    if (rc < 0) {
	return rc;
    }
    return clamp;
}
#endif

#if HAVE_DECL_TCP_NOTSENT_LOWAT
int setsock_tcp_notsent_low_watermark (int inSock, int watermark) {
    assert(inSock >= 0);
    assert(watermark > 0);
    int rc;
    Socklen_t len;

    len = sizeof(watermark);
    rc = setsockopt(inSock, IPPROTO_TCP, TCP_NOTSENT_LOWAT, (char*)(&watermark), len);
    WARN_errno(rc == SOCKET_ERROR, "setsockopt TCP_NOTSENT_LOWAT");
    return rc;
}

int getsock_tcp_notsent_low_watermark (int inSock) {
    assert(inSock >= 0);
    int watermark = 0;
    int rc;
    Socklen_t len;

    /* send buffer -- query for buffer size */
    len = sizeof(watermark);
    rc = getsockopt(inSock, IPPROTO_TCP, TCP_NOTSENT_LOWAT, (char*)(&watermark), &len);
    WARN_errno(rc == SOCKET_ERROR, "getsockopt TCP_NOTSENT_LOWAT");
    if (rc < 0) {
	return rc;
    }
    return watermark;
}
#endif
#ifdef __cplusplus
} /* end extern "C" */
#endif