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