switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / Ipv4Address.cpp
blob99628f4f11e75a9496cdd81e89712e99caa3b71b
1 /**
2 ** \file Ipv4Address.cpp
3 ** \date 2006-09-21
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2007 Anders Hedstrom
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.
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.
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.
23 #include <cstring>
24 #include "Ipv4Address.h"
25 #include "Utility.h"
26 #include "Parse.h"
27 #ifndef _WIN32
28 #include <netdb.h>
29 #endif
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE {
34 #endif
38 Ipv4Address::Ipv4Address(port_t port) : m_valid(true)
40 memset(&m_addr, 0, sizeof(m_addr));
41 m_addr.sin_family = AF_INET;
42 m_addr.sin_port = htons( port );
46 Ipv4Address::Ipv4Address(ipaddr_t a,port_t port) : m_valid(true)
48 memset(&m_addr, 0, sizeof(m_addr));
49 m_addr.sin_family = AF_INET;
50 m_addr.sin_port = htons( port );
51 memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr));
55 Ipv4Address::Ipv4Address(struct in_addr& a,port_t port) : m_valid(true)
57 memset(&m_addr, 0, sizeof(m_addr));
58 m_addr.sin_family = AF_INET;
59 m_addr.sin_port = htons( port );
60 m_addr.sin_addr = a;
64 Ipv4Address::Ipv4Address(const std::string& host,port_t port) : m_valid(false)
66 memset(&m_addr, 0, sizeof(m_addr));
67 m_addr.sin_family = AF_INET;
68 m_addr.sin_port = htons( port );
70 ipaddr_t a;
71 if (Utility::u2ip(host, a))
73 memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr));
74 m_valid = true;
80 Ipv4Address::Ipv4Address(struct sockaddr_in& sa)
82 m_addr = sa;
83 m_valid = sa.sin_family == AF_INET;
87 Ipv4Address::~Ipv4Address()
92 Ipv4Address::operator struct sockaddr *()
94 return (struct sockaddr *)&m_addr;
98 Ipv4Address::operator socklen_t()
100 return sizeof(struct sockaddr_in);
104 void Ipv4Address::SetPort(port_t port)
106 m_addr.sin_port = htons( port );
110 port_t Ipv4Address::GetPort()
112 return ntohs( m_addr.sin_port );
116 bool Ipv4Address::Resolve(const std::string& hostname,struct in_addr& a)
118 struct sockaddr_in sa;
119 memset(&a, 0, sizeof(a));
120 if (Utility::isipv4(hostname))
122 if (!Utility::u2ip(hostname, sa, AI_NUMERICHOST))
123 return false;
124 a = sa.sin_addr;
125 return true;
127 if (!Utility::u2ip(hostname, sa))
128 return false;
129 a = sa.sin_addr;
130 return true;
134 bool Ipv4Address::Reverse(struct in_addr& a,std::string& name)
136 struct sockaddr_in sa;
137 memset(&sa, 0, sizeof(sa));
138 sa.sin_family = AF_INET;
139 sa.sin_addr = a;
140 return Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name);
144 std::string Ipv4Address::Convert(bool include_port)
146 if (include_port)
147 return Convert(m_addr.sin_addr) + ":" + Utility::l2string(GetPort());
148 return Convert(m_addr.sin_addr);
152 std::string Ipv4Address::Convert(struct in_addr& a)
154 struct sockaddr_in sa;
155 memset(&sa, 0, sizeof(sa));
156 sa.sin_family = AF_INET;
157 sa.sin_addr = a;
158 std::string name;
159 Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name, NI_NUMERICHOST);
160 return name;
164 void Ipv4Address::SetAddress(struct sockaddr *sa)
166 memcpy(&m_addr, sa, sizeof(struct sockaddr_in));
170 int Ipv4Address::GetFamily()
172 return m_addr.sin_family;
176 bool Ipv4Address::IsValid()
178 return m_valid;
182 bool Ipv4Address::operator==(SocketAddress& a)
184 if (a.GetFamily() != GetFamily())
185 return false;
186 if ((socklen_t)a != sizeof(m_addr))
187 return false;
188 struct sockaddr *sa = a;
189 struct sockaddr_in *p = (struct sockaddr_in *)sa;
190 if (p -> sin_port != m_addr.sin_port)
191 return false;
192 if (memcmp(&p -> sin_addr, &m_addr.sin_addr, 4))
193 return false;
194 return true;
198 std::auto_ptr<SocketAddress> Ipv4Address::GetCopy()
200 return std::auto_ptr<SocketAddress>(new Ipv4Address(m_addr));
204 std::string Ipv4Address::Reverse()
206 std::string tmp;
207 Reverse(m_addr.sin_addr, tmp);
208 return tmp;
212 #ifdef SOCKETS_NAMESPACE
213 } // namespace SOCKETS_NAMESPACE {
214 #endif