MPMA Framework 0.4
|
00001 00002 //written by Luke Lenhart, 2007-2008 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 #pragma once 00006 00007 #include "../base/Setup.h" 00008 00009 #ifdef MPMA_COMPILE_NET 00010 00011 #include "../base/Types.h" 00012 00013 #include <string> 00014 00016 namespace NET 00017 { 00019 class Address 00020 { 00021 public: 00023 inline Address() 00024 { 00025 ZeroOut(); 00026 } 00027 00029 inline Address(const std::string &addressColonPort) 00030 { 00031 ZeroOut(); 00032 SetAddress(addressColonPort); 00033 } 00034 00036 inline Address(const std::string &addressString, uint16 portNumber) 00037 { 00038 ZeroOut(); 00039 SetAddress(addressString); 00040 port=portNumber; 00041 } 00042 00044 std::string GetIPString() const; 00045 00047 inline nuint GetIP() const 00048 { 00049 return ip; 00050 } 00051 00053 std::string GetIPPortString() const; 00054 00056 std::string GetNameString() const; 00057 00059 std::string GetNamePortString() const; 00060 00062 inline uint16 GetPort() const 00063 { 00064 return port; 00065 } 00066 00068 void SetAddress(const std::string &addressString); 00069 00071 inline void SetIP(uint32 ipValue) 00072 { 00073 ip=ipValue; 00074 name.clear(); 00075 } 00076 00078 inline void SetPort(uint16 portNumber) 00079 { 00080 port=portNumber; 00081 } 00082 00084 inline bool operator==(const Address &o) const 00085 { 00086 return port==o.port && ip==o.ip; 00087 } 00088 00089 private: 00090 uint32 ip; 00091 uint16 port; 00092 mutable std::string name; //cached dns name 00093 00094 //resets all members to 0 00095 inline void ZeroOut() 00096 { 00097 ip=0; 00098 name="0.0.0.0"; 00099 port=0; 00100 } 00101 00102 //performs dns lookups 00103 static uint32 DnsNameToIP(const std::string &sourceName); 00104 static std::string DnsIpToName(uint32 sourceIP); 00105 00106 //ip string to uint32 00107 static uint32 ParseIPString(const std::string &ipstr); 00108 }; 00109 00110 // -- common utility information 00111 00112 //TODO: handle the case of multiple network adapters 00113 00115 std::string GetLocalIP(); 00116 00118 std::string GetNetworkBroadcastIP(); 00119 00121 std::string GetDirectBroadcastIP(); 00122 } 00123 00124 #endif //#ifdef MPMA_COMPILE_NET