]> sjero.net Git - iperf/blob - src/sockets.c
72eddd82a8c288fee619f9e536ce52a9cf76ab6f
[iperf] / src / sockets.c
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                              
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
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: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
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. 
25  * 
26  *     
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. 
31  * 
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  * ________________________________________________________________ 
46  *
47  * socket.c
48  * by Mark Gates <mgates@nlanr.net>
49  * -------------------------------------------------------------------
50  * set/getsockopt and read/write wrappers
51  * ------------------------------------------------------------------- */
52
53 #include "headers.h"
54 #include "util.h"
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 /* -------------------------------------------------------------------
61  * If inMSS > 0, set the TCP maximum segment size  for inSock.
62  * Otherwise leave it as the system default.
63  * ------------------------------------------------------------------- */
64
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";
68
69 const char warn_mss_notset[] =
70 "WARNING: attempt to set TCP maximum segment size to %d, but got %d\n";
71
72 void setsock_tcp_mss( int inSock, int inMSS ) {
73 #ifdef TCP_MAXSEG
74     int rc;
75     int newMSS;
76     Socklen_t len;
77
78     assert( inSock != INVALID_SOCKET );
79
80     if ( inMSS > 0 ) {
81         /* set */
82         newMSS = inMSS;
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 );
87             return;
88         }
89
90         /* verify results */
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 );
95         }
96     }
97 #endif
98 } /* end setsock_tcp_mss */
99
100 /* -------------------------------------------------------------------
101  * returns the TCP maximum segment size
102  * ------------------------------------------------------------------- */
103
104 int getsock_tcp_mss( int inSock ) {
105     int theMSS = 0;
106
107 #ifdef TCP_MAXSEG
108     int rc;
109     Socklen_t len;
110     assert( inSock >= 0 );
111
112     /* query for MSS */
113     len = sizeof( theMSS );
114     rc = getsockopt( inSock, IPPROTO_TCP, TCP_MAXSEG, (char*) &theMSS, &len );
115     WARN_errno( rc == SOCKET_ERROR, "getsockopt TCP_MAXSEG" );
116 #endif
117
118     return theMSS;
119 } /* end getsock_tcp_mss */
120
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.
125  *
126  * from Stevens, 1998, section 3.9
127  * ------------------------------------------------------------------- */
128
129 ssize_t readn( int inSock, void *outBuf, size_t inLen ) {
130     size_t  nleft;
131     ssize_t nread;
132     char *ptr;
133
134     assert( inSock >= 0 );
135     assert( outBuf != NULL );
136     assert( inLen > 0 );
137
138     ptr   = (char*) outBuf;
139     nleft = inLen;
140
141     while ( nleft > 0 ) {
142         nread = read( inSock, ptr, nleft );
143         if ( nread < 0 ) {
144             if ( errno == EINTR )
145                 nread = 0;  /* interupted, call read again */
146             else
147                 return -1;  /* error */
148         } else if ( nread == 0 )
149             break;        /* EOF */
150
151         nleft -= nread;
152         ptr   += nread;
153     }
154
155     return(inLen - nleft);
156 } /* end readn */
157
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.
162  *
163  * from Stevens, 1998, section 3.9
164  * ------------------------------------------------------------------- */
165
166 ssize_t writen( int inSock, const void *inBuf, size_t inLen ) {
167     size_t  nleft;
168     ssize_t nwritten;
169     const char *ptr;
170
171     assert( inSock >= 0 );
172     assert( inBuf != NULL );
173     assert( inLen > 0 );
174
175     ptr   = (char*) inBuf;
176     nleft = inLen;
177
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 */
183             else
184                 return -1;    /* error */
185         }
186
187         nleft -= nwritten;
188         ptr   += nwritten;
189     }
190
191     return inLen;
192 } /* end writen */
193
194 #ifdef __cplusplus
195 } /* end extern "C" */
196 #endif