]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sckaddr.cpp | |
3 | // Purpose: Network address manager | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 26/04/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997, 1998 Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "sckaddr.h" | |
14 | #endif | |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #endif | |
25 | ||
f4ada568 GL |
26 | #include <stdio.h> |
27 | #include <stdlib.h> | |
28 | #include <ctype.h> | |
469e1e5c | 29 | #ifndef __MWERKS__ |
f4ada568 | 30 | #include <memory.h> |
469e1e5c | 31 | #endif |
f4ada568 GL |
32 | |
33 | #include "wx/defs.h" | |
34 | #include "wx/object.h" | |
35 | ||
36 | #if defined(__WINDOWS__) | |
37 | #include <winsock.h> | |
38 | #endif // __WINDOWS__ | |
39 | ||
40 | #if defined(__UNIX__) | |
41 | ||
42 | #ifdef VMS | |
43 | #include <socket.h> | |
44 | #include <in.h> | |
45 | #else | |
1b826605 JS |
46 | #if defined(__FreeBSD__) || defined (__NetBSD__) |
47 | #include <sys/types.h> | |
48 | #endif | |
23e09f11 | 49 | #include <sys/types.h> |
f4ada568 GL |
50 | #include <sys/socket.h> |
51 | #include <netinet/in.h> | |
52 | #include <arpa/inet.h> | |
53 | #endif | |
54 | #include <unistd.h> | |
55 | #include <netdb.h> | |
56 | ||
57 | #endif // __UNIX__ | |
58 | ||
59 | #include "wx/sckaddr.h" | |
60 | ||
f4ada568 GL |
61 | #define CHECK_ADDRTYPE(var, type) |
62 | ||
63 | #if !USE_SHARED_LIBRARY | |
64 | IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject) | |
65 | IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress) | |
66 | #ifdef ENABLE_IPV6 | |
67 | IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress) | |
68 | #endif | |
69 | #ifdef __UNIX__ | |
70 | IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress) | |
71 | #endif | |
72 | #endif | |
73 | ||
74 | wxIPV4address::wxIPV4address() | |
75 | { | |
76 | m_addr = new sockaddr_in; | |
77 | Clear(); | |
78 | } | |
79 | ||
80 | wxIPV4address::~wxIPV4address() | |
81 | { | |
82 | } | |
83 | ||
84 | int wxIPV4address::SockAddrLen() | |
85 | { | |
86 | return sizeof(*m_addr); | |
87 | } | |
88 | ||
89 | int wxIPV4address::GetFamily() | |
90 | { | |
91 | return AF_INET; | |
92 | } | |
93 | ||
94 | void wxIPV4address::Clear() | |
95 | { | |
96 | memset(m_addr, 0, sizeof(*m_addr)); | |
97 | m_addr->sin_family = AF_INET; | |
98 | m_addr->sin_addr.s_addr = INADDR_ANY; | |
99 | } | |
100 | ||
101 | /* | |
102 | const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr) | |
103 | { | |
104 | wxIPV4address *ip_addr = (wxIPV4address *)&addr; | |
105 | CHECK_ADDRTYPE(addr, wxIPV4address); | |
106 | m_addr = ip_addr->m_addr; | |
107 | return *this; | |
108 | } | |
109 | */ | |
110 | ||
111 | bool wxIPV4address::Hostname(const wxString& name) | |
112 | { | |
113 | struct hostent *hostent; | |
114 | struct in_addr *addr; | |
115 | ||
116 | if (name.IsNull()) | |
117 | return FALSE; | |
118 | ||
119 | if (!name.IsNumber()) { | |
120 | if ((hostent = gethostbyname(name.GetData())) == 0) { | |
121 | return FALSE; | |
122 | } | |
123 | } else { | |
124 | long len_addr = inet_addr(name.GetData()); | |
125 | if (len_addr == -1) | |
126 | return FALSE; | |
127 | m_addr->sin_addr.s_addr = len_addr; | |
128 | return TRUE; | |
129 | } | |
130 | ||
131 | addr = (struct in_addr *) *(hostent->h_addr_list); | |
132 | ||
133 | m_addr->sin_addr.s_addr = addr[0].s_addr; | |
134 | return TRUE; | |
135 | } | |
136 | ||
137 | bool wxIPV4address::Hostname(unsigned long addr) | |
138 | { | |
139 | m_addr->sin_addr.s_addr = htonl(addr); | |
140 | return TRUE; | |
141 | } | |
142 | ||
143 | bool wxIPV4address::Service(const wxString& name) | |
144 | { | |
145 | struct servent *servent; | |
146 | ||
147 | if (name.IsNull()) | |
148 | return FALSE; | |
149 | ||
150 | if (!name.IsNumber()) { | |
151 | if ((servent = getservbyname(name, "tcp")) == 0) | |
152 | return FALSE; | |
153 | } else { | |
154 | if ((servent = getservbyport(atoi(name), "tcp")) == 0) { | |
155 | m_addr->sin_port = htons(atoi(name)); | |
156 | return TRUE; | |
157 | } | |
158 | } | |
159 | ||
160 | m_addr->sin_port = servent->s_port; | |
161 | return TRUE; | |
162 | } | |
163 | ||
164 | bool wxIPV4address::Service(unsigned short port) | |
165 | { | |
166 | m_addr->sin_port = htons(port); | |
167 | return TRUE; | |
168 | } | |
169 | ||
170 | bool wxIPV4address::LocalHost() | |
171 | { | |
172 | static char buf[256]; | |
173 | ||
174 | if (gethostname(buf, sizeof(buf)) < 0) | |
175 | return Hostname("localhost"); | |
176 | else | |
177 | return Hostname(buf); | |
178 | } | |
179 | ||
180 | wxString wxIPV4address::Hostname() | |
181 | { | |
182 | struct hostent *h_ent; | |
183 | ||
184 | h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr), | |
185 | GetFamily()); | |
186 | return wxString(h_ent->h_name); | |
187 | } | |
188 | ||
189 | unsigned short wxIPV4address::Service() | |
190 | { | |
191 | return ntohs(m_addr->sin_port); | |
192 | } | |
193 | ||
194 | void wxIPV4address::Build(struct sockaddr *&addr, size_t& len) | |
195 | { | |
196 | addr = (struct sockaddr *)m_addr; | |
197 | len = sizeof(*m_addr); | |
198 | } | |
199 | ||
200 | void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len) | |
201 | { | |
202 | if (len != sizeof(*m_addr)) | |
203 | return; | |
204 | *m_addr = *(struct sockaddr_in *)addr; | |
205 | } | |
206 | ||
207 | #ifdef IPV6_ENABLE | |
208 | ||
209 | wxIPV6address::wxIPV6address() | |
210 | { | |
211 | m_addr = new sockaddr_in6; | |
212 | Clear(); | |
213 | } | |
214 | ||
215 | wxIPV6address::~wxIPV6address() | |
216 | { | |
217 | } | |
218 | ||
219 | int wxIPV6address::SockAddrLen() | |
220 | { | |
221 | return sizeof(*m_addr); | |
222 | } | |
223 | ||
224 | int wxIPV6address::GetFamily() | |
225 | { | |
226 | return AF_INET6; | |
227 | } | |
228 | ||
229 | void wxIPV6address::Clear() | |
230 | { | |
231 | memset(m_addr, 0, sizeof(*m_addr)); | |
232 | m_addr->sin6_family = AF_INET6; | |
233 | m_addr->sin6_addr.s_addr = INADDR_ANY; | |
234 | } | |
235 | ||
236 | /* | |
237 | const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr) | |
238 | { | |
239 | wxIPV6address *ip_addr = (wxIPV6address *)&addr; | |
240 | ||
241 | CHECK_ADDRTYPE(addr, wxIPV6address); | |
242 | m_addr = ip_addr->m_addr; | |
243 | return *this; | |
244 | } | |
245 | */ | |
246 | ||
247 | bool wxIPV6address::Hostname(const wxString& name) | |
248 | { | |
249 | struct hostent *hostent; | |
250 | struct in_addr *addr; | |
251 | ||
252 | if (name.IsNull()) | |
253 | return FALSE; | |
254 | ||
255 | if (!name.IsNumber()) { | |
256 | hostent = gethostbyname2((char*) name, AF_INET6); | |
257 | if (!hostent) | |
258 | return FALSE; | |
259 | } else { | |
260 | // Don't how to do | |
261 | return FALSE; | |
262 | } | |
263 | ||
264 | addr = (struct in6_addr *) *(hostent->h_addr_list); | |
265 | ||
266 | m_addr->sin6_addr.s6_addr = addr[0].s6_addr; | |
267 | return TRUE; | |
268 | } | |
269 | ||
270 | bool wxIPV6address::Hostname(unsigned char addr[16]) | |
271 | { | |
272 | m_addr->sin6_addr.s6_addr = addr; | |
273 | return TRUE; | |
274 | } | |
275 | ||
276 | bool wxIPV6address::Service(const char *name) | |
277 | { | |
278 | struct servent *servent; | |
279 | ||
280 | if (!name || !strlen(name)) | |
281 | return FALSE; | |
282 | ||
283 | if (!isdigit(*name)) { | |
284 | if ((servent = getservbyname((char*) name, "tcp")) == 0) | |
285 | return FALSE; | |
286 | } else { | |
287 | if ((servent = getservbyport(atoi(name), "tcp")) == 0) { | |
288 | m_addr->sin_port = htons(atoi(name)); | |
289 | return TRUE; | |
290 | } | |
291 | } | |
292 | ||
293 | m_addr->sin_port = servent->s_port; | |
294 | return TRUE; | |
295 | } | |
296 | ||
297 | bool wxIPV6address::Service(unsigned short port) | |
298 | { | |
299 | m_addr->sin_port = htons(port); | |
300 | return TRUE; | |
301 | } | |
302 | ||
303 | bool wxIPV6address::LocalHost() | |
304 | { | |
305 | static char buf[256]; | |
306 | ||
307 | if (gethostname(buf, sizeof(buf)) < 0) | |
308 | return Hostname("localhost"); | |
309 | else | |
310 | return Hostname(buf); | |
311 | } | |
312 | ||
313 | const wxString& wxIPV6address::Hostname() | |
314 | { | |
315 | struct hostent *h_ent; | |
316 | ||
317 | h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr), | |
318 | GetFamily()); | |
319 | return wxString(h_ent->h_name); | |
320 | } | |
321 | ||
322 | unsigned short wxIPV6address::Service() | |
323 | { | |
324 | return ntohs(m_addr->sin_port); | |
325 | } | |
326 | ||
327 | void wxIPV6address::Build(struct sockaddr& *addr, size_t& len) | |
328 | { | |
329 | len = sizeof(*m_addr); | |
330 | addr = m_addr; | |
331 | } | |
332 | ||
333 | void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len) | |
334 | { | |
335 | if (len != sizeof(*m_addr)) | |
336 | return; | |
337 | *m_addr = *(struct sockaddr_in6 *)addr; | |
338 | } | |
339 | ||
340 | #endif | |
341 | ||
342 | #ifdef __UNIX__ | |
343 | #include <sys/un.h> | |
344 | ||
345 | wxUNIXaddress::wxUNIXaddress() | |
346 | { | |
347 | m_addr = new sockaddr_un; | |
348 | Clear(); | |
349 | } | |
350 | ||
351 | wxUNIXaddress::~wxUNIXaddress() | |
352 | { | |
353 | } | |
354 | ||
355 | int wxUNIXaddress::SockAddrLen() | |
356 | { | |
357 | return sizeof(*m_addr); | |
358 | } | |
359 | ||
360 | int wxUNIXaddress::GetFamily() | |
361 | { | |
362 | return AF_UNIX; | |
363 | } | |
364 | ||
365 | void wxUNIXaddress::Clear() | |
366 | { | |
367 | memset(m_addr, 0, sizeof(m_addr)); | |
368 | m_addr->sun_family = AF_UNIX; | |
369 | } | |
370 | ||
371 | /* | |
372 | const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr) | |
373 | { | |
374 | wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr; | |
375 | CHECK_ADDRTYPE(addr, wxUNIXaddress); | |
376 | m_addr = unx_addr->m_addr; | |
377 | return *this; | |
378 | } | |
379 | */ | |
380 | ||
381 | void wxUNIXaddress::Filename(const wxString& fname) | |
382 | { | |
383 | sprintf(m_addr->sun_path, "%s", WXSTRINGCAST fname); | |
384 | } | |
385 | ||
386 | wxString wxUNIXaddress::Filename() | |
387 | { | |
388 | return wxString(m_addr->sun_path); | |
389 | } | |
390 | ||
391 | void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len) | |
392 | { | |
393 | addr = (struct sockaddr *)m_addr; | |
394 | len = sizeof(*m_addr); | |
395 | } | |
396 | ||
397 | void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len) | |
398 | { | |
399 | if (len != sizeof(*m_addr)) | |
400 | return; | |
401 | *m_addr = *(struct sockaddr_un *)addr; | |
402 | } | |
403 | #endif |