]> sjero.net Git - iperf/blob - src/sockets.c
DCCP support for iperf
[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  * returns the DCCP maximum datagram (payload) size
123  * ------------------------------------------------------------------- */
124 int getsock_dccp_mps(int inSock)
125 {
126     int rc, theMPS = 0;
127     Socklen_t len = sizeof(theMPS);
128
129     assert(inSock >= 0);
130
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");
133
134     return theMPS;
135 }
136
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.
141  *
142  * from Stevens, 1998, section 3.9
143  * ------------------------------------------------------------------- */
144
145 ssize_t readn( int inSock, void *outBuf, size_t inLen ) {
146     size_t  nleft;
147     ssize_t nread;
148     char *ptr;
149
150     assert( inSock >= 0 );
151     assert( outBuf != NULL );
152     assert( inLen > 0 );
153
154     ptr   = (char*) outBuf;
155     nleft = inLen;
156
157     while ( nleft > 0 ) {
158         nread = read( inSock, ptr, nleft );
159         if ( nread < 0 ) {
160             if ( errno == EINTR )
161                 nread = 0;  /* interupted, call read again */
162             else
163                 return -1;  /* error */
164         } else if ( nread == 0 )
165             break;        /* EOF */
166
167         nleft -= nread;
168         ptr   += nread;
169     }
170
171     return(inLen - nleft);
172 } /* end readn */
173
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.
178  *
179  * from Stevens, 1998, section 3.9
180  * ------------------------------------------------------------------- */
181
182 ssize_t writen( int inSock, const void *inBuf, size_t inLen ) {
183     size_t  nleft;
184     ssize_t nwritten;
185     const char *ptr;
186
187     assert( inSock >= 0 );
188     assert( inBuf != NULL );
189     assert( inLen > 0 );
190
191     ptr   = (char*) inBuf;
192     nleft = inLen;
193
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 */
199             else
200                 return -1;    /* error */
201         }
202
203         nleft -= nwritten;
204         ptr   += nwritten;
205     }
206
207     return inLen;
208 } /* end writen */
209
210 #ifdef __cplusplus
211 } /* end extern "C" */
212 #endif