ipv4addr.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** @file ipv4addr.h Interface for IPV4Addr class */
00020 
00021 #ifndef __IPV4ADDR_H__
00022 #define __IPV4ADDR_H__
00023 
00024 #include <hn/osdep.h>
00025 
00026 /**
00027  * IPV4Address encapsulates an IP address, with optional port.
00028  */
00029 class DLLEXPORT IPV4Address {
00030 public:
00031         //! @name Construction and destruction
00032         //@{
00033         explicit IPV4Address(const std::string &addr, uint16_t port = 0);
00034         explicit IPV4Address(uint32_t addr, uint16_t port = 0);
00035         IPV4Address();
00036         //@}
00037 
00038         //! @name Accessors
00039         //@{
00040         uint32_t    getAddr() const { return m_addr; }
00041         uint16_t    getPort() const { return m_port; }
00042         uint32_t    getIp() const { return m_addr; }
00043         void setAddr(uint32_t addr) { m_addr = addr; }
00044         void setPort(uint16_t port) { m_port = port; }
00045 
00046         // Returns "127.0.0.1:25" for example
00047         // Note - these are not inlined because we don't have Socket namespace
00048         // yet at this point in this header, and reordering things doesn't help.
00049         std::string getStr()   const;
00050         void setAddr(const std::string &addr);
00051         std::string getAddrStr() const;
00052         //@}
00053 
00054         //! Output operator to streams
00055         friend DLLEXPORT std::ostream& operator<<(
00056                 std::ostream &o, const IPV4Address &a
00057         );
00058 
00059         /**
00060          * @name Comparison operators
00061          */
00062         //@{
00063         friend bool operator<(const IPV4Address &x, const IPV4Address &y) {
00064                 if (x.m_addr != y.m_addr) {
00065                         return x.m_addr < y.m_addr;
00066                 } else {
00067                         return x.m_port < y.m_port;
00068                 }
00069         }
00070         friend bool operator==(const IPV4Address &x, const IPV4Address &y) {
00071                 return x.m_addr == y.m_addr && x.m_port == y.m_port;
00072                 }
00073         friend bool operator!=(const IPV4Address &x, const IPV4Address &y) {
00074                 return !(x == y);
00075         }
00076         operator bool() const { return m_addr && m_port; }
00077         //@}
00078 private:
00079         uint32_t m_addr;         //!< IP address
00080         uint16_t m_port;         //!< Port
00081 };
00082 
00083 // Few useful utility functions, defined in Socket namespace
00084 namespace Socket {
00085         /**
00086          * Makes internet address from string. Basically this is inet_ntoa()
00087          *
00088          * @param data         Dot-separated IP address, e.g. "127.0.0.1"
00089          * @return             Internet address, used in binary formats.
00090          */
00091         DLLEXPORT uint32_t makeAddr(const std::string &data);
00092 
00093         /**
00094          * Creates human-readable address from ip. Basically this is inet_aton.
00095          *
00096          * @param ip           Ip address
00097          * @return             Human-readable address, e.g. "127.0.0.1"
00098          */
00099         DLLEXPORT std::string getAddr(uint32_t ip);
00100 }
00101 
00102 #endif