Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

SockAddrIn.h

Go to the documentation of this file.
00001 /*  SockAddrIn.h - Socket address wrapper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
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         // skip spaces (shouldn't be any though)
00078         while (isspace(*((unsigned char *)literal_ip))) ++literal_ip;
00079 
00080         // skip '[' - this is a common notation from mail days
00081         if (*literal_ip == '[')
00082             ++literal_ip;
00083 
00084         // Can't be valid if > 128 characters (IPv6=?)
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         // literal IP too long
00091         if (pos >= sizeof(ip_buf)/sizeof(ip_buf[0])) return false;
00092         ip_buf[pos] = '\0';
00093 
00094         // conver the IP, checking for success
00095         unsigned long addr = inet_addr(ip_buf);
00096         if (addr == htonl((unsigned long)-1))
00097             return false;
00098 
00099         // got it!
00100         SetAddr(addr);
00101         return true;
00102     }
00103 
00107     bool SetHostName(const char* hostName) 
00108     {
00109         // check for literal ip
00110         while (isspace(*((unsigned char *)hostName))) ++hostName;
00111         if (*hostName == '[') {
00112             return SetLiteralIP(hostName);
00113         }
00114 
00115         // otherwise, it's actually a "host by name" effort
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 } // namespace OW32
00126 
00127 #endif // OW32_SockAddrIn_h

Generated on Sun Jun 5 01:29:18 2005 for OW32 by  doxygen 1.3.9.1