1 /*---------------------------------------------------------------
2 * Copyright (c) 1999,2000,2001,2002,2003
3 * The Board of Trustees of the University of Illinois
5 *---------------------------------------------------------------
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software (Iperf) and associated
8 * documentation files (the "Software"), to deal in the Software
9 * without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute,
11 * sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do
13 * so, subject to the following conditions:
16 * Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and
18 * the following disclaimers.
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimers in the documentation and/or other materials
24 * provided with the distribution.
27 * Neither the names of the University of Illinois, NCSA,
28 * nor the names of its contributors may be used to endorse
29 * or promote products derived from this Software without
30 * specific prior written permission.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
36 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
38 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 * ________________________________________________________________
41 * National Laboratory for Applied Network Research
42 * National Center for Supercomputing Applications
43 * University of Illinois at Urbana-Champaign
44 * http://www.ncsa.uiuc.edu
45 * ________________________________________________________________
48 * by Mark Gates <mgates@nlanr.net>
49 * -------------------------------------------------------------------
51 * ------------------------------------------------------------------- */
61 /* -------------------------------------------------------------------
62 * Set the socket buffer size in bytes (returns -1 on error, 0 otherwise).
63 * ------------------------------------------------------------------- */
64 int set_buffer_sock_size(int inSock, int inWinSize , bool inSend)
66 // note: results are verified after connect() or listen(),
67 // since some OS's don't show the corrected value until then.
70 return setsockopt(inSock, SOL_SOCKET,
71 inSend ? SO_SNDBUF: SO_RCVBUF,
72 &inWinSize, sizeof(inWinSize));
75 fprintf(stderr, "%s: no support for SO_SNDBUF\n", __FUNCTION__);
80 /* -------------------------------------------------------------------
81 * If inTCPWin > 0, set the TCP window size (via the socket buffer
82 * sizes) for inSock. Otherwise leave it as the system default.
84 * This must be called prior to calling listen() or connect() on
85 * the socket, for TCP window sizes > 64 KB to be effective.
87 * This now works on UNICOS also, by setting TCP_WINSHIFT.
88 * This now works on AIX, by enabling RFC1323.
89 * returns -1 on error, 0 on no error.
90 * ------------------------------------------------------------------- */
91 int setsock_tcp_windowsize(int inSock, int inTCPWin, int inSend)
93 assert( inSock >= 0 );
97 fprintf(stderr, "WARNING: A TCP window size of %d bytes is "
98 "considered small and will give poor performance. "
99 "See the Iperf documentation.\n", inTCPWin);
101 /* UNICOS requires setting the winshift explicitly */
102 if ( inTCPWin > 65535 ) {
104 int scaledWin = inTCPWin >> 16;
105 while ( scaledWin > 0 ) {
110 /* set TCP window shift */
111 rc = setsockopt( inSock, IPPROTO_TCP, TCP_WINSHIFT,
112 (char*) &winShift, sizeof( winShift ));
117 /* Note: you cannot verify TCP window shift, since it returns
118 * a structure and not the same integer we use to set it. (ugh) */
120 #endif /* TCP_WINSHIFT */
123 /* On AIX, RFC 1323 extensions can be set system-wide,
124 * using the 'no' network options command. But we can also set them
125 * per-socket, so let's try just in case. */
126 if ( inTCPWin > 65535 ) {
127 /* enable RFC 1323 */
129 rc = setsockopt( inSock, IPPROTO_TCP, TCP_RFC1323,
130 (char*) &on, sizeof( on ));
135 #endif /* TCP_RFC1323 */
137 return set_buffer_sock_size(inSock, inTCPWin, inSend);
140 /* -------------------------------------------------------------------
141 * returns the send/receive socket buffer size in bytes
143 * ------------------------------------------------------------------- */
144 int get_buffer_sock_size( int inSock, int inSend )
148 Socklen_t len = sizeof(bufSize);
152 rc = getsockopt( inSock, SOL_SOCKET, SO_SNDBUF,
153 (char*) &bufSize, &len );
155 rc = getsockopt( inSock, SOL_SOCKET, SO_RCVBUF,
156 (char*) &bufSize, &len );
164 } /* end extern "C" */