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