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