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 * -------------------------------------------------------------------
50 * set/getsockopt and read/write wrappers
51 * ------------------------------------------------------------------- */
60 /* -------------------------------------------------------------------
61 * If inMSS > 0, set the TCP maximum segment size for inSock.
62 * Otherwise leave it as the system default.
63 * ------------------------------------------------------------------- */
65 const char warn_mss_fail[] = "\
66 WARNING: attempt to set TCP maxmimum segment size to %d failed.\n\
67 Setting the MSS may not be implemented on this OS.\n";
69 const char warn_mss_notset[] =
70 "WARNING: attempt to set TCP maximum segment size to %d, but got %d\n";
72 void setsock_tcp_mss( int inSock, int inMSS ) {
78 assert( inSock != INVALID_SOCKET );
83 len = sizeof( newMSS );
84 rc = setsockopt( inSock, IPPROTO_TCP, TCP_MAXSEG, (char*) &newMSS, len );
85 if ( rc == SOCKET_ERROR ) {
86 fprintf( stderr, warn_mss_fail, newMSS );
91 rc = getsockopt( inSock, IPPROTO_TCP, TCP_MAXSEG, (char*) &newMSS, &len );
92 WARN_errno( rc == SOCKET_ERROR, "getsockopt TCP_MAXSEG" );
93 if ( newMSS != inMSS ) {
94 fprintf( stderr, warn_mss_notset, inMSS, newMSS );
98 } /* end setsock_tcp_mss */
100 /* -------------------------------------------------------------------
101 * returns the TCP maximum segment size
102 * ------------------------------------------------------------------- */
104 int getsock_tcp_mss( int inSock ) {
110 assert( inSock >= 0 );
113 len = sizeof( theMSS );
114 rc = getsockopt( inSock, IPPROTO_TCP, TCP_MAXSEG, (char*) &theMSS, &len );
115 WARN_errno( rc == SOCKET_ERROR, "getsockopt TCP_MAXSEG" );
119 } /* end getsock_tcp_mss */
121 /* -------------------------------------------------------------------
122 * Attempts to reads n bytes from a socket.
123 * Returns number actually read, or -1 on error.
124 * If number read < inLen then we reached EOF.
126 * from Stevens, 1998, section 3.9
127 * ------------------------------------------------------------------- */
129 ssize_t readn( int inSock, void *outBuf, size_t inLen ) {
134 assert( inSock >= 0 );
135 assert( outBuf != NULL );
138 ptr = (char*) outBuf;
141 while ( nleft > 0 ) {
142 nread = read( inSock, ptr, nleft );
144 if ( errno == EINTR )
145 nread = 0; /* interupted, call read again */
147 return -1; /* error */
148 } else if ( nread == 0 )
155 return(inLen - nleft);
158 /* -------------------------------------------------------------------
159 * Attempts to write n bytes to a socket.
160 * returns number actually written, or -1 on error.
161 * number written is always inLen if there is not an error.
163 * from Stevens, 1998, section 3.9
164 * ------------------------------------------------------------------- */
166 ssize_t writen( int inSock, const void *inBuf, size_t inLen ) {
171 assert( inSock >= 0 );
172 assert( inBuf != NULL );
178 while ( nleft > 0 ) {
179 nwritten = write( inSock, ptr, nleft );
180 if ( nwritten <= 0 ) {
181 if ( errno == EINTR )
182 nwritten = 0; /* interupted, call write again */
184 return -1; /* error */
195 } /* end extern "C" */