]>
Commit | Line | Data |
---|---|---|
51fe4b60 | 1 | ///////////////////////////////////////////////////////////////////////////// |
60913641 | 2 | // Name: src/unix/sockunix.cpp |
51fe4b60 VZ |
3 | // Purpose: wxSocketImpl implementation for Unix systems |
4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia, David Elliott, | |
5 | // Vadim Zeitlin | |
6 | // Created: April 1997 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997 Guilhem Lavaux | |
9 | // (c) 2008 Vadim Zeitlin | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
97e6ee04 | 13 | |
7e1e6965 | 14 | #include "wx/wxprec.h" |
7e1e6965 | 15 | |
ebf94940 VZ |
16 | #if wxUSE_SOCKETS |
17 | ||
ebf94940 VZ |
18 | #include "wx/private/fd.h" |
19 | #include "wx/private/socket.h" | |
60913641 | 20 | #include "wx/unix/private/sockunix.h" |
97e6ee04 DE |
21 | |
22 | #if defined(__VISAGECPP__) | |
1aa7b427 | 23 | #define BSD_SELECT /* use Berkeley Sockets select */ |
97e6ee04 DE |
24 | #endif |
25 | ||
ebf94940 VZ |
26 | #if defined(__WATCOMC__) |
27 | #include <errno.h> | |
28 | #include <nerrno.h> | |
29 | #endif | |
02564412 | 30 | |
97e6ee04 DE |
31 | #include <assert.h> |
32 | #include <sys/types.h> | |
33 | #ifdef __VISAGECPP__ | |
34 | #include <string.h> | |
35 | #include <sys/time.h> | |
36 | #include <types.h> | |
37 | #include <netinet/in.h> | |
38 | #endif | |
39 | #include <netdb.h> | |
40 | #include <sys/ioctl.h> | |
41 | ||
bc023abb MW |
42 | #ifdef HAVE_SYS_SELECT_H |
43 | # include <sys/select.h> | |
44 | #endif | |
45 | ||
97e6ee04 DE |
46 | #ifdef __VMS__ |
47 | #include <socket.h> | |
48 | struct sockaddr_un | |
49 | { | |
50 | u_char sun_len; /* sockaddr len including null */ | |
51 | u_char sun_family; /* AF_UNIX */ | |
52 | char sun_path[108]; /* path name (gag) */ | |
53 | }; | |
54 | #else | |
55 | #include <sys/socket.h> | |
56 | #include <sys/un.h> | |
57 | #endif | |
58 | ||
59 | #ifndef __VISAGECPP__ | |
60 | #include <sys/time.h> | |
61 | #include <netinet/in.h> | |
62 | #include <arpa/inet.h> | |
63 | #include <errno.h> | |
64 | #include <string.h> | |
65 | #include <unistd.h> | |
66 | #else | |
67 | #include <nerrno.h> | |
68 | # if __IBMCPP__ < 400 | |
69 | #include <machine/endian.h> | |
70 | #include <socket.h> | |
71 | #include <ioctl.h> | |
72 | #include <select.h> | |
73 | #include <unistd.h> | |
74 | ||
75 | #define EBADF SOCEBADF | |
76 | ||
77 | # ifdef min | |
78 | # undef min | |
79 | # endif | |
80 | # else | |
81 | #include <sys/socket.h> | |
82 | #include <sys/ioctl.h> | |
83 | #include <sys/select.h> | |
84 | ||
85 | #define close(a) soclose(a) | |
86 | #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) | |
87 | int _System bsdselect(int, | |
88 | struct fd_set *, | |
89 | struct fd_set *, | |
90 | struct fd_set *, | |
91 | struct timeval *); | |
92 | int _System soclose(int); | |
93 | # endif | |
94 | #endif | |
ebdab982 SN |
95 | #ifdef __EMX__ |
96 | #include <sys/select.h> | |
97 | #endif | |
98 | ||
97e6ee04 DE |
99 | #include <stdio.h> |
100 | #include <stdlib.h> | |
101 | #include <stddef.h> | |
102 | #include <ctype.h> | |
103 | #ifdef sun | |
104 | # include <sys/filio.h> | |
105 | #endif | |
106 | #ifdef sgi | |
107 | # include <bstring.h> | |
108 | #endif | |
d1f8e97b SN |
109 | #ifdef _AIX |
110 | # include <strings.h> | |
111 | #endif | |
97e6ee04 DE |
112 | #include <signal.h> |
113 | ||
9e03e02d | 114 | #ifndef WX_SOCKLEN_T |
97e6ee04 DE |
115 | |
116 | #ifdef VMS | |
9e03e02d | 117 | # define WX_SOCKLEN_T unsigned int |
97e6ee04 DE |
118 | #else |
119 | # ifdef __GLIBC__ | |
120 | # if __GLIBC__ == 2 | |
9e03e02d | 121 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 122 | # endif |
fcbd7e5a | 123 | # elif defined(__WXMAC__) |
9e03e02d | 124 | # define WX_SOCKLEN_T socklen_t |
97e6ee04 | 125 | # else |
9e03e02d | 126 | # define WX_SOCKLEN_T int |
97e6ee04 DE |
127 | # endif |
128 | #endif | |
129 | ||
130 | #endif /* SOCKLEN_T */ | |
131 | ||
ddc1a35f | 132 | #ifndef SOCKOPTLEN_T |
9e03e02d | 133 | #define SOCKOPTLEN_T WX_SOCKLEN_T |
ddc1a35f DE |
134 | #endif |
135 | ||
97e6ee04 DE |
136 | /* UnixWare reportedly needs this for FIONBIO definition */ |
137 | #ifdef __UNIXWARE__ | |
138 | #include <sys/filio.h> | |
139 | #endif | |
140 | ||
141 | /* | |
142 | * INADDR_BROADCAST is identical to INADDR_NONE which is not defined | |
143 | * on all systems. INADDR_BROADCAST should be fine to indicate an error. | |
144 | */ | |
145 | #ifndef INADDR_NONE | |
146 | #define INADDR_NONE INADDR_BROADCAST | |
147 | #endif | |
148 | ||
62088a3c VZ |
149 | // ---------------------------------------------------------------------------- |
150 | // implementation of thread-safe/reentrant functions if they're missing | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
71b4a9b8 SN |
153 | #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME)) |
154 | # include "wx/thread.h" | |
155 | #endif | |
97e6ee04 | 156 | |
71b4a9b8 SN |
157 | #if defined(HAVE_GETHOSTBYNAME) |
158 | static struct hostent * deepCopyHostent(struct hostent *h, | |
159 | const struct hostent *he, | |
160 | char *buffer, int size, int *err) | |
161 | { | |
2887cb4e | 162 | /* copy old structure */ |
71b4a9b8 | 163 | memcpy(h, he, sizeof(struct hostent)); |
2887cb4e SN |
164 | |
165 | /* copy name */ | |
71b4a9b8 SN |
166 | int len = strlen(h->h_name); |
167 | if (len > size) | |
2887cb4e SN |
168 | { |
169 | *err = ENOMEM; | |
170 | return NULL; | |
171 | } | |
71b4a9b8 SN |
172 | memcpy(buffer, h->h_name, len); |
173 | buffer[len] = '\0'; | |
174 | h->h_name = buffer; | |
2887cb4e SN |
175 | |
176 | /* track position in the buffer */ | |
177 | int pos = len + 1; | |
178 | ||
179 | /* reuse len to store address length */ | |
71b4a9b8 | 180 | len = h->h_length; |
2887cb4e SN |
181 | |
182 | /* ensure pointer alignment */ | |
183 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
184 | if(misalign < sizeof(char *)) | |
185 | pos += misalign; | |
186 | ||
187 | /* leave space for pointer list */ | |
188 | char **p = h->h_addr_list, **q; | |
189 | char **h_addr_list = (char **)(buffer + pos); | |
190 | while(*(p++) != 0) | |
191 | pos += sizeof(char *); | |
192 | ||
193 | /* copy addresses and fill new pointer list */ | |
194 | for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++) | |
195 | { | |
196 | if (size < pos + len) | |
197 | { | |
71b4a9b8 SN |
198 | *err = ENOMEM; |
199 | return NULL; | |
200 | } | |
2887cb4e SN |
201 | memcpy(buffer + pos, *p, len); /* copy content */ |
202 | *q = buffer + pos; /* set copied pointer to copied content */ | |
203 | pos += len; | |
71b4a9b8 | 204 | } |
2887cb4e SN |
205 | *++q = 0; /* null terminate the pointer list */ |
206 | h->h_addr_list = h_addr_list; /* copy pointer to pointers */ | |
207 | ||
208 | /* ensure word alignment of pointers */ | |
209 | misalign = sizeof(char *) - pos%sizeof(char *); | |
210 | if(misalign < sizeof(char *)) | |
211 | pos += misalign; | |
01ba4b67 | 212 | |
2887cb4e SN |
213 | /* leave space for pointer list */ |
214 | p = h->h_aliases; | |
215 | char **h_aliases = (char **)(buffer + pos); | |
216 | while(*(p++) != 0) | |
217 | pos += sizeof(char *); | |
01ba4b67 | 218 | |
2887cb4e SN |
219 | /* copy aliases and fill new pointer list */ |
220 | for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++) | |
221 | { | |
222 | len = strlen(*p); | |
223 | if (size <= pos + len) | |
224 | { | |
225 | *err = ENOMEM; | |
226 | return NULL; | |
227 | } | |
228 | memcpy(buffer + pos, *p, len); /* copy content */ | |
229 | buffer[pos + len] = '\0'; | |
230 | *q = buffer + pos; /* set copied pointer to copied content */ | |
231 | pos += len + 1; | |
71b4a9b8 | 232 | } |
2887cb4e SN |
233 | *++q = 0; /* null terminate the pointer list */ |
234 | h->h_aliases = h_aliases; /* copy pointer to pointers */ | |
235 | ||
71b4a9b8 SN |
236 | return h; |
237 | } | |
238 | #endif | |
239 | ||
2887cb4e SN |
240 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
241 | static wxMutex nameLock; | |
242 | #endif | |
71b4a9b8 SN |
243 | struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h, |
244 | void *buffer, int size, int *err) | |
245 | ||
246 | { | |
539ae551 | 247 | struct hostent *he = NULL; |
71b4a9b8 SN |
248 | *err = 0; |
249 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
250 | if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err)) | |
251 | he = NULL; | |
252 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
253 | he = gethostbyname_r(hostname, h, (char*)buffer, size, err); | |
254 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
255 | if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer)) | |
256 | { | |
257 | he = NULL; | |
258 | *err = h_errno; | |
259 | } | |
260 | else | |
261 | he = h; | |
262 | #elif defined(HAVE_GETHOSTBYNAME) | |
263 | #if wxUSE_THREADS | |
71b4a9b8 SN |
264 | wxMutexLocker locker(nameLock); |
265 | #endif | |
266 | he = gethostbyname(hostname); | |
267 | if (!he) | |
268 | *err = h_errno; | |
269 | else | |
270 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
271 | #endif | |
272 | return he; | |
273 | } | |
274 | ||
2887cb4e SN |
275 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
276 | static wxMutex addrLock; | |
277 | #endif | |
71b4a9b8 SN |
278 | struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size, |
279 | int proto, struct hostent *h, | |
280 | void *buffer, int size, int *err) | |
281 | { | |
539ae551 | 282 | struct hostent *he = NULL; |
71b4a9b8 SN |
283 | *err = 0; |
284 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
285 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
286 | (char*)buffer, size, &he, err)) | |
287 | he = NULL; | |
288 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
289 | he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err); | |
290 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
291 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
292 | (struct hostent_data*) buffer)) | |
293 | { | |
294 | he = NULL; | |
295 | *err = h_errno; | |
296 | } | |
297 | else | |
298 | he = h; | |
299 | #elif defined(HAVE_GETHOSTBYNAME) | |
300 | #if wxUSE_THREADS | |
71b4a9b8 SN |
301 | wxMutexLocker locker(addrLock); |
302 | #endif | |
303 | he = gethostbyaddr(addr_buf, buf_size, proto); | |
304 | if (!he) | |
305 | *err = h_errno; | |
306 | else | |
307 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
308 | #endif | |
309 | return he; | |
310 | } | |
311 | ||
f0b805fa | 312 | #if defined(HAVE_GETSERVBYNAME) |
71b4a9b8 SN |
313 | static struct servent * deepCopyServent(struct servent *s, |
314 | const struct servent *se, | |
315 | char *buffer, int size) | |
316 | { | |
2887cb4e | 317 | /* copy plain old structure */ |
71b4a9b8 | 318 | memcpy(s, se, sizeof(struct servent)); |
2887cb4e SN |
319 | |
320 | /* copy name */ | |
71b4a9b8 | 321 | int len = strlen(s->s_name); |
2887cb4e SN |
322 | if (len >= size) |
323 | { | |
324 | return NULL; | |
325 | } | |
71b4a9b8 SN |
326 | memcpy(buffer, s->s_name, len); |
327 | buffer[len] = '\0'; | |
328 | s->s_name = buffer; | |
2887cb4e SN |
329 | |
330 | /* track position in the buffer */ | |
331 | int pos = len + 1; | |
332 | ||
333 | /* copy protocol */ | |
71b4a9b8 | 334 | len = strlen(s->s_proto); |
2887cb4e SN |
335 | if (pos + len >= size) |
336 | { | |
337 | return NULL; | |
71b4a9b8 | 338 | } |
2887cb4e SN |
339 | memcpy(buffer + pos, s->s_proto, len); |
340 | buffer[pos + len] = '\0'; | |
341 | s->s_proto = buffer + pos; | |
342 | ||
343 | /* track position in the buffer */ | |
344 | pos += len + 1; | |
345 | ||
346 | /* ensure pointer alignment */ | |
347 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
348 | if(misalign < sizeof(char *)) | |
349 | pos += misalign; | |
01ba4b67 | 350 | |
2887cb4e SN |
351 | /* leave space for pointer list */ |
352 | char **p = s->s_aliases, **q; | |
353 | char **s_aliases = (char **)(buffer + pos); | |
354 | while(*(p++) != 0) | |
355 | pos += sizeof(char *); | |
01ba4b67 | 356 | |
2887cb4e SN |
357 | /* copy addresses and fill new pointer list */ |
358 | for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){ | |
359 | len = strlen(*p); | |
360 | if (size <= pos + len) | |
361 | { | |
362 | return NULL; | |
363 | } | |
364 | memcpy(buffer + pos, *p, len); /* copy content */ | |
365 | buffer[pos + len] = '\0'; | |
366 | *q = buffer + pos; /* set copied pointer to copied content */ | |
367 | pos += len + 1; | |
368 | } | |
369 | *++q = 0; /* null terminate the pointer list */ | |
370 | s->s_aliases = s_aliases; /* copy pointer to pointers */ | |
71b4a9b8 SN |
371 | return s; |
372 | } | |
373 | #endif | |
374 | ||
2887cb4e SN |
375 | #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS |
376 | static wxMutex servLock; | |
377 | #endif | |
71b4a9b8 SN |
378 | struct servent *wxGetservbyname_r(const char *port, const char *protocol, |
379 | struct servent *serv, void *buffer, int size) | |
380 | { | |
539ae551 | 381 | struct servent *se = NULL; |
71b4a9b8 SN |
382 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_6) |
383 | if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se)) | |
384 | se = NULL; | |
385 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5) | |
386 | se = getservbyname_r(port, protocol, serv, (char*)buffer, size); | |
387 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4) | |
388 | if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer)) | |
389 | se = NULL; | |
390 | else | |
391 | se = serv; | |
392 | #elif defined(HAVE_GETSERVBYNAME) | |
393 | #if wxUSE_THREADS | |
71b4a9b8 SN |
394 | wxMutexLocker locker(servLock); |
395 | #endif | |
396 | se = getservbyname(port, protocol); | |
397 | if (se) | |
398 | se = deepCopyServent(serv, se, (char*)buffer, size); | |
399 | #endif | |
400 | return se; | |
401 | } | |
402 | ||
62088a3c VZ |
403 | // ============================================================================ |
404 | // wxSocketImpl implementation | |
405 | // ============================================================================ | |
406 | ||
54cb21d6 VZ |
407 | /* static */ |
408 | wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket) | |
409 | { | |
410 | return new wxSocketImplUnix(wxsocket); | |
411 | } | |
412 | ||
97e6ee04 | 413 | |
2b036c4b | 414 | wxSocketError wxSocketImplUnix::GetLastError() const |
97e6ee04 | 415 | { |
2b036c4b VZ |
416 | switch ( errno ) |
417 | { | |
418 | case 0: | |
419 | return wxSOCKET_NOERROR; | |
97e6ee04 | 420 | |
2b036c4b VZ |
421 | case ENOTSOCK: |
422 | return wxSOCKET_INVSOCK; | |
97e6ee04 | 423 | |
42dfe2b2 VZ |
424 | // unfortunately EAGAIN only has the "would block" meaning for read(), |
425 | // not for connect() for which it means something rather different but | |
426 | // we can't distinguish between these two situations currently... | |
14372de8 VZ |
427 | // |
428 | // also notice that EWOULDBLOCK can be different from EAGAIN on some | |
429 | // systems (HP-UX being the only known example) while it's defined as | |
430 | // EAGAIN on most others (e.g. Linux) | |
42dfe2b2 | 431 | case EAGAIN: |
14372de8 VZ |
432 | #ifdef EWOULDBLOCK |
433 | #if EWOULDBLOCK != EAGAIN | |
434 | case EWOULDBLOCK: | |
435 | #endif | |
436 | #endif // EWOULDBLOCK | |
2b036c4b VZ |
437 | case EINPROGRESS: |
438 | return wxSOCKET_WOULDBLOCK; | |
97e6ee04 | 439 | |
2b036c4b VZ |
440 | default: |
441 | return wxSOCKET_IOERR; | |
442 | } | |
97e6ee04 DE |
443 | } |
444 | ||
51fe4b60 | 445 | void wxSocketImplUnix::DoEnableEvents(bool flag) |
2804f77d | 446 | { |
51fe4b60 | 447 | wxSocketManager * const manager = wxSocketManager::Get(); |
f0fbbe23 VZ |
448 | if ( flag ) |
449 | { | |
51fe4b60 VZ |
450 | manager->Install_Callback(this, wxSOCKET_INPUT); |
451 | manager->Install_Callback(this, wxSOCKET_OUTPUT); | |
f0fbbe23 VZ |
452 | } |
453 | else // off | |
454 | { | |
51fe4b60 VZ |
455 | manager->Uninstall_Callback(this, wxSOCKET_INPUT); |
456 | manager->Uninstall_Callback(this, wxSOCKET_OUTPUT); | |
60edcf45 | 457 | } |
60edcf45 VZ |
458 | } |
459 | ||
97e6ee04 | 460 | |
51fe4b60 | 461 | void wxSocketImplUnix::OnStateChange(wxSocketNotify event) |
53a161e1 | 462 | { |
53a161e1 VZ |
463 | NotifyOnStateChange(event); |
464 | ||
51fe4b60 | 465 | if ( event == wxSOCKET_LOST ) |
53a161e1 VZ |
466 | Shutdown(); |
467 | } | |
468 | ||
a9d859df | 469 | void wxSocketImplUnix::OnReadWaiting() |
97e6ee04 DE |
470 | { |
471 | char c; | |
472 | ||
bb154f79 KH |
473 | if (m_fd == INVALID_SOCKET) |
474 | { | |
475 | return; | |
476 | } | |
477 | ||
14372de8 | 478 | int num = recv(m_fd, &c, 1, MSG_PEEK); |
bb154f79 KH |
479 | |
480 | if (num > 0) | |
97e6ee04 | 481 | { |
51fe4b60 | 482 | OnStateChange(wxSOCKET_INPUT); |
97e6ee04 DE |
483 | } |
484 | else | |
485 | { | |
09e6e5ec | 486 | if (m_server && m_stream) |
97e6ee04 | 487 | { |
51fe4b60 | 488 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 | 489 | } |
e37e082e VZ |
490 | else if (num == 0) |
491 | { | |
01c03554 VZ |
492 | if (m_stream) |
493 | { | |
494 | /* graceful shutdown */ | |
51fe4b60 | 495 | OnStateChange(wxSOCKET_LOST); |
01c03554 VZ |
496 | } |
497 | else | |
498 | { | |
499 | /* Empty datagram received */ | |
51fe4b60 | 500 | OnStateChange(wxSOCKET_INPUT); |
01c03554 | 501 | } |
e37e082e | 502 | } |
97e6ee04 DE |
503 | else |
504 | { | |
e400d27d | 505 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 506 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 | 507 | { |
51fe4b60 | 508 | OnStateChange(wxSOCKET_INPUT); |
bb154f79 | 509 | } |
7e1e6965 | 510 | else |
bb154f79 | 511 | { |
51fe4b60 | 512 | OnStateChange(wxSOCKET_LOST); |
7e1e6965 | 513 | } |
97e6ee04 DE |
514 | } |
515 | } | |
516 | } | |
517 | ||
a9d859df | 518 | void wxSocketImplUnix::OnWriteWaiting() |
97e6ee04 | 519 | { |
09e6e5ec | 520 | if (m_establishing && !m_server) |
97e6ee04 DE |
521 | { |
522 | int error; | |
ddc1a35f | 523 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 524 | |
948c96ef | 525 | m_establishing = false; |
97e6ee04 | 526 | |
f71bb8ef | 527 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
528 | |
529 | if (error) | |
530 | { | |
51fe4b60 | 531 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
532 | } |
533 | else | |
534 | { | |
51fe4b60 | 535 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 DE |
536 | /* We have to fire this event by hand because CONNECTION (for clients) |
537 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
538 | * events with the above macro. | |
539 | */ | |
51fe4b60 | 540 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
541 | } |
542 | } | |
543 | else | |
544 | { | |
51fe4b60 | 545 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
546 | } |
547 | } | |
548 | ||
a9d859df VZ |
549 | void wxSocketImplUnix::OnExceptionWaiting() |
550 | { | |
551 | wxFAIL_MSG( "not supposed to be called" ); | |
552 | } | |
553 | ||
97e6ee04 DE |
554 | /* |
555 | * ------------------------------------------------------------------------- | |
556 | * GAddress | |
557 | * ------------------------------------------------------------------------- | |
558 | */ | |
559 | ||
560 | /* CHECK_ADDRESS verifies that the current address family is either | |
51fe4b60 VZ |
561 | * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it |
562 | * initalizes it to be a wxSOCKET_*family*. In other cases, it returns | |
97e6ee04 DE |
563 | * an appropiate error code. |
564 | * | |
565 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
566 | */ | |
567 | #define CHECK_ADDRESS(address, family) \ | |
568 | { \ | |
51fe4b60 VZ |
569 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
570 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 571 | return address->m_error; \ |
51fe4b60 | 572 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 573 | { \ |
51fe4b60 VZ |
574 | address->m_error = wxSOCKET_INVADDR; \ |
575 | return wxSOCKET_INVADDR; \ | |
97e6ee04 DE |
576 | } \ |
577 | } | |
578 | ||
579 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
580 | { \ | |
51fe4b60 VZ |
581 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
582 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 583 | return retval; \ |
51fe4b60 | 584 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 585 | { \ |
51fe4b60 | 586 | address->m_error = wxSOCKET_INVADDR; \ |
97e6ee04 DE |
587 | return retval; \ |
588 | } \ | |
589 | } | |
590 | ||
591 | ||
592 | GAddress *GAddress_new(void) | |
593 | { | |
594 | GAddress *address; | |
595 | ||
596 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
597 | return NULL; | |
598 | ||
51fe4b60 | 599 | address->m_family = wxSOCKET_NOFAMILY; |
97e6ee04 DE |
600 | address->m_addr = NULL; |
601 | address->m_len = 0; | |
602 | ||
603 | return address; | |
604 | } | |
605 | ||
606 | GAddress *GAddress_copy(GAddress *address) | |
607 | { | |
608 | GAddress *addr2; | |
609 | ||
610 | assert(address != NULL); | |
611 | ||
612 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
613 | return NULL; | |
614 | ||
615 | memcpy(addr2, address, sizeof(GAddress)); | |
616 | ||
617 | if (address->m_addr && address->m_len > 0) | |
618 | { | |
619 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
620 | if (addr2->m_addr == NULL) | |
621 | { | |
622 | free(addr2); | |
623 | return NULL; | |
624 | } | |
625 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
626 | } | |
627 | ||
628 | return addr2; | |
629 | } | |
630 | ||
631 | void GAddress_destroy(GAddress *address) | |
632 | { | |
633 | assert(address != NULL); | |
634 | ||
635 | if (address->m_addr) | |
636 | free(address->m_addr); | |
637 | ||
638 | free(address); | |
639 | } | |
640 | ||
641 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
642 | { | |
643 | assert(address != NULL); | |
644 | ||
645 | address->m_family = type; | |
646 | } | |
647 | ||
648 | GAddressType GAddress_GetFamily(GAddress *address) | |
649 | { | |
650 | assert(address != NULL); | |
651 | ||
652 | return address->m_family; | |
653 | } | |
654 | ||
51fe4b60 | 655 | wxSocketError _GAddress_translate_from(GAddress *address, |
97e6ee04 DE |
656 | struct sockaddr *addr, int len) |
657 | { | |
658 | address->m_realfamily = addr->sa_family; | |
659 | switch (addr->sa_family) | |
660 | { | |
661 | case AF_INET: | |
51fe4b60 | 662 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
663 | break; |
664 | case AF_UNIX: | |
51fe4b60 | 665 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 | 666 | break; |
8575ff50 | 667 | #if wxUSE_IPV6 |
97e6ee04 | 668 | case AF_INET6: |
51fe4b60 | 669 | address->m_family = wxSOCKET_INET6; |
97e6ee04 | 670 | break; |
8575ff50 | 671 | #endif // wxUSE_IPV6 |
97e6ee04 DE |
672 | default: |
673 | { | |
51fe4b60 VZ |
674 | address->m_error = wxSOCKET_INVOP; |
675 | return wxSOCKET_INVOP; | |
97e6ee04 DE |
676 | } |
677 | } | |
678 | ||
679 | if (address->m_addr) | |
680 | free(address->m_addr); | |
681 | ||
682 | address->m_len = len; | |
683 | address->m_addr = (struct sockaddr *)malloc(len); | |
684 | ||
685 | if (address->m_addr == NULL) | |
686 | { | |
51fe4b60 VZ |
687 | address->m_error = wxSOCKET_MEMERR; |
688 | return wxSOCKET_MEMERR; | |
97e6ee04 | 689 | } |
1aa7b427 | 690 | |
97e6ee04 DE |
691 | memcpy(address->m_addr, addr, len); |
692 | ||
51fe4b60 | 693 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
694 | } |
695 | ||
51fe4b60 | 696 | wxSocketError _GAddress_translate_to(GAddress *address, |
97e6ee04 DE |
697 | struct sockaddr **addr, int *len) |
698 | { | |
699 | if (!address->m_addr) | |
700 | { | |
51fe4b60 VZ |
701 | address->m_error = wxSOCKET_INVADDR; |
702 | return wxSOCKET_INVADDR; | |
97e6ee04 DE |
703 | } |
704 | ||
705 | *len = address->m_len; | |
706 | *addr = (struct sockaddr *)malloc(address->m_len); | |
707 | if (*addr == NULL) | |
708 | { | |
51fe4b60 VZ |
709 | address->m_error = wxSOCKET_MEMERR; |
710 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
711 | } |
712 | ||
713 | memcpy(*addr, address->m_addr, address->m_len); | |
51fe4b60 | 714 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
715 | } |
716 | ||
717 | /* | |
718 | * ------------------------------------------------------------------------- | |
719 | * Internet address family | |
720 | * ------------------------------------------------------------------------- | |
721 | */ | |
722 | ||
51fe4b60 | 723 | wxSocketError _GAddress_Init_INET(GAddress *address) |
97e6ee04 DE |
724 | { |
725 | address->m_len = sizeof(struct sockaddr_in); | |
726 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
727 | if (address->m_addr == NULL) | |
728 | { | |
51fe4b60 VZ |
729 | address->m_error = wxSOCKET_MEMERR; |
730 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
731 | } |
732 | ||
51fe4b60 | 733 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
734 | address->m_realfamily = PF_INET; |
735 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
736 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
737 | ||
51fe4b60 | 738 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
739 | } |
740 | ||
51fe4b60 | 741 | wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) |
97e6ee04 DE |
742 | { |
743 | struct hostent *he; | |
744 | struct in_addr *addr; | |
745 | ||
746 | assert(address != NULL); | |
747 | ||
748 | CHECK_ADDRESS(address, INET); | |
749 | ||
750 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
751 | ||
752 | /* If it is a numeric host name, convert it now */ | |
753 | #if defined(HAVE_INET_ATON) | |
754 | if (inet_aton(hostname, addr) == 0) | |
755 | { | |
756 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 757 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
758 | { |
759 | #else | |
760 | /* Use gethostbyname by default */ | |
9d715960 | 761 | #ifndef __WXMAC__ |
ba2a81d7 | 762 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 763 | if (val) |
9d715960 | 764 | #endif |
97e6ee04 DE |
765 | { |
766 | #endif | |
767 | struct in_addr *array_addr; | |
768 | ||
769 | /* It is a real name, we solve it */ | |
127189eb SN |
770 | struct hostent h; |
771 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
772 | struct hostent_data buffer; | |
773 | #else | |
774 | char buffer[1024]; | |
775 | #endif | |
776 | int err; | |
777 | he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err); | |
778 | if (he == NULL) | |
97e6ee04 DE |
779 | { |
780 | /* Reset to invalid address */ | |
781 | addr->s_addr = INADDR_NONE; | |
51fe4b60 VZ |
782 | address->m_error = wxSOCKET_NOHOST; |
783 | return wxSOCKET_NOHOST; | |
97e6ee04 | 784 | } |
1aa7b427 | 785 | |
97e6ee04 DE |
786 | array_addr = (struct in_addr *) *(he->h_addr_list); |
787 | addr->s_addr = array_addr[0].s_addr; | |
788 | } | |
1aa7b427 | 789 | |
51fe4b60 | 790 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
791 | } |
792 | ||
60edcf45 | 793 | |
51fe4b60 | 794 | wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) |
60edcf45 VZ |
795 | { |
796 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
797 | } | |
798 | ||
51fe4b60 | 799 | wxSocketError GAddress_INET_SetAnyAddress(GAddress *address) |
97e6ee04 DE |
800 | { |
801 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
802 | } | |
803 | ||
51fe4b60 | 804 | wxSocketError GAddress_INET_SetHostAddress(GAddress *address, |
97e6ee04 DE |
805 | unsigned long hostaddr) |
806 | { | |
807 | struct in_addr *addr; | |
808 | ||
809 | assert(address != NULL); | |
810 | ||
811 | CHECK_ADDRESS(address, INET); | |
812 | ||
813 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 814 | addr->s_addr = htonl(hostaddr); |
97e6ee04 | 815 | |
51fe4b60 | 816 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
817 | } |
818 | ||
51fe4b60 | 819 | wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
97e6ee04 DE |
820 | const char *protocol) |
821 | { | |
822 | struct servent *se; | |
823 | struct sockaddr_in *addr; | |
824 | ||
825 | assert(address != NULL); | |
826 | CHECK_ADDRESS(address, INET); | |
827 | ||
828 | if (!port) | |
829 | { | |
51fe4b60 VZ |
830 | address->m_error = wxSOCKET_INVPORT; |
831 | return wxSOCKET_INVPORT; | |
97e6ee04 | 832 | } |
b082b524 | 833 | |
127189eb SN |
834 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_4) |
835 | struct servent_data buffer; | |
f6bc1c74 | 836 | #else |
127189eb | 837 | char buffer[1024]; |
f6bc1c74 | 838 | #endif |
127189eb SN |
839 | struct servent serv; |
840 | se = wxGetservbyname_r(port, protocol, &serv, | |
841 | (void*)&buffer, sizeof(buffer)); | |
97e6ee04 DE |
842 | if (!se) |
843 | { | |
844 | /* the cast to int suppresses compiler warnings about subscript having the | |
845 | type char */ | |
846 | if (isdigit((int)port[0])) | |
847 | { | |
848 | int port_int; | |
849 | ||
850 | port_int = atoi(port); | |
851 | addr = (struct sockaddr_in *)address->m_addr; | |
852 | addr->sin_port = htons(port_int); | |
51fe4b60 | 853 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
854 | } |
855 | ||
51fe4b60 VZ |
856 | address->m_error = wxSOCKET_INVPORT; |
857 | return wxSOCKET_INVPORT; | |
97e6ee04 DE |
858 | } |
859 | ||
860 | addr = (struct sockaddr_in *)address->m_addr; | |
861 | addr->sin_port = se->s_port; | |
862 | ||
51fe4b60 | 863 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
864 | } |
865 | ||
51fe4b60 | 866 | wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) |
97e6ee04 DE |
867 | { |
868 | struct sockaddr_in *addr; | |
869 | ||
870 | assert(address != NULL); | |
871 | CHECK_ADDRESS(address, INET); | |
b082b524 | 872 | |
97e6ee04 DE |
873 | addr = (struct sockaddr_in *)address->m_addr; |
874 | addr->sin_port = htons(port); | |
875 | ||
51fe4b60 | 876 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
877 | } |
878 | ||
51fe4b60 | 879 | wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
97e6ee04 DE |
880 | { |
881 | struct hostent *he; | |
882 | char *addr_buf; | |
883 | struct sockaddr_in *addr; | |
884 | ||
b082b524 | 885 | assert(address != NULL); |
97e6ee04 DE |
886 | CHECK_ADDRESS(address, INET); |
887 | ||
888 | addr = (struct sockaddr_in *)address->m_addr; | |
889 | addr_buf = (char *)&(addr->sin_addr); | |
890 | ||
127189eb SN |
891 | struct hostent temphost; |
892 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
893 | struct hostent_data buffer; | |
894 | #else | |
895 | char buffer[1024]; | |
896 | #endif | |
897 | int err; | |
898 | he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost, | |
899 | (void*)&buffer, sizeof(buffer), &err); | |
97e6ee04 DE |
900 | if (he == NULL) |
901 | { | |
51fe4b60 VZ |
902 | address->m_error = wxSOCKET_NOHOST; |
903 | return wxSOCKET_NOHOST; | |
97e6ee04 DE |
904 | } |
905 | ||
906 | strncpy(hostname, he->h_name, sbuf); | |
907 | ||
51fe4b60 | 908 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
909 | } |
910 | ||
911 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
912 | { | |
913 | struct sockaddr_in *addr; | |
914 | ||
b082b524 DE |
915 | assert(address != NULL); |
916 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
917 | |
918 | addr = (struct sockaddr_in *)address->m_addr; | |
919 | ||
9d715960 | 920 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
921 | } |
922 | ||
923 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
924 | { | |
925 | struct sockaddr_in *addr; | |
926 | ||
b082b524 DE |
927 | assert(address != NULL); |
928 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
929 | |
930 | addr = (struct sockaddr_in *)address->m_addr; | |
931 | return ntohs(addr->sin_port); | |
932 | } | |
933 | ||
8575ff50 VZ |
934 | #if wxUSE_IPV6 |
935 | /* | |
936 | * ------------------------------------------------------------------------- | |
937 | * Internet IPv6 address family | |
938 | * ------------------------------------------------------------------------- | |
939 | */ | |
940 | ||
51fe4b60 | 941 | wxSocketError _GAddress_Init_INET6(GAddress *address) |
8575ff50 VZ |
942 | { |
943 | struct in6_addr any_address = IN6ADDR_ANY_INIT; | |
944 | address->m_len = sizeof(struct sockaddr_in6); | |
945 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
946 | if (address->m_addr == NULL) | |
947 | { | |
51fe4b60 VZ |
948 | address->m_error = wxSOCKET_MEMERR; |
949 | return wxSOCKET_MEMERR; | |
8575ff50 VZ |
950 | } |
951 | memset(address->m_addr,0,address->m_len); | |
952 | ||
51fe4b60 | 953 | address->m_family = wxSOCKET_INET6; |
8575ff50 VZ |
954 | address->m_realfamily = AF_INET6; |
955 | ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6; | |
956 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address; | |
957 | ||
51fe4b60 | 958 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
959 | } |
960 | ||
51fe4b60 | 961 | wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) |
8575ff50 VZ |
962 | { |
963 | assert(address != NULL); | |
964 | CHECK_ADDRESS(address, INET6); | |
965 | ||
966 | addrinfo hints; | |
967 | memset( & hints, 0, sizeof( hints ) ); | |
968 | hints.ai_family = AF_INET6; | |
969 | addrinfo * info = 0; | |
970 | if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info ) | |
971 | { | |
51fe4b60 VZ |
972 | address->m_error = wxSOCKET_NOHOST; |
973 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
974 | } |
975 | ||
976 | memcpy( address->m_addr, info->ai_addr, info->ai_addrlen ); | |
977 | freeaddrinfo( info ); | |
51fe4b60 | 978 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
979 | } |
980 | ||
51fe4b60 | 981 | wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address) |
8575ff50 VZ |
982 | { |
983 | assert(address != NULL); | |
984 | ||
985 | CHECK_ADDRESS(address, INET6); | |
986 | ||
987 | struct in6_addr addr; | |
988 | memset( & addr, 0, sizeof( addr ) ); | |
989 | return GAddress_INET6_SetHostAddress(address, addr); | |
990 | } | |
51fe4b60 | 991 | wxSocketError GAddress_INET6_SetHostAddress(GAddress *address, |
8575ff50 VZ |
992 | struct in6_addr hostaddr) |
993 | { | |
994 | assert(address != NULL); | |
995 | ||
996 | CHECK_ADDRESS(address, INET6); | |
997 | ||
998 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
999 | ||
51fe4b60 | 1000 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1001 | } |
1002 | ||
51fe4b60 | 1003 | wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, |
8575ff50 VZ |
1004 | const char *protocol) |
1005 | { | |
1006 | struct servent *se; | |
1007 | struct sockaddr_in6 *addr; | |
1008 | ||
1009 | assert(address != NULL); | |
1010 | CHECK_ADDRESS(address, INET6); | |
1011 | ||
1012 | if (!port) | |
1013 | { | |
51fe4b60 VZ |
1014 | address->m_error = wxSOCKET_INVPORT; |
1015 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1016 | } |
1017 | ||
1018 | se = getservbyname(port, protocol); | |
1019 | if (!se) | |
1020 | { | |
1021 | if (isdigit(port[0])) | |
1022 | { | |
1023 | int port_int; | |
1024 | ||
1025 | port_int = atoi(port); | |
1026 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1027 | addr->sin6_port = htons((u_short) port_int); | |
51fe4b60 | 1028 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1029 | } |
1030 | ||
51fe4b60 VZ |
1031 | address->m_error = wxSOCKET_INVPORT; |
1032 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1033 | } |
1034 | ||
1035 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1036 | addr->sin6_port = se->s_port; | |
1037 | ||
51fe4b60 | 1038 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1039 | } |
1040 | ||
51fe4b60 | 1041 | wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) |
8575ff50 VZ |
1042 | { |
1043 | struct sockaddr_in6 *addr; | |
1044 | ||
1045 | assert(address != NULL); | |
1046 | CHECK_ADDRESS(address, INET6); | |
1047 | ||
1048 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1049 | addr->sin6_port = htons(port); | |
1050 | ||
51fe4b60 | 1051 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1052 | } |
1053 | ||
51fe4b60 | 1054 | wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
8575ff50 VZ |
1055 | { |
1056 | struct hostent *he; | |
1057 | char *addr_buf; | |
1058 | struct sockaddr_in6 *addr; | |
1059 | ||
1060 | assert(address != NULL); | |
1061 | CHECK_ADDRESS(address, INET6); | |
1062 | ||
1063 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1064 | addr_buf = (char *)&(addr->sin6_addr); | |
1065 | ||
1066 | he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6); | |
1067 | if (he == NULL) | |
1068 | { | |
51fe4b60 VZ |
1069 | address->m_error = wxSOCKET_NOHOST; |
1070 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
1071 | } |
1072 | ||
1073 | strncpy(hostname, he->h_name, sbuf); | |
1074 | ||
51fe4b60 | 1075 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1076 | } |
1077 | ||
51fe4b60 | 1078 | wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) |
8575ff50 VZ |
1079 | { |
1080 | assert(address != NULL); | |
1081 | assert(hostaddr != NULL); | |
51fe4b60 | 1082 | CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR); |
8575ff50 | 1083 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; |
51fe4b60 | 1084 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1085 | } |
1086 | ||
1087 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1088 | { | |
1089 | assert(address != NULL); | |
1090 | CHECK_ADDRESS_RETVAL(address, INET6, 0); | |
1091 | ||
1092 | return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port ); | |
1093 | } | |
1094 | ||
1095 | #endif // wxUSE_IPV6 | |
1096 | ||
97e6ee04 DE |
1097 | /* |
1098 | * ------------------------------------------------------------------------- | |
1099 | * Unix address family | |
1100 | * ------------------------------------------------------------------------- | |
1101 | */ | |
1102 | ||
1103 | #ifndef __VISAGECPP__ | |
51fe4b60 | 1104 | wxSocketError _GAddress_Init_UNIX(GAddress *address) |
97e6ee04 DE |
1105 | { |
1106 | address->m_len = sizeof(struct sockaddr_un); | |
1107 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
1108 | if (address->m_addr == NULL) | |
1109 | { | |
51fe4b60 VZ |
1110 | address->m_error = wxSOCKET_MEMERR; |
1111 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1112 | } |
1113 | ||
51fe4b60 | 1114 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 DE |
1115 | address->m_realfamily = PF_UNIX; |
1116 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
1117 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
1118 | ||
51fe4b60 | 1119 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1120 | } |
1121 | ||
1122 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
1123 | ||
51fe4b60 | 1124 | wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) |
97e6ee04 DE |
1125 | { |
1126 | struct sockaddr_un *addr; | |
1127 | ||
b082b524 | 1128 | assert(address != NULL); |
97e6ee04 | 1129 | |
b082b524 | 1130 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
1131 | |
1132 | addr = ((struct sockaddr_un *)address->m_addr); | |
1133 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
1134 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
1135 | ||
51fe4b60 | 1136 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1137 | } |
1138 | ||
51fe4b60 | 1139 | wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) |
97e6ee04 DE |
1140 | { |
1141 | struct sockaddr_un *addr; | |
1142 | ||
1143 | assert(address != NULL); | |
1144 | CHECK_ADDRESS(address, UNIX); | |
1145 | ||
1146 | addr = (struct sockaddr_un *)address->m_addr; | |
1147 | ||
1148 | strncpy(path, addr->sun_path, sbuf); | |
1149 | ||
51fe4b60 | 1150 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1151 | } |
1152 | #endif /* !defined(__VISAGECPP__) */ | |
54cb21d6 | 1153 | |
ebf94940 | 1154 | #endif /* wxUSE_SOCKETS */ |