Panda3D
|
00001 #ifndef __SOCKET_ADDRESS_H__ 00002 #define __SOCKET_ADDRESS_H__ 00003 00004 #include "pandabase.h" 00005 #include "numeric_types.h" 00006 #include "socket_portable.h" 00007 00008 /////////////////////////////////// 00009 // Class : Socket_Address 00010 // 00011 // Description: A simple place to store and munipulate tcp and port address for 00012 // communication layer 00013 // 00014 ////////////////////////////// 00015 class EXPCL_PANDA_NATIVENET Socket_Address 00016 { 00017 public: 00018 typedef struct sockaddr_in AddressType; 00019 Socket_Address(const AddressType &inaddr); 00020 AddressType & GetAddressInfo() { return _addr; } 00021 const AddressType & GetAddressInfo() const { return _addr; } 00022 PUBLISHED: 00023 00024 inline Socket_Address(short port = 0); 00025 inline Socket_Address(const Socket_Address &inaddr); 00026 00027 inline virtual ~Socket_Address(); 00028 00029 inline bool set_any_IP(int port); 00030 inline bool set_port(int port); 00031 inline bool set_broadcast(int port); 00032 00033 inline bool set_host(const std::string &hostname, int port) ; 00034 inline bool set_host(const std::string &hostname) ; 00035 inline bool set_host(unsigned int ip4adr, int port); 00036 inline void clear(); 00037 00038 inline unsigned short get_port() const; 00039 inline std::string get_ip() const ; 00040 inline std::string get_ip_port() const; 00041 inline unsigned long GetIPAddressRaw() const; 00042 00043 inline bool operator== (const Socket_Address &in) const; 00044 inline bool operator < (const Socket_Address &in) const; 00045 00046 00047 inline bool isMcastRange(); 00048 00049 private: 00050 AddressType _addr; 00051 00052 }; 00053 00054 ////////////////////////////////////////////////////////////// 00055 // Function name : Socket_Address::GetIPAdddressRaw 00056 // Description : Return a RAW sockaddr_in 00057 ////////////////////////////////////////////////////////////// 00058 inline unsigned long Socket_Address::GetIPAddressRaw() const 00059 { 00060 return _addr.sin_addr.s_addr; 00061 } 00062 00063 /////////////////////////////////////////////////////////////////// 00064 // Function name : Socket_Address 00065 // Description : Constructor that lets us set a port value 00066 //////////////////////////////////////////////////////////////////// 00067 inline Socket_Address::Socket_Address(short port) 00068 { 00069 _addr.sin_family = AF_INET; 00070 _addr.sin_addr.s_addr = INADDR_ANY; 00071 _addr.sin_port = htons(port); 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function name : Socket_Address Constructor 00076 // Description : Copy Constructor 00077 //////////////////////////////////////////////////////////////////// 00078 inline Socket_Address::Socket_Address(const Socket_Address &inaddr) 00079 { 00080 _addr.sin_family = inaddr._addr.sin_family; 00081 _addr.sin_addr.s_addr = inaddr._addr.sin_addr.s_addr; 00082 _addr.sin_port = inaddr._addr.sin_port; 00083 } 00084 00085 inline Socket_Address::Socket_Address(const AddressType &inaddr) 00086 { 00087 _addr.sin_family = inaddr.sin_family; 00088 _addr.sin_addr.s_addr = inaddr.sin_addr.s_addr; 00089 _addr.sin_port = inaddr.sin_port; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function name : ~Socket_Address::Socket_Address 00094 // Description : Normal Destructor 00095 //////////////////////////////////////////////////////////////////// 00096 inline Socket_Address::~Socket_Address() 00097 {} 00098 00099 ////////////////////////////////////////////////////////////// 00100 // Function name : Socket_Address::operator== 00101 // Description : Allow for normal == operation on a address item.. 00102 // Will simplify the use in sorted containers.. 00103 ////////////////////////////////////////////////////////////// 00104 inline bool Socket_Address::operator==(const Socket_Address &in) const 00105 { 00106 return ((_addr.sin_family == in._addr.sin_family) && 00107 (_addr.sin_addr.s_addr == in._addr.sin_addr.s_addr) && 00108 (_addr.sin_port == in._addr.sin_port) 00109 ); 00110 } 00111 00112 //////////////////////////////////////////////////////////////////// 00113 // Function name : set_broadcast 00114 // Description : Set to the broadcast address and a specified port 00115 //////////////////////////////////////////////////////////////////// 00116 inline bool Socket_Address::set_broadcast(int port) 00117 { 00118 _addr.sin_family = AF_INET; 00119 _addr.sin_addr.s_addr = 0xffffffff; 00120 _addr.sin_port = htons(port); 00121 return true; 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function name : set_any_IP 00126 // Description : Set to any address and a specified port 00127 //////////////////////////////////////////////////////////////////// 00128 inline bool Socket_Address::set_any_IP(int port) 00129 { 00130 _addr.sin_family = AF_INET; 00131 _addr.sin_addr.s_addr = INADDR_ANY; 00132 _addr.sin_port = htons(port); 00133 return true; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function name : set_port 00138 // Description : Set to a specified port 00139 //////////////////////////////////////////////////////////////////// 00140 inline bool Socket_Address::set_port(int port) 00141 { 00142 _addr.sin_port = htons(port); 00143 return true; 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function name : clear 00148 // Description : Set the internal values to a suitable known value 00149 //////////////////////////////////////////////////////////////////// 00150 inline void Socket_Address::clear() 00151 { 00152 _addr.sin_family = AF_INET; 00153 _addr.sin_addr.s_addr = INADDR_ANY; 00154 _addr.sin_port = htons(0); 00155 } 00156 00157 //////////////////////////////////////////////////////////////////// 00158 // Function name : get_port 00159 // Description : Get the port portion as an integer 00160 //////////////////////////////////////////////////////////////////// 00161 inline unsigned short Socket_Address::get_port() const 00162 { 00163 return ntohs(_addr.sin_port); 00164 } 00165 00166 //////////////////////////////////////////////////////////////////// 00167 // Function name : get_ip 00168 // Description : Return the ip address portion in dot notation string 00169 //////////////////////////////////////////////////////////////////// 00170 inline std::string Socket_Address::get_ip() const 00171 { 00172 return std::string(inet_ntoa(_addr.sin_addr)); 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function name : get_ip_port 00177 // Description : Return the ip address/port in dot notation string 00178 //////////////////////////////////////////////////////////////////// 00179 inline std::string Socket_Address::get_ip_port() const 00180 { 00181 char buf1[100]; // 100 is more than enough for any ip address:port combo.. 00182 sprintf(buf1, "%s:%d", inet_ntoa(_addr.sin_addr), get_port()); 00183 return std::string(buf1); 00184 } 00185 00186 //////////////////////////////////////////////////////////////////// 00187 // Function name : set_host 00188 // Description : this function will take a port and string-based tcp address and initialize 00189 // the address with the information 00190 // 00191 // Return type : bool (address is undefined after an error) 00192 //////////////////////////////////////////////////////////////////// 00193 inline bool Socket_Address::set_host(const std::string &hostname, int port) 00194 { 00195 struct hostent *hp = NULL; 00196 00197 // 00198 // hmm inet_addr does not resolve 255.255.255.255 on ME/98 ?? 00199 // 00200 // * HACK * ?? 00201 if(hostname == "255.255.255.255") 00202 return set_broadcast(port); 00203 // 00204 // 00205 00206 PN_uint32 addr = (long)inet_addr (hostname.c_str()); 00207 if(addr == INADDR_NONE) 00208 { 00209 hp = gethostbyname(hostname.c_str()); 00210 if(hp== NULL) 00211 return false; 00212 else 00213 memcpy(&(_addr.sin_addr),hp->h_addr_list[0] , (unsigned int) hp->h_length); 00214 } 00215 else 00216 (void) memcpy(&_addr.sin_addr,&addr,sizeof(addr)); 00217 00218 _addr.sin_port = htons(port); 00219 _addr.sin_family = AF_INET; 00220 return true; 00221 } 00222 00223 ////////////////////////////////////////////////////////////// 00224 // Function name : Socket_Address::set_host 00225 // Description : 00226 ////////////////////////////////////////////////////////////// 00227 inline bool Socket_Address::set_host(const std::string &hostname) 00228 { 00229 std::string::size_type pos = hostname.find(':'); 00230 if (pos == std::string::npos) 00231 return false; 00232 00233 std::string host = hostname.substr(0, pos); 00234 std::string port = hostname.substr(pos + 1, 100);; 00235 00236 int port_dig = atoi(port.c_str()); 00237 00238 return set_host(host, port_dig); 00239 } 00240 00241 ////////////////////////////////////////////////////////////// 00242 // Function name : Socket_Address::set_host 00243 // Description : 00244 ////////////////////////////////////////////////////////////// 00245 inline bool Socket_Address::set_host(PN_uint32 in_hostname, int port) 00246 { 00247 memcpy(&_addr.sin_addr, &in_hostname, sizeof(in_hostname)); 00248 _addr.sin_port = htons(port); 00249 _addr.sin_family = AF_INET; 00250 return true; 00251 } 00252 00253 ////////////////////////////////////////////////////////////// 00254 // Function name : < 00255 // Description : 00256 ////////////////////////////////////////////////////////////// 00257 inline bool Socket_Address::operator < (const Socket_Address &in) const 00258 { 00259 if (_addr.sin_port < in._addr.sin_port) 00260 return true; 00261 00262 if (_addr.sin_port > in._addr.sin_port) 00263 return false; 00264 00265 if (_addr.sin_addr.s_addr < in._addr.sin_addr.s_addr) 00266 return true; 00267 00268 if (_addr.sin_addr.s_addr > in._addr.sin_addr.s_addr) 00269 return false; 00270 00271 00272 return (_addr.sin_family < in._addr.sin_family); 00273 } 00274 ////////////////////////////////////////////////////////////// 00275 // Function name : isMcastRange 00276 // Description : return true if the address is in the mcast range. 00277 ////////////////////////////////////////////////////////////// 00278 inline bool Socket_Address::isMcastRange(void) 00279 { 00280 PN_uint32 address = ntohl(_addr.sin_addr.s_addr); 00281 //224.0.0.0-239.255.255.255 .. e0,ef 00282 if(address >= 0xe0000000 && address < 0xefffffff) 00283 return true; 00284 return false; 00285 } 00286 00287 00288 #endif //__SOCKET_ADDRESS_H__