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 * returns the DCCP maximum datagram (payload) size
123 * ------------------------------------------------------------------- */
124 int getsock_dccp_mps(int inSock)
127 Socklen_t len = sizeof(theMPS);
131 rc = getsockopt(inSock, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &theMPS, &len);
132 WARN_errno(rc == SOCKET_ERROR, "getsockopt DCCP_SOCKOPT_GET_CUR_MPS");
137 /* -------------------------------------------------------------------
138 * Attempts to reads n bytes from a socket.
139 * Returns number actually read, or -1 on error.
140 * If number read < inLen then we reached EOF.
142 * from Stevens, 1998, section 3.9
143 * ------------------------------------------------------------------- */
145 ssize_t readn( int inSock, void *outBuf, size_t inLen ) {
150 assert( inSock >= 0 );
151 assert( outBuf != NULL );
154 ptr = (char*) outBuf;
157 while ( nleft > 0 ) {
158 nread = read( inSock, ptr, nleft );
160 if ( errno == EINTR )
161 nread = 0; /* interupted, call read again */
163 return -1; /* error */
164 } else if ( nread == 0 )
171 return(inLen - nleft);
174 /* -------------------------------------------------------------------
175 * Attempts to write n bytes to a socket.
176 * returns number actually written, or -1 on error.
177 * number written is always inLen if there is not an error.
179 * from Stevens, 1998, section 3.9
180 * ------------------------------------------------------------------- */
182 ssize_t writen( int inSock, const void *inBuf, size_t inLen ) {
187 assert( inSock >= 0 );
188 assert( inBuf != NULL );
194 while ( nleft > 0 ) {
195 nwritten = write( inSock, ptr, nleft );
196 if ( nwritten <= 0 ) {
197 if ( errno == EINTR )
198 nwritten = 0; /* interupted, call write again */
200 return -1; /* error */
211 } /* end extern "C" */