00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #ifndef OW32_SockAddrIn_h
00027 #define OW32_SockAddrIn_h
00028
00029 #include <OW32/OW32Libs.h>
00030
00031 namespace OW32
00032 {
00033
00037 class OW32_LIB_EXPORT CSockAddrIn :
00038 public SOCKADDR_IN
00039 {
00040 public:
00045 CSockAddrIn(u_short port = 0, u_long addr = INADDR_ANY)
00046 {
00047 sin_family = AF_INET;
00048 sin_port = port;
00049 sin_addr.s_addr = addr;
00050 }
00051
00055 void SetPort(u_short port) { sin_port = port; }
00056
00060 unsigned long GetPort() { return sin_port; }
00061
00065 void SetAddr(u_long addr) { sin_addr.s_addr = addr; }
00066
00070 unsigned long GetAddr() { return sin_addr.s_addr; }
00071
00075 bool SetLiteralIP(const char* literal_ip)
00076 {
00077
00078 while (isspace(*((unsigned char *)literal_ip))) ++literal_ip;
00079
00080
00081 if (*literal_ip == '[')
00082 ++literal_ip;
00083
00084
00085 char ip_buf[128];
00086 int pos=0;
00087 while (pos < sizeof(ip_buf)/sizeof(ip_buf[0]) && *literal_ip != '\0' && *literal_ip != ']') {
00088 ip_buf[pos++] = *literal_ip++;
00089 }
00090
00091 if (pos >= sizeof(ip_buf)/sizeof(ip_buf[0])) return false;
00092 ip_buf[pos] = '\0';
00093
00094
00095 unsigned long addr = inet_addr(ip_buf);
00096 if (addr == htonl((unsigned long)-1))
00097 return false;
00098
00099
00100 SetAddr(addr);
00101 return true;
00102 }
00103
00107 bool SetHostName(const char* hostName)
00108 {
00109
00110 while (isspace(*((unsigned char *)hostName))) ++hostName;
00111 if (*hostName == '[') {
00112 return SetLiteralIP(hostName);
00113 }
00114
00115
00116 struct hostent* hent = gethostbyname(hostName);
00117 if (hent == NULL)
00118 return false;
00119
00120 memmove(&sin_addr, *(hent->h_addr_list), sizeof(sin_addr));
00121 return true;
00122 }
00123 };
00124
00125 }
00126
00127 #endif // OW32_SockAddrIn_h