📄 socketaddr.c
字号:
/*--------------------------------------------------------------- * 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 __cplusplusextern "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 SocketAddrvoid 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 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -