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