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 <sys/types.h> 00044 #ifdef WIN32 00045 #include <windows.h> 00046 #else 00047 #include <sys/socket.h> 00048 #include <netinet/in.h> 00049 #include <arpa/inet.h> 00050 #include <netdb.h> 00051 #include <unistd.h> 00052 #endif 00053 #include <stdio.h> 00054 #include <string.h> 00055 #include <ctype.h> 00056 00057 #include "OSGBase.h" 00058 #include "OSGBaseFunctions.h" 00059 #include "OSGSocketAddress.h" 00060 #include "OSGSocketException.h" 00061 00062 OSG_USING_NAMESPACE 00063 00080 /*-------------------------------------------------------------------------*/ 00081 /* constructors & destructors */ 00082 00085 SocketAddress::SocketAddress(const char *host,int port) 00086 { 00087 memset(&_sockaddr,0,sizeof(_sockaddr)); 00088 _sockaddr.sin_family = AF_INET; 00089 if(host) 00090 setHost(std::string(host)); 00091 setPort(port); 00092 } 00093 00098 SocketAddress::SocketAddress(SocketAddress::Type type,int port) 00099 { 00100 _sockaddr.sin_family = AF_INET; 00101 switch(type) 00102 { 00103 case ANY: _sockaddr.sin_addr.s_addr = osghtonl(INADDR_ANY); 00104 break; 00105 case BROADCAST: _sockaddr.sin_addr.s_addr = osghtonl(INADDR_BROADCAST); 00106 // setHost(std::string("192.168.0.255")); 00107 break; 00108 default: _sockaddr.sin_addr.s_addr = osghtonl(INADDR_ANY); 00109 } 00110 setPort(port); 00111 } 00112 00115 SocketAddress::SocketAddress(const SocketAddress &source) 00116 { 00117 _sockaddr = source._sockaddr; 00118 } 00119 00122 SocketAddress::~SocketAddress() 00123 { 00124 } 00125 00126 /*-------------------------------------------------------------------------*/ 00127 /* get, set */ 00128 00131 void SocketAddress::setPort(int port) 00132 { 00133 _sockaddr.sin_port = osghtons( port ); 00134 } 00135 00138 void SocketAddress::setHost(const std::string &host) 00139 { 00140 struct hostent *hent; 00141 char const *c; 00142 00143 // number or name ? 00144 for(c=host.c_str(); 00145 *c!='\0' && (isdigit(*c) || *c == '.'); 00146 c++); 00147 if(! *c ) 00148 { 00149 // inet_aton(const char *cp, struct in_addr *pin); 00150 00151 // ip number was given 00152 _sockaddr.sin_addr.s_addr = inet_addr(host.c_str()); 00153 } 00154 else 00155 { 00156 // get address of host by name 00157 hent = gethostbyname(host.c_str()); 00158 if(hent == NULL) 00159 { 00160 throw SocketHostError("gethostbyname()"); 00161 } 00162 // set address 00163 _sockaddr.sin_addr = *(struct in_addr *) hent->h_addr; 00164 } 00165 } 00166 00169 std::string SocketAddress::getHost(void) const 00170 { 00171 return std::string(inet_ntoa(_sockaddr.sin_addr)); 00172 } 00173 00176 std::string SocketAddress::getHostByName() const 00177 { 00178 struct hostent *hent; 00179 std::string result; 00180 00181 hent=gethostbyaddr((SocketAddrT*)getSockAddr(), 00182 getSockAddrSize(),AF_INET); 00183 if(hent == NULL) 00184 { 00185 // if no host assigned or host unknown 00186 // then return ip address 00187 result=inet_ntoa(_sockaddr.sin_addr); 00188 } 00189 else 00190 { 00191 result=hent->h_name; 00192 } 00193 return result; 00194 } 00195 00198 bool SocketAddress::isMulticast(void) 00199 { 00200 UInt32 addr = osgntohl(_sockaddr.sin_addr.s_addr); 00201 return addr & 0xC0000; 00202 } 00203 00206 sockaddr *SocketAddress::getSockAddr(void) const 00207 { 00208 return const_cast<struct sockaddr *>( 00209 reinterpret_cast<const struct sockaddr *>(&_sockaddr)); 00210 } 00211 00214 int SocketAddress::getSockAddrSize(void) const 00215 { 00216 return sizeof(struct sockaddr_in); 00217 } 00218 00221 int SocketAddress::getPort(void) const 00222 { 00223 return osgntohs(_sockaddr.sin_port); 00224 } 00225 00226 /*-------------------------------------------------------------------------*/ 00227 /* Comparision */ 00228 00231 bool SocketAddress::operator == (const SocketAddress &other) const 00232 { 00233 return _sockaddr.sin_addr.s_addr == other._sockaddr.sin_addr.s_addr && 00234 _sockaddr.sin_port == other._sockaddr.sin_port; 00235 } 00236 00239 bool SocketAddress::operator != (const SocketAddress &other) const 00240 { 00241 return ! (*this == other); 00242 } 00243 00246 bool SocketAddress::operator < (const SocketAddress &other) const 00247 { 00248 return _sockaddr.sin_addr.s_addr < other._sockaddr.sin_addr.s_addr || 00249 ( 00250 _sockaddr.sin_addr.s_addr == other._sockaddr.sin_addr.s_addr && 00251 _sockaddr.sin_port < other._sockaddr.sin_port 00252 ); 00253 } 00254 00255 /*-------------------------------------------------------------------------*/ 00256 /* cvs id's */ 00257 00258 #ifdef __sgi 00259 #pragma set woff 1174 00260 #endif 00261 00262 #ifdef OSG_LINUX_ICC 00263 #pragma warning( disable : 177 ) 00264 #endif 00265 00266 namespace 00267 { 00268 static Char8 cvsid_cpp [] = "@(#)$Id: $"; 00269 static Char8 cvsid_hpp [] = OSG_SOCKET_ADDRESS_HEADER_CVSID; 00270 } 00271
1.4.3