00001 /*---------------------------------------------------------------------------*\ 00002 * OpenSG * 00003 * * 00004 * * 00005 * Copyright (C) 2000-2002 by the OpenSG Forum * 00006 * * 00007 * www.opensg.org * 00008 * * 00009 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de * 00010 * * 00011 \*---------------------------------------------------------------------------*/ 00012 /*---------------------------------------------------------------------------*\ 00013 * License * 00014 * * 00015 * This library is free software; you can redistribute it and/or modify it * 00016 * under the terms of the GNU Library General Public License as published * 00017 * by the Free Software Foundation, version 2. * 00018 * * 00019 * This library is distributed in the hope that it will be useful, but * 00020 * WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00022 * Library General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU Library General Public * 00025 * License along with this library; if not, write to the Free Software * 00026 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * 00027 * * 00028 \*---------------------------------------------------------------------------*/ 00029 /*---------------------------------------------------------------------------*\ 00030 * Changes * 00031 * * 00032 * * 00033 * * 00034 * * 00035 * * 00036 * * 00037 \*---------------------------------------------------------------------------*/ 00038 00039 //--------------------------------------------------------------------------- 00040 // Includes 00041 //--------------------------------------------------------------------------- 00042 00043 #include "OSGConfig.h" 00044 00045 #include <sys/types.h> 00046 #ifdef WIN32 00047 #ifdef WIN32_LEAN_AND_MEAN 00048 #include <winsock2.h> 00049 #endif 00050 #else 00051 #include <sys/socket.h> 00052 #include <netinet/in.h> 00053 #include <arpa/inet.h> 00054 #include <netdb.h> 00055 #include <unistd.h> 00056 #endif 00057 #include <stdio.h> 00058 #include <string.h> 00059 #include <ctype.h> 00060 00061 #include "OSGBase.h" 00062 #include "OSGBaseFunctions.h" 00063 #include "OSGSocketAddress.h" 00064 #include "OSGSocketException.h" 00065 00066 OSG_USING_NAMESPACE 00067 00084 /*-------------------------------------------------------------------------*/ 00085 /* constructors & destructors */ 00086 00089 SocketAddress::SocketAddress(const char *host,int port) : 00090 _sockaddr(NULL) 00091 { 00092 _sockaddr = new sockaddr_in; 00093 00094 memset(_sockaddr,0,sizeof(sockaddr_in)); 00095 00096 _sockaddr->sin_family = AF_INET; 00097 00098 if(host) 00099 setHost(std::string(host)); 00100 00101 setPort(port); 00102 } 00103 00108 SocketAddress::SocketAddress(SocketAddress::Type type,int port) : 00109 _sockaddr(NULL) 00110 { 00111 _sockaddr = new sockaddr_in; 00112 00113 memset(_sockaddr,0,sizeof(sockaddr_in)); 00114 00115 _sockaddr->sin_family = AF_INET; 00116 00117 switch(type) 00118 { 00119 case ANY: _sockaddr->sin_addr.s_addr = osghtonl(INADDR_ANY); 00120 break; 00121 case BROADCAST: _sockaddr->sin_addr.s_addr = osghtonl(INADDR_BROADCAST); 00122 // setHost(std::string("192.168.0.255")); 00123 break; 00124 default: _sockaddr->sin_addr.s_addr = osghtonl(INADDR_ANY); 00125 } 00126 setPort(port); 00127 } 00128 00131 SocketAddress::SocketAddress(const SocketAddress &source) : 00132 _sockaddr(NULL) 00133 { 00134 _sockaddr = new sockaddr_in; 00135 00136 *_sockaddr = *(source._sockaddr); 00137 } 00138 00141 SocketAddress::~SocketAddress() 00142 { 00143 delete _sockaddr; 00144 } 00145 00146 /*-------------------------------------------------------------------------*/ 00147 /* get, set */ 00148 00151 void SocketAddress::setPort(int port) 00152 { 00153 _sockaddr->sin_port = osghtons( port ); 00154 } 00155 00158 void SocketAddress::setHost(const std::string &host) 00159 { 00160 struct hostent *hent; 00161 char const *c; 00162 00163 // number or name ? 00164 for(c=host.c_str(); 00165 *c!='\0' && (isdigit(*c) || *c == '.'); 00166 c++); 00167 if(! *c ) 00168 { 00169 // inet_aton(const char *cp, struct in_addr *pin); 00170 00171 // ip number was given 00172 _sockaddr->sin_addr.s_addr = inet_addr(host.c_str()); 00173 } 00174 else 00175 { 00176 // get address of host by name 00177 hent = gethostbyname(host.c_str()); 00178 if(hent == NULL) 00179 { 00180 throw SocketHostError("gethostbyname()"); 00181 } 00182 // set address 00183 _sockaddr->sin_addr = *(struct in_addr *) hent->h_addr; 00184 } 00185 } 00186 00189 std::string SocketAddress::getHost(void) const 00190 { 00191 return std::string(inet_ntoa(_sockaddr->sin_addr)); 00192 } 00193 00196 std::string SocketAddress::getHostByName() const 00197 { 00198 struct hostent *hent; 00199 std::string result; 00200 00201 hent=gethostbyaddr((SocketAddrT*)getSockAddr(), 00202 getSockAddrSize(),AF_INET); 00203 if(hent == NULL) 00204 { 00205 // if no host assigned or host unknown 00206 // then return ip address 00207 result=inet_ntoa(_sockaddr->sin_addr); 00208 } 00209 else 00210 { 00211 result=hent->h_name; 00212 } 00213 return result; 00214 } 00215 00218 bool SocketAddress::isMulticast(void) 00219 { 00220 UInt32 addr = osgntohl(_sockaddr->sin_addr.s_addr); 00221 return addr & 0xC0000; 00222 } 00223 00226 sockaddr *SocketAddress::getSockAddr(void) const 00227 { 00228 return const_cast<struct sockaddr *>( 00229 reinterpret_cast<const struct sockaddr *>(_sockaddr)); 00230 } 00231 00234 int SocketAddress::getSockAddrSize(void) const 00235 { 00236 return sizeof(sockaddr_in); 00237 } 00238 00241 int SocketAddress::getPort(void) const 00242 { 00243 return osgntohs(_sockaddr->sin_port); 00244 } 00245 00246 /*-------------------------------------------------------------------------*/ 00247 /* Comparision */ 00248 00249 void SocketAddress::operator = (const SocketAddress &other) const 00250 { 00251 if(this != &other) 00252 { 00253 *_sockaddr = *(other._sockaddr); 00254 } 00255 } 00256 00259 bool SocketAddress::operator == (const SocketAddress &other) const 00260 { 00261 return _sockaddr->sin_addr.s_addr == other._sockaddr->sin_addr.s_addr && 00262 _sockaddr->sin_port == other._sockaddr->sin_port; 00263 } 00264 00267 bool SocketAddress::operator != (const SocketAddress &other) const 00268 { 00269 return ! (*this == other); 00270 } 00271 00274 bool SocketAddress::operator < (const SocketAddress &other) const 00275 { 00276 return _sockaddr->sin_addr.s_addr < other._sockaddr->sin_addr.s_addr || 00277 ( 00278 _sockaddr->sin_addr.s_addr == other._sockaddr->sin_addr.s_addr && 00279 _sockaddr->sin_port < other._sockaddr->sin_port 00280 ); 00281 } 00282 00283 /*-------------------------------------------------------------------------*/ 00284 /* cvs id's */ 00285 00286 #ifdef __sgi 00287 #pragma set woff 1174 00288 #endif 00289 00290 #ifdef OSG_LINUX_ICC 00291 #pragma warning( disable : 177 ) 00292 #endif 00293 00294 namespace 00295 { 00296 static Char8 cvsid_cpp [] = "@(#)$Id: $"; 00297 static Char8 cvsid_hpp [] = OSG_SOCKET_ADDRESS_HEADER_CVSID; 00298 } 00299
1.5.5