[complement-svn] SF.net SVN: complement: [1826] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-02-28 11:16:26
|
Revision: 1826 http://complement.svn.sourceforge.net/complement/?rev=1826&view=rev Author: complement Date: 2008-02-28 03:16:23 -0800 (Thu, 28 Feb 2008) Log Message: ----------- move findhost and iface list utilities into separate files; libsockios: Version 1.14.2. Modified Paths: -------------- trunk/complement/explore/include/sockios/sockstream trunk/complement/explore/include/sockios/sockstream.cc trunk/complement/explore/lib/sockios/ChangeLog trunk/complement/explore/lib/sockios/Makefile.inc trunk/complement/explore/lib/sockios/_sockstream.cc Added Paths: ----------- trunk/complement/explore/include/sockios/netinfo.h trunk/complement/explore/lib/sockios/netinfo.cc Copied: trunk/complement/explore/include/sockios/netinfo.h (from rev 1817, trunk/complement/explore/include/sockios/sockstream) =================================================================== --- trunk/complement/explore/include/sockios/netinfo.h (rev 0) +++ trunk/complement/explore/include/sockios/netinfo.h 2008-02-28 11:16:23 UTC (rev 1826) @@ -0,0 +1,268 @@ +// -*- C++ -*- Time-stamp: <07/09/06 23:42:19 ptr> + +/* + * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 + * Petr Ovtchenkov + * + * Portion Copyright (c) 1999-2001 + * Parallel Graphics Ltd. + * + * Licensed under the Academic Free License version 3.0 + * + */ + +#ifndef __SOCKIOS_NETINFO_H +#define __SOCKIOS_NETINFO_H + +#ifndef __config_feature_h +#include <config/feature.h> +#endif + +#ifdef __FIT_NONREENTRANT +extern "C" int x_getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **); +extern "C" void x_freeaddrinfo( struct addrinfo * ); +extern "C" int x_res_search(const char *, int, int, unsigned char *, int); +extern "C" int x_res_init(void); +#endif + +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/nameser.h> +#include <resolv.h> +#include <sys/ioctl.h> +#include <net/if.h> + +#include <string> +#include <stdexcept> + +#ifdef WIN32 +# include <winsock2.h> +#else // WIN32 +# include <unistd.h> +# include <sys/types.h> +# if defined(__hpux) && !defined(_INCLUDE_XOPEN_SOURCE_EXTENDED) +# define _INCLUDE_XOPEN_SOURCE_EXTENDED +# endif +# include <sys/socket.h> +# if !defined(__UCLIBC__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) +# include <stropts.h> +# endif +# ifdef __sun +# include <sys/conf.h> +# endif +# include <netinet/in.h> +# include <arpa/inet.h> +# include <netdb.h> +# ifdef __hpux +// # ifndef socklen_t // HP-UX 10.01 +// typedef int socklen_t; +// # endif +# endif +# include <cerrno> +#endif // !WIN32 + +#ifdef STLPORT +_STLP_BEGIN_NAMESPACE +#else +namespace std { +#endif + +in_addr findhost( const char *hostname ) throw( std::domain_error ); +std::string hostname( unsigned long inet_addr ); +std::string hostname(); + +int service( const char *name, const char *proto ) throw( std::domain_error ); +std::string service( int port, const char *proto ) throw( std::domain_error ); + +/* + * Expected host name, return (via back insert iterator) + * all IPs (in_addr) for specified host name; nothing will be added in case of + * failure. + */ +template <class BackInsertIterator> +void gethostaddr( const char *hostname, BackInsertIterator bi ) +{ + int _errno = 0; + +#ifndef __FIT_GETHOSTBYADDR + hostent _host; +# ifndef __hpux + char tmpbuf[4096]; +# else // __hpux + hostent_data tmpbuf; +# endif // __hpux +# ifdef __linux + hostent *host = 0; + gethostbyname_r( hostname, &_host, tmpbuf, 4096, &host, &_errno ); +# elif defined(__hpux) + _errno = gethostbyname_r( hostname, &_host, &tmpbuf ); + hostent *host = &_host; +# elif defined(__sun) + hostent *host = gethostbyname_r( hostname, &_host, tmpbuf, 4096, &_errno ); +# else // !__linux !__hpux !__sun +# error "Check port of gethostbyname_r" +# endif // __linux __hpux __sun +#else // __FIT_GETHOSTBYADDR + hostent *host = gethostbyname( hostname ); +# ifdef WIN32 + if ( host == 0 ) { + _errno = WSAGetLastError(); + + // specific to Wins only: + // cool M$ can't resolve IP address in gethostbyname, try once more + // via inet_addr() and gethostbyaddr() + // Returned _errno depend upon WinSock version, and applied patches, + // with some of it even gethostbyname may be succeed. + if ( _errno == WSAHOST_NOT_FOUND || _errno == WSATRY_AGAIN ) { + unsigned long ipaddr = ::inet_addr( hostname ); + if ( ipaddr != INADDR_NONE ) { + host = gethostbyaddr( (const char *)&ipaddr, sizeof(ipaddr), AF_INET ); + if ( host != 0 ) { // Oh, that's was IP indeed... + memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); + WSASetLastError( 0 ); // clear error + _errno = 0; + } else { + _errno = WSAGetLastError(); + } + } + } + } +# endif // WIN32 +#endif // __FIT_GETHOSTBYADDR + + if ( host != 0 && host->h_length == sizeof(in_addr) ) { + for ( char **_inet = host->h_addr_list; *_inet != 0; ++_inet ) { + *bi++ = *((in_addr *)*_inet); + } + } +} + +/* + * Expected host name, return (via back insert iterator) + * all sockaddr for specified host name; nothing will be added in case of + * failure. (Alternative implementation to gethostaddr above; it most + * useful on systems without reentrant gethostbyname_r, but that has + * reentrant getaddrinfo, like FreeBSD >= 5.3) + */ +template <class BackInsertIterator> +void gethostaddr2( const char *hostname, BackInsertIterator bi ) +{ + // addrinfo hints; + addrinfo *hosts_list = 0; + +#ifndef __FIT_NONREENTRANT + int _errno = getaddrinfo( hostname, 0, 0, &hosts_list ); +#else + int _errno = x_getaddrinfo( hostname, 0, 0, &hosts_list ); +#endif + if ( _errno == 0 ) { + addrinfo *host = hosts_list; + if ( host != 0 && host->ai_addr != 0 ) { + while ( host != 0 ) { + // *bi++ = ((in_addr *)host->ai_addr->sa_data)->s_addr; + *bi++ = *host->ai_addr; + host = host->ai_next; + } + } + } + if ( hosts_list != 0 ) { +#ifndef __FIT_NONREENTRANT + freeaddrinfo( hosts_list ); +#else + x_freeaddrinfo( hosts_list ); +#endif + } +} + +struct net_iface +{ + net_iface() + { } + + net_iface( const char *nm, unsigned f, const sockaddr& address ) : + name( nm ), + flags( f ) + { + addr.any = address; + } + + std::string name; + union { + sockaddr_in inet; + sockaddr any; + } addr; + // struct in_addr mask; + unsigned flags; +}; + +template <class C> +void get_ifaces( C& lst ) +{ + int sock = socket(AF_INET, SOCK_DGRAM, 0); + struct ifconf ifc; + char st_buf[100 * sizeof(struct ifreq)]; + char *buf = 0; + int len = sizeof( st_buf ); + ifc.ifc_len = len; + ifc.ifc_buf = st_buf; + for ( ; ; ) { + if ( ioctl(sock, SIOCGIFCONF, &ifc) < 0 ) { + if ( errno != EINVAL ) { + if ( ifc.ifc_buf != st_buf ) { + free( ifc.ifc_buf ); + } + close( sock ); + throw std::runtime_error( std::string("SIOCGIFCONF ioctl error getting list of interfaces") ); + } + } else { + if ( ifc.ifc_len < sizeof(struct ifreq) ) { + if ( ifc.ifc_buf != st_buf ) { + free( ifc.ifc_buf ); + } + close( sock ); + throw std::runtime_error( std::string("SIOCGIFCONF ioctl gave too small return buffer") ); + } + } + if ( ifc.ifc_len >= len ) { + len = ifc.ifc_len + 10 * sizeof(struct ifreq); + if ( ifc.ifc_buf != st_buf ) { + free( ifc.ifc_buf ); + } + ifc.ifc_buf = (char *)malloc( len ); + ifc.ifc_len = len; + } else { + break; + } + } + struct ifreq* ifr = (struct ifreq *) ifc.ifc_req; + struct ifreq* last = (struct ifreq *) ((char *) ifr + ifc.ifc_len); + struct ifreq ifrflags; + while ( ifr < last ) { + memset(&ifrflags, 0, sizeof(ifrflags) ); + strncpy( ifrflags.ifr_name, ifr->ifr_name, sizeof( ifrflags.ifr_name ) ); + if ( ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0 ) { + if ( errno != ENXIO ) { + if ( ifc.ifc_buf != st_buf ) { + free( ifc.ifc_buf ); + } + close( sock ); + throw std::runtime_error( std::string("SIOCGIFFLAGS error getting flags for interface") ); + } + } + lst.push_back( net_iface( ifr->ifr_name, ifrflags.ifr_flags, ifr->ifr_addr ) ); + ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq)); + } + + if ( ifc.ifc_buf != st_buf ) { + free( ifc.ifc_buf ); + } + close( sock ); +} + +#ifdef STLPORT +_STLP_END_NAMESPACE +#else +} // namespace std +#endif + +#endif // __SOCKSIOS_NETINFO_H Modified: trunk/complement/explore/include/sockios/sockstream =================================================================== --- trunk/complement/explore/include/sockios/sockstream 2008-02-28 11:15:13 UTC (rev 1825) +++ trunk/complement/explore/include/sockios/sockstream 2008-02-28 11:16:23 UTC (rev 1826) @@ -26,19 +26,8 @@ #include <mt/xmt.h> #endif -#ifdef __FIT_NONREENTRANT -extern "C" int x_getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **); -extern "C" void x_freeaddrinfo( struct addrinfo * ); -extern "C" int x_res_search(const char *, int, int, unsigned char *, int); -extern "C" int x_res_init(void); -#endif - #include <netdb.h> #include <netinet/in.h> -#include <arpa/nameser.h> -#include <resolv.h> -#include <sys/ioctl.h> -#include <net/if.h> #include <iosfwd> #include <ios> @@ -73,204 +62,14 @@ # include <cerrno> #endif // !WIN32 +#include <sockios/netinfo.h> + #ifdef STLPORT _STLP_BEGIN_NAMESPACE #else namespace std { #endif -in_addr findhost( const char *hostname ) throw( std::domain_error ); -std::string hostname( unsigned long inet_addr ); -std::string hostname(); - -int service( const char *name, const char *proto ) throw( std::domain_error ); -std::string service( int port, const char *proto ) throw( std::domain_error ); - -/* - * Expected host name, return (via back insert iterator) - * all IPs (in_addr) for specified host name; nothing will be added in case of - * failure. - */ -template <class BackInsertIterator> -void gethostaddr( const char *hostname, BackInsertIterator bi ) -{ - int _errno = 0; - -#ifndef __FIT_GETHOSTBYADDR - hostent _host; -# ifndef __hpux - char tmpbuf[4096]; -# else // __hpux - hostent_data tmpbuf; -# endif // __hpux -# ifdef __linux - hostent *host = 0; - gethostbyname_r( hostname, &_host, tmpbuf, 4096, &host, &_errno ); -# elif defined(__hpux) - _errno = gethostbyname_r( hostname, &_host, &tmpbuf ); - hostent *host = &_host; -# elif defined(__sun) - hostent *host = gethostbyname_r( hostname, &_host, tmpbuf, 4096, &_errno ); -# else // !__linux !__hpux !__sun -# error "Check port of gethostbyname_r" -# endif // __linux __hpux __sun -#else // __FIT_GETHOSTBYADDR - hostent *host = gethostbyname( hostname ); -# ifdef WIN32 - if ( host == 0 ) { - _errno = WSAGetLastError(); - - // specific to Wins only: - // cool M$ can't resolve IP address in gethostbyname, try once more - // via inet_addr() and gethostbyaddr() - // Returned _errno depend upon WinSock version, and applied patches, - // with some of it even gethostbyname may be succeed. - if ( _errno == WSAHOST_NOT_FOUND || _errno == WSATRY_AGAIN ) { - unsigned long ipaddr = ::inet_addr( hostname ); - if ( ipaddr != INADDR_NONE ) { - host = gethostbyaddr( (const char *)&ipaddr, sizeof(ipaddr), AF_INET ); - if ( host != 0 ) { // Oh, that's was IP indeed... - memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); - WSASetLastError( 0 ); // clear error - _errno = 0; - } else { - _errno = WSAGetLastError(); - } - } - } - } -# endif // WIN32 -#endif // __FIT_GETHOSTBYADDR - - if ( host != 0 && host->h_length == sizeof(in_addr) ) { - for ( char **_inet = host->h_addr_list; *_inet != 0; ++_inet ) { - *bi++ = *((in_addr *)*_inet); - } - } -} - -/* - * Expected host name, return (via back insert iterator) - * all sockaddr for specified host name; nothing will be added in case of - * failure. (Alternative implementation to gethostaddr above; it most - * useful on systems without reentrant gethostbyname_r, but that has - * reentrant getaddrinfo, like FreeBSD >= 5.3) - */ -template <class BackInsertIterator> -void gethostaddr2( const char *hostname, BackInsertIterator bi ) -{ - // addrinfo hints; - addrinfo *hosts_list = 0; - -#ifndef __FIT_NONREENTRANT - int _errno = getaddrinfo( hostname, 0, 0, &hosts_list ); -#else - int _errno = x_getaddrinfo( hostname, 0, 0, &hosts_list ); -#endif - if ( _errno == 0 ) { - addrinfo *host = hosts_list; - if ( host != 0 && host->ai_addr != 0 ) { - while ( host != 0 ) { - // *bi++ = ((in_addr *)host->ai_addr->sa_data)->s_addr; - *bi++ = *host->ai_addr; - host = host->ai_next; - } - } - } - if ( hosts_list != 0 ) { -#ifndef __FIT_NONREENTRANT - freeaddrinfo( hosts_list ); -#else - x_freeaddrinfo( hosts_list ); -#endif - } -} - -struct net_iface -{ - net_iface() - { } - - net_iface( const char *nm, unsigned f, const sockaddr& address ) : - name( nm ), - flags( f ) - { - addr.any = address; - } - - std::string name; - union { - sockaddr_in inet; - sockaddr any; - } addr; - // struct in_addr mask; - unsigned flags; -}; - -template <class C> -void get_ifaces( C& lst ) -{ - int sock = socket(AF_INET, SOCK_DGRAM, 0); - struct ifconf ifc; - char st_buf[100 * sizeof(struct ifreq)]; - char *buf = 0; - int len = sizeof( st_buf ); - ifc.ifc_len = len; - ifc.ifc_buf = st_buf; - for ( ; ; ) { - if ( ioctl(sock, SIOCGIFCONF, &ifc) < 0 ) { - if ( errno != EINVAL ) { - if ( ifc.ifc_buf != st_buf ) { - free( ifc.ifc_buf ); - } - close( sock ); - throw std::runtime_error( std::string("SIOCGIFCONF ioctl error getting list of interfaces") ); - } - } else { - if ( ifc.ifc_len < sizeof(struct ifreq) ) { - if ( ifc.ifc_buf != st_buf ) { - free( ifc.ifc_buf ); - } - close( sock ); - throw std::runtime_error( std::string("SIOCGIFCONF ioctl gave too small return buffer") ); - } - } - if ( ifc.ifc_len >= len ) { - len = ifc.ifc_len + 10 * sizeof(struct ifreq); - if ( ifc.ifc_buf != st_buf ) { - free( ifc.ifc_buf ); - } - ifc.ifc_buf = (char *)malloc( len ); - ifc.ifc_len = len; - } else { - break; - } - } - struct ifreq* ifr = (struct ifreq *) ifc.ifc_req; - struct ifreq* last = (struct ifreq *) ((char *) ifr + ifc.ifc_len); - struct ifreq ifrflags; - while ( ifr < last ) { - memset(&ifrflags, 0, sizeof(ifrflags) ); - strncpy( ifrflags.ifr_name, ifr->ifr_name, sizeof( ifrflags.ifr_name ) ); - if ( ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0 ) { - if ( errno != ENXIO ) { - if ( ifc.ifc_buf != st_buf ) { - free( ifc.ifc_buf ); - } - close( sock ); - throw std::runtime_error( std::string("SIOCGIFFLAGS error getting flags for interface") ); - } - } - lst.push_back( net_iface( ifr->ifr_name, ifrflags.ifr_flags, ifr->ifr_addr ) ); - ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq)); - } - - if ( ifc.ifc_buf != st_buf ) { - free( ifc.ifc_buf ); - } - close( sock ); -} - class sock_base { public: Modified: trunk/complement/explore/include/sockios/sockstream.cc =================================================================== --- trunk/complement/explore/include/sockios/sockstream.cc 2008-02-28 11:15:13 UTC (rev 1825) +++ trunk/complement/explore/include/sockios/sockstream.cc 2008-02-28 11:16:23 UTC (rev 1826) @@ -1,7 +1,7 @@ // -*- C++ -*- Time-stamp: <07/09/06 23:48:33 ptr> /* - * Copyright (c) 1997-1999, 2002, 2003, 2005-2007 + * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 * Petr Ovtchenkov * * Portion Copyright (c) 1999-2001 @@ -11,6 +11,8 @@ * */ +#include <sockios/netinfo.h> + #ifdef __unix extern "C" int nanosleep(const struct timespec *, struct timespec *); #endif Modified: trunk/complement/explore/lib/sockios/ChangeLog =================================================================== --- trunk/complement/explore/lib/sockios/ChangeLog 2008-02-28 11:15:13 UTC (rev 1825) +++ trunk/complement/explore/lib/sockios/ChangeLog 2008-02-28 11:16:23 UTC (rev 1826) @@ -1,3 +1,11 @@ +2008-02-28 Petr Ovtchenkov <pt...@is...> + + * sochstream, _sockstream.cc, netinfo.h, netinfo.cc: + move findhost and iface list utilities into separate + files; + + * ibsockios: Version 1.14.2. + 2007-11-21 Petr Ovtchenkov <pt...@is...> * sockstream.cc: UDP require larger buffer, because it should Modified: trunk/complement/explore/lib/sockios/Makefile.inc =================================================================== --- trunk/complement/explore/lib/sockios/Makefile.inc 2008-02-28 11:15:13 UTC (rev 1825) +++ trunk/complement/explore/lib/sockios/Makefile.inc 2008-02-28 11:16:23 UTC (rev 1826) @@ -3,8 +3,8 @@ LIBNAME = sockios MAJOR = 1 MINOR = 14 -PATCH = 1 -SRC_CC = _sockstream.cc _sockmgr.cc +PATCH = 2 +SRC_CC = _sockstream.cc _sockmgr.cc netinfo.c SRC_C = freebsd/getaddrinfo.c \ freebsd/ns_parse.c \ freebsd/res_comp.c \ Modified: trunk/complement/explore/lib/sockios/_sockstream.cc =================================================================== --- trunk/complement/explore/lib/sockios/_sockstream.cc 2008-02-28 11:15:13 UTC (rev 1825) +++ trunk/complement/explore/lib/sockios/_sockstream.cc 2008-02-28 11:16:23 UTC (rev 1826) @@ -200,218 +200,3 @@ } // namespace std #endif // WIN32 - -#ifdef STLPORT -_STLP_BEGIN_NAMESPACE -#else -namespace std { -#endif - -::in_addr findhost( const char *hostname ) throw( std::domain_error ) -{ - in_addr inet; - int _errno; - -#ifndef __FIT_GETHOSTBYADDR - hostent _host; -# ifndef __hpux - char tmpbuf[1024]; -# else // __hpux - hostent_data tmpbuf; -# endif // __hpux -# ifdef __linux - hostent *host = 0; - gethostbyname_r( hostname, &_host, tmpbuf, 1024, &host, &_errno ); -# elif defined(__hpux) - _errno = gethostbyname_r( hostname, &_host, &tmpbuf ); - hostent *host = &_host; -# elif defined(__sun) - hostent *host = gethostbyname_r( hostname, &_host, tmpbuf, 1024, &_errno ); -# else // !__linux !__hpux !__sun -# error "Check port of gethostbyname_r" -# endif // __linux __hpux __sun - if ( host != 0 ) { - memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); - } -#else // __FIT_GETHOSTBYADDR - hostent *host = gethostbyname( hostname ); - if ( host != 0 ) { - memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); - } -# ifdef WIN32 - else { - _errno = WSAGetLastError(); - - // specific to Wins only: - // cool M$ can't resolve IP address in gethostbyname, try once more - // via inet_addr() and gethostbyaddr() - // Returned _errno depend upon WinSock version, and applied patches, - // with some of it even gethostbyname may be succeed. - if ( _errno == WSAHOST_NOT_FOUND || _errno == WSATRY_AGAIN ) { - unsigned long ipaddr = ::inet_addr( hostname ); - if ( ipaddr != INADDR_NONE ) { - host = gethostbyaddr( (const char *)&ipaddr, sizeof(ipaddr), AF_INET ); - if ( host != 0 ) { // Oh, that's was IP indeed... - memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); - WSASetLastError( 0 ); // clear error - _errno = 0; - } else { - _errno = WSAGetLastError(); - } - } - } - } -# endif // WIN32 -#endif // __FIT_GETHOSTBYADDR - if ( host == 0 ) { - throw std::domain_error( "host not found" ); - } - - return inet; -} - -std::string hostname( unsigned long inet_addr ) -{ - std::string _hostname; - -#ifdef __FIT_GETHOSTBYADDR - hostent *he; -#else - hostent he; -#ifndef __hpux - char tmp_buff[1024]; -#else - hostent_data tmp_buff; -#endif -# ifdef __linux - hostent *phe = 0; -# endif -#endif - int err = 0; - in_addr in; - in.s_addr = inet_addr; -#ifdef __FIT_GETHOSTBYADDR - // For Win 'he' is thread private data, so that's safe - // It's MT-safe also for HP-UX 11.00 - he = gethostbyaddr( (char *)&in.s_addr, sizeof(in_addr), AF_INET ); - if ( he != 0 ) { - _hostname = he->h_name; - } else { - _hostname = "unknown"; - } -#else // __FIT_GETHOSTBYADDR - if ( -# ifdef __sun - gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, - &he, tmp_buff, 1024, &err ) != 0 -# elif defined(__linux) - gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, - &he, tmp_buff, 1024, &phe, &err ) == 0 -# elif defined(__hpux) // reentrant variant for HP-UX before 11.00 - gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, - &he, &tmp_buff ) == 0 -# else -# error "Check port of gethostbyaddr_r" -# endif - ) - { - _hostname = he.h_name; - } else { - _hostname = "unknown"; - } -#endif // __FIT_GETHOSTBYADDR - - _hostname += " ["; - _hostname += inet_ntoa( in ); - _hostname += "]"; - - return _hostname; -} - -std::string hostname() -{ - std::string _hostname; - char tmp_buff[1024]; - - if ( gethostname( tmp_buff, 1024 ) == 0 ) { - _hostname = tmp_buff; - } else { - _hostname = "unknown"; - } - // getdomainname may be called here, but POSIX not specify such call - - return _hostname; -} - -int service( const char *name, const char *proto ) throw( std::domain_error ) -{ -#ifdef _WIN32 - typedef u_short uint16_t; -#endif -#ifndef __FIT_GETHOSTBYADDR - char tmp_buf[1024]; - struct servent se; -# ifdef __linux - struct servent *sep = 0; - if ( getservbyname_r( name, proto, &se, tmp_buf, 1024, &sep ) != 0 ) { - throw std::domain_error( "service not found" ); - } - return ntohs( uint16_t(se.s_port) ); -# endif -# ifdef __sun - if ( getservbyname_r( name, proto, &se, tmp_buf, 1024 ) == 0 ) { - throw std::domain_error( "service not found" ); - } - return ntohs( uint16_t(se.s_port) ); -# endif -#else // __FIT_GETHOSTBYADDR - struct servent *s = ::getservbyname( name, proto ); - if ( s == 0 ) { - throw std::domain_error( "service not found" ); - } - return ntohs( uint16_t(s->s_port) ); -#endif -} - -std::string service( int port, const char *proto ) throw( std::domain_error ) -{ -#ifdef _WIN32 - typedef u_short uint16_t; -#endif - std::string _servname; - - port = htons( uint16_t(port) ); - -#ifndef __FIT_GETHOSTBYADDR - char tmp_buf[1024]; - struct servent se; -# ifdef __linux - struct servent *sep = 0; - if ( getservbyport_r( port, proto, &se, tmp_buf, 1024, &sep ) != 0 ) { - throw std::domain_error( "service not found" ); - } - _servname.assign( se.s_name ); - return _servname; -# endif -# ifdef __sun - if ( getservbyport_r( port, proto, &se, tmp_buf, 1024 ) == 0 ) { - throw std::domain_error( "service not found" ); - } - _servname.assign( se.s_name ); - return _servname; -# endif -#else // __FIT_GETHOSTBYADDR - struct servent *s = ::getservbyport( port, proto ); - if ( s == 0 ) { - throw std::domain_error( "service not found" ); - } - _servname.assign( s->s_name ); - return _servname; -#endif -} - -#ifdef STLPORT -_STLP_END_NAMESPACE -#else -} // namespace std -#endif Copied: trunk/complement/explore/lib/sockios/netinfo.cc (from rev 1817, trunk/complement/explore/lib/sockios/_sockstream.cc) =================================================================== --- trunk/complement/explore/lib/sockios/netinfo.cc (rev 0) +++ trunk/complement/explore/lib/sockios/netinfo.cc 2008-02-28 11:16:23 UTC (rev 1826) @@ -0,0 +1,239 @@ +// -*- C++ -*- Time-stamp: <06/06/28 10:33:02 ptr> + +/* + * Copyright (c) 1997-1999, 2002, 2005 + * Petr Ovtchenkov + * + * Portion Copyright (c) 1999-2000 + * Parallel Graphics Ltd. + * + * Licensed under the Academic Free License Version 2.1 + * + * This material is provided "as is", with absolutely no warranty expressed + * or implied. Any use is at your own risk. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. + * + */ + +#include <string> +#include <sockios/netinfo.h> + +#ifdef STLPORT +_STLP_BEGIN_NAMESPACE +#else +namespace std { +#endif + +::in_addr findhost( const char *hostname ) throw( std::domain_error ) +{ + in_addr inet; + int _errno; + +#ifndef __FIT_GETHOSTBYADDR + hostent _host; +# ifndef __hpux + char tmpbuf[1024]; +# else // __hpux + hostent_data tmpbuf; +# endif // __hpux +# ifdef __linux + hostent *host = 0; + gethostbyname_r( hostname, &_host, tmpbuf, 1024, &host, &_errno ); +# elif defined(__hpux) + _errno = gethostbyname_r( hostname, &_host, &tmpbuf ); + hostent *host = &_host; +# elif defined(__sun) + hostent *host = gethostbyname_r( hostname, &_host, tmpbuf, 1024, &_errno ); +# else // !__linux !__hpux !__sun +# error "Check port of gethostbyname_r" +# endif // __linux __hpux __sun + if ( host != 0 ) { + memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); + } +#else // __FIT_GETHOSTBYADDR + hostent *host = gethostbyname( hostname ); + if ( host != 0 ) { + memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); + } +# ifdef WIN32 + else { + _errno = WSAGetLastError(); + + // specific to Wins only: + // cool M$ can't resolve IP address in gethostbyname, try once more + // via inet_addr() and gethostbyaddr() + // Returned _errno depend upon WinSock version, and applied patches, + // with some of it even gethostbyname may be succeed. + if ( _errno == WSAHOST_NOT_FOUND || _errno == WSATRY_AGAIN ) { + unsigned long ipaddr = ::inet_addr( hostname ); + if ( ipaddr != INADDR_NONE ) { + host = gethostbyaddr( (const char *)&ipaddr, sizeof(ipaddr), AF_INET ); + if ( host != 0 ) { // Oh, that's was IP indeed... + memcpy( (char *)&inet, (char *)host->h_addr, host->h_length ); + WSASetLastError( 0 ); // clear error + _errno = 0; + } else { + _errno = WSAGetLastError(); + } + } + } + } +# endif // WIN32 +#endif // __FIT_GETHOSTBYADDR + if ( host == 0 ) { + throw std::domain_error( "host not found" ); + } + + return inet; +} + +std::string hostname( unsigned long inet_addr ) +{ + std::string _hostname; + +#ifdef __FIT_GETHOSTBYADDR + hostent *he; +#else + hostent he; +#ifndef __hpux + char tmp_buff[1024]; +#else + hostent_data tmp_buff; +#endif +# ifdef __linux + hostent *phe = 0; +# endif +#endif + int err = 0; + in_addr in; + in.s_addr = inet_addr; +#ifdef __FIT_GETHOSTBYADDR + // For Win 'he' is thread private data, so that's safe + // It's MT-safe also for HP-UX 11.00 + he = gethostbyaddr( (char *)&in.s_addr, sizeof(in_addr), AF_INET ); + if ( he != 0 ) { + _hostname = he->h_name; + } else { + _hostname = "unknown"; + } +#else // __FIT_GETHOSTBYADDR + if ( +# ifdef __sun + gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, + &he, tmp_buff, 1024, &err ) != 0 +# elif defined(__linux) + gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, + &he, tmp_buff, 1024, &phe, &err ) == 0 +# elif defined(__hpux) // reentrant variant for HP-UX before 11.00 + gethostbyaddr_r( (char *)&in.s_addr, sizeof(in_addr), AF_INET, + &he, &tmp_buff ) == 0 +# else +# error "Check port of gethostbyaddr_r" +# endif + ) + { + _hostname = he.h_name; + } else { + _hostname = "unknown"; + } +#endif // __FIT_GETHOSTBYADDR + + _hostname += " ["; + _hostname += inet_ntoa( in ); + _hostname += "]"; + + return _hostname; +} + +std::string hostname() +{ + std::string _hostname; + char tmp_buff[1024]; + + if ( gethostname( tmp_buff, 1024 ) == 0 ) { + _hostname = tmp_buff; + } else { + _hostname = "unknown"; + } + // getdomainname may be called here, but POSIX not specify such call + + return _hostname; +} + +int service( const char *name, const char *proto ) throw( std::domain_error ) +{ +#ifdef _WIN32 + typedef u_short uint16_t; +#endif +#ifndef __FIT_GETHOSTBYADDR + char tmp_buf[1024]; + struct servent se; +# ifdef __linux + struct servent *sep = 0; + if ( getservbyname_r( name, proto, &se, tmp_buf, 1024, &sep ) != 0 ) { + throw std::domain_error( "service not found" ); + } + return ntohs( uint16_t(se.s_port) ); +# endif +# ifdef __sun + if ( getservbyname_r( name, proto, &se, tmp_buf, 1024 ) == 0 ) { + throw std::domain_error( "service not found" ); + } + return ntohs( uint16_t(se.s_port) ); +# endif +#else // __FIT_GETHOSTBYADDR + struct servent *s = ::getservbyname( name, proto ); + if ( s == 0 ) { + throw std::domain_error( "service not found" ); + } + return ntohs( uint16_t(s->s_port) ); +#endif +} + +std::string service( int port, const char *proto ) throw( std::domain_error ) +{ +#ifdef _WIN32 + typedef u_short uint16_t; +#endif + std::string _servname; + + port = htons( uint16_t(port) ); + +#ifndef __FIT_GETHOSTBYADDR + char tmp_buf[1024]; + struct servent se; +# ifdef __linux + struct servent *sep = 0; + if ( getservbyport_r( port, proto, &se, tmp_buf, 1024, &sep ) != 0 ) { + throw std::domain_error( "service not found" ); + } + _servname.assign( se.s_name ); + return _servname; +# endif +# ifdef __sun + if ( getservbyport_r( port, proto, &se, tmp_buf, 1024 ) == 0 ) { + throw std::domain_error( "service not found" ); + } + _servname.assign( se.s_name ); + return _servname; +# endif +#else // __FIT_GETHOSTBYADDR + struct servent *s = ::getservbyport( port, proto ); + if ( s == 0 ) { + throw std::domain_error( "service not found" ); + } + _servname.assign( s->s_name ); + return _servname; +#endif +} + +#ifdef STLPORT +_STLP_END_NAMESPACE +#else +} // namespace std +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |