1 | /** |
---|
2 | ** \file Ipv4Address.cpp |
---|
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 | #include "Ipv4Address.h" |
---|
24 | #include "Utility.h" |
---|
25 | #include "Parse.h" |
---|
26 | #ifndef _WIN32 |
---|
27 | #include <netdb.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | |
---|
31 | #ifdef SOCKETS_NAMESPACE |
---|
32 | namespace SOCKETS_NAMESPACE { |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | Ipv4Address::Ipv4Address(port_t port) : m_valid(true) |
---|
38 | { |
---|
39 | memset(&m_addr, 0, sizeof(m_addr)); |
---|
40 | m_addr.sin_family = AF_INET; |
---|
41 | m_addr.sin_port = htons( port ); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | Ipv4Address::Ipv4Address(ipaddr_t a,port_t port) : m_valid(true) |
---|
46 | { |
---|
47 | memset(&m_addr, 0, sizeof(m_addr)); |
---|
48 | m_addr.sin_family = AF_INET; |
---|
49 | m_addr.sin_port = htons( port ); |
---|
50 | memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr)); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | Ipv4Address::Ipv4Address(struct in_addr& a,port_t port) : m_valid(true) |
---|
55 | { |
---|
56 | memset(&m_addr, 0, sizeof(m_addr)); |
---|
57 | m_addr.sin_family = AF_INET; |
---|
58 | m_addr.sin_port = htons( port ); |
---|
59 | m_addr.sin_addr = a; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | Ipv4Address::Ipv4Address(const std::string& host,port_t port) : m_valid(false) |
---|
64 | { |
---|
65 | memset(&m_addr, 0, sizeof(m_addr)); |
---|
66 | m_addr.sin_family = AF_INET; |
---|
67 | m_addr.sin_port = htons( port ); |
---|
68 | { |
---|
69 | ipaddr_t a; |
---|
70 | if (Utility::u2ip(host, a)) |
---|
71 | { |
---|
72 | memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr)); |
---|
73 | m_valid = true; |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | Ipv4Address::Ipv4Address(struct sockaddr_in& sa) |
---|
80 | { |
---|
81 | m_addr = sa; |
---|
82 | m_valid = sa.sin_family == AF_INET; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | Ipv4Address::~Ipv4Address() |
---|
87 | { |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | Ipv4Address::operator struct sockaddr *() |
---|
92 | { |
---|
93 | return (struct sockaddr *)&m_addr; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | Ipv4Address::operator socklen_t() |
---|
98 | { |
---|
99 | return sizeof(struct sockaddr_in); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | void Ipv4Address::SetPort(port_t port) |
---|
104 | { |
---|
105 | m_addr.sin_port = htons( port ); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | port_t Ipv4Address::GetPort() |
---|
110 | { |
---|
111 | return ntohs( m_addr.sin_port ); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | bool Ipv4Address::Resolve(const std::string& hostname,struct in_addr& a) |
---|
116 | { |
---|
117 | struct sockaddr_in sa; |
---|
118 | memset(&a, 0, sizeof(a)); |
---|
119 | if (Utility::isipv4(hostname)) |
---|
120 | { |
---|
121 | if (!Utility::u2ip(hostname, sa, AI_NUMERICHOST)) |
---|
122 | return false; |
---|
123 | a = sa.sin_addr; |
---|
124 | return true; |
---|
125 | } |
---|
126 | if (!Utility::u2ip(hostname, sa)) |
---|
127 | return false; |
---|
128 | a = sa.sin_addr; |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | bool Ipv4Address::Reverse(struct in_addr& a,std::string& name) |
---|
134 | { |
---|
135 | struct sockaddr_in sa; |
---|
136 | memset(&sa, 0, sizeof(sa)); |
---|
137 | sa.sin_family = AF_INET; |
---|
138 | sa.sin_addr = a; |
---|
139 | return Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | std::string Ipv4Address::Convert(bool include_port) |
---|
144 | { |
---|
145 | if (include_port) |
---|
146 | return Convert(m_addr.sin_addr) + ":" + Utility::l2string(GetPort()); |
---|
147 | return Convert(m_addr.sin_addr); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | std::string Ipv4Address::Convert(struct in_addr& a) |
---|
152 | { |
---|
153 | struct sockaddr_in sa; |
---|
154 | memset(&sa, 0, sizeof(sa)); |
---|
155 | sa.sin_family = AF_INET; |
---|
156 | sa.sin_addr = a; |
---|
157 | std::string name; |
---|
158 | Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name, NI_NUMERICHOST); |
---|
159 | return name; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | void Ipv4Address::SetAddress(struct sockaddr *sa) |
---|
164 | { |
---|
165 | memcpy(&m_addr, sa, sizeof(struct sockaddr_in)); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | int Ipv4Address::GetFamily() |
---|
170 | { |
---|
171 | return m_addr.sin_family; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | bool Ipv4Address::IsValid() |
---|
176 | { |
---|
177 | return m_valid; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | bool Ipv4Address::operator==(SocketAddress& a) |
---|
182 | { |
---|
183 | if (a.GetFamily() != GetFamily()) |
---|
184 | return false; |
---|
185 | if ((socklen_t)a != sizeof(m_addr)) |
---|
186 | return false; |
---|
187 | struct sockaddr *sa = a; |
---|
188 | struct sockaddr_in *p = (struct sockaddr_in *)sa; |
---|
189 | if (p -> sin_port != m_addr.sin_port) |
---|
190 | return false; |
---|
191 | if (memcmp(&p -> sin_addr, &m_addr.sin_addr, 4)) |
---|
192 | return false; |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | std::auto_ptr<SocketAddress> Ipv4Address::GetCopy() |
---|
198 | { |
---|
199 | return std::auto_ptr<SocketAddress>(new Ipv4Address(m_addr)); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | std::string Ipv4Address::Reverse() |
---|
204 | { |
---|
205 | std::string tmp; |
---|
206 | Reverse(m_addr.sin_addr, tmp); |
---|
207 | return tmp; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | #ifdef SOCKETS_NAMESPACE |
---|
212 | } // namespace SOCKETS_NAMESPACE { |
---|
213 | #endif |
---|
214 | |
---|