]>
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 | ||
7e1e6965 | 149 | #if defined(__VISAGECPP__) || defined(__WATCOMC__) |
97e6ee04 | 150 | |
7e1e6965 WS |
151 | #define MASK_SIGNAL() { |
152 | #define UNMASK_SIGNAL() } | |
153 | ||
154 | #else | |
defbeca1 | 155 | extern "C" { typedef void (*wxSigHandler)(int); } |
7e1e6965 WS |
156 | |
157 | #define MASK_SIGNAL() \ | |
158 | { \ | |
defbeca1 | 159 | wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN); |
7e1e6965 WS |
160 | |
161 | #define UNMASK_SIGNAL() \ | |
162 | signal(SIGPIPE, old_handler); \ | |
163 | } | |
164 | ||
165 | #endif | |
97e6ee04 | 166 | |
e400d27d KH |
167 | /* If a SIGPIPE is issued by a socket call on a remotely closed socket, |
168 | the program will "crash" unless it explicitly handles the SIGPIPE. | |
169 | By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will | |
170 | use SO_NOSIGPIPE (if available), the BSD equivalent. */ | |
bb154f79 KH |
171 | #ifdef MSG_NOSIGNAL |
172 | # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL | |
173 | #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */ | |
174 | # define GSOCKET_MSG_NOSIGNAL 0 | |
175 | #endif /* MSG_NOSIGNAL */ | |
97e6ee04 | 176 | |
71b4a9b8 SN |
177 | #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME)) |
178 | # include "wx/thread.h" | |
179 | #endif | |
97e6ee04 | 180 | |
71b4a9b8 SN |
181 | #if defined(HAVE_GETHOSTBYNAME) |
182 | static struct hostent * deepCopyHostent(struct hostent *h, | |
183 | const struct hostent *he, | |
184 | char *buffer, int size, int *err) | |
185 | { | |
2887cb4e | 186 | /* copy old structure */ |
71b4a9b8 | 187 | memcpy(h, he, sizeof(struct hostent)); |
2887cb4e SN |
188 | |
189 | /* copy name */ | |
71b4a9b8 SN |
190 | int len = strlen(h->h_name); |
191 | if (len > size) | |
2887cb4e SN |
192 | { |
193 | *err = ENOMEM; | |
194 | return NULL; | |
195 | } | |
71b4a9b8 SN |
196 | memcpy(buffer, h->h_name, len); |
197 | buffer[len] = '\0'; | |
198 | h->h_name = buffer; | |
2887cb4e SN |
199 | |
200 | /* track position in the buffer */ | |
201 | int pos = len + 1; | |
202 | ||
203 | /* reuse len to store address length */ | |
71b4a9b8 | 204 | len = h->h_length; |
2887cb4e SN |
205 | |
206 | /* ensure pointer alignment */ | |
207 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
208 | if(misalign < sizeof(char *)) | |
209 | pos += misalign; | |
210 | ||
211 | /* leave space for pointer list */ | |
212 | char **p = h->h_addr_list, **q; | |
213 | char **h_addr_list = (char **)(buffer + pos); | |
214 | while(*(p++) != 0) | |
215 | pos += sizeof(char *); | |
216 | ||
217 | /* copy addresses and fill new pointer list */ | |
218 | for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++) | |
219 | { | |
220 | if (size < pos + len) | |
221 | { | |
71b4a9b8 SN |
222 | *err = ENOMEM; |
223 | return NULL; | |
224 | } | |
2887cb4e SN |
225 | memcpy(buffer + pos, *p, len); /* copy content */ |
226 | *q = buffer + pos; /* set copied pointer to copied content */ | |
227 | pos += len; | |
71b4a9b8 | 228 | } |
2887cb4e SN |
229 | *++q = 0; /* null terminate the pointer list */ |
230 | h->h_addr_list = h_addr_list; /* copy pointer to pointers */ | |
231 | ||
232 | /* ensure word alignment of pointers */ | |
233 | misalign = sizeof(char *) - pos%sizeof(char *); | |
234 | if(misalign < sizeof(char *)) | |
235 | pos += misalign; | |
01ba4b67 | 236 | |
2887cb4e SN |
237 | /* leave space for pointer list */ |
238 | p = h->h_aliases; | |
239 | char **h_aliases = (char **)(buffer + pos); | |
240 | while(*(p++) != 0) | |
241 | pos += sizeof(char *); | |
01ba4b67 | 242 | |
2887cb4e SN |
243 | /* copy aliases and fill new pointer list */ |
244 | for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++) | |
245 | { | |
246 | len = strlen(*p); | |
247 | if (size <= pos + len) | |
248 | { | |
249 | *err = ENOMEM; | |
250 | return NULL; | |
251 | } | |
252 | memcpy(buffer + pos, *p, len); /* copy content */ | |
253 | buffer[pos + len] = '\0'; | |
254 | *q = buffer + pos; /* set copied pointer to copied content */ | |
255 | pos += len + 1; | |
71b4a9b8 | 256 | } |
2887cb4e SN |
257 | *++q = 0; /* null terminate the pointer list */ |
258 | h->h_aliases = h_aliases; /* copy pointer to pointers */ | |
259 | ||
71b4a9b8 SN |
260 | return h; |
261 | } | |
262 | #endif | |
263 | ||
2887cb4e SN |
264 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
265 | static wxMutex nameLock; | |
266 | #endif | |
71b4a9b8 SN |
267 | struct hostent * wxGethostbyname_r(const char *hostname, struct hostent *h, |
268 | void *buffer, int size, int *err) | |
269 | ||
270 | { | |
539ae551 | 271 | struct hostent *he = NULL; |
71b4a9b8 SN |
272 | *err = 0; |
273 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
274 | if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err)) | |
275 | he = NULL; | |
276 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
277 | he = gethostbyname_r(hostname, h, (char*)buffer, size, err); | |
278 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
279 | if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer)) | |
280 | { | |
281 | he = NULL; | |
282 | *err = h_errno; | |
283 | } | |
284 | else | |
285 | he = h; | |
286 | #elif defined(HAVE_GETHOSTBYNAME) | |
287 | #if wxUSE_THREADS | |
71b4a9b8 SN |
288 | wxMutexLocker locker(nameLock); |
289 | #endif | |
290 | he = gethostbyname(hostname); | |
291 | if (!he) | |
292 | *err = h_errno; | |
293 | else | |
294 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
295 | #endif | |
296 | return he; | |
297 | } | |
298 | ||
2887cb4e SN |
299 | #if defined(HAVE_GETHOSTBYNAME) && wxUSE_THREADS |
300 | static wxMutex addrLock; | |
301 | #endif | |
71b4a9b8 SN |
302 | struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size, |
303 | int proto, struct hostent *h, | |
304 | void *buffer, int size, int *err) | |
305 | { | |
539ae551 | 306 | struct hostent *he = NULL; |
71b4a9b8 SN |
307 | *err = 0; |
308 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) | |
309 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
310 | (char*)buffer, size, &he, err)) | |
311 | he = NULL; | |
312 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) | |
313 | he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err); | |
314 | #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
315 | if (gethostbyaddr_r(addr_buf, buf_size, proto, h, | |
316 | (struct hostent_data*) buffer)) | |
317 | { | |
318 | he = NULL; | |
319 | *err = h_errno; | |
320 | } | |
321 | else | |
322 | he = h; | |
323 | #elif defined(HAVE_GETHOSTBYNAME) | |
324 | #if wxUSE_THREADS | |
71b4a9b8 SN |
325 | wxMutexLocker locker(addrLock); |
326 | #endif | |
327 | he = gethostbyaddr(addr_buf, buf_size, proto); | |
328 | if (!he) | |
329 | *err = h_errno; | |
330 | else | |
331 | he = deepCopyHostent(h, he, (char*)buffer, size, err); | |
332 | #endif | |
333 | return he; | |
334 | } | |
335 | ||
f0b805fa | 336 | #if defined(HAVE_GETSERVBYNAME) |
71b4a9b8 SN |
337 | static struct servent * deepCopyServent(struct servent *s, |
338 | const struct servent *se, | |
339 | char *buffer, int size) | |
340 | { | |
2887cb4e | 341 | /* copy plain old structure */ |
71b4a9b8 | 342 | memcpy(s, se, sizeof(struct servent)); |
2887cb4e SN |
343 | |
344 | /* copy name */ | |
71b4a9b8 | 345 | int len = strlen(s->s_name); |
2887cb4e SN |
346 | if (len >= size) |
347 | { | |
348 | return NULL; | |
349 | } | |
71b4a9b8 SN |
350 | memcpy(buffer, s->s_name, len); |
351 | buffer[len] = '\0'; | |
352 | s->s_name = buffer; | |
2887cb4e SN |
353 | |
354 | /* track position in the buffer */ | |
355 | int pos = len + 1; | |
356 | ||
357 | /* copy protocol */ | |
71b4a9b8 | 358 | len = strlen(s->s_proto); |
2887cb4e SN |
359 | if (pos + len >= size) |
360 | { | |
361 | return NULL; | |
71b4a9b8 | 362 | } |
2887cb4e SN |
363 | memcpy(buffer + pos, s->s_proto, len); |
364 | buffer[pos + len] = '\0'; | |
365 | s->s_proto = buffer + pos; | |
366 | ||
367 | /* track position in the buffer */ | |
368 | pos += len + 1; | |
369 | ||
370 | /* ensure pointer alignment */ | |
371 | unsigned int misalign = sizeof(char *) - pos%sizeof(char *); | |
372 | if(misalign < sizeof(char *)) | |
373 | pos += misalign; | |
01ba4b67 | 374 | |
2887cb4e SN |
375 | /* leave space for pointer list */ |
376 | char **p = s->s_aliases, **q; | |
377 | char **s_aliases = (char **)(buffer + pos); | |
378 | while(*(p++) != 0) | |
379 | pos += sizeof(char *); | |
01ba4b67 | 380 | |
2887cb4e SN |
381 | /* copy addresses and fill new pointer list */ |
382 | for (p = s->s_aliases, q = s_aliases; *p != 0; p++, q++){ | |
383 | len = strlen(*p); | |
384 | if (size <= pos + len) | |
385 | { | |
386 | return NULL; | |
387 | } | |
388 | memcpy(buffer + pos, *p, len); /* copy content */ | |
389 | buffer[pos + len] = '\0'; | |
390 | *q = buffer + pos; /* set copied pointer to copied content */ | |
391 | pos += len + 1; | |
392 | } | |
393 | *++q = 0; /* null terminate the pointer list */ | |
394 | s->s_aliases = s_aliases; /* copy pointer to pointers */ | |
71b4a9b8 SN |
395 | return s; |
396 | } | |
397 | #endif | |
398 | ||
2887cb4e SN |
399 | #if defined(HAVE_GETSERVBYNAME) && wxUSE_THREADS |
400 | static wxMutex servLock; | |
401 | #endif | |
71b4a9b8 SN |
402 | struct servent *wxGetservbyname_r(const char *port, const char *protocol, |
403 | struct servent *serv, void *buffer, int size) | |
404 | { | |
539ae551 | 405 | struct servent *se = NULL; |
71b4a9b8 SN |
406 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_6) |
407 | if (getservbyname_r(port, protocol, serv, (char*)buffer, size, &se)) | |
408 | se = NULL; | |
409 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5) | |
410 | se = getservbyname_r(port, protocol, serv, (char*)buffer, size); | |
411 | #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4) | |
412 | if (getservbyname_r(port, protocol, serv, (struct servent_data*) buffer)) | |
413 | se = NULL; | |
414 | else | |
415 | se = serv; | |
416 | #elif defined(HAVE_GETSERVBYNAME) | |
417 | #if wxUSE_THREADS | |
71b4a9b8 SN |
418 | wxMutexLocker locker(servLock); |
419 | #endif | |
420 | se = getservbyname(port, protocol); | |
421 | if (se) | |
422 | se = deepCopyServent(serv, se, (char*)buffer, size); | |
423 | #endif | |
424 | return se; | |
425 | } | |
426 | ||
97e6ee04 DE |
427 | /* debugging helpers */ |
428 | #ifdef __GSOCKET_DEBUG__ | |
51fe4b60 | 429 | # define SOCKET_DEBUG(args) printf args |
97e6ee04 | 430 | #else |
51fe4b60 | 431 | # define SOCKET_DEBUG(args) |
97e6ee04 DE |
432 | #endif /* __GSOCKET_DEBUG__ */ |
433 | ||
54cb21d6 VZ |
434 | /* static */ |
435 | wxSocketImpl *wxSocketImpl::Create(wxSocketBase& wxsocket) | |
436 | { | |
437 | return new wxSocketImplUnix(wxsocket); | |
438 | } | |
439 | ||
97e6ee04 | 440 | |
51fe4b60 | 441 | /* |
97e6ee04 DE |
442 | * Disallow further read/write operations on this socket, close |
443 | * the fd and disable all callbacks. | |
444 | */ | |
51fe4b60 | 445 | void wxSocketImplUnix::Shutdown() |
97e6ee04 | 446 | { |
eb97543d | 447 | /* Don't allow events to fire after socket has been closed */ |
f0fbbe23 | 448 | DisableEvents(); |
97e6ee04 | 449 | |
51fe4b60 | 450 | wxSocketImpl::Shutdown(); |
97e6ee04 DE |
451 | } |
452 | ||
51fe4b60 | 453 | /* |
97e6ee04 | 454 | * Waits for an incoming client connection. Returns a pointer to |
51fe4b60 VZ |
455 | * a wxSocketImplUnix object, or NULL if there was an error, in which case |
456 | * the last error field will be updated for the calling wxSocketImplUnix. | |
97e6ee04 | 457 | * |
51fe4b60 VZ |
458 | * Error codes (set in the calling wxSocketImplUnix) |
459 | * wxSOCKET_INVSOCK - the socket is not valid or not a server. | |
460 | * wxSOCKET_TIMEDOUT - timeout, no incoming connections. | |
461 | * wxSOCKET_WOULDBLOCK - the call would block and the socket is nonblocking. | |
462 | * wxSOCKET_MEMERR - couldn't allocate memory. | |
463 | * wxSOCKET_IOERR - low-level error. | |
97e6ee04 | 464 | */ |
51fe4b60 | 465 | wxSocketImpl *wxSocketImplUnix::WaitConnection(wxSocketBase& wxsocket) |
97e6ee04 | 466 | { |
8575ff50 | 467 | wxSockAddr from; |
9e03e02d | 468 | WX_SOCKLEN_T fromlen = sizeof(from); |
51fe4b60 VZ |
469 | wxSocketImpl *connection; |
470 | wxSocketError err; | |
97e6ee04 DE |
471 | int arg = 1; |
472 | ||
97e6ee04 | 473 | /* If the socket has already been created, we exit immediately */ |
09e6e5ec | 474 | if (m_fd == INVALID_SOCKET || !m_server) |
97e6ee04 | 475 | { |
51fe4b60 | 476 | m_error = wxSOCKET_INVSOCK; |
97e6ee04 DE |
477 | return NULL; |
478 | } | |
479 | ||
51fe4b60 VZ |
480 | /* Create a wxSocketImplUnix object for the new connection */ |
481 | connection = wxSocketImplUnix::Create(wxsocket); | |
97e6ee04 | 482 | |
444cb1fd | 483 | if (!connection) |
97e6ee04 | 484 | { |
51fe4b60 | 485 | m_error = wxSOCKET_MEMERR; |
97e6ee04 DE |
486 | return NULL; |
487 | } | |
488 | ||
489 | /* Wait for a connection (with timeout) */ | |
51fe4b60 | 490 | if (Input_Timeout() == wxSOCKET_TIMEDOUT) |
97e6ee04 | 491 | { |
85431efa | 492 | delete connection; |
51fe4b60 | 493 | /* m_error set by Input_Timeout */ |
97e6ee04 DE |
494 | return NULL; |
495 | } | |
496 | ||
8575ff50 | 497 | connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen); |
97e6ee04 | 498 | |
bb154f79 | 499 | /* Reenable CONNECTION events */ |
51fe4b60 | 500 | EnableEvent(wxSOCKET_CONNECTION); |
bb154f79 | 501 | |
97e6ee04 DE |
502 | if (connection->m_fd == INVALID_SOCKET) |
503 | { | |
504 | if (errno == EWOULDBLOCK) | |
51fe4b60 | 505 | m_error = wxSOCKET_WOULDBLOCK; |
97e6ee04 | 506 | else |
51fe4b60 | 507 | m_error = wxSOCKET_IOERR; |
97e6ee04 | 508 | |
85431efa | 509 | delete connection; |
97e6ee04 DE |
510 | return NULL; |
511 | } | |
512 | ||
513 | /* Initialize all fields */ | |
948c96ef DE |
514 | connection->m_server = false; |
515 | connection->m_stream = true; | |
97e6ee04 DE |
516 | |
517 | /* Setup the peer address field */ | |
518 | connection->m_peer = GAddress_new(); | |
519 | if (!connection->m_peer) | |
520 | { | |
85431efa | 521 | delete connection; |
51fe4b60 | 522 | m_error = wxSOCKET_MEMERR; |
97e6ee04 DE |
523 | return NULL; |
524 | } | |
1aa7b427 | 525 | |
8575ff50 | 526 | err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen); |
51fe4b60 | 527 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 528 | { |
85431efa | 529 | delete connection; |
09e6e5ec | 530 | m_error = err; |
97e6ee04 DE |
531 | return NULL; |
532 | } | |
1aa7b427 | 533 | |
97e6ee04 DE |
534 | #if defined(__EMX__) || defined(__VISAGECPP__) |
535 | ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg)); | |
536 | #else | |
537 | ioctl(connection->m_fd, FIONBIO, &arg); | |
538 | #endif | |
2804f77d VZ |
539 | if (m_use_events) |
540 | connection->Notify(true); | |
97e6ee04 DE |
541 | |
542 | return connection; | |
543 | } | |
544 | ||
51fe4b60 | 545 | void wxSocketImplUnix::Notify(bool flag) |
2804f77d VZ |
546 | { |
547 | if (flag == m_use_events) | |
548 | return; | |
2804f77d | 549 | m_use_events = flag; |
f0fbbe23 | 550 | DoEnableEvents(flag); |
2804f77d VZ |
551 | } |
552 | ||
51fe4b60 | 553 | void wxSocketImplUnix::DoEnableEvents(bool flag) |
2804f77d | 554 | { |
51fe4b60 | 555 | wxSocketManager * const manager = wxSocketManager::Get(); |
f0fbbe23 VZ |
556 | if ( flag ) |
557 | { | |
51fe4b60 VZ |
558 | manager->Install_Callback(this, wxSOCKET_INPUT); |
559 | manager->Install_Callback(this, wxSOCKET_OUTPUT); | |
f0fbbe23 VZ |
560 | } |
561 | else // off | |
562 | { | |
51fe4b60 VZ |
563 | manager->Uninstall_Callback(this, wxSOCKET_INPUT); |
564 | manager->Uninstall_Callback(this, wxSOCKET_OUTPUT); | |
60edcf45 | 565 | } |
60edcf45 VZ |
566 | } |
567 | ||
51fe4b60 | 568 | wxSocketError wxSocketImplUnix::DoHandleConnect(int ret) |
97e6ee04 | 569 | { |
f0fbbe23 VZ |
570 | /* We only call EnableEvents() if we know we aren't shutting down the socket. |
571 | * NB: EnableEvents() needs to be called whether the socket is blocking or | |
0d34c30e KH |
572 | * non-blocking, it just shouldn't be called prior to knowing there is a |
573 | * connection _if_ blocking sockets are being used. | |
574 | * If connect above returns 0, we are already connected and need to make the | |
f0fbbe23 | 575 | * call to EnableEvents() now. |
a3687636 | 576 | */ |
f0fbbe23 | 577 | if ( m_non_blocking || (ret == 0) ) |
2804f77d | 578 | EnableEvents(); |
bb154f79 | 579 | |
97e6ee04 DE |
580 | if (ret == -1) |
581 | { | |
51fe4b60 | 582 | const int err = errno; |
97e6ee04 | 583 | |
51fe4b60 | 584 | /* If connect failed with EINPROGRESS and the wxSocketImplUnix object |
97e6ee04 DE |
585 | * is in blocking mode, we select() for the specified timeout |
586 | * checking for writability to see if the connection request | |
587 | * completes. | |
588 | */ | |
09e6e5ec | 589 | if ((err == EINPROGRESS) && (!m_non_blocking)) |
97e6ee04 | 590 | { |
51fe4b60 | 591 | if (Output_Timeout() == wxSOCKET_TIMEDOUT) |
97e6ee04 | 592 | { |
09e6e5ec | 593 | Close(); |
51fe4b60 VZ |
594 | /* m_error is set in Output_Timeout */ |
595 | return wxSOCKET_TIMEDOUT; | |
97e6ee04 DE |
596 | } |
597 | else | |
598 | { | |
599 | int error; | |
ddc1a35f | 600 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 601 | |
f71bb8ef | 602 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len); |
f0fbbe23 | 603 | EnableEvents(); |
bb154f79 | 604 | |
97e6ee04 | 605 | if (!error) |
51fe4b60 | 606 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
607 | } |
608 | } | |
609 | ||
51fe4b60 VZ |
610 | /* If connect failed with EINPROGRESS and the wxSocketImplUnix object |
611 | * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK | |
612 | * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket; | |
613 | * this way if the connection completes, a wxSOCKET_CONNECTION | |
97e6ee04 DE |
614 | * event will be generated, if enabled. |
615 | */ | |
09e6e5ec | 616 | if ((err == EINPROGRESS) && (m_non_blocking)) |
97e6ee04 | 617 | { |
948c96ef | 618 | m_establishing = true; |
51fe4b60 VZ |
619 | m_error = wxSOCKET_WOULDBLOCK; |
620 | return wxSOCKET_WOULDBLOCK; | |
97e6ee04 DE |
621 | } |
622 | ||
623 | /* If connect failed with an error other than EINPROGRESS, | |
51fe4b60 | 624 | * then the call to Connect has failed. |
97e6ee04 | 625 | */ |
09e6e5ec | 626 | Close(); |
51fe4b60 | 627 | m_error = wxSOCKET_IOERR; |
97e6ee04 | 628 | |
51fe4b60 | 629 | return wxSOCKET_IOERR; |
01ba4b67 VZ |
630 | } |
631 | ||
51fe4b60 | 632 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
633 | } |
634 | ||
635 | /* Generic IO */ | |
636 | ||
637 | /* Like recv(), send(), ... */ | |
51fe4b60 | 638 | int wxSocketImplUnix::Read(char *buffer, int size) |
97e6ee04 DE |
639 | { |
640 | int ret; | |
641 | ||
09e6e5ec | 642 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 643 | { |
51fe4b60 | 644 | m_error = wxSOCKET_INVSOCK; |
97e6ee04 DE |
645 | return -1; |
646 | } | |
647 | ||
9d715960 | 648 | /* Disable events during query of socket status */ |
51fe4b60 | 649 | DisableEvent(wxSOCKET_INPUT); |
b082b524 | 650 | |
97e6ee04 | 651 | /* If the socket is blocking, wait for data (with a timeout) */ |
51fe4b60 VZ |
652 | if (Input_Timeout() == wxSOCKET_TIMEDOUT) { |
653 | m_error = wxSOCKET_TIMEDOUT; | |
976abb72 VZ |
654 | /* Don't return here immediately, otherwise socket events would not be |
655 | * re-enabled! */ | |
9d715960 | 656 | ret = -1; |
976abb72 | 657 | } |
1aa7b427 DS |
658 | else |
659 | { | |
9d715960 DE |
660 | /* Read the data */ |
661 | if (m_stream) | |
662 | ret = Recv_Stream(buffer, size); | |
663 | else | |
664 | ret = Recv_Dgram(buffer, size); | |
b082b524 | 665 | |
01c03554 VZ |
666 | /* |
667 | * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP | |
668 | * socket and empty datagrams are possible), then the connection has been | |
669 | * gracefully closed. | |
670 | * | |
671 | * Otherwise, recv has returned an error (-1), in which case we have lost | |
672 | * the socket only if errno does _not_ indicate that there may be more data | |
673 | * to read. | |
9b67181b | 674 | */ |
01c03554 | 675 | if ((ret == 0) && m_stream) |
98e7a7f9 | 676 | { |
f4854380 | 677 | /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */ |
2804f77d VZ |
678 | if (m_use_events) |
679 | { | |
51fe4b60 | 680 | m_detected = wxSOCKET_LOST_FLAG; |
a9d859df | 681 | OnReadWaiting(); |
2804f77d VZ |
682 | return 0; |
683 | } | |
98e7a7f9 | 684 | } |
1aa7b427 DS |
685 | else if (ret == -1) |
686 | { | |
976abb72 | 687 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
51fe4b60 | 688 | m_error = wxSOCKET_WOULDBLOCK; |
976abb72 | 689 | else |
51fe4b60 | 690 | m_error = wxSOCKET_IOERR; |
976abb72 | 691 | } |
97e6ee04 | 692 | } |
b082b524 | 693 | |
9d715960 | 694 | /* Enable events again now that we are done processing */ |
51fe4b60 | 695 | EnableEvent(wxSOCKET_INPUT); |
97e6ee04 DE |
696 | |
697 | return ret; | |
698 | } | |
699 | ||
51fe4b60 | 700 | int wxSocketImplUnix::Write(const char *buffer, int size) |
b082b524 | 701 | { |
97e6ee04 DE |
702 | int ret; |
703 | ||
51fe4b60 | 704 | SOCKET_DEBUG(( "Write #1, size %d\n", size )); |
97e6ee04 | 705 | |
09e6e5ec | 706 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 707 | { |
51fe4b60 | 708 | m_error = wxSOCKET_INVSOCK; |
97e6ee04 DE |
709 | return -1; |
710 | } | |
711 | ||
51fe4b60 | 712 | SOCKET_DEBUG(( "Write #2, size %d\n", size )); |
97e6ee04 DE |
713 | |
714 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
51fe4b60 | 715 | if (Output_Timeout() == wxSOCKET_TIMEDOUT) |
97e6ee04 DE |
716 | return -1; |
717 | ||
51fe4b60 | 718 | SOCKET_DEBUG(( "Write #3, size %d\n", size )); |
97e6ee04 DE |
719 | |
720 | /* Write the data */ | |
09e6e5ec DE |
721 | if (m_stream) |
722 | ret = Send_Stream(buffer, size); | |
97e6ee04 | 723 | else |
09e6e5ec | 724 | ret = Send_Dgram(buffer, size); |
b082b524 | 725 | |
51fe4b60 | 726 | SOCKET_DEBUG(( "Write #4, size %d\n", size )); |
97e6ee04 DE |
727 | |
728 | if (ret == -1) | |
729 | { | |
7e1e6965 | 730 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
97e6ee04 | 731 | { |
51fe4b60 VZ |
732 | m_error = wxSOCKET_WOULDBLOCK; |
733 | SOCKET_DEBUG(( "Write error WOULDBLOCK\n" )); | |
97e6ee04 DE |
734 | } |
735 | else | |
736 | { | |
51fe4b60 VZ |
737 | m_error = wxSOCKET_IOERR; |
738 | SOCKET_DEBUG(( "Write error IOERR\n" )); | |
97e6ee04 DE |
739 | } |
740 | ||
741 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
742 | * in MSW). Once the first OUTPUT event is received, users can assume | |
743 | * that the socket is writable until a read operation fails. Only then | |
744 | * will further OUTPUT events be posted. | |
745 | */ | |
51fe4b60 | 746 | EnableEvent(wxSOCKET_OUTPUT); |
1aa7b427 | 747 | |
97e6ee04 DE |
748 | return -1; |
749 | } | |
b082b524 | 750 | |
51fe4b60 | 751 | SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size, ret )); |
97e6ee04 DE |
752 | |
753 | return ret; | |
754 | } | |
755 | ||
97e6ee04 DE |
756 | /* Flags */ |
757 | ||
51fe4b60 | 758 | void wxSocketImplUnix::EnableEvent(wxSocketNotify event) |
97e6ee04 | 759 | { |
2804f77d VZ |
760 | if (m_use_events) |
761 | { | |
762 | m_detected &= ~(1 << event); | |
51fe4b60 | 763 | wxSocketManager::Get()->Install_Callback(this, event); |
2804f77d | 764 | } |
97e6ee04 DE |
765 | } |
766 | ||
51fe4b60 | 767 | void wxSocketImplUnix::DisableEvent(wxSocketNotify event) |
97e6ee04 | 768 | { |
2804f77d VZ |
769 | if (m_use_events) |
770 | { | |
771 | m_detected |= (1 << event); | |
51fe4b60 | 772 | wxSocketManager::Get()->Uninstall_Callback(this, event); |
2804f77d | 773 | } |
97e6ee04 DE |
774 | } |
775 | ||
51fe4b60 | 776 | /* |
97e6ee04 DE |
777 | * For blocking sockets, wait until data is available or |
778 | * until timeout ellapses. | |
779 | */ | |
51fe4b60 | 780 | wxSocketError wxSocketImplUnix::Input_Timeout() |
97e6ee04 | 781 | { |
97e6ee04 DE |
782 | fd_set readfds; |
783 | int ret; | |
784 | ||
87c0312a VZ |
785 | // Linux select() will overwrite the struct on return so make a copy |
786 | struct timeval tv = m_timeout; | |
97e6ee04 | 787 | |
09e6e5ec | 788 | if (!m_non_blocking) |
97e6ee04 | 789 | { |
17a1ebd1 VZ |
790 | wxFD_ZERO(&readfds); |
791 | wxFD_SET(m_fd, &readfds); | |
09e6e5ec | 792 | ret = select(m_fd + 1, &readfds, NULL, NULL, &tv); |
97e6ee04 DE |
793 | if (ret == 0) |
794 | { | |
51fe4b60 VZ |
795 | SOCKET_DEBUG(( "Input_Timeout, select returned 0\n" )); |
796 | m_error = wxSOCKET_TIMEDOUT; | |
797 | return wxSOCKET_TIMEDOUT; | |
97e6ee04 | 798 | } |
1aa7b427 | 799 | |
97e6ee04 DE |
800 | if (ret == -1) |
801 | { | |
51fe4b60 VZ |
802 | SOCKET_DEBUG(( "Input_Timeout, select returned -1\n" )); |
803 | if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); } | |
804 | if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); } | |
805 | if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); } | |
806 | if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); } | |
807 | m_error = wxSOCKET_TIMEDOUT; | |
808 | return wxSOCKET_TIMEDOUT; | |
97e6ee04 DE |
809 | } |
810 | } | |
1aa7b427 | 811 | |
51fe4b60 | 812 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
813 | } |
814 | ||
51fe4b60 | 815 | /* |
97e6ee04 DE |
816 | * For blocking sockets, wait until data can be sent without |
817 | * blocking or until timeout ellapses. | |
818 | */ | |
51fe4b60 | 819 | wxSocketError wxSocketImplUnix::Output_Timeout() |
97e6ee04 | 820 | { |
97e6ee04 DE |
821 | fd_set writefds; |
822 | int ret; | |
823 | ||
87c0312a VZ |
824 | // Linux select() will overwrite the struct on return so make a copy |
825 | struct timeval tv = m_timeout; | |
97e6ee04 | 826 | |
51fe4b60 | 827 | SOCKET_DEBUG( ("m_non_blocking has: %d\n", (int)m_non_blocking) ); |
97e6ee04 | 828 | |
09e6e5ec | 829 | if (!m_non_blocking) |
97e6ee04 | 830 | { |
17a1ebd1 VZ |
831 | wxFD_ZERO(&writefds); |
832 | wxFD_SET(m_fd, &writefds); | |
09e6e5ec | 833 | ret = select(m_fd + 1, NULL, &writefds, NULL, &tv); |
97e6ee04 DE |
834 | if (ret == 0) |
835 | { | |
51fe4b60 VZ |
836 | SOCKET_DEBUG(( "Output_Timeout, select returned 0\n" )); |
837 | m_error = wxSOCKET_TIMEDOUT; | |
838 | return wxSOCKET_TIMEDOUT; | |
97e6ee04 | 839 | } |
1aa7b427 | 840 | |
97e6ee04 DE |
841 | if (ret == -1) |
842 | { | |
51fe4b60 VZ |
843 | SOCKET_DEBUG(( "Output_Timeout, select returned -1\n" )); |
844 | if (errno == EBADF) { SOCKET_DEBUG(( "Invalid file descriptor\n" )); } | |
845 | if (errno == EINTR) { SOCKET_DEBUG(( "A non blocked signal was caught\n" )); } | |
846 | if (errno == EINVAL) { SOCKET_DEBUG(( "The highest number descriptor is negative\n" )); } | |
847 | if (errno == ENOMEM) { SOCKET_DEBUG(( "Not enough memory\n" )); } | |
848 | m_error = wxSOCKET_TIMEDOUT; | |
849 | return wxSOCKET_TIMEDOUT; | |
97e6ee04 | 850 | } |
1aa7b427 DS |
851 | |
852 | if ( ! wxFD_ISSET(m_fd, &writefds) ) | |
853 | { | |
51fe4b60 | 854 | SOCKET_DEBUG(( "Output_Timeout is buggy!\n" )); |
97e6ee04 | 855 | } |
1aa7b427 DS |
856 | else |
857 | { | |
51fe4b60 | 858 | SOCKET_DEBUG(( "Output_Timeout seems correct\n" )); |
97e6ee04 DE |
859 | } |
860 | } | |
861 | else | |
862 | { | |
51fe4b60 | 863 | SOCKET_DEBUG(( "Output_Timeout, didn't try select!\n" )); |
97e6ee04 DE |
864 | } |
865 | ||
51fe4b60 | 866 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
867 | } |
868 | ||
51fe4b60 | 869 | int wxSocketImplUnix::Recv_Stream(char *buffer, int size) |
97e6ee04 | 870 | { |
bb154f79 | 871 | int ret; |
7e1e6965 | 872 | do |
bb154f79 KH |
873 | { |
874 | ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); | |
1aa7b427 DS |
875 | } |
876 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
877 | ||
bb154f79 | 878 | return ret; |
97e6ee04 DE |
879 | } |
880 | ||
51fe4b60 | 881 | int wxSocketImplUnix::Recv_Dgram(char *buffer, int size) |
97e6ee04 | 882 | { |
8575ff50 | 883 | wxSockAddr from; |
9e03e02d | 884 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 | 885 | int ret; |
51fe4b60 | 886 | wxSocketError err; |
97e6ee04 DE |
887 | |
888 | fromlen = sizeof(from); | |
889 | ||
7e1e6965 | 890 | do |
bb154f79 | 891 | { |
8575ff50 | 892 | ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen); |
1aa7b427 DS |
893 | } |
894 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
97e6ee04 DE |
895 | |
896 | if (ret == -1) | |
897 | return -1; | |
898 | ||
51fe4b60 | 899 | /* Translate a system address into a wxSocketImplUnix address */ |
09e6e5ec | 900 | if (!m_peer) |
97e6ee04 | 901 | { |
09e6e5ec DE |
902 | m_peer = GAddress_new(); |
903 | if (!m_peer) | |
97e6ee04 | 904 | { |
51fe4b60 | 905 | m_error = wxSOCKET_MEMERR; |
97e6ee04 DE |
906 | return -1; |
907 | } | |
908 | } | |
1aa7b427 | 909 | |
8575ff50 | 910 | err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen); |
51fe4b60 | 911 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 912 | { |
09e6e5ec DE |
913 | GAddress_destroy(m_peer); |
914 | m_peer = NULL; | |
915 | m_error = err; | |
97e6ee04 DE |
916 | return -1; |
917 | } | |
918 | ||
919 | return ret; | |
920 | } | |
921 | ||
51fe4b60 | 922 | int wxSocketImplUnix::Send_Stream(const char *buffer, int size) |
97e6ee04 DE |
923 | { |
924 | int ret; | |
925 | ||
7e1e6965 WS |
926 | MASK_SIGNAL(); |
927 | ||
928 | do | |
bb154f79 KH |
929 | { |
930 | ret = send(m_fd, (char *)buffer, size, GSOCKET_MSG_NOSIGNAL); | |
1aa7b427 DS |
931 | } |
932 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 933 | |
97e6ee04 | 934 | UNMASK_SIGNAL(); |
97e6ee04 DE |
935 | |
936 | return ret; | |
937 | } | |
938 | ||
51fe4b60 | 939 | int wxSocketImplUnix::Send_Dgram(const char *buffer, int size) |
97e6ee04 DE |
940 | { |
941 | struct sockaddr *addr; | |
942 | int len, ret; | |
51fe4b60 | 943 | wxSocketError err; |
97e6ee04 | 944 | |
09e6e5ec | 945 | if (!m_peer) |
97e6ee04 | 946 | { |
51fe4b60 | 947 | m_error = wxSOCKET_INVADDR; |
97e6ee04 DE |
948 | return -1; |
949 | } | |
950 | ||
09e6e5ec | 951 | err = _GAddress_translate_to(m_peer, &addr, &len); |
51fe4b60 | 952 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 953 | { |
09e6e5ec | 954 | m_error = err; |
97e6ee04 DE |
955 | return -1; |
956 | } | |
957 | ||
97e6ee04 | 958 | MASK_SIGNAL(); |
7e1e6965 WS |
959 | |
960 | do | |
bb154f79 KH |
961 | { |
962 | ret = sendto(m_fd, (char *)buffer, size, 0, addr, len); | |
1aa7b427 DS |
963 | } |
964 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 965 | |
97e6ee04 | 966 | UNMASK_SIGNAL(); |
97e6ee04 DE |
967 | |
968 | /* Frees memory allocated from _GAddress_translate_to */ | |
969 | free(addr); | |
970 | ||
971 | return ret; | |
972 | } | |
973 | ||
51fe4b60 | 974 | void wxSocketImplUnix::OnStateChange(wxSocketNotify event) |
53a161e1 | 975 | { |
f0fbbe23 | 976 | DisableEvent(event); |
53a161e1 VZ |
977 | NotifyOnStateChange(event); |
978 | ||
51fe4b60 | 979 | if ( event == wxSOCKET_LOST ) |
53a161e1 VZ |
980 | Shutdown(); |
981 | } | |
982 | ||
a9d859df | 983 | void wxSocketImplUnix::OnReadWaiting() |
97e6ee04 DE |
984 | { |
985 | char c; | |
986 | ||
bb154f79 KH |
987 | if (m_fd == INVALID_SOCKET) |
988 | { | |
989 | return; | |
990 | } | |
991 | ||
97e6ee04 DE |
992 | /* If we have already detected a LOST event, then don't try |
993 | * to do any further processing. | |
994 | */ | |
51fe4b60 | 995 | if ((m_detected & wxSOCKET_LOST_FLAG) != 0) |
97e6ee04 | 996 | { |
948c96ef | 997 | m_establishing = false; |
97e6ee04 | 998 | |
51fe4b60 | 999 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
1000 | return; |
1001 | } | |
1002 | ||
bb154f79 KH |
1003 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
1004 | ||
1005 | if (num > 0) | |
97e6ee04 | 1006 | { |
51fe4b60 | 1007 | OnStateChange(wxSOCKET_INPUT); |
97e6ee04 DE |
1008 | } |
1009 | else | |
1010 | { | |
09e6e5ec | 1011 | if (m_server && m_stream) |
97e6ee04 | 1012 | { |
51fe4b60 | 1013 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 | 1014 | } |
e37e082e VZ |
1015 | else if (num == 0) |
1016 | { | |
01c03554 VZ |
1017 | if (m_stream) |
1018 | { | |
1019 | /* graceful shutdown */ | |
51fe4b60 | 1020 | OnStateChange(wxSOCKET_LOST); |
01c03554 VZ |
1021 | } |
1022 | else | |
1023 | { | |
1024 | /* Empty datagram received */ | |
51fe4b60 | 1025 | OnStateChange(wxSOCKET_INPUT); |
01c03554 | 1026 | } |
e37e082e | 1027 | } |
97e6ee04 DE |
1028 | else |
1029 | { | |
e400d27d | 1030 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 1031 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 | 1032 | { |
51fe4b60 | 1033 | OnStateChange(wxSOCKET_INPUT); |
bb154f79 | 1034 | } |
7e1e6965 | 1035 | else |
bb154f79 | 1036 | { |
51fe4b60 | 1037 | OnStateChange(wxSOCKET_LOST); |
7e1e6965 | 1038 | } |
97e6ee04 DE |
1039 | } |
1040 | } | |
1041 | } | |
1042 | ||
a9d859df | 1043 | void wxSocketImplUnix::OnWriteWaiting() |
97e6ee04 DE |
1044 | { |
1045 | /* If we have already detected a LOST event, then don't try | |
1046 | * to do any further processing. | |
1047 | */ | |
51fe4b60 | 1048 | if ((m_detected & wxSOCKET_LOST_FLAG) != 0) |
97e6ee04 | 1049 | { |
948c96ef | 1050 | m_establishing = false; |
97e6ee04 | 1051 | |
51fe4b60 | 1052 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
1053 | return; |
1054 | } | |
1055 | ||
09e6e5ec | 1056 | if (m_establishing && !m_server) |
97e6ee04 DE |
1057 | { |
1058 | int error; | |
ddc1a35f | 1059 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 1060 | |
948c96ef | 1061 | m_establishing = false; |
97e6ee04 | 1062 | |
f71bb8ef | 1063 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
1064 | |
1065 | if (error) | |
1066 | { | |
51fe4b60 | 1067 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
1068 | } |
1069 | else | |
1070 | { | |
51fe4b60 | 1071 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 DE |
1072 | /* We have to fire this event by hand because CONNECTION (for clients) |
1073 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
1074 | * events with the above macro. | |
1075 | */ | |
51fe4b60 | 1076 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
1077 | } |
1078 | } | |
1079 | else | |
1080 | { | |
51fe4b60 | 1081 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
1082 | } |
1083 | } | |
1084 | ||
a9d859df VZ |
1085 | void wxSocketImplUnix::OnExceptionWaiting() |
1086 | { | |
1087 | wxFAIL_MSG( "not supposed to be called" ); | |
1088 | } | |
1089 | ||
97e6ee04 DE |
1090 | /* |
1091 | * ------------------------------------------------------------------------- | |
1092 | * GAddress | |
1093 | * ------------------------------------------------------------------------- | |
1094 | */ | |
1095 | ||
1096 | /* CHECK_ADDRESS verifies that the current address family is either | |
51fe4b60 VZ |
1097 | * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it |
1098 | * initalizes it to be a wxSOCKET_*family*. In other cases, it returns | |
97e6ee04 DE |
1099 | * an appropiate error code. |
1100 | * | |
1101 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1102 | */ | |
1103 | #define CHECK_ADDRESS(address, family) \ | |
1104 | { \ | |
51fe4b60 VZ |
1105 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
1106 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 1107 | return address->m_error; \ |
51fe4b60 | 1108 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 1109 | { \ |
51fe4b60 VZ |
1110 | address->m_error = wxSOCKET_INVADDR; \ |
1111 | return wxSOCKET_INVADDR; \ | |
97e6ee04 DE |
1112 | } \ |
1113 | } | |
1114 | ||
1115 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1116 | { \ | |
51fe4b60 VZ |
1117 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
1118 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 1119 | return retval; \ |
51fe4b60 | 1120 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 1121 | { \ |
51fe4b60 | 1122 | address->m_error = wxSOCKET_INVADDR; \ |
97e6ee04 DE |
1123 | return retval; \ |
1124 | } \ | |
1125 | } | |
1126 | ||
1127 | ||
1128 | GAddress *GAddress_new(void) | |
1129 | { | |
1130 | GAddress *address; | |
1131 | ||
1132 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1133 | return NULL; | |
1134 | ||
51fe4b60 | 1135 | address->m_family = wxSOCKET_NOFAMILY; |
97e6ee04 DE |
1136 | address->m_addr = NULL; |
1137 | address->m_len = 0; | |
1138 | ||
1139 | return address; | |
1140 | } | |
1141 | ||
1142 | GAddress *GAddress_copy(GAddress *address) | |
1143 | { | |
1144 | GAddress *addr2; | |
1145 | ||
1146 | assert(address != NULL); | |
1147 | ||
1148 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1149 | return NULL; | |
1150 | ||
1151 | memcpy(addr2, address, sizeof(GAddress)); | |
1152 | ||
1153 | if (address->m_addr && address->m_len > 0) | |
1154 | { | |
1155 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
1156 | if (addr2->m_addr == NULL) | |
1157 | { | |
1158 | free(addr2); | |
1159 | return NULL; | |
1160 | } | |
1161 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1162 | } | |
1163 | ||
1164 | return addr2; | |
1165 | } | |
1166 | ||
1167 | void GAddress_destroy(GAddress *address) | |
1168 | { | |
1169 | assert(address != NULL); | |
1170 | ||
1171 | if (address->m_addr) | |
1172 | free(address->m_addr); | |
1173 | ||
1174 | free(address); | |
1175 | } | |
1176 | ||
1177 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1178 | { | |
1179 | assert(address != NULL); | |
1180 | ||
1181 | address->m_family = type; | |
1182 | } | |
1183 | ||
1184 | GAddressType GAddress_GetFamily(GAddress *address) | |
1185 | { | |
1186 | assert(address != NULL); | |
1187 | ||
1188 | return address->m_family; | |
1189 | } | |
1190 | ||
51fe4b60 | 1191 | wxSocketError _GAddress_translate_from(GAddress *address, |
97e6ee04 DE |
1192 | struct sockaddr *addr, int len) |
1193 | { | |
1194 | address->m_realfamily = addr->sa_family; | |
1195 | switch (addr->sa_family) | |
1196 | { | |
1197 | case AF_INET: | |
51fe4b60 | 1198 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
1199 | break; |
1200 | case AF_UNIX: | |
51fe4b60 | 1201 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 | 1202 | break; |
8575ff50 | 1203 | #if wxUSE_IPV6 |
97e6ee04 | 1204 | case AF_INET6: |
51fe4b60 | 1205 | address->m_family = wxSOCKET_INET6; |
97e6ee04 | 1206 | break; |
8575ff50 | 1207 | #endif // wxUSE_IPV6 |
97e6ee04 DE |
1208 | default: |
1209 | { | |
51fe4b60 VZ |
1210 | address->m_error = wxSOCKET_INVOP; |
1211 | return wxSOCKET_INVOP; | |
97e6ee04 DE |
1212 | } |
1213 | } | |
1214 | ||
1215 | if (address->m_addr) | |
1216 | free(address->m_addr); | |
1217 | ||
1218 | address->m_len = len; | |
1219 | address->m_addr = (struct sockaddr *)malloc(len); | |
1220 | ||
1221 | if (address->m_addr == NULL) | |
1222 | { | |
51fe4b60 VZ |
1223 | address->m_error = wxSOCKET_MEMERR; |
1224 | return wxSOCKET_MEMERR; | |
97e6ee04 | 1225 | } |
1aa7b427 | 1226 | |
97e6ee04 DE |
1227 | memcpy(address->m_addr, addr, len); |
1228 | ||
51fe4b60 | 1229 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1230 | } |
1231 | ||
51fe4b60 | 1232 | wxSocketError _GAddress_translate_to(GAddress *address, |
97e6ee04 DE |
1233 | struct sockaddr **addr, int *len) |
1234 | { | |
1235 | if (!address->m_addr) | |
1236 | { | |
51fe4b60 VZ |
1237 | address->m_error = wxSOCKET_INVADDR; |
1238 | return wxSOCKET_INVADDR; | |
97e6ee04 DE |
1239 | } |
1240 | ||
1241 | *len = address->m_len; | |
1242 | *addr = (struct sockaddr *)malloc(address->m_len); | |
1243 | if (*addr == NULL) | |
1244 | { | |
51fe4b60 VZ |
1245 | address->m_error = wxSOCKET_MEMERR; |
1246 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1247 | } |
1248 | ||
1249 | memcpy(*addr, address->m_addr, address->m_len); | |
51fe4b60 | 1250 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1251 | } |
1252 | ||
1253 | /* | |
1254 | * ------------------------------------------------------------------------- | |
1255 | * Internet address family | |
1256 | * ------------------------------------------------------------------------- | |
1257 | */ | |
1258 | ||
51fe4b60 | 1259 | wxSocketError _GAddress_Init_INET(GAddress *address) |
97e6ee04 DE |
1260 | { |
1261 | address->m_len = sizeof(struct sockaddr_in); | |
1262 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1263 | if (address->m_addr == NULL) | |
1264 | { | |
51fe4b60 VZ |
1265 | address->m_error = wxSOCKET_MEMERR; |
1266 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1267 | } |
1268 | ||
51fe4b60 | 1269 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
1270 | address->m_realfamily = PF_INET; |
1271 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1272 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1273 | ||
51fe4b60 | 1274 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1275 | } |
1276 | ||
51fe4b60 | 1277 | wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) |
97e6ee04 DE |
1278 | { |
1279 | struct hostent *he; | |
1280 | struct in_addr *addr; | |
1281 | ||
1282 | assert(address != NULL); | |
1283 | ||
1284 | CHECK_ADDRESS(address, INET); | |
1285 | ||
1286 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1287 | ||
1288 | /* If it is a numeric host name, convert it now */ | |
1289 | #if defined(HAVE_INET_ATON) | |
1290 | if (inet_aton(hostname, addr) == 0) | |
1291 | { | |
1292 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 1293 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
1294 | { |
1295 | #else | |
1296 | /* Use gethostbyname by default */ | |
9d715960 | 1297 | #ifndef __WXMAC__ |
ba2a81d7 | 1298 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 1299 | if (val) |
9d715960 | 1300 | #endif |
97e6ee04 DE |
1301 | { |
1302 | #endif | |
1303 | struct in_addr *array_addr; | |
1304 | ||
1305 | /* It is a real name, we solve it */ | |
127189eb SN |
1306 | struct hostent h; |
1307 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
1308 | struct hostent_data buffer; | |
1309 | #else | |
1310 | char buffer[1024]; | |
1311 | #endif | |
1312 | int err; | |
1313 | he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err); | |
1314 | if (he == NULL) | |
97e6ee04 DE |
1315 | { |
1316 | /* Reset to invalid address */ | |
1317 | addr->s_addr = INADDR_NONE; | |
51fe4b60 VZ |
1318 | address->m_error = wxSOCKET_NOHOST; |
1319 | return wxSOCKET_NOHOST; | |
97e6ee04 | 1320 | } |
1aa7b427 | 1321 | |
97e6ee04 DE |
1322 | array_addr = (struct in_addr *) *(he->h_addr_list); |
1323 | addr->s_addr = array_addr[0].s_addr; | |
1324 | } | |
1aa7b427 | 1325 | |
51fe4b60 | 1326 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1327 | } |
1328 | ||
60edcf45 | 1329 | |
51fe4b60 | 1330 | wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) |
60edcf45 VZ |
1331 | { |
1332 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
1333 | } | |
1334 | ||
51fe4b60 | 1335 | wxSocketError GAddress_INET_SetAnyAddress(GAddress *address) |
97e6ee04 DE |
1336 | { |
1337 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1338 | } | |
1339 | ||
51fe4b60 | 1340 | wxSocketError GAddress_INET_SetHostAddress(GAddress *address, |
97e6ee04 DE |
1341 | unsigned long hostaddr) |
1342 | { | |
1343 | struct in_addr *addr; | |
1344 | ||
1345 | assert(address != NULL); | |
1346 | ||
1347 | CHECK_ADDRESS(address, INET); | |
1348 | ||
1349 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 1350 | addr->s_addr = htonl(hostaddr); |
97e6ee04 | 1351 | |
51fe4b60 | 1352 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1353 | } |
1354 | ||
51fe4b60 | 1355 | wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
97e6ee04 DE |
1356 | const char *protocol) |
1357 | { | |
1358 | struct servent *se; | |
1359 | struct sockaddr_in *addr; | |
1360 | ||
1361 | assert(address != NULL); | |
1362 | CHECK_ADDRESS(address, INET); | |
1363 | ||
1364 | if (!port) | |
1365 | { | |
51fe4b60 VZ |
1366 | address->m_error = wxSOCKET_INVPORT; |
1367 | return wxSOCKET_INVPORT; | |
97e6ee04 | 1368 | } |
b082b524 | 1369 | |
127189eb SN |
1370 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_4) |
1371 | struct servent_data buffer; | |
f6bc1c74 | 1372 | #else |
127189eb | 1373 | char buffer[1024]; |
f6bc1c74 | 1374 | #endif |
127189eb SN |
1375 | struct servent serv; |
1376 | se = wxGetservbyname_r(port, protocol, &serv, | |
1377 | (void*)&buffer, sizeof(buffer)); | |
97e6ee04 DE |
1378 | if (!se) |
1379 | { | |
1380 | /* the cast to int suppresses compiler warnings about subscript having the | |
1381 | type char */ | |
1382 | if (isdigit((int)port[0])) | |
1383 | { | |
1384 | int port_int; | |
1385 | ||
1386 | port_int = atoi(port); | |
1387 | addr = (struct sockaddr_in *)address->m_addr; | |
1388 | addr->sin_port = htons(port_int); | |
51fe4b60 | 1389 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1390 | } |
1391 | ||
51fe4b60 VZ |
1392 | address->m_error = wxSOCKET_INVPORT; |
1393 | return wxSOCKET_INVPORT; | |
97e6ee04 DE |
1394 | } |
1395 | ||
1396 | addr = (struct sockaddr_in *)address->m_addr; | |
1397 | addr->sin_port = se->s_port; | |
1398 | ||
51fe4b60 | 1399 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1400 | } |
1401 | ||
51fe4b60 | 1402 | wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) |
97e6ee04 DE |
1403 | { |
1404 | struct sockaddr_in *addr; | |
1405 | ||
1406 | assert(address != NULL); | |
1407 | CHECK_ADDRESS(address, INET); | |
b082b524 | 1408 | |
97e6ee04 DE |
1409 | addr = (struct sockaddr_in *)address->m_addr; |
1410 | addr->sin_port = htons(port); | |
1411 | ||
51fe4b60 | 1412 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1413 | } |
1414 | ||
51fe4b60 | 1415 | wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
97e6ee04 DE |
1416 | { |
1417 | struct hostent *he; | |
1418 | char *addr_buf; | |
1419 | struct sockaddr_in *addr; | |
1420 | ||
b082b524 | 1421 | assert(address != NULL); |
97e6ee04 DE |
1422 | CHECK_ADDRESS(address, INET); |
1423 | ||
1424 | addr = (struct sockaddr_in *)address->m_addr; | |
1425 | addr_buf = (char *)&(addr->sin_addr); | |
1426 | ||
127189eb SN |
1427 | struct hostent temphost; |
1428 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
1429 | struct hostent_data buffer; | |
1430 | #else | |
1431 | char buffer[1024]; | |
1432 | #endif | |
1433 | int err; | |
1434 | he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost, | |
1435 | (void*)&buffer, sizeof(buffer), &err); | |
97e6ee04 DE |
1436 | if (he == NULL) |
1437 | { | |
51fe4b60 VZ |
1438 | address->m_error = wxSOCKET_NOHOST; |
1439 | return wxSOCKET_NOHOST; | |
97e6ee04 DE |
1440 | } |
1441 | ||
1442 | strncpy(hostname, he->h_name, sbuf); | |
1443 | ||
51fe4b60 | 1444 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1445 | } |
1446 | ||
1447 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1448 | { | |
1449 | struct sockaddr_in *addr; | |
1450 | ||
b082b524 DE |
1451 | assert(address != NULL); |
1452 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1453 | |
1454 | addr = (struct sockaddr_in *)address->m_addr; | |
1455 | ||
9d715960 | 1456 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
1457 | } |
1458 | ||
1459 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1460 | { | |
1461 | struct sockaddr_in *addr; | |
1462 | ||
b082b524 DE |
1463 | assert(address != NULL); |
1464 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1465 | |
1466 | addr = (struct sockaddr_in *)address->m_addr; | |
1467 | return ntohs(addr->sin_port); | |
1468 | } | |
1469 | ||
8575ff50 VZ |
1470 | #if wxUSE_IPV6 |
1471 | /* | |
1472 | * ------------------------------------------------------------------------- | |
1473 | * Internet IPv6 address family | |
1474 | * ------------------------------------------------------------------------- | |
1475 | */ | |
1476 | ||
51fe4b60 | 1477 | wxSocketError _GAddress_Init_INET6(GAddress *address) |
8575ff50 VZ |
1478 | { |
1479 | struct in6_addr any_address = IN6ADDR_ANY_INIT; | |
1480 | address->m_len = sizeof(struct sockaddr_in6); | |
1481 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1482 | if (address->m_addr == NULL) | |
1483 | { | |
51fe4b60 VZ |
1484 | address->m_error = wxSOCKET_MEMERR; |
1485 | return wxSOCKET_MEMERR; | |
8575ff50 VZ |
1486 | } |
1487 | memset(address->m_addr,0,address->m_len); | |
1488 | ||
51fe4b60 | 1489 | address->m_family = wxSOCKET_INET6; |
8575ff50 VZ |
1490 | address->m_realfamily = AF_INET6; |
1491 | ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6; | |
1492 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address; | |
1493 | ||
51fe4b60 | 1494 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1495 | } |
1496 | ||
51fe4b60 | 1497 | wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) |
8575ff50 VZ |
1498 | { |
1499 | assert(address != NULL); | |
1500 | CHECK_ADDRESS(address, INET6); | |
1501 | ||
1502 | addrinfo hints; | |
1503 | memset( & hints, 0, sizeof( hints ) ); | |
1504 | hints.ai_family = AF_INET6; | |
1505 | addrinfo * info = 0; | |
1506 | if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info ) | |
1507 | { | |
51fe4b60 VZ |
1508 | address->m_error = wxSOCKET_NOHOST; |
1509 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
1510 | } |
1511 | ||
1512 | memcpy( address->m_addr, info->ai_addr, info->ai_addrlen ); | |
1513 | freeaddrinfo( info ); | |
51fe4b60 | 1514 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1515 | } |
1516 | ||
51fe4b60 | 1517 | wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address) |
8575ff50 VZ |
1518 | { |
1519 | assert(address != NULL); | |
1520 | ||
1521 | CHECK_ADDRESS(address, INET6); | |
1522 | ||
1523 | struct in6_addr addr; | |
1524 | memset( & addr, 0, sizeof( addr ) ); | |
1525 | return GAddress_INET6_SetHostAddress(address, addr); | |
1526 | } | |
51fe4b60 | 1527 | wxSocketError GAddress_INET6_SetHostAddress(GAddress *address, |
8575ff50 VZ |
1528 | struct in6_addr hostaddr) |
1529 | { | |
1530 | assert(address != NULL); | |
1531 | ||
1532 | CHECK_ADDRESS(address, INET6); | |
1533 | ||
1534 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
1535 | ||
51fe4b60 | 1536 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1537 | } |
1538 | ||
51fe4b60 | 1539 | wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, |
8575ff50 VZ |
1540 | const char *protocol) |
1541 | { | |
1542 | struct servent *se; | |
1543 | struct sockaddr_in6 *addr; | |
1544 | ||
1545 | assert(address != NULL); | |
1546 | CHECK_ADDRESS(address, INET6); | |
1547 | ||
1548 | if (!port) | |
1549 | { | |
51fe4b60 VZ |
1550 | address->m_error = wxSOCKET_INVPORT; |
1551 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1552 | } |
1553 | ||
1554 | se = getservbyname(port, protocol); | |
1555 | if (!se) | |
1556 | { | |
1557 | if (isdigit(port[0])) | |
1558 | { | |
1559 | int port_int; | |
1560 | ||
1561 | port_int = atoi(port); | |
1562 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1563 | addr->sin6_port = htons((u_short) port_int); | |
51fe4b60 | 1564 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1565 | } |
1566 | ||
51fe4b60 VZ |
1567 | address->m_error = wxSOCKET_INVPORT; |
1568 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1569 | } |
1570 | ||
1571 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1572 | addr->sin6_port = se->s_port; | |
1573 | ||
51fe4b60 | 1574 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1575 | } |
1576 | ||
51fe4b60 | 1577 | wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) |
8575ff50 VZ |
1578 | { |
1579 | struct sockaddr_in6 *addr; | |
1580 | ||
1581 | assert(address != NULL); | |
1582 | CHECK_ADDRESS(address, INET6); | |
1583 | ||
1584 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1585 | addr->sin6_port = htons(port); | |
1586 | ||
51fe4b60 | 1587 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1588 | } |
1589 | ||
51fe4b60 | 1590 | wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
8575ff50 VZ |
1591 | { |
1592 | struct hostent *he; | |
1593 | char *addr_buf; | |
1594 | struct sockaddr_in6 *addr; | |
1595 | ||
1596 | assert(address != NULL); | |
1597 | CHECK_ADDRESS(address, INET6); | |
1598 | ||
1599 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1600 | addr_buf = (char *)&(addr->sin6_addr); | |
1601 | ||
1602 | he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6); | |
1603 | if (he == NULL) | |
1604 | { | |
51fe4b60 VZ |
1605 | address->m_error = wxSOCKET_NOHOST; |
1606 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
1607 | } |
1608 | ||
1609 | strncpy(hostname, he->h_name, sbuf); | |
1610 | ||
51fe4b60 | 1611 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1612 | } |
1613 | ||
51fe4b60 | 1614 | wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) |
8575ff50 VZ |
1615 | { |
1616 | assert(address != NULL); | |
1617 | assert(hostaddr != NULL); | |
51fe4b60 | 1618 | CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR); |
8575ff50 | 1619 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; |
51fe4b60 | 1620 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1621 | } |
1622 | ||
1623 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1624 | { | |
1625 | assert(address != NULL); | |
1626 | CHECK_ADDRESS_RETVAL(address, INET6, 0); | |
1627 | ||
1628 | return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port ); | |
1629 | } | |
1630 | ||
1631 | #endif // wxUSE_IPV6 | |
1632 | ||
97e6ee04 DE |
1633 | /* |
1634 | * ------------------------------------------------------------------------- | |
1635 | * Unix address family | |
1636 | * ------------------------------------------------------------------------- | |
1637 | */ | |
1638 | ||
1639 | #ifndef __VISAGECPP__ | |
51fe4b60 | 1640 | wxSocketError _GAddress_Init_UNIX(GAddress *address) |
97e6ee04 DE |
1641 | { |
1642 | address->m_len = sizeof(struct sockaddr_un); | |
1643 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
1644 | if (address->m_addr == NULL) | |
1645 | { | |
51fe4b60 VZ |
1646 | address->m_error = wxSOCKET_MEMERR; |
1647 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1648 | } |
1649 | ||
51fe4b60 | 1650 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 DE |
1651 | address->m_realfamily = PF_UNIX; |
1652 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
1653 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
1654 | ||
51fe4b60 | 1655 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1656 | } |
1657 | ||
1658 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
1659 | ||
51fe4b60 | 1660 | wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) |
97e6ee04 DE |
1661 | { |
1662 | struct sockaddr_un *addr; | |
1663 | ||
b082b524 | 1664 | assert(address != NULL); |
97e6ee04 | 1665 | |
b082b524 | 1666 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
1667 | |
1668 | addr = ((struct sockaddr_un *)address->m_addr); | |
1669 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
1670 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
1671 | ||
51fe4b60 | 1672 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1673 | } |
1674 | ||
51fe4b60 | 1675 | wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) |
97e6ee04 DE |
1676 | { |
1677 | struct sockaddr_un *addr; | |
1678 | ||
1679 | assert(address != NULL); | |
1680 | CHECK_ADDRESS(address, UNIX); | |
1681 | ||
1682 | addr = (struct sockaddr_un *)address->m_addr; | |
1683 | ||
1684 | strncpy(path, addr->sun_path, sbuf); | |
1685 | ||
51fe4b60 | 1686 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1687 | } |
1688 | #endif /* !defined(__VISAGECPP__) */ | |
54cb21d6 | 1689 | |
ebf94940 | 1690 | #endif /* wxUSE_SOCKETS */ |