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