| 1 | /** |
|---|
| 2 | ** \file Ipv4Address.h |
|---|
| 3 | ** \date 2006-09-21 |
|---|
| 4 | ** \author grymse@alhem.net |
|---|
| 5 | **/ |
|---|
| 6 | /* |
|---|
| 7 | Copyright (C) 2007 Anders Hedstrom |
|---|
| 8 | |
|---|
| 9 | This program is free software; you can redistribute it and/or |
|---|
| 10 | modify it under the terms of the GNU General Public License |
|---|
| 11 | as published by the Free Software Foundation; either version 2 |
|---|
| 12 | of the License, or (at your option) any later version. |
|---|
| 13 | |
|---|
| 14 | This program is distributed in the hope that it will be useful, |
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | GNU General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU General Public License |
|---|
| 20 | along with this program; if not, write to the Free Software |
|---|
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | #ifndef _SOCKETS_Ipv4Address_H |
|---|
| 24 | #define _SOCKETS_Ipv4Address_H |
|---|
| 25 | |
|---|
| 26 | #include "sockets-config.h" |
|---|
| 27 | #include "SocketAddress.h" |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #ifdef SOCKETS_NAMESPACE |
|---|
| 31 | namespace SOCKETS_NAMESPACE { |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /* Ipv4 address implementation. |
|---|
| 36 | \ingroup basic */ |
|---|
| 37 | class Ipv4Address : public SocketAddress |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | /** Create empty Ipv4 address structure. |
|---|
| 41 | \param port Port number */ |
|---|
| 42 | Ipv4Address(port_t port = 0); |
|---|
| 43 | /** Create Ipv4 address structure. |
|---|
| 44 | \param a Socket address in network byte order (as returned by Utility::u2ip) |
|---|
| 45 | \param port Port number in host byte order */ |
|---|
| 46 | Ipv4Address(ipaddr_t a,port_t port); |
|---|
| 47 | /** Create Ipv4 address structure. |
|---|
| 48 | \param a Socket address in network byte order |
|---|
| 49 | \param port Port number in host byte order */ |
|---|
| 50 | Ipv4Address(struct in_addr& a,port_t port); |
|---|
| 51 | /** Create Ipv4 address structure. |
|---|
| 52 | \param host Hostname to be resolved |
|---|
| 53 | \param port Port number in host byte order */ |
|---|
| 54 | Ipv4Address(const std::string& host,port_t port); |
|---|
| 55 | Ipv4Address(struct sockaddr_in&); |
|---|
| 56 | ~Ipv4Address(); |
|---|
| 57 | |
|---|
| 58 | // SocketAddress implementation |
|---|
| 59 | |
|---|
| 60 | operator struct sockaddr *(); |
|---|
| 61 | operator socklen_t(); |
|---|
| 62 | bool operator==(SocketAddress&); |
|---|
| 63 | |
|---|
| 64 | void SetPort(port_t port); |
|---|
| 65 | port_t GetPort(); |
|---|
| 66 | |
|---|
| 67 | void SetAddress(struct sockaddr *sa); |
|---|
| 68 | int GetFamily(); |
|---|
| 69 | |
|---|
| 70 | bool IsValid(); |
|---|
| 71 | std::auto_ptr<SocketAddress> GetCopy(); |
|---|
| 72 | |
|---|
| 73 | /** Convert address struct to text. */ |
|---|
| 74 | std::string Convert(bool include_port = false); |
|---|
| 75 | std::string Reverse(); |
|---|
| 76 | |
|---|
| 77 | /** Resolve hostname. */ |
|---|
| 78 | static bool Resolve(const std::string& hostname,struct in_addr& a); |
|---|
| 79 | /** Reverse resolve (IP to hostname). */ |
|---|
| 80 | static bool Reverse(struct in_addr& a,std::string& name); |
|---|
| 81 | /** Convert address struct to text. */ |
|---|
| 82 | static std::string Convert(struct in_addr& a); |
|---|
| 83 | |
|---|
| 84 | private: |
|---|
| 85 | Ipv4Address(const Ipv4Address& ) {} // copy constructor |
|---|
| 86 | Ipv4Address& operator=(const Ipv4Address& ) { return *this; } // assignment operator |
|---|
| 87 | struct sockaddr_in m_addr; |
|---|
| 88 | bool m_valid; |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | #ifdef SOCKETS_NAMESPACE |
|---|
| 95 | } // namespace SOCKETS_NAMESPACE { |
|---|
| 96 | #endif |
|---|
| 97 | #endif // _SOCKETS_Ipv4Address_H |
|---|
| 98 | |
|---|