-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocketAddr.c
422 lines (371 loc) · 15.8 KB
/
SocketAddr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*---------------------------------------------------------------
* Copyright (c) 1999,2000,2001,2002,2003
* The Board of Trustees of the University of Illinois
* All Rights Reserved.
*---------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software (Iperf) and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and
* the following disclaimers.
*
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimers in the documentation and/or other materials
* provided with the distribution.
*
*
* Neither the names of the University of Illinois, NCSA,
* nor the names of its contributors may be used to endorse
* or promote products derived from this Software without
* specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ________________________________________________________________
* National Laboratory for Applied Network Research
* National Center for Supercomputing Applications
* University of Illinois at Urbana-Champaign
* http://www.ncsa.uiuc.edu
* ________________________________________________________________
*
* Socket.cpp
* by Ajay Tirumala <tirumala@ncsa.uiuc.edu>
* and Mark Gates <mgates@nlanr.net>
* ------------------------------------------------------------------- */
#define HEADERS()
#include "headers.h"
#include "SocketAddr.h"
#ifdef __cplusplus
extern "C" {
#endif
/* -------------------------------------------------------------------
* Create a socket address. If inHostname is not null, resolve that
* address and fill it in. Fill in the port number. Use IPv6 ADDR_ANY
* if that is what is desired.
* ------------------------------------------------------------------- */
void SockAddr_remoteAddr( thread_Settings *inSettings ) {
SockAddr_zeroAddress( &inSettings->peer );
if ( inSettings->mHost != NULL ) {
SockAddr_setHostname( inSettings->mHost, &inSettings->peer,
isIPV6( inSettings ) );
} else {
#ifdef HAVE_IPV6
if ( isIPV6( inSettings ) ) {
((struct sockaddr*)&inSettings->peer)->sa_family = AF_INET6;
} else {
((struct sockaddr*)&inSettings->peer)->sa_family = AF_INET;
}
}
if ( SockAddr_isIPv6( &inSettings->peer ) ) {
inSettings->size_peer = sizeof( struct sockaddr_in6 );
} else {
inSettings->size_peer = sizeof( struct sockaddr_in );
}
#else
((struct sockaddr*)&inSettings->peer)->sa_family = AF_INET;
}
inSettings->size_peer = sizeof( struct sockaddr_in );
#endif
SockAddr_setPort( &inSettings->peer, inSettings->mPort );
}
// end SocketAddr
void SockAddr_localAddr( thread_Settings *inSettings ) {
SockAddr_zeroAddress( &inSettings->local );
if ( inSettings->mLocalhost != NULL ) {
SockAddr_setHostname( inSettings->mLocalhost, &inSettings->local,
isIPV6( inSettings ) );
} else {
#ifdef HAVE_IPV6
if ( isIPV6( inSettings ) ) {
((struct sockaddr*)&inSettings->local)->sa_family = AF_INET6;
} else {
((struct sockaddr*)&inSettings->local)->sa_family = AF_INET;
}
}
if ( SockAddr_isIPv6( &inSettings->local ) ) {
inSettings->size_local = sizeof( struct sockaddr_in6 );
} else {
inSettings->size_local = sizeof( struct sockaddr_in );
}
#else
((struct sockaddr*)&inSettings->local)->sa_family = AF_INET;
}
inSettings->size_local = sizeof( struct sockaddr_in );
#endif
SockAddr_setPort( &inSettings->local, inSettings->mPort );
}
// end SocketAddr
/* -------------------------------------------------------------------
* Resolve the hostname address and fill it in.
* ------------------------------------------------------------------- */
void SockAddr_setHostname( const char* inHostname,
iperf_sockaddr *inSockAddr,
int isIPv6 ) {
// ..I think this works for both ipv6 & ipv4... we'll see
#if defined(HAVE_IPV6)
{
struct addrinfo *res, *itr;
int ret_ga;
ret_ga = getaddrinfo(inHostname, NULL, NULL, &res);
if ( ret_ga ) {
fprintf(stderr, "error: %s\n", gai_strerror(ret_ga));
exit(1);
}
if ( !res->ai_addr ) {
fprintf(stderr, "getaddrinfo failed to get an address... target was '%s'\n", inHostname);
exit(1);
}
// Check address type before filling in the address
// ai_family = PF_xxx; ai_protocol = IPPROTO_xxx, see netdb.h
// ...but AF_INET6 == PF_INET6
itr = res;
if ( isIPv6 ) {
// First check all results for a IPv6 Address
while ( itr != NULL ) {
if ( itr->ai_family == AF_INET6 ) {
memcpy(inSockAddr, (itr->ai_addr),
(itr->ai_addrlen));
freeaddrinfo(res);
return;
} else {
itr = itr->ai_next;
}
}
}
itr = res;
// Now find a IPv4 Address
while ( itr != NULL ) {
if ( itr->ai_family == AF_INET ) {
memcpy(inSockAddr, (itr->ai_addr),
(itr->ai_addrlen));
freeaddrinfo(res);
return;
} else {
itr = itr->ai_next;
}
}
}
#else
// first try just converting dotted decimal
// on Windows gethostbyname doesn't understand dotted decimal
int rc = inet_pton( AF_INET, inHostname,
(unsigned char*)&(((struct sockaddr_in*)inSockAddr)->sin_addr) );
inSockAddr->sin_family = AF_INET;
if ( rc == 0 ) {
struct hostent *hostP = gethostbyname( inHostname );
if ( hostP == NULL ) {
/* this is the same as herror() but works on more systems */
const char* format;
switch ( h_errno ) {
case HOST_NOT_FOUND:
format = "%s: Unknown host\n";
break;
case NO_ADDRESS:
format = "%s: No address associated with name\n";
break;
case NO_RECOVERY:
format = "%s: Unknown server error\n";
break;
case TRY_AGAIN:
format = "%s: Host name lookup failure\n";
break;
default:
format = "%s: Unknown resolver error\n";
break;
}
fprintf( stderr, format, inHostname );
exit(1);
return; // TODO throw
}
memcpy(&(((struct sockaddr_in*)inSockAddr)->sin_addr), *(hostP->h_addr_list),
(hostP->h_length));
}
#endif
}
// end setHostname
/* -------------------------------------------------------------------
* Copy the IP address into the string.
* ------------------------------------------------------------------- */
void SockAddr_getHostAddress( iperf_sockaddr *inSockAddr, char* outAddress,
size_t len ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET ) {
inet_ntop( AF_INET, &(((struct sockaddr_in*) inSockAddr)->sin_addr),
outAddress, len);
}
#ifdef HAVE_IPV6
else {
inet_ntop( AF_INET6, &(((struct sockaddr_in6*) inSockAddr)->sin6_addr),
outAddress, len);
}
#endif
}
// end getHostAddress
/* -------------------------------------------------------------------
* Set the address to any (generally all zeros).
* ------------------------------------------------------------------- */
void SockAddr_setAddressAny( iperf_sockaddr *inSockAddr ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET )
memset( &(((struct sockaddr_in*) inSockAddr)->sin_addr), 0,
sizeof( struct in_addr ));
#if defined(HAVE_IPV6)
else
memset( &(((struct sockaddr_in6*) inSockAddr)->sin6_addr), 0,
sizeof( struct in6_addr ));
#endif
}
// end setAddressAny
/* -------------------------------------------------------------------
* Set the port to the given port. Handles the byte swapping.
* ------------------------------------------------------------------- */
void SockAddr_setPort( iperf_sockaddr *inSockAddr, unsigned short inPort ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET )
((struct sockaddr_in*) inSockAddr)->sin_port = htons( inPort );
#if defined(HAVE_IPV6)
else
((struct sockaddr_in6*) inSockAddr)->sin6_port = htons( inPort );
#endif
}
// end setPort
/* -------------------------------------------------------------------
* Set the port to zero, which lets the OS pick the port.
* ------------------------------------------------------------------- */
void SockAddr_setPortAny( iperf_sockaddr *inSockAddr ) {
SockAddr_setPort( inSockAddr, 0 );
}
// end setPortAny
/* -------------------------------------------------------------------
* Return the port. Handles the byte swapping.
* ------------------------------------------------------------------- */
unsigned short SockAddr_getPort( iperf_sockaddr *inSockAddr ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET )
return ntohs( ((struct sockaddr_in*) inSockAddr)->sin_port );
#if defined(HAVE_IPV6)
else
return ntohs( ((struct sockaddr_in6*) inSockAddr)->sin6_port);
#endif
return 0;
}
// end getPort
/* -------------------------------------------------------------------
* Return the IPv4 Internet Address from the sockaddr_in structure
* ------------------------------------------------------------------- */
struct in_addr* SockAddr_get_in_addr( iperf_sockaddr *inSockAddr ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET )
return &(((struct sockaddr_in*) inSockAddr)->sin_addr);
fprintf(stderr, "FATAL: get_in_addr called on IPv6 address\n");
return NULL;
}
/* -------------------------------------------------------------------
* Return the IPv6 Internet Address from the sockaddr_in6 structure
* ------------------------------------------------------------------- */
#ifdef HAVE_IPV6
struct in6_addr* SockAddr_get_in6_addr( iperf_sockaddr *inSockAddr ) {
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET6 )
return &(((struct sockaddr_in6*) inSockAddr)->sin6_addr);
fprintf(stderr, "FATAL: get_in6_addr called on IPv4 address\n");
return NULL;
}
#endif
/* -------------------------------------------------------------------
* Return the size of the appropriate address structure.
* ------------------------------------------------------------------- */
Socklen_t SockAddr_get_sizeof_sockaddr( iperf_sockaddr *inSockAddr ) {
#if defined(HAVE_IPV6)
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET6 ) {
return(sizeof(struct sockaddr_in6));
}
#endif
return(sizeof(struct sockaddr_in));
}
// end get_sizeof_sockaddr
/* -------------------------------------------------------------------
* Return if IPv6 socket
* ------------------------------------------------------------------- */
int SockAddr_isIPv6( iperf_sockaddr *inSockAddr ) {
#if defined(HAVE_IPV6)
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET6 ) {
return 1;
}
#endif
return 0;
}
// end get_sizeof_sockaddr
/* -------------------------------------------------------------------
* Return true if the address is a IPv4 multicast address.
* ------------------------------------------------------------------- */
int SockAddr_isMulticast( iperf_sockaddr *inSockAddr ) {
#if defined(HAVE_IPV6)
if ( ((struct sockaddr*)inSockAddr)->sa_family == AF_INET6 ) {
return( IN6_IS_ADDR_MULTICAST(&(((struct sockaddr_in6*) inSockAddr)->sin6_addr) ));
} else
#endif
{
// 224.0.0.0 to 239.255.255.255 (e0.00.00.00 to ef.ff.ff.ff)
const unsigned long kMulticast_Mask = 0xe0000000L;
return(kMulticast_Mask ==
(ntohl( ((struct sockaddr_in*) inSockAddr)->sin_addr.s_addr) & kMulticast_Mask));
}
}
// end isMulticast
/* -------------------------------------------------------------------
* Zero out the address structure.
* ------------------------------------------------------------------- */
void SockAddr_zeroAddress( iperf_sockaddr *inSockAddr ) {
memset( inSockAddr, 0, sizeof( iperf_sockaddr ));
}
// zeroAddress
/* -------------------------------------------------------------------
* Compare two sockaddrs and return true if they are equal
* ------------------------------------------------------------------- */
int SockAddr_are_Equal( struct sockaddr* first, struct sockaddr* second ) {
if ( first->sa_family == AF_INET && second->sa_family == AF_INET ) {
// compare IPv4 adresses
return( ((long) ((struct sockaddr_in*)first)->sin_addr.s_addr == (long) ((struct sockaddr_in*)second)->sin_addr.s_addr)
&& ( ((struct sockaddr_in*)first)->sin_port == ((struct sockaddr_in*)second)->sin_port) );
}
#if defined(HAVE_IPV6)
if ( first->sa_family == AF_INET6 && second->sa_family == AF_INET6 ) {
// compare IPv6 addresses
return( !memcmp(((struct sockaddr_in6*)first)->sin6_addr.s6_addr, ((struct sockaddr_in6*)second)->sin6_addr.s6_addr, sizeof(struct in6_addr))
&& (((struct sockaddr_in6*)first)->sin6_port == ((struct sockaddr_in6*)second)->sin6_port) );
}
#endif
return 0;
}
/* -------------------------------------------------------------------
* Compare two sockaddrs and return true if the hosts are equal
* ------------------------------------------------------------------- */
int SockAddr_Hostare_Equal( struct sockaddr* first, struct sockaddr* second ) {
if ( first->sa_family == AF_INET && second->sa_family == AF_INET ) {
// compare IPv4 adresses
return( (long) ((struct sockaddr_in*)first)->sin_addr.s_addr ==
(long) ((struct sockaddr_in*)second)->sin_addr.s_addr);
}
#if defined(HAVE_IPV6)
if ( first->sa_family == AF_INET6 && second->sa_family == AF_INET6 ) {
// compare IPv6 addresses
return( !memcmp(((struct sockaddr_in6*)first)->sin6_addr.s6_addr,
((struct sockaddr_in6*)second)->sin6_addr.s6_addr, sizeof(struct in6_addr)));
}
#endif
return 0;
}
#ifdef __cplusplus
} /* end extern "C" */
#endif