/*--------------------------------------------------------------- * 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.
* ------------------------------------------------------------------- */
/* ------------------------------------------------------------------- * Resolve the hostname address and fill it in.
* ------------------------------------------------------------------- */
void SockAddr_setHostname( constchar* inHostname,
iperf_sockaddr *inSockAddr, int isIPv6 ) {
// ..I think this works for both ipv6 & ipv4... we'll see #ifdefined(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,
(unsignedchar*)&(((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 */ constchar* 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).
* ------------------------------------------------------------------- */
/* ------------------------------------------------------------------- * Set the port to the given port. Handles the byte swapping.
* ------------------------------------------------------------------- */
/* ------------------------------------------------------------------- * Set the port to zero, which lets the OS pick the port.
* ------------------------------------------------------------------- */
/* ------------------------------------------------------------------- * Return the IPv4 Internet Address from the sockaddr_in structure
* ------------------------------------------------------------------- */
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.
* ------------------------------------------------------------------- */
#ifdefined(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 ) {
#ifdefined(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 ) {
#ifdefined(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) constunsignedlong kMulticast_Mask = 0xe0000000L;
/* ------------------------------------------------------------------- * Zero out the address structure.
* ------------------------------------------------------------------- */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.