]>
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) */ | |
c6b10632 | 490 | if ( !BlockForInputWithTimeout() ) |
97e6ee04 | 491 | { |
85431efa | 492 | delete connection; |
97e6ee04 DE |
493 | return NULL; |
494 | } | |
495 | ||
8575ff50 | 496 | connection->m_fd = accept(m_fd, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen); |
97e6ee04 | 497 | |
bb154f79 | 498 | /* Reenable CONNECTION events */ |
51fe4b60 | 499 | EnableEvent(wxSOCKET_CONNECTION); |
bb154f79 | 500 | |
97e6ee04 DE |
501 | if (connection->m_fd == INVALID_SOCKET) |
502 | { | |
503 | if (errno == EWOULDBLOCK) | |
51fe4b60 | 504 | m_error = wxSOCKET_WOULDBLOCK; |
97e6ee04 | 505 | else |
51fe4b60 | 506 | m_error = wxSOCKET_IOERR; |
97e6ee04 | 507 | |
85431efa | 508 | delete connection; |
97e6ee04 DE |
509 | return NULL; |
510 | } | |
511 | ||
512 | /* Initialize all fields */ | |
948c96ef DE |
513 | connection->m_server = false; |
514 | connection->m_stream = true; | |
97e6ee04 DE |
515 | |
516 | /* Setup the peer address field */ | |
517 | connection->m_peer = GAddress_new(); | |
518 | if (!connection->m_peer) | |
519 | { | |
85431efa | 520 | delete connection; |
51fe4b60 | 521 | m_error = wxSOCKET_MEMERR; |
97e6ee04 DE |
522 | return NULL; |
523 | } | |
1aa7b427 | 524 | |
8575ff50 | 525 | err = _GAddress_translate_from(connection->m_peer, (sockaddr*)&from, fromlen); |
51fe4b60 | 526 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 527 | { |
85431efa | 528 | delete connection; |
09e6e5ec | 529 | m_error = err; |
97e6ee04 DE |
530 | return NULL; |
531 | } | |
1aa7b427 | 532 | |
97e6ee04 DE |
533 | #if defined(__EMX__) || defined(__VISAGECPP__) |
534 | ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg)); | |
535 | #else | |
536 | ioctl(connection->m_fd, FIONBIO, &arg); | |
537 | #endif | |
2804f77d VZ |
538 | if (m_use_events) |
539 | connection->Notify(true); | |
97e6ee04 DE |
540 | |
541 | return connection; | |
542 | } | |
543 | ||
51fe4b60 | 544 | void wxSocketImplUnix::Notify(bool flag) |
2804f77d VZ |
545 | { |
546 | if (flag == m_use_events) | |
547 | return; | |
2804f77d | 548 | m_use_events = flag; |
f0fbbe23 | 549 | DoEnableEvents(flag); |
2804f77d VZ |
550 | } |
551 | ||
51fe4b60 | 552 | void wxSocketImplUnix::DoEnableEvents(bool flag) |
2804f77d | 553 | { |
51fe4b60 | 554 | wxSocketManager * const manager = wxSocketManager::Get(); |
f0fbbe23 VZ |
555 | if ( flag ) |
556 | { | |
51fe4b60 VZ |
557 | manager->Install_Callback(this, wxSOCKET_INPUT); |
558 | manager->Install_Callback(this, wxSOCKET_OUTPUT); | |
f0fbbe23 VZ |
559 | } |
560 | else // off | |
561 | { | |
51fe4b60 VZ |
562 | manager->Uninstall_Callback(this, wxSOCKET_INPUT); |
563 | manager->Uninstall_Callback(this, wxSOCKET_OUTPUT); | |
60edcf45 | 564 | } |
60edcf45 VZ |
565 | } |
566 | ||
51fe4b60 | 567 | wxSocketError wxSocketImplUnix::DoHandleConnect(int ret) |
97e6ee04 | 568 | { |
f0fbbe23 VZ |
569 | /* We only call EnableEvents() if we know we aren't shutting down the socket. |
570 | * NB: EnableEvents() needs to be called whether the socket is blocking or | |
0d34c30e KH |
571 | * non-blocking, it just shouldn't be called prior to knowing there is a |
572 | * connection _if_ blocking sockets are being used. | |
573 | * If connect above returns 0, we are already connected and need to make the | |
f0fbbe23 | 574 | * call to EnableEvents() now. |
a3687636 | 575 | */ |
f0fbbe23 | 576 | if ( m_non_blocking || (ret == 0) ) |
2804f77d | 577 | EnableEvents(); |
bb154f79 | 578 | |
97e6ee04 DE |
579 | if (ret == -1) |
580 | { | |
51fe4b60 | 581 | const int err = errno; |
97e6ee04 | 582 | |
51fe4b60 | 583 | /* If connect failed with EINPROGRESS and the wxSocketImplUnix object |
97e6ee04 DE |
584 | * is in blocking mode, we select() for the specified timeout |
585 | * checking for writability to see if the connection request | |
586 | * completes. | |
587 | */ | |
09e6e5ec | 588 | if ((err == EINPROGRESS) && (!m_non_blocking)) |
97e6ee04 | 589 | { |
c6b10632 | 590 | if ( !BlockForOutputWithTimeout() ) |
97e6ee04 | 591 | { |
09e6e5ec | 592 | Close(); |
51fe4b60 | 593 | return wxSOCKET_TIMEDOUT; |
97e6ee04 | 594 | } |
97e6ee04 | 595 | |
c6b10632 VZ |
596 | int error; |
597 | SOCKOPTLEN_T len = sizeof(error); | |
bb154f79 | 598 | |
c6b10632 VZ |
599 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len); |
600 | EnableEvents(); | |
601 | ||
602 | if (!error) | |
603 | return wxSOCKET_NOERROR; | |
97e6ee04 DE |
604 | } |
605 | ||
51fe4b60 VZ |
606 | /* If connect failed with EINPROGRESS and the wxSocketImplUnix object |
607 | * is set to nonblocking, we set m_error to wxSOCKET_WOULDBLOCK | |
608 | * (and return wxSOCKET_WOULDBLOCK) but we don't close the socket; | |
609 | * this way if the connection completes, a wxSOCKET_CONNECTION | |
97e6ee04 DE |
610 | * event will be generated, if enabled. |
611 | */ | |
09e6e5ec | 612 | if ((err == EINPROGRESS) && (m_non_blocking)) |
97e6ee04 | 613 | { |
948c96ef | 614 | m_establishing = true; |
51fe4b60 VZ |
615 | m_error = wxSOCKET_WOULDBLOCK; |
616 | return wxSOCKET_WOULDBLOCK; | |
97e6ee04 DE |
617 | } |
618 | ||
619 | /* If connect failed with an error other than EINPROGRESS, | |
51fe4b60 | 620 | * then the call to Connect has failed. |
97e6ee04 | 621 | */ |
09e6e5ec | 622 | Close(); |
51fe4b60 | 623 | m_error = wxSOCKET_IOERR; |
97e6ee04 | 624 | |
51fe4b60 | 625 | return wxSOCKET_IOERR; |
01ba4b67 VZ |
626 | } |
627 | ||
51fe4b60 | 628 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
629 | } |
630 | ||
631 | /* Generic IO */ | |
632 | ||
633 | /* Like recv(), send(), ... */ | |
07792edb | 634 | int wxSocketImplUnix::Read(void *buffer, int size) |
97e6ee04 DE |
635 | { |
636 | int ret; | |
637 | ||
09e6e5ec | 638 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 639 | { |
51fe4b60 | 640 | m_error = wxSOCKET_INVSOCK; |
97e6ee04 DE |
641 | return -1; |
642 | } | |
643 | ||
9d715960 | 644 | /* Disable events during query of socket status */ |
51fe4b60 | 645 | DisableEvent(wxSOCKET_INPUT); |
b082b524 | 646 | |
97e6ee04 | 647 | /* If the socket is blocking, wait for data (with a timeout) */ |
c6b10632 VZ |
648 | if ( !BlockForInputWithTimeout() ) |
649 | { | |
51fe4b60 | 650 | m_error = wxSOCKET_TIMEDOUT; |
976abb72 VZ |
651 | /* Don't return here immediately, otherwise socket events would not be |
652 | * re-enabled! */ | |
9d715960 | 653 | ret = -1; |
976abb72 | 654 | } |
1aa7b427 DS |
655 | else |
656 | { | |
9d715960 DE |
657 | /* Read the data */ |
658 | if (m_stream) | |
659 | ret = Recv_Stream(buffer, size); | |
660 | else | |
661 | ret = Recv_Dgram(buffer, size); | |
b082b524 | 662 | |
01c03554 VZ |
663 | /* |
664 | * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP | |
665 | * socket and empty datagrams are possible), then the connection has been | |
666 | * gracefully closed. | |
667 | * | |
668 | * Otherwise, recv has returned an error (-1), in which case we have lost | |
669 | * the socket only if errno does _not_ indicate that there may be more data | |
670 | * to read. | |
9b67181b | 671 | */ |
01c03554 | 672 | if ((ret == 0) && m_stream) |
98e7a7f9 | 673 | { |
f4854380 | 674 | /* Make sure wxSOCKET_LOST event gets sent and shut down the socket */ |
2804f77d VZ |
675 | if (m_use_events) |
676 | { | |
51fe4b60 | 677 | m_detected = wxSOCKET_LOST_FLAG; |
a9d859df | 678 | OnReadWaiting(); |
2804f77d VZ |
679 | return 0; |
680 | } | |
98e7a7f9 | 681 | } |
1aa7b427 DS |
682 | else if (ret == -1) |
683 | { | |
976abb72 | 684 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
51fe4b60 | 685 | m_error = wxSOCKET_WOULDBLOCK; |
976abb72 | 686 | else |
51fe4b60 | 687 | m_error = wxSOCKET_IOERR; |
976abb72 | 688 | } |
97e6ee04 | 689 | } |
b082b524 | 690 | |
9d715960 | 691 | /* Enable events again now that we are done processing */ |
51fe4b60 | 692 | EnableEvent(wxSOCKET_INPUT); |
97e6ee04 DE |
693 | |
694 | return ret; | |
695 | } | |
696 | ||
07792edb | 697 | int wxSocketImplUnix::Write(const void *buffer, int size) |
b082b524 | 698 | { |
97e6ee04 DE |
699 | int ret; |
700 | ||
51fe4b60 | 701 | SOCKET_DEBUG(( "Write #1, size %d\n", size )); |
97e6ee04 | 702 | |
09e6e5ec | 703 | if (m_fd == INVALID_SOCKET || m_server) |
97e6ee04 | 704 | { |
51fe4b60 | 705 | m_error = wxSOCKET_INVSOCK; |
97e6ee04 DE |
706 | return -1; |
707 | } | |
708 | ||
51fe4b60 | 709 | SOCKET_DEBUG(( "Write #2, size %d\n", size )); |
97e6ee04 DE |
710 | |
711 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
c6b10632 | 712 | if ( !BlockForOutputWithTimeout() ) |
97e6ee04 DE |
713 | return -1; |
714 | ||
51fe4b60 | 715 | SOCKET_DEBUG(( "Write #3, size %d\n", size )); |
97e6ee04 DE |
716 | |
717 | /* Write the data */ | |
09e6e5ec DE |
718 | if (m_stream) |
719 | ret = Send_Stream(buffer, size); | |
97e6ee04 | 720 | else |
09e6e5ec | 721 | ret = Send_Dgram(buffer, size); |
b082b524 | 722 | |
51fe4b60 | 723 | SOCKET_DEBUG(( "Write #4, size %d\n", size )); |
97e6ee04 DE |
724 | |
725 | if (ret == -1) | |
726 | { | |
7e1e6965 | 727 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) |
97e6ee04 | 728 | { |
51fe4b60 VZ |
729 | m_error = wxSOCKET_WOULDBLOCK; |
730 | SOCKET_DEBUG(( "Write error WOULDBLOCK\n" )); | |
97e6ee04 DE |
731 | } |
732 | else | |
733 | { | |
51fe4b60 VZ |
734 | m_error = wxSOCKET_IOERR; |
735 | SOCKET_DEBUG(( "Write error IOERR\n" )); | |
97e6ee04 DE |
736 | } |
737 | ||
738 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
739 | * in MSW). Once the first OUTPUT event is received, users can assume | |
740 | * that the socket is writable until a read operation fails. Only then | |
741 | * will further OUTPUT events be posted. | |
742 | */ | |
51fe4b60 | 743 | EnableEvent(wxSOCKET_OUTPUT); |
1aa7b427 | 744 | |
97e6ee04 DE |
745 | return -1; |
746 | } | |
b082b524 | 747 | |
51fe4b60 | 748 | SOCKET_DEBUG(( "Write #5, size %d ret %d\n", size, ret )); |
97e6ee04 DE |
749 | |
750 | return ret; | |
751 | } | |
752 | ||
97e6ee04 DE |
753 | /* Flags */ |
754 | ||
51fe4b60 | 755 | void wxSocketImplUnix::EnableEvent(wxSocketNotify event) |
97e6ee04 | 756 | { |
2804f77d VZ |
757 | if (m_use_events) |
758 | { | |
759 | m_detected &= ~(1 << event); | |
51fe4b60 | 760 | wxSocketManager::Get()->Install_Callback(this, event); |
2804f77d | 761 | } |
97e6ee04 DE |
762 | } |
763 | ||
51fe4b60 | 764 | void wxSocketImplUnix::DisableEvent(wxSocketNotify event) |
97e6ee04 | 765 | { |
2804f77d VZ |
766 | if (m_use_events) |
767 | { | |
768 | m_detected |= (1 << event); | |
51fe4b60 | 769 | wxSocketManager::Get()->Uninstall_Callback(this, event); |
2804f77d | 770 | } |
97e6ee04 DE |
771 | } |
772 | ||
07792edb | 773 | int wxSocketImplUnix::Recv_Stream(void *buffer, int size) |
97e6ee04 | 774 | { |
bb154f79 | 775 | int ret; |
7e1e6965 | 776 | do |
bb154f79 KH |
777 | { |
778 | ret = recv(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); | |
1aa7b427 DS |
779 | } |
780 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
781 | ||
bb154f79 | 782 | return ret; |
97e6ee04 DE |
783 | } |
784 | ||
07792edb | 785 | int wxSocketImplUnix::Recv_Dgram(void *buffer, int size) |
97e6ee04 | 786 | { |
8575ff50 | 787 | wxSockAddr from; |
9e03e02d | 788 | WX_SOCKLEN_T fromlen = sizeof(from); |
97e6ee04 | 789 | int ret; |
51fe4b60 | 790 | wxSocketError err; |
97e6ee04 DE |
791 | |
792 | fromlen = sizeof(from); | |
793 | ||
7e1e6965 | 794 | do |
bb154f79 | 795 | { |
8575ff50 | 796 | ret = recvfrom(m_fd, buffer, size, 0, (sockaddr*)&from, (WX_SOCKLEN_T *) &fromlen); |
1aa7b427 DS |
797 | } |
798 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
97e6ee04 DE |
799 | |
800 | if (ret == -1) | |
801 | return -1; | |
802 | ||
51fe4b60 | 803 | /* Translate a system address into a wxSocketImplUnix address */ |
09e6e5ec | 804 | if (!m_peer) |
97e6ee04 | 805 | { |
09e6e5ec DE |
806 | m_peer = GAddress_new(); |
807 | if (!m_peer) | |
97e6ee04 | 808 | { |
51fe4b60 | 809 | m_error = wxSOCKET_MEMERR; |
97e6ee04 DE |
810 | return -1; |
811 | } | |
812 | } | |
1aa7b427 | 813 | |
8575ff50 | 814 | err = _GAddress_translate_from(m_peer, (sockaddr*)&from, fromlen); |
51fe4b60 | 815 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 816 | { |
09e6e5ec DE |
817 | GAddress_destroy(m_peer); |
818 | m_peer = NULL; | |
819 | m_error = err; | |
97e6ee04 DE |
820 | return -1; |
821 | } | |
822 | ||
823 | return ret; | |
824 | } | |
825 | ||
07792edb | 826 | int wxSocketImplUnix::Send_Stream(const void *buffer, int size) |
97e6ee04 DE |
827 | { |
828 | int ret; | |
829 | ||
7e1e6965 WS |
830 | MASK_SIGNAL(); |
831 | ||
832 | do | |
bb154f79 | 833 | { |
07792edb | 834 | ret = send(m_fd, buffer, size, GSOCKET_MSG_NOSIGNAL); |
1aa7b427 DS |
835 | } |
836 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 837 | |
97e6ee04 | 838 | UNMASK_SIGNAL(); |
97e6ee04 DE |
839 | |
840 | return ret; | |
841 | } | |
842 | ||
07792edb | 843 | int wxSocketImplUnix::Send_Dgram(const void *buffer, int size) |
97e6ee04 DE |
844 | { |
845 | struct sockaddr *addr; | |
846 | int len, ret; | |
51fe4b60 | 847 | wxSocketError err; |
97e6ee04 | 848 | |
09e6e5ec | 849 | if (!m_peer) |
97e6ee04 | 850 | { |
51fe4b60 | 851 | m_error = wxSOCKET_INVADDR; |
97e6ee04 DE |
852 | return -1; |
853 | } | |
854 | ||
09e6e5ec | 855 | err = _GAddress_translate_to(m_peer, &addr, &len); |
51fe4b60 | 856 | if (err != wxSOCKET_NOERROR) |
97e6ee04 | 857 | { |
09e6e5ec | 858 | m_error = err; |
97e6ee04 DE |
859 | return -1; |
860 | } | |
861 | ||
97e6ee04 | 862 | MASK_SIGNAL(); |
7e1e6965 WS |
863 | |
864 | do | |
bb154f79 | 865 | { |
07792edb | 866 | ret = sendto(m_fd, buffer, size, 0, addr, len); |
1aa7b427 DS |
867 | } |
868 | while (ret == -1 && errno == EINTR); /* Loop until not interrupted */ | |
7e1e6965 | 869 | |
97e6ee04 | 870 | UNMASK_SIGNAL(); |
97e6ee04 DE |
871 | |
872 | /* Frees memory allocated from _GAddress_translate_to */ | |
873 | free(addr); | |
874 | ||
875 | return ret; | |
876 | } | |
877 | ||
51fe4b60 | 878 | void wxSocketImplUnix::OnStateChange(wxSocketNotify event) |
53a161e1 | 879 | { |
f0fbbe23 | 880 | DisableEvent(event); |
53a161e1 VZ |
881 | NotifyOnStateChange(event); |
882 | ||
51fe4b60 | 883 | if ( event == wxSOCKET_LOST ) |
53a161e1 VZ |
884 | Shutdown(); |
885 | } | |
886 | ||
a9d859df | 887 | void wxSocketImplUnix::OnReadWaiting() |
97e6ee04 DE |
888 | { |
889 | char c; | |
890 | ||
bb154f79 KH |
891 | if (m_fd == INVALID_SOCKET) |
892 | { | |
893 | return; | |
894 | } | |
895 | ||
97e6ee04 DE |
896 | /* If we have already detected a LOST event, then don't try |
897 | * to do any further processing. | |
898 | */ | |
51fe4b60 | 899 | if ((m_detected & wxSOCKET_LOST_FLAG) != 0) |
97e6ee04 | 900 | { |
948c96ef | 901 | m_establishing = false; |
97e6ee04 | 902 | |
51fe4b60 | 903 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
904 | return; |
905 | } | |
906 | ||
bb154f79 KH |
907 | int num = recv(m_fd, &c, 1, MSG_PEEK | GSOCKET_MSG_NOSIGNAL); |
908 | ||
909 | if (num > 0) | |
97e6ee04 | 910 | { |
51fe4b60 | 911 | OnStateChange(wxSOCKET_INPUT); |
97e6ee04 DE |
912 | } |
913 | else | |
914 | { | |
09e6e5ec | 915 | if (m_server && m_stream) |
97e6ee04 | 916 | { |
51fe4b60 | 917 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 | 918 | } |
e37e082e VZ |
919 | else if (num == 0) |
920 | { | |
01c03554 VZ |
921 | if (m_stream) |
922 | { | |
923 | /* graceful shutdown */ | |
51fe4b60 | 924 | OnStateChange(wxSOCKET_LOST); |
01c03554 VZ |
925 | } |
926 | else | |
927 | { | |
928 | /* Empty datagram received */ | |
51fe4b60 | 929 | OnStateChange(wxSOCKET_INPUT); |
01c03554 | 930 | } |
e37e082e | 931 | } |
97e6ee04 DE |
932 | else |
933 | { | |
e400d27d | 934 | /* Do not throw a lost event in cases where the socket isn't really lost */ |
7e1e6965 | 935 | if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR)) |
bb154f79 | 936 | { |
51fe4b60 | 937 | OnStateChange(wxSOCKET_INPUT); |
bb154f79 | 938 | } |
7e1e6965 | 939 | else |
bb154f79 | 940 | { |
51fe4b60 | 941 | OnStateChange(wxSOCKET_LOST); |
7e1e6965 | 942 | } |
97e6ee04 DE |
943 | } |
944 | } | |
945 | } | |
946 | ||
a9d859df | 947 | void wxSocketImplUnix::OnWriteWaiting() |
97e6ee04 DE |
948 | { |
949 | /* If we have already detected a LOST event, then don't try | |
950 | * to do any further processing. | |
951 | */ | |
51fe4b60 | 952 | if ((m_detected & wxSOCKET_LOST_FLAG) != 0) |
97e6ee04 | 953 | { |
948c96ef | 954 | m_establishing = false; |
97e6ee04 | 955 | |
51fe4b60 | 956 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
957 | return; |
958 | } | |
959 | ||
09e6e5ec | 960 | if (m_establishing && !m_server) |
97e6ee04 DE |
961 | { |
962 | int error; | |
ddc1a35f | 963 | SOCKOPTLEN_T len = sizeof(error); |
97e6ee04 | 964 | |
948c96ef | 965 | m_establishing = false; |
97e6ee04 | 966 | |
f71bb8ef | 967 | getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len); |
97e6ee04 DE |
968 | |
969 | if (error) | |
970 | { | |
51fe4b60 | 971 | OnStateChange(wxSOCKET_LOST); |
97e6ee04 DE |
972 | } |
973 | else | |
974 | { | |
51fe4b60 | 975 | OnStateChange(wxSOCKET_CONNECTION); |
97e6ee04 DE |
976 | /* We have to fire this event by hand because CONNECTION (for clients) |
977 | * and OUTPUT are internally the same and we just disabled CONNECTION | |
978 | * events with the above macro. | |
979 | */ | |
51fe4b60 | 980 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
981 | } |
982 | } | |
983 | else | |
984 | { | |
51fe4b60 | 985 | OnStateChange(wxSOCKET_OUTPUT); |
97e6ee04 DE |
986 | } |
987 | } | |
988 | ||
a9d859df VZ |
989 | void wxSocketImplUnix::OnExceptionWaiting() |
990 | { | |
991 | wxFAIL_MSG( "not supposed to be called" ); | |
992 | } | |
993 | ||
97e6ee04 DE |
994 | /* |
995 | * ------------------------------------------------------------------------- | |
996 | * GAddress | |
997 | * ------------------------------------------------------------------------- | |
998 | */ | |
999 | ||
1000 | /* CHECK_ADDRESS verifies that the current address family is either | |
51fe4b60 VZ |
1001 | * wxSOCKET_NOFAMILY or wxSOCKET_*family*, and if it is wxSOCKET_NOFAMILY, it |
1002 | * initalizes it to be a wxSOCKET_*family*. In other cases, it returns | |
97e6ee04 DE |
1003 | * an appropiate error code. |
1004 | * | |
1005 | * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. | |
1006 | */ | |
1007 | #define CHECK_ADDRESS(address, family) \ | |
1008 | { \ | |
51fe4b60 VZ |
1009 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
1010 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 1011 | return address->m_error; \ |
51fe4b60 | 1012 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 1013 | { \ |
51fe4b60 VZ |
1014 | address->m_error = wxSOCKET_INVADDR; \ |
1015 | return wxSOCKET_INVADDR; \ | |
97e6ee04 DE |
1016 | } \ |
1017 | } | |
1018 | ||
1019 | #define CHECK_ADDRESS_RETVAL(address, family, retval) \ | |
1020 | { \ | |
51fe4b60 VZ |
1021 | if (address->m_family == wxSOCKET_NOFAMILY) \ |
1022 | if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \ | |
97e6ee04 | 1023 | return retval; \ |
51fe4b60 | 1024 | if (address->m_family != wxSOCKET_##family) \ |
97e6ee04 | 1025 | { \ |
51fe4b60 | 1026 | address->m_error = wxSOCKET_INVADDR; \ |
97e6ee04 DE |
1027 | return retval; \ |
1028 | } \ | |
1029 | } | |
1030 | ||
1031 | ||
1032 | GAddress *GAddress_new(void) | |
1033 | { | |
1034 | GAddress *address; | |
1035 | ||
1036 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1037 | return NULL; | |
1038 | ||
51fe4b60 | 1039 | address->m_family = wxSOCKET_NOFAMILY; |
97e6ee04 DE |
1040 | address->m_addr = NULL; |
1041 | address->m_len = 0; | |
1042 | ||
1043 | return address; | |
1044 | } | |
1045 | ||
1046 | GAddress *GAddress_copy(GAddress *address) | |
1047 | { | |
1048 | GAddress *addr2; | |
1049 | ||
1050 | assert(address != NULL); | |
1051 | ||
1052 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1053 | return NULL; | |
1054 | ||
1055 | memcpy(addr2, address, sizeof(GAddress)); | |
1056 | ||
1057 | if (address->m_addr && address->m_len > 0) | |
1058 | { | |
1059 | addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len); | |
1060 | if (addr2->m_addr == NULL) | |
1061 | { | |
1062 | free(addr2); | |
1063 | return NULL; | |
1064 | } | |
1065 | memcpy(addr2->m_addr, address->m_addr, addr2->m_len); | |
1066 | } | |
1067 | ||
1068 | return addr2; | |
1069 | } | |
1070 | ||
1071 | void GAddress_destroy(GAddress *address) | |
1072 | { | |
1073 | assert(address != NULL); | |
1074 | ||
1075 | if (address->m_addr) | |
1076 | free(address->m_addr); | |
1077 | ||
1078 | free(address); | |
1079 | } | |
1080 | ||
1081 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1082 | { | |
1083 | assert(address != NULL); | |
1084 | ||
1085 | address->m_family = type; | |
1086 | } | |
1087 | ||
1088 | GAddressType GAddress_GetFamily(GAddress *address) | |
1089 | { | |
1090 | assert(address != NULL); | |
1091 | ||
1092 | return address->m_family; | |
1093 | } | |
1094 | ||
51fe4b60 | 1095 | wxSocketError _GAddress_translate_from(GAddress *address, |
97e6ee04 DE |
1096 | struct sockaddr *addr, int len) |
1097 | { | |
1098 | address->m_realfamily = addr->sa_family; | |
1099 | switch (addr->sa_family) | |
1100 | { | |
1101 | case AF_INET: | |
51fe4b60 | 1102 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
1103 | break; |
1104 | case AF_UNIX: | |
51fe4b60 | 1105 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 | 1106 | break; |
8575ff50 | 1107 | #if wxUSE_IPV6 |
97e6ee04 | 1108 | case AF_INET6: |
51fe4b60 | 1109 | address->m_family = wxSOCKET_INET6; |
97e6ee04 | 1110 | break; |
8575ff50 | 1111 | #endif // wxUSE_IPV6 |
97e6ee04 DE |
1112 | default: |
1113 | { | |
51fe4b60 VZ |
1114 | address->m_error = wxSOCKET_INVOP; |
1115 | return wxSOCKET_INVOP; | |
97e6ee04 DE |
1116 | } |
1117 | } | |
1118 | ||
1119 | if (address->m_addr) | |
1120 | free(address->m_addr); | |
1121 | ||
1122 | address->m_len = len; | |
1123 | address->m_addr = (struct sockaddr *)malloc(len); | |
1124 | ||
1125 | if (address->m_addr == NULL) | |
1126 | { | |
51fe4b60 VZ |
1127 | address->m_error = wxSOCKET_MEMERR; |
1128 | return wxSOCKET_MEMERR; | |
97e6ee04 | 1129 | } |
1aa7b427 | 1130 | |
97e6ee04 DE |
1131 | memcpy(address->m_addr, addr, len); |
1132 | ||
51fe4b60 | 1133 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1134 | } |
1135 | ||
51fe4b60 | 1136 | wxSocketError _GAddress_translate_to(GAddress *address, |
97e6ee04 DE |
1137 | struct sockaddr **addr, int *len) |
1138 | { | |
1139 | if (!address->m_addr) | |
1140 | { | |
51fe4b60 VZ |
1141 | address->m_error = wxSOCKET_INVADDR; |
1142 | return wxSOCKET_INVADDR; | |
97e6ee04 DE |
1143 | } |
1144 | ||
1145 | *len = address->m_len; | |
1146 | *addr = (struct sockaddr *)malloc(address->m_len); | |
1147 | if (*addr == NULL) | |
1148 | { | |
51fe4b60 VZ |
1149 | address->m_error = wxSOCKET_MEMERR; |
1150 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1151 | } |
1152 | ||
1153 | memcpy(*addr, address->m_addr, address->m_len); | |
51fe4b60 | 1154 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1155 | } |
1156 | ||
1157 | /* | |
1158 | * ------------------------------------------------------------------------- | |
1159 | * Internet address family | |
1160 | * ------------------------------------------------------------------------- | |
1161 | */ | |
1162 | ||
51fe4b60 | 1163 | wxSocketError _GAddress_Init_INET(GAddress *address) |
97e6ee04 DE |
1164 | { |
1165 | address->m_len = sizeof(struct sockaddr_in); | |
1166 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1167 | if (address->m_addr == NULL) | |
1168 | { | |
51fe4b60 VZ |
1169 | address->m_error = wxSOCKET_MEMERR; |
1170 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1171 | } |
1172 | ||
51fe4b60 | 1173 | address->m_family = wxSOCKET_INET; |
97e6ee04 DE |
1174 | address->m_realfamily = PF_INET; |
1175 | ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET; | |
1176 | ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY; | |
1177 | ||
51fe4b60 | 1178 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1179 | } |
1180 | ||
51fe4b60 | 1181 | wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) |
97e6ee04 DE |
1182 | { |
1183 | struct hostent *he; | |
1184 | struct in_addr *addr; | |
1185 | ||
1186 | assert(address != NULL); | |
1187 | ||
1188 | CHECK_ADDRESS(address, INET); | |
1189 | ||
1190 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
1191 | ||
1192 | /* If it is a numeric host name, convert it now */ | |
1193 | #if defined(HAVE_INET_ATON) | |
1194 | if (inet_aton(hostname, addr) == 0) | |
1195 | { | |
1196 | #elif defined(HAVE_INET_ADDR) | |
8c0f8906 | 1197 | if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 ) |
97e6ee04 DE |
1198 | { |
1199 | #else | |
1200 | /* Use gethostbyname by default */ | |
9d715960 | 1201 | #ifndef __WXMAC__ |
ba2a81d7 | 1202 | int val = 1; /* VA doesn't like constants in conditional expressions */ |
97e6ee04 | 1203 | if (val) |
9d715960 | 1204 | #endif |
97e6ee04 DE |
1205 | { |
1206 | #endif | |
1207 | struct in_addr *array_addr; | |
1208 | ||
1209 | /* It is a real name, we solve it */ | |
127189eb SN |
1210 | struct hostent h; |
1211 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
1212 | struct hostent_data buffer; | |
1213 | #else | |
1214 | char buffer[1024]; | |
1215 | #endif | |
1216 | int err; | |
1217 | he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err); | |
1218 | if (he == NULL) | |
97e6ee04 DE |
1219 | { |
1220 | /* Reset to invalid address */ | |
1221 | addr->s_addr = INADDR_NONE; | |
51fe4b60 VZ |
1222 | address->m_error = wxSOCKET_NOHOST; |
1223 | return wxSOCKET_NOHOST; | |
97e6ee04 | 1224 | } |
1aa7b427 | 1225 | |
97e6ee04 DE |
1226 | array_addr = (struct in_addr *) *(he->h_addr_list); |
1227 | addr->s_addr = array_addr[0].s_addr; | |
1228 | } | |
1aa7b427 | 1229 | |
51fe4b60 | 1230 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1231 | } |
1232 | ||
60edcf45 | 1233 | |
51fe4b60 | 1234 | wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address) |
60edcf45 VZ |
1235 | { |
1236 | return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST); | |
1237 | } | |
1238 | ||
51fe4b60 | 1239 | wxSocketError GAddress_INET_SetAnyAddress(GAddress *address) |
97e6ee04 DE |
1240 | { |
1241 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1242 | } | |
1243 | ||
51fe4b60 | 1244 | wxSocketError GAddress_INET_SetHostAddress(GAddress *address, |
97e6ee04 DE |
1245 | unsigned long hostaddr) |
1246 | { | |
1247 | struct in_addr *addr; | |
1248 | ||
1249 | assert(address != NULL); | |
1250 | ||
1251 | CHECK_ADDRESS(address, INET); | |
1252 | ||
1253 | addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr); | |
9d715960 | 1254 | addr->s_addr = htonl(hostaddr); |
97e6ee04 | 1255 | |
51fe4b60 | 1256 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1257 | } |
1258 | ||
51fe4b60 | 1259 | wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, |
97e6ee04 DE |
1260 | const char *protocol) |
1261 | { | |
1262 | struct servent *se; | |
1263 | struct sockaddr_in *addr; | |
1264 | ||
1265 | assert(address != NULL); | |
1266 | CHECK_ADDRESS(address, INET); | |
1267 | ||
1268 | if (!port) | |
1269 | { | |
51fe4b60 VZ |
1270 | address->m_error = wxSOCKET_INVPORT; |
1271 | return wxSOCKET_INVPORT; | |
97e6ee04 | 1272 | } |
b082b524 | 1273 | |
127189eb SN |
1274 | #if defined(HAVE_FUNC_GETSERVBYNAME_R_4) |
1275 | struct servent_data buffer; | |
f6bc1c74 | 1276 | #else |
127189eb | 1277 | char buffer[1024]; |
f6bc1c74 | 1278 | #endif |
127189eb SN |
1279 | struct servent serv; |
1280 | se = wxGetservbyname_r(port, protocol, &serv, | |
1281 | (void*)&buffer, sizeof(buffer)); | |
97e6ee04 DE |
1282 | if (!se) |
1283 | { | |
1284 | /* the cast to int suppresses compiler warnings about subscript having the | |
1285 | type char */ | |
1286 | if (isdigit((int)port[0])) | |
1287 | { | |
1288 | int port_int; | |
1289 | ||
1290 | port_int = atoi(port); | |
1291 | addr = (struct sockaddr_in *)address->m_addr; | |
1292 | addr->sin_port = htons(port_int); | |
51fe4b60 | 1293 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1294 | } |
1295 | ||
51fe4b60 VZ |
1296 | address->m_error = wxSOCKET_INVPORT; |
1297 | return wxSOCKET_INVPORT; | |
97e6ee04 DE |
1298 | } |
1299 | ||
1300 | addr = (struct sockaddr_in *)address->m_addr; | |
1301 | addr->sin_port = se->s_port; | |
1302 | ||
51fe4b60 | 1303 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1304 | } |
1305 | ||
51fe4b60 | 1306 | wxSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) |
97e6ee04 DE |
1307 | { |
1308 | struct sockaddr_in *addr; | |
1309 | ||
1310 | assert(address != NULL); | |
1311 | CHECK_ADDRESS(address, INET); | |
b082b524 | 1312 | |
97e6ee04 DE |
1313 | addr = (struct sockaddr_in *)address->m_addr; |
1314 | addr->sin_port = htons(port); | |
1315 | ||
51fe4b60 | 1316 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1317 | } |
1318 | ||
51fe4b60 | 1319 | wxSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
97e6ee04 DE |
1320 | { |
1321 | struct hostent *he; | |
1322 | char *addr_buf; | |
1323 | struct sockaddr_in *addr; | |
1324 | ||
b082b524 | 1325 | assert(address != NULL); |
97e6ee04 DE |
1326 | CHECK_ADDRESS(address, INET); |
1327 | ||
1328 | addr = (struct sockaddr_in *)address->m_addr; | |
1329 | addr_buf = (char *)&(addr->sin_addr); | |
1330 | ||
127189eb SN |
1331 | struct hostent temphost; |
1332 | #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) | |
1333 | struct hostent_data buffer; | |
1334 | #else | |
1335 | char buffer[1024]; | |
1336 | #endif | |
1337 | int err; | |
1338 | he = wxGethostbyaddr_r(addr_buf, sizeof(addr->sin_addr), AF_INET, &temphost, | |
1339 | (void*)&buffer, sizeof(buffer), &err); | |
97e6ee04 DE |
1340 | if (he == NULL) |
1341 | { | |
51fe4b60 VZ |
1342 | address->m_error = wxSOCKET_NOHOST; |
1343 | return wxSOCKET_NOHOST; | |
97e6ee04 DE |
1344 | } |
1345 | ||
1346 | strncpy(hostname, he->h_name, sbuf); | |
1347 | ||
51fe4b60 | 1348 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1349 | } |
1350 | ||
1351 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1352 | { | |
1353 | struct sockaddr_in *addr; | |
1354 | ||
b082b524 DE |
1355 | assert(address != NULL); |
1356 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1357 | |
1358 | addr = (struct sockaddr_in *)address->m_addr; | |
1359 | ||
9d715960 | 1360 | return ntohl(addr->sin_addr.s_addr); |
97e6ee04 DE |
1361 | } |
1362 | ||
1363 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1364 | { | |
1365 | struct sockaddr_in *addr; | |
1366 | ||
b082b524 DE |
1367 | assert(address != NULL); |
1368 | CHECK_ADDRESS_RETVAL(address, INET, 0); | |
97e6ee04 DE |
1369 | |
1370 | addr = (struct sockaddr_in *)address->m_addr; | |
1371 | return ntohs(addr->sin_port); | |
1372 | } | |
1373 | ||
8575ff50 VZ |
1374 | #if wxUSE_IPV6 |
1375 | /* | |
1376 | * ------------------------------------------------------------------------- | |
1377 | * Internet IPv6 address family | |
1378 | * ------------------------------------------------------------------------- | |
1379 | */ | |
1380 | ||
51fe4b60 | 1381 | wxSocketError _GAddress_Init_INET6(GAddress *address) |
8575ff50 VZ |
1382 | { |
1383 | struct in6_addr any_address = IN6ADDR_ANY_INIT; | |
1384 | address->m_len = sizeof(struct sockaddr_in6); | |
1385 | address->m_addr = (struct sockaddr *) malloc(address->m_len); | |
1386 | if (address->m_addr == NULL) | |
1387 | { | |
51fe4b60 VZ |
1388 | address->m_error = wxSOCKET_MEMERR; |
1389 | return wxSOCKET_MEMERR; | |
8575ff50 VZ |
1390 | } |
1391 | memset(address->m_addr,0,address->m_len); | |
1392 | ||
51fe4b60 | 1393 | address->m_family = wxSOCKET_INET6; |
8575ff50 VZ |
1394 | address->m_realfamily = AF_INET6; |
1395 | ((struct sockaddr_in6 *)address->m_addr)->sin6_family = AF_INET6; | |
1396 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = any_address; | |
1397 | ||
51fe4b60 | 1398 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1399 | } |
1400 | ||
51fe4b60 | 1401 | wxSocketError GAddress_INET6_SetHostName(GAddress *address, const char *hostname) |
8575ff50 VZ |
1402 | { |
1403 | assert(address != NULL); | |
1404 | CHECK_ADDRESS(address, INET6); | |
1405 | ||
1406 | addrinfo hints; | |
1407 | memset( & hints, 0, sizeof( hints ) ); | |
1408 | hints.ai_family = AF_INET6; | |
1409 | addrinfo * info = 0; | |
1410 | if ( getaddrinfo( hostname, "0", & hints, & info ) || ! info ) | |
1411 | { | |
51fe4b60 VZ |
1412 | address->m_error = wxSOCKET_NOHOST; |
1413 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
1414 | } |
1415 | ||
1416 | memcpy( address->m_addr, info->ai_addr, info->ai_addrlen ); | |
1417 | freeaddrinfo( info ); | |
51fe4b60 | 1418 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1419 | } |
1420 | ||
51fe4b60 | 1421 | wxSocketError GAddress_INET6_SetAnyAddress(GAddress *address) |
8575ff50 VZ |
1422 | { |
1423 | assert(address != NULL); | |
1424 | ||
1425 | CHECK_ADDRESS(address, INET6); | |
1426 | ||
1427 | struct in6_addr addr; | |
1428 | memset( & addr, 0, sizeof( addr ) ); | |
1429 | return GAddress_INET6_SetHostAddress(address, addr); | |
1430 | } | |
51fe4b60 | 1431 | wxSocketError GAddress_INET6_SetHostAddress(GAddress *address, |
8575ff50 VZ |
1432 | struct in6_addr hostaddr) |
1433 | { | |
1434 | assert(address != NULL); | |
1435 | ||
1436 | CHECK_ADDRESS(address, INET6); | |
1437 | ||
1438 | ((struct sockaddr_in6 *)address->m_addr)->sin6_addr = hostaddr; | |
1439 | ||
51fe4b60 | 1440 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1441 | } |
1442 | ||
51fe4b60 | 1443 | wxSocketError GAddress_INET6_SetPortName(GAddress *address, const char *port, |
8575ff50 VZ |
1444 | const char *protocol) |
1445 | { | |
1446 | struct servent *se; | |
1447 | struct sockaddr_in6 *addr; | |
1448 | ||
1449 | assert(address != NULL); | |
1450 | CHECK_ADDRESS(address, INET6); | |
1451 | ||
1452 | if (!port) | |
1453 | { | |
51fe4b60 VZ |
1454 | address->m_error = wxSOCKET_INVPORT; |
1455 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1456 | } |
1457 | ||
1458 | se = getservbyname(port, protocol); | |
1459 | if (!se) | |
1460 | { | |
1461 | if (isdigit(port[0])) | |
1462 | { | |
1463 | int port_int; | |
1464 | ||
1465 | port_int = atoi(port); | |
1466 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1467 | addr->sin6_port = htons((u_short) port_int); | |
51fe4b60 | 1468 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1469 | } |
1470 | ||
51fe4b60 VZ |
1471 | address->m_error = wxSOCKET_INVPORT; |
1472 | return wxSOCKET_INVPORT; | |
8575ff50 VZ |
1473 | } |
1474 | ||
1475 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1476 | addr->sin6_port = se->s_port; | |
1477 | ||
51fe4b60 | 1478 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1479 | } |
1480 | ||
51fe4b60 | 1481 | wxSocketError GAddress_INET6_SetPort(GAddress *address, unsigned short port) |
8575ff50 VZ |
1482 | { |
1483 | struct sockaddr_in6 *addr; | |
1484 | ||
1485 | assert(address != NULL); | |
1486 | CHECK_ADDRESS(address, INET6); | |
1487 | ||
1488 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1489 | addr->sin6_port = htons(port); | |
1490 | ||
51fe4b60 | 1491 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1492 | } |
1493 | ||
51fe4b60 | 1494 | wxSocketError GAddress_INET6_GetHostName(GAddress *address, char *hostname, size_t sbuf) |
8575ff50 VZ |
1495 | { |
1496 | struct hostent *he; | |
1497 | char *addr_buf; | |
1498 | struct sockaddr_in6 *addr; | |
1499 | ||
1500 | assert(address != NULL); | |
1501 | CHECK_ADDRESS(address, INET6); | |
1502 | ||
1503 | addr = (struct sockaddr_in6 *)address->m_addr; | |
1504 | addr_buf = (char *)&(addr->sin6_addr); | |
1505 | ||
1506 | he = gethostbyaddr(addr_buf, sizeof(addr->sin6_addr), AF_INET6); | |
1507 | if (he == NULL) | |
1508 | { | |
51fe4b60 VZ |
1509 | address->m_error = wxSOCKET_NOHOST; |
1510 | return wxSOCKET_NOHOST; | |
8575ff50 VZ |
1511 | } |
1512 | ||
1513 | strncpy(hostname, he->h_name, sbuf); | |
1514 | ||
51fe4b60 | 1515 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1516 | } |
1517 | ||
51fe4b60 | 1518 | wxSocketError GAddress_INET6_GetHostAddress(GAddress *address,struct in6_addr *hostaddr) |
8575ff50 VZ |
1519 | { |
1520 | assert(address != NULL); | |
1521 | assert(hostaddr != NULL); | |
51fe4b60 | 1522 | CHECK_ADDRESS_RETVAL(address, INET6, wxSOCKET_INVADDR); |
8575ff50 | 1523 | *hostaddr = ( (struct sockaddr_in6 *)address->m_addr )->sin6_addr; |
51fe4b60 | 1524 | return wxSOCKET_NOERROR; |
8575ff50 VZ |
1525 | } |
1526 | ||
1527 | unsigned short GAddress_INET6_GetPort(GAddress *address) | |
1528 | { | |
1529 | assert(address != NULL); | |
1530 | CHECK_ADDRESS_RETVAL(address, INET6, 0); | |
1531 | ||
1532 | return ntohs( ((struct sockaddr_in6 *)address->m_addr)->sin6_port ); | |
1533 | } | |
1534 | ||
1535 | #endif // wxUSE_IPV6 | |
1536 | ||
97e6ee04 DE |
1537 | /* |
1538 | * ------------------------------------------------------------------------- | |
1539 | * Unix address family | |
1540 | * ------------------------------------------------------------------------- | |
1541 | */ | |
1542 | ||
1543 | #ifndef __VISAGECPP__ | |
51fe4b60 | 1544 | wxSocketError _GAddress_Init_UNIX(GAddress *address) |
97e6ee04 DE |
1545 | { |
1546 | address->m_len = sizeof(struct sockaddr_un); | |
1547 | address->m_addr = (struct sockaddr *)malloc(address->m_len); | |
1548 | if (address->m_addr == NULL) | |
1549 | { | |
51fe4b60 VZ |
1550 | address->m_error = wxSOCKET_MEMERR; |
1551 | return wxSOCKET_MEMERR; | |
97e6ee04 DE |
1552 | } |
1553 | ||
51fe4b60 | 1554 | address->m_family = wxSOCKET_UNIX; |
97e6ee04 DE |
1555 | address->m_realfamily = PF_UNIX; |
1556 | ((struct sockaddr_un *)address->m_addr)->sun_family = AF_UNIX; | |
1557 | ((struct sockaddr_un *)address->m_addr)->sun_path[0] = 0; | |
1558 | ||
51fe4b60 | 1559 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1560 | } |
1561 | ||
1562 | #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) | |
1563 | ||
51fe4b60 | 1564 | wxSocketError GAddress_UNIX_SetPath(GAddress *address, const char *path) |
97e6ee04 DE |
1565 | { |
1566 | struct sockaddr_un *addr; | |
1567 | ||
b082b524 | 1568 | assert(address != NULL); |
97e6ee04 | 1569 | |
b082b524 | 1570 | CHECK_ADDRESS(address, UNIX); |
97e6ee04 DE |
1571 | |
1572 | addr = ((struct sockaddr_un *)address->m_addr); | |
1573 | strncpy(addr->sun_path, path, UNIX_SOCK_PATHLEN); | |
1574 | addr->sun_path[UNIX_SOCK_PATHLEN - 1] = '\0'; | |
1575 | ||
51fe4b60 | 1576 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1577 | } |
1578 | ||
51fe4b60 | 1579 | wxSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf) |
97e6ee04 DE |
1580 | { |
1581 | struct sockaddr_un *addr; | |
1582 | ||
1583 | assert(address != NULL); | |
1584 | CHECK_ADDRESS(address, UNIX); | |
1585 | ||
1586 | addr = (struct sockaddr_un *)address->m_addr; | |
1587 | ||
1588 | strncpy(path, addr->sun_path, sbuf); | |
1589 | ||
51fe4b60 | 1590 | return wxSOCKET_NOERROR; |
97e6ee04 DE |
1591 | } |
1592 | #endif /* !defined(__VISAGECPP__) */ | |
54cb21d6 | 1593 | |
ebf94940 | 1594 | #endif /* wxUSE_SOCKETS */ |