1 /* ------------------------------------------------------------------------- 
   2  * Project:     GSocket (Generic Socket) for WX 
   4  * Copyright:   (c) Guilhem Lavaux 
   5  * Licence:     wxWindows Licence 
   6  * Authors:     David Elliott (C++ conversion, maintainer) 
   8  *              Guillermo Rodriguez Garcia <guille@iies.es> 
   9  * Purpose:     GSocket main Unix and OS/2 file 
  10  * Licence:     The wxWindows licence 
  12  * ------------------------------------------------------------------------- 
  15 #if defined(__WATCOMC__) 
  16 #include "wx/wxprec.h" 
  21 #ifndef __GSOCKET_STANDALONE__ 
  25 #if defined(__VISAGECPP__) 
  26 #define BSD_SELECT /* use Berkeley Sockets select */ 
  29 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) 
  32 #include <sys/types.h> 
  37 #include <netinet/in.h> 
  40 #include <sys/ioctl.h> 
  42 #ifdef HAVE_SYS_SELECT_H 
  43 #   include <sys/select.h> 
  50     u_char  sun_len
;        /* sockaddr len including null */ 
  51     u_char  sun_family
;     /* AF_UNIX */ 
  52     char    sun_path
[108];  /* path name (gag) */ 
  55 #include <sys/socket.h> 
  61 #include <netinet/in.h> 
  62 #include <arpa/inet.h> 
  69 #include <machine/endian.h> 
  75 #define EBADF   SOCEBADF 
  81 #include <sys/socket.h> 
  82 #include <sys/ioctl.h> 
  83 #include <sys/select.h> 
  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, 
  92 int _System 
soclose(int); 
  96 #include <sys/select.h> 
 104 #  include <sys/filio.h> 
 107 #  include <bstring.h> 
 110 #  include <strings.h> 
 117 #  define WX_SOCKLEN_T unsigned int 
 121 #      define WX_SOCKLEN_T socklen_t 
 123 #  elif defined(__WXMAC__) 
 124 #    define WX_SOCKLEN_T socklen_t 
 126 #    define WX_SOCKLEN_T int 
 130 #endif /* SOCKLEN_T */ 
 133 #define SOCKOPTLEN_T WX_SOCKLEN_T 
 137  * MSW defines this, Unices don't. 
 139 #ifndef INVALID_SOCKET 
 140 #define INVALID_SOCKET -1 
 143 /* UnixWare reportedly needs this for FIONBIO definition */ 
 145 #include <sys/filio.h> 
 149  * INADDR_BROADCAST is identical to INADDR_NONE which is not defined 
 150  * on all systems. INADDR_BROADCAST should be fine to indicate an error. 
 153 #define INADDR_NONE INADDR_BROADCAST 
 156 #if defined(__VISAGECPP__) || defined(__WATCOMC__) 
 158     #define MASK_SIGNAL() { 
 159     #define UNMASK_SIGNAL() } 
 162     extern "C" { typedef void (*wxSigHandler
)(int); } 
 164     #define MASK_SIGNAL()                       \ 
 166         wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN); 
 168     #define UNMASK_SIGNAL()                     \ 
 169         signal(SIGPIPE, old_handler);           \ 
 174 /* If a SIGPIPE is issued by a socket call on a remotely closed socket, 
 175    the program will "crash" unless it explicitly handles the SIGPIPE. 
 176    By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will 
 177    use SO_NOSIGPIPE (if available), the BSD equivalent. */ 
 179 #  define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL 
 180 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */ 
 181 #  define GSOCKET_MSG_NOSIGNAL 0 
 182 #endif /* MSG_NOSIGNAL */ 
 184 #ifndef __GSOCKET_STANDALONE__ 
 185 #  include "wx/unix/gsockunx.h" 
 186 #  include "wx/unix/private.h" 
 187 #  include "wx/gsocket.h" 
 188 #if wxUSE_THREADS && (defined(HAVE_GETHOSTBYNAME) || defined(HAVE_GETSERVBYNAME)) 
 189 #  include "wx/thread.h" 
 192 #  include "gsockunx.h" 
 193 #  include "gsocket.h" 
 197 #endif /* __GSOCKET_STANDALONE__ */ 
 199 #if defined(HAVE_GETHOSTBYNAME) 
 200 static struct hostent 
* deepCopyHostent(struct hostent 
*h
, 
 201                                         const struct hostent 
*he
, 
 202                                         char *buffer
, int size
, int *err
) 
 204   memcpy(h
, he
, sizeof(struct hostent
)); 
 205   int len 
= strlen(h
->h_name
); 
 208   memcpy(buffer
, h
->h_name
, len
); 
 214   for (char **p 
= h
->h_addr_list
; *p 
!= 0; p
++) { 
 219     memcpy(buffer
, *p
, len
); 
 224   for (char **q 
= h
->h_aliases
; size 
> 0 && *q 
!= 0; q
++){ 
 228     memcpy(buffer
, *q
, len
); 
 238 struct hostent 
* wxGethostbyname_r(const char *hostname
, struct hostent 
*h
, 
 239                                    void *buffer
, int size
, int *err
) 
 242   struct hostent 
*he 
= NULL
; 
 244 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) 
 245   if (gethostbyname_r(hostname
, h
, (char*)buffer
, size
, &he
, err
)) 
 247 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) 
 248   he 
= gethostbyname_r(hostname
, h
, (char*)buffer
, size
, err
); 
 249 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) 
 250   if (gethostbyname_r(hostname
, h
, (struct hostent_data
*) buffer
)) 
 257 #elif defined(HAVE_GETHOSTBYNAME) 
 259   static wxMutex nameLock
; 
 260   wxMutexLocker 
locker(nameLock
); 
 262   he 
= gethostbyname(hostname
); 
 266     he 
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
); 
 271 struct hostent 
* wxGethostbyaddr_r(const char *addr_buf
, int buf_size
, 
 272                                    int proto
, struct hostent 
*h
, 
 273                                    void *buffer
, int size
, int *err
) 
 275   struct hostent 
*he 
= NULL
; 
 277 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_6) 
 278   if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, 
 279                       (char*)buffer
, size
, &he
, err
)) 
 281 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) 
 282   he 
= gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, (char*)buffer
, size
, err
); 
 283 #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3) 
 284   if (gethostbyaddr_r(addr_buf
, buf_size
, proto
, h
, 
 285                         (struct hostent_data
*) buffer
)) 
 292 #elif defined(HAVE_GETHOSTBYNAME) 
 294   static wxMutex addrLock
; 
 295   wxMutexLocker 
locker(addrLock
); 
 297   he 
= gethostbyaddr(addr_buf
, buf_size
, proto
); 
 301     he 
= deepCopyHostent(h
, he
, (char*)buffer
, size
, err
); 
 306 #if defined(HAVE_GETSERVBYNAME) 
 307 static struct servent 
* deepCopyServent(struct servent 
*s
, 
 308                                         const struct servent 
*se
, 
 309                                         char *buffer
, int size
) 
 311   memcpy(s
, se
, sizeof(struct servent
)); 
 312   int len 
= strlen(s
->s_name
); 
 315   memcpy(buffer
, s
->s_name
, len
); 
 320   len 
= strlen(s
->s_proto
); 
 323   memcpy(buffer
, s
->s_proto
, len
); 
 328   for (char **q 
= s
->s_aliases
; size 
> 0 && *q 
!= 0; q
++){ 
 332     memcpy(buffer
, *q
, len
); 
 342 struct servent 
*wxGetservbyname_r(const char *port
, const char *protocol
, 
 343                                   struct servent 
*serv
, void *buffer
, int size
) 
 345   struct servent 
*se 
= NULL
; 
 346 #if defined(HAVE_FUNC_GETSERVBYNAME_R_6) 
 347   if (getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
, &se
)) 
 349 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_5) 
 350   se 
= getservbyname_r(port
, protocol
, serv
, (char*)buffer
, size
); 
 351 #elif defined(HAVE_FUNC_GETSERVBYNAME_R_4) 
 352   if (getservbyname_r(port
, protocol
, serv
, (struct servent_data
*) buffer
)) 
 356 #elif defined(HAVE_GETSERVBYNAME) 
 358   static wxMutex servLock
; 
 359   wxMutexLocker 
locker(servLock
); 
 361   se 
= getservbyname(port
, protocol
); 
 363     se 
= deepCopyServent(serv
, se
, (char*)buffer
, size
); 
 368 /* debugging helpers */ 
 369 #ifdef __GSOCKET_DEBUG__ 
 370 #  define GSocket_Debug(args) printf args 
 372 #  define GSocket_Debug(args) 
 373 #endif /* __GSOCKET_DEBUG__ */ 
 375 /* Table of GUI-related functions. We must call them indirectly because 
 376  * of wxBase and GUI separation: */ 
 378 static GSocketGUIFunctionsTable 
*gs_gui_functions
; 
 380 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
 
 383     virtual bool OnInit(); 
 384     virtual void OnExit(); 
 385     virtual bool CanUseEventLoop(); 
 386     virtual bool Init_Socket(GSocket 
*socket
); 
 387     virtual void Destroy_Socket(GSocket 
*socket
); 
 388     virtual void Install_Callback(GSocket 
*socket
, GSocketEvent event
); 
 389     virtual void Uninstall_Callback(GSocket 
*socket
, GSocketEvent event
); 
 390     virtual void Enable_Events(GSocket 
*socket
); 
 391     virtual void Disable_Events(GSocket 
*socket
); 
 394 bool GSocketGUIFunctionsTableNull::OnInit() 
 396 void GSocketGUIFunctionsTableNull::OnExit() 
 398 bool GSocketGUIFunctionsTableNull::CanUseEventLoop() 
 400 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket 
*WXUNUSED(socket
)) 
 402 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket 
*WXUNUSED(socket
)) 
 404 void GSocketGUIFunctionsTableNull::Install_Callback(GSocket 
*WXUNUSED(socket
), GSocketEvent 
WXUNUSED(event
)) 
 406 void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket 
*WXUNUSED(socket
), GSocketEvent 
WXUNUSED(event
)) 
 408 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket 
*WXUNUSED(socket
)) 
 410 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket 
*WXUNUSED(socket
)) 
 412 /* Global initialisers */ 
 414 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable 
*guifunc
) 
 416   gs_gui_functions 
= guifunc
; 
 419 int GSocket_Init(void) 
 421   if (!gs_gui_functions
) 
 423     static GSocketGUIFunctionsTableNull table
; 
 424     gs_gui_functions 
= &table
; 
 426   if ( !gs_gui_functions
->OnInit() ) 
 431 void GSocket_Cleanup(void) 
 433   if (gs_gui_functions
) 
 435       gs_gui_functions
->OnExit(); 
 439 /* Constructors / Destructors for GSocket */ 
 445   m_fd                  
= INVALID_SOCKET
; 
 446   for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) 
 453   m_error               
= GSOCK_NOERROR
; 
 456   m_gui_dependent       
= NULL
; 
 457   m_non_blocking        
= false; 
 459   m_timeout             
= 10*60*1000; 
 460                                 /* 10 minutes * 60 sec * 1000 millisec */ 
 461   m_establishing        
= false; 
 463   assert(gs_gui_functions
); 
 464   /* Per-socket GUI-specific initialization */ 
 465   m_ok 
= gs_gui_functions
->Init_Socket(this); 
 468 void GSocket::Close() 
 470     gs_gui_functions
->Disable_Events(this); 
 471     /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */ 
 472 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))) 
 475     m_fd 
= INVALID_SOCKET
; 
 482   /* Check that the socket is really shutdowned */ 
 483   if (m_fd 
!= INVALID_SOCKET
) 
 486   /* Per-socket GUI-specific cleanup */ 
 487   gs_gui_functions
->Destroy_Socket(this); 
 489   /* Destroy private addresses */ 
 491     GAddress_destroy(m_local
); 
 494     GAddress_destroy(m_peer
); 
 498  *  Disallow further read/write operations on this socket, close 
 499  *  the fd and disable all callbacks. 
 501 void GSocket::Shutdown() 
 507   /* Don't allow events to fire after socket has been closed */ 
 508   gs_gui_functions
->Disable_Events(this); 
 510   /* If socket has been created, shutdown it */ 
 511   if (m_fd 
!= INVALID_SOCKET
) 
 517   /* Disable GUI callbacks */ 
 518   for (evt 
= 0; evt 
< GSOCK_MAX_EVENT
; evt
++) 
 519     m_cbacks
[evt
] = NULL
; 
 521   m_detected 
= GSOCK_LOST_FLAG
; 
 524 /* Address handling */ 
 530  *  Set or get the local or peer address for this socket. The 'set' 
 531  *  functions return GSOCK_NOERROR on success, an error code otherwise. 
 532  *  The 'get' functions return a pointer to a GAddress object on success, 
 533  *  or NULL otherwise, in which case they set the error code of the 
 534  *  corresponding GSocket. 
 537  *    GSOCK_INVSOCK - the socket is not valid. 
 538  *    GSOCK_INVADDR - the address is not valid. 
 540 GSocketError 
GSocket::SetLocal(GAddress 
*address
) 
 544   /* the socket must be initialized, or it must be a server */ 
 545   if ((m_fd 
!= INVALID_SOCKET 
&& !m_server
)) 
 547     m_error 
= GSOCK_INVSOCK
; 
 548     return GSOCK_INVSOCK
; 
 552   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 554     m_error 
= GSOCK_INVADDR
; 
 555     return GSOCK_INVADDR
; 
 559     GAddress_destroy(m_local
); 
 561   m_local 
= GAddress_copy(address
); 
 563   return GSOCK_NOERROR
; 
 566 GSocketError 
GSocket::SetPeer(GAddress 
*address
) 
 571   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 573     m_error 
= GSOCK_INVADDR
; 
 574     return GSOCK_INVADDR
; 
 578     GAddress_destroy(m_peer
); 
 580   m_peer 
= GAddress_copy(address
); 
 582   return GSOCK_NOERROR
; 
 585 GAddress 
*GSocket::GetLocal() 
 588   struct sockaddr addr
; 
 589   WX_SOCKLEN_T size 
= sizeof(addr
); 
 594   /* try to get it from the m_local var first */ 
 596     return GAddress_copy(m_local
); 
 598   /* else, if the socket is initialized, try getsockname */ 
 599   if (m_fd 
== INVALID_SOCKET
) 
 601     m_error 
= GSOCK_INVSOCK
; 
 605   if (getsockname(m_fd
, &addr
, (WX_SOCKLEN_T 
*) &size
) < 0) 
 607     m_error 
= GSOCK_IOERR
; 
 611   /* got a valid address from getsockname, create a GAddress object */ 
 612   address 
= GAddress_new(); 
 615     m_error 
= GSOCK_MEMERR
; 
 619   err 
= _GAddress_translate_from(address
, &addr
, size
); 
 620   if (err 
!= GSOCK_NOERROR
) 
 622     GAddress_destroy(address
); 
 630 GAddress 
*GSocket::GetPeer() 
 634   /* try to get it from the m_peer var */ 
 636     return GAddress_copy(m_peer
); 
 641 /* Server specific parts */ 
 643 /* GSocket_SetServer: 
 644  *  Sets up this socket as a server. The local address must have been 
 645  *  set with GSocket_SetLocal() before GSocket_SetServer() is called. 
 646  *  Returns GSOCK_NOERROR on success, one of the following otherwise: 
 649  *    GSOCK_INVSOCK - the socket is in use. 
 650  *    GSOCK_INVADDR - the local address has not been set. 
 651  *    GSOCK_IOERR   - low-level error. 
 653 GSocketError 
GSocket::SetServer() 
 659   /* must not be in use */ 
 660   if (m_fd 
!= INVALID_SOCKET
) 
 662     m_error 
= GSOCK_INVSOCK
; 
 663     return GSOCK_INVSOCK
; 
 666   /* the local addr must have been set */ 
 669     m_error 
= GSOCK_INVADDR
; 
 670     return GSOCK_INVADDR
; 
 673   /* Initialize all fields */ 
 677   /* Create the socket */ 
 678   m_fd 
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0); 
 680   if (m_fd 
== INVALID_SOCKET
) 
 682     m_error 
= GSOCK_IOERR
; 
 686   /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ 
 688   setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
)); 
 691   ioctl(m_fd
, FIONBIO
, &arg
); 
 692   gs_gui_functions
->Enable_Events(this); 
 694   /* allow a socket to re-bind if the socket is in the TIME_WAIT 
 695      state after being previously closed. 
 699     setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
)); 
 701     setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(u_long
)); 
 705   /* Bind to the local address, 
 706    * retrieve the actual address bound, 
 707    * and listen up to 5 connections. 
 709   if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) || 
 712                    (WX_SOCKLEN_T 
*) &m_local
->m_len
) != 0) || 
 713       (listen(m_fd
, 5) != 0)) 
 716     m_error 
= GSOCK_IOERR
; 
 720   return GSOCK_NOERROR
; 
 723 /* GSocket_WaitConnection: 
 724  *  Waits for an incoming client connection. Returns a pointer to 
 725  *  a GSocket object, or NULL if there was an error, in which case 
 726  *  the last error field will be updated for the calling GSocket. 
 728  *  Error codes (set in the calling GSocket) 
 729  *    GSOCK_INVSOCK    - the socket is not valid or not a server. 
 730  *    GSOCK_TIMEDOUT   - timeout, no incoming connections. 
 731  *    GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. 
 732  *    GSOCK_MEMERR     - couldn't allocate memory. 
 733  *    GSOCK_IOERR      - low-level error. 
 735 GSocket 
*GSocket::WaitConnection() 
 737   struct sockaddr from
; 
 738   WX_SOCKLEN_T fromlen 
= sizeof(from
); 
 745   /* If the socket has already been created, we exit immediately */ 
 746   if (m_fd 
== INVALID_SOCKET 
|| !m_server
) 
 748     m_error 
= GSOCK_INVSOCK
; 
 752   /* Create a GSocket object for the new connection */ 
 753   connection 
= GSocket_new(); 
 757     m_error 
= GSOCK_MEMERR
; 
 761   /* Wait for a connection (with timeout) */ 
 762   if (Input_Timeout() == GSOCK_TIMEDOUT
) 
 765     /* m_error set by _GSocket_Input_Timeout */ 
 769   connection
->m_fd 
= accept(m_fd
, &from
, (WX_SOCKLEN_T 
*) &fromlen
); 
 771   /* Reenable CONNECTION events */ 
 772   Enable(GSOCK_CONNECTION
); 
 774   if (connection
->m_fd 
== INVALID_SOCKET
) 
 776     if (errno 
== EWOULDBLOCK
) 
 777       m_error 
= GSOCK_WOULDBLOCK
; 
 779       m_error 
= GSOCK_IOERR
; 
 785   /* Initialize all fields */ 
 786   connection
->m_server   
= false; 
 787   connection
->m_stream   
= true; 
 789   /* Setup the peer address field */ 
 790   connection
->m_peer 
= GAddress_new(); 
 791   if (!connection
->m_peer
) 
 794     m_error 
= GSOCK_MEMERR
; 
 798   err 
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
); 
 799   if (err 
!= GSOCK_NOERROR
) 
 806 #if defined(__EMX__) || defined(__VISAGECPP__) 
 807   ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
 809   ioctl(connection
->m_fd
, FIONBIO
, &arg
); 
 811   gs_gui_functions
->Enable_Events(connection
); 
 816 bool GSocket::SetReusable() 
 818     /* socket must not be null, and must not be in use/already bound */ 
 819     if (this && m_fd 
== INVALID_SOCKET
) 
 829 /* Client specific parts */ 
 832  *  For stream (connection oriented) sockets, GSocket_Connect() tries 
 833  *  to establish a client connection to a server using the peer address 
 834  *  as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the 
 835  *  connection has been successfully established, or one of the error 
 836  *  codes listed below. Note that for nonblocking sockets, a return 
 837  *  value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection 
 838  *  request can be completed later; you should use GSocket_Select() 
 839  *  to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the 
 840  *  corresponding asynchronous events. 
 842  *  For datagram (non connection oriented) sockets, GSocket_Connect() 
 843  *  just sets the peer address established with GSocket_SetPeer() as 
 844  *  default destination. 
 847  *    GSOCK_INVSOCK    - the socket is in use or not valid. 
 848  *    GSOCK_INVADDR    - the peer address has not been established. 
 849  *    GSOCK_TIMEDOUT   - timeout, the connection failed. 
 850  *    GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) 
 851  *    GSOCK_MEMERR     - couldn't allocate memory. 
 852  *    GSOCK_IOERR      - low-level error. 
 854 GSocketError 
GSocket::Connect(GSocketStream stream
) 
 861   /* Enable CONNECTION events (needed for nonblocking connections) */ 
 862   Enable(GSOCK_CONNECTION
); 
 864   if (m_fd 
!= INVALID_SOCKET
) 
 866     m_error 
= GSOCK_INVSOCK
; 
 867     return GSOCK_INVSOCK
; 
 872     m_error 
= GSOCK_INVADDR
; 
 873     return GSOCK_INVADDR
; 
 876   /* Streamed or dgram socket? */ 
 877   m_stream   
= (stream 
== GSOCK_STREAMED
); 
 879   m_establishing 
= false; 
 881   /* Create the socket */ 
 882   m_fd 
= socket(m_peer
->m_realfamily
, 
 883                      m_stream
? SOCK_STREAM 
: SOCK_DGRAM
, 0); 
 885   if (m_fd 
== INVALID_SOCKET
) 
 887     m_error 
= GSOCK_IOERR
; 
 891   /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ 
 893   setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
)); 
 896 #if defined(__EMX__) || defined(__VISAGECPP__) 
 897   ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
 899   ioctl(m_fd
, FIONBIO
, &arg
); 
 902   // If the reuse flag is set, use the applicable socket reuse flags(s) 
 905     setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
)); 
 907     setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEPORT
, (const char*)&arg
, sizeof(u_long
)); 
 911   // If a local address has been set, then we need to bind to it before calling connect 
 912   if (m_local 
&& m_local
->m_addr
) 
 914      bind(m_fd
, m_local
->m_addr
, m_local
->m_len
); 
 917   /* Connect it to the peer address, with a timeout (see below) */ 
 918   ret 
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
); 
 920   /* We only call Enable_Events if we know we aren't shutting down the socket. 
 921    * NB: Enable_Events needs to be called whether the socket is blocking or 
 922    * non-blocking, it just shouldn't be called prior to knowing there is a 
 923    * connection _if_ blocking sockets are being used. 
 924    * If connect above returns 0, we are already connected and need to make the 
 925    * call to Enable_Events now. 
 928   if (m_non_blocking 
|| ret 
== 0) 
 929     gs_gui_functions
->Enable_Events(this); 
 935     /* If connect failed with EINPROGRESS and the GSocket object 
 936      * is in blocking mode, we select() for the specified timeout 
 937      * checking for writability to see if the connection request 
 940     if ((err 
== EINPROGRESS
) && (!m_non_blocking
)) 
 942       if (Output_Timeout() == GSOCK_TIMEDOUT
) 
 945         /* m_error is set in _GSocket_Output_Timeout */ 
 946         return GSOCK_TIMEDOUT
; 
 951         SOCKOPTLEN_T len 
= sizeof(error
); 
 953         getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
); 
 955         gs_gui_functions
->Enable_Events(this); 
 958           return GSOCK_NOERROR
; 
 962     /* If connect failed with EINPROGRESS and the GSocket object 
 963      * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK 
 964      * (and return GSOCK_WOULDBLOCK) but we don't close the socket; 
 965      * this way if the connection completes, a GSOCK_CONNECTION 
 966      * event will be generated, if enabled. 
 968     if ((err 
== EINPROGRESS
) && (m_non_blocking
)) 
 970       m_establishing 
= true; 
 971       m_error 
= GSOCK_WOULDBLOCK
; 
 972       return GSOCK_WOULDBLOCK
; 
 975     /* If connect failed with an error other than EINPROGRESS, 
 976      * then the call to GSocket_Connect has failed. 
 979     m_error 
= GSOCK_IOERR
; 
 984   return GSOCK_NOERROR
; 
 987 /* Datagram sockets */ 
 989 /* GSocket_SetNonOriented: 
 990  *  Sets up this socket as a non-connection oriented (datagram) socket. 
 991  *  Before using this function, the local address must have been set 
 992  *  with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR 
 993  *  on success, or one of the following otherwise. 
 996  *    GSOCK_INVSOCK - the socket is in use. 
 997  *    GSOCK_INVADDR - the local address has not been set. 
 998  *    GSOCK_IOERR   - low-level error. 
1000 GSocketError 
GSocket::SetNonOriented() 
1006   if (m_fd 
!= INVALID_SOCKET
) 
1008     m_error 
= GSOCK_INVSOCK
; 
1009     return GSOCK_INVSOCK
; 
1014     m_error 
= GSOCK_INVADDR
; 
1015     return GSOCK_INVADDR
; 
1018   /* Initialize all fields */ 
1022   /* Create the socket */ 
1023   m_fd 
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0); 
1025   if (m_fd 
== INVALID_SOCKET
) 
1027     m_error 
= GSOCK_IOERR
; 
1030 #if defined(__EMX__) || defined(__VISAGECPP__) 
1031   ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
1033   ioctl(m_fd
, FIONBIO
, &arg
); 
1035   gs_gui_functions
->Enable_Events(this); 
1037   /* Bind to the local address, 
1038    * and retrieve the actual address bound. 
1040   if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) || 
1043                    (WX_SOCKLEN_T 
*) &m_local
->m_len
) != 0)) 
1046     m_error 
= GSOCK_IOERR
; 
1050   return GSOCK_NOERROR
; 
1055 /* Like recv(), send(), ... */ 
1056 int GSocket::Read(char *buffer
, int size
) 
1062   if (m_fd 
== INVALID_SOCKET 
|| m_server
) 
1064     m_error 
= GSOCK_INVSOCK
; 
1068   /* Disable events during query of socket status */ 
1069   Disable(GSOCK_INPUT
); 
1071   /* If the socket is blocking, wait for data (with a timeout) */ 
1072   if (Input_Timeout() == GSOCK_TIMEDOUT
) { 
1073     m_error 
= GSOCK_TIMEDOUT
; 
1074     /* Don't return here immediately, otherwise socket events would not be 
1082       ret 
= Recv_Stream(buffer
, size
); 
1084       ret 
= Recv_Dgram(buffer
, size
); 
1086     /* If recv returned zero, then the connection is lost, and errno is not set. 
1087      * Otherwise, recv has returned an error (-1), in which case we have lost the 
1088      * socket only if errno does _not_ indicate that there may be more data to read. 
1092       m_error 
= GSOCK_IOERR
; 
1093       m_detected 
= GSOCK_LOST_FLAG
; 
1095       // Signal an error for return 
1100       if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
)) 
1101         m_error 
= GSOCK_WOULDBLOCK
; 
1103         m_error 
= GSOCK_IOERR
; 
1107   /* Enable events again now that we are done processing */ 
1108   Enable(GSOCK_INPUT
); 
1113 int GSocket::Write(const char *buffer
, int size
) 
1119   GSocket_Debug(( "GSocket_Write #1, size %d\n", size 
)); 
1121   if (m_fd 
== INVALID_SOCKET 
|| m_server
) 
1123     m_error 
= GSOCK_INVSOCK
; 
1127   GSocket_Debug(( "GSocket_Write #2, size %d\n", size 
)); 
1129   /* If the socket is blocking, wait for writability (with a timeout) */ 
1130   if (Output_Timeout() == GSOCK_TIMEDOUT
) 
1133   GSocket_Debug(( "GSocket_Write #3, size %d\n", size 
)); 
1135   /* Write the data */ 
1137     ret 
= Send_Stream(buffer
, size
); 
1139     ret 
= Send_Dgram(buffer
, size
); 
1141   GSocket_Debug(( "GSocket_Write #4, size %d\n", size 
)); 
1145     if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
)) 
1147       m_error 
= GSOCK_WOULDBLOCK
; 
1148       GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); 
1152       m_error 
= GSOCK_IOERR
; 
1153       GSocket_Debug(( "GSocket_Write error IOERR\n" )); 
1156     /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect 
1157      * in MSW). Once the first OUTPUT event is received, users can assume 
1158      * that the socket is writable until a read operation fails. Only then 
1159      * will further OUTPUT events be posted. 
1161     Enable(GSOCK_OUTPUT
); 
1166   GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret 
)); 
1172  *  Polls the socket to determine its status. This function will 
1173  *  check for the events specified in the 'flags' parameter, and 
1174  *  it will return a mask indicating which operations can be 
1175  *  performed. This function won't block, regardless of the 
1176  *  mode (blocking | nonblocking) of the socket. 
1178 GSocketEventFlags 
GSocket::Select(GSocketEventFlags flags
) 
1180   if (!gs_gui_functions
->CanUseEventLoop()) 
1183     GSocketEventFlags result 
= 0; 
1192         return (GSOCK_LOST_FLAG 
& flags
); 
1194     /* Do not use a static struct, Linux can garble it */ 
1195     tv
.tv_sec 
= m_timeout 
/ 1000; 
1196     tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
1198     wxFD_ZERO(&readfds
); 
1199     wxFD_ZERO(&writefds
); 
1200     wxFD_ZERO(&exceptfds
); 
1201     wxFD_SET(m_fd
, &readfds
); 
1202     if (flags 
& GSOCK_OUTPUT_FLAG 
|| flags 
& GSOCK_CONNECTION_FLAG
) 
1203       wxFD_SET(m_fd
, &writefds
); 
1204     wxFD_SET(m_fd
, &exceptfds
); 
1206     /* Check 'sticky' CONNECTION flag first */ 
1207     result 
|= (GSOCK_CONNECTION_FLAG 
& m_detected
); 
1209     /* If we have already detected a LOST event, then don't try 
1210      * to do any further processing. 
1212     if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1214       m_establishing 
= false; 
1216       return (GSOCK_LOST_FLAG 
& flags
); 
1219     /* Try select now */ 
1220     if (select(m_fd 
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0) 
1222       /* What to do here? */ 
1223       return (result 
& flags
); 
1226     /* Check for exceptions and errors */ 
1227     if (wxFD_ISSET(m_fd
, &exceptfds
)) 
1229       m_establishing 
= false; 
1230       m_detected 
= GSOCK_LOST_FLAG
; 
1232       /* LOST event: Abort any further processing */ 
1233       return (GSOCK_LOST_FLAG 
& flags
); 
1236     /* Check for readability */ 
1237     if (wxFD_ISSET(m_fd
, &readfds
)) 
1239       result 
|= GSOCK_INPUT_FLAG
; 
1241       if (m_server 
&& m_stream
) 
1243         /* This is a TCP server socket that detected a connection. 
1244           While the INPUT_FLAG is also set, it doesn't matter on 
1245           this kind of  sockets, as we can only Accept() from them. */         
1246         result 
|= GSOCK_CONNECTION_FLAG
; 
1247         m_detected 
|= GSOCK_CONNECTION_FLAG
; 
1251     /* Check for writability */ 
1252     if (wxFD_ISSET(m_fd
, &writefds
)) 
1254       if (m_establishing 
&& !m_server
) 
1257         SOCKOPTLEN_T len 
= sizeof(error
); 
1259         m_establishing 
= false; 
1261         getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
); 
1265           m_detected 
= GSOCK_LOST_FLAG
; 
1267           /* LOST event: Abort any further processing */ 
1268           return (GSOCK_LOST_FLAG 
& flags
); 
1272           result 
|= GSOCK_CONNECTION_FLAG
; 
1273           m_detected 
|= GSOCK_CONNECTION_FLAG
; 
1278         result 
|= GSOCK_OUTPUT_FLAG
; 
1282     return (result 
& flags
); 
1288     return flags 
& m_detected
; 
1294 /* GSocket_SetNonBlocking: 
1295  *  Sets the socket to non-blocking mode. All IO calls will return 
1298 void GSocket::SetNonBlocking(bool non_block
) 
1302   GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) ); 
1304   m_non_blocking 
= non_block
; 
1307 /* GSocket_SetTimeout: 
1308  *  Sets the timeout for blocking calls. Time is expressed in 
1311 void GSocket::SetTimeout(unsigned long millisec
) 
1315   m_timeout 
= millisec
; 
1318 /* GSocket_GetError: 
1319  *  Returns the last error occurred for this socket. Note that successful 
1320  *  operations do not clear this back to GSOCK_NOERROR, so use it only 
1323 GSocketError WXDLLIMPEXP_NET 
GSocket::GetError() 
1333  *   There is data to be read in the input buffer. If, after a read 
1334  *   operation, there is still data available, the callback function will 
1337  *   The socket is available for writing. That is, the next write call 
1338  *   won't block. This event is generated only once, when the connection is 
1339  *   first established, and then only if a call failed with GSOCK_WOULDBLOCK, 
1340  *   when the output buffer empties again. This means that the app should 
1341  *   assume that it can write since the first OUTPUT event, and no more 
1342  *   OUTPUT events will be generated unless an error occurs. 
1344  *   Connection successfully established, for client sockets, or incoming 
1345  *   client connection, for server sockets. Wait for this event (also watch 
1346  *   out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. 
1348  *   The connection is lost (or a connection request failed); this could 
1349  *   be due to a failure, or due to the peer closing it gracefully. 
1352 /* GSocket_SetCallback: 
1353  *  Enables the callbacks specified by 'flags'. Note that 'flags' 
1354  *  may be a combination of flags OR'ed toghether, so the same 
1355  *  callback function can be made to accept different events. 
1356  *  The callback function must have the following prototype: 
1358  *  void function(GSocket *socket, GSocketEvent event, char *cdata) 
1360 void GSocket::SetCallback(GSocketEventFlags flags
, 
1361                          GSocketCallback callback
, char *cdata
) 
1367   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1369     if ((flags 
& (1 << count
)) != 0) 
1371       m_cbacks
[count
] = callback
; 
1372       m_data
[count
] = cdata
; 
1377 /* GSocket_UnsetCallback: 
1378  *  Disables all callbacks specified by 'flags', which may be a 
1379  *  combination of flags OR'ed toghether. 
1381 void GSocket::UnsetCallback(GSocketEventFlags flags
) 
1387   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1389     if ((flags 
& (1 << count
)) != 0) 
1391       m_cbacks
[count
] = NULL
; 
1392       m_data
[count
] = NULL
; 
1397 GSocketError 
GSocket::GetSockOpt(int level
, int optname
, 
1398                                 void *optval
, int *optlen
) 
1400     if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0) 
1401         return GSOCK_NOERROR
; 
1403     return GSOCK_OPTERR
; 
1406 GSocketError 
GSocket::SetSockOpt(int level
, int optname
, 
1407                                 const void *optval
, int optlen
) 
1409     if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0) 
1410         return GSOCK_NOERROR
; 
1412     return GSOCK_OPTERR
; 
1415 #define CALL_CALLBACK(socket, event) {                                  \ 
1416   socket->Disable(event);                                               \ 
1417   if (socket->m_cbacks[event])                                          \ 
1418     socket->m_cbacks[event](socket, event, socket->m_data[event]);      \ 
1422 void GSocket::Enable(GSocketEvent event
) 
1424   m_detected 
&= ~(1 << event
); 
1425   gs_gui_functions
->Install_Callback(this, event
); 
1428 void GSocket::Disable(GSocketEvent event
) 
1430   m_detected 
|= (1 << event
); 
1431   gs_gui_functions
->Uninstall_Callback(this, event
); 
1434 /* _GSocket_Input_Timeout: 
1435  *  For blocking sockets, wait until data is available or 
1436  *  until timeout ellapses. 
1438 GSocketError 
GSocket::Input_Timeout() 
1444   /* Linux select() will overwrite the struct on return */ 
1445   tv
.tv_sec  
= (m_timeout 
/ 1000); 
1446   tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
1448   if (!m_non_blocking
) 
1450     wxFD_ZERO(&readfds
); 
1451     wxFD_SET(m_fd
, &readfds
); 
1452     ret 
= select(m_fd 
+ 1, &readfds
, NULL
, NULL
, &tv
); 
1455       GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); 
1456       m_error 
= GSOCK_TIMEDOUT
; 
1457       return GSOCK_TIMEDOUT
; 
1462       GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); 
1463       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1464       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1465       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1466       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1467       m_error 
= GSOCK_TIMEDOUT
; 
1468       return GSOCK_TIMEDOUT
; 
1472   return GSOCK_NOERROR
; 
1475 /* _GSocket_Output_Timeout: 
1476  *  For blocking sockets, wait until data can be sent without 
1477  *  blocking or until timeout ellapses. 
1479 GSocketError 
GSocket::Output_Timeout() 
1485   /* Linux select() will overwrite the struct on return */ 
1486   tv
.tv_sec  
= (m_timeout 
/ 1000); 
1487   tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
1489   GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) ); 
1491   if (!m_non_blocking
) 
1493     wxFD_ZERO(&writefds
); 
1494     wxFD_SET(m_fd
, &writefds
); 
1495     ret 
= select(m_fd 
+ 1, NULL
, &writefds
, NULL
, &tv
); 
1498       GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); 
1499       m_error 
= GSOCK_TIMEDOUT
; 
1500       return GSOCK_TIMEDOUT
; 
1505       GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); 
1506       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1507       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1508       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1509       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1510       m_error 
= GSOCK_TIMEDOUT
; 
1511       return GSOCK_TIMEDOUT
; 
1514     if ( ! wxFD_ISSET(m_fd
, &writefds
) ) 
1516         GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); 
1520         GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); 
1525     GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); 
1528   return GSOCK_NOERROR
; 
1531 int GSocket::Recv_Stream(char *buffer
, int size
) 
1536     ret 
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
); 
1538   while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1543 int GSocket::Recv_Dgram(char *buffer
, int size
) 
1545   struct sockaddr from
; 
1546   WX_SOCKLEN_T fromlen 
= sizeof(from
); 
1550   fromlen 
= sizeof(from
); 
1554     ret 
= recvfrom(m_fd
, buffer
, size
, 0, &from
, (WX_SOCKLEN_T 
*) &fromlen
); 
1556   while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1561   /* Translate a system address into a GSocket address */ 
1564     m_peer 
= GAddress_new(); 
1567       m_error 
= GSOCK_MEMERR
; 
1572   err 
= _GAddress_translate_from(m_peer
, &from
, fromlen
); 
1573   if (err 
!= GSOCK_NOERROR
) 
1575     GAddress_destroy(m_peer
); 
1584 int GSocket::Send_Stream(const char *buffer
, int size
) 
1592     ret 
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
); 
1594   while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1601 int GSocket::Send_Dgram(const char *buffer
, int size
) 
1603   struct sockaddr 
*addr
; 
1609     m_error 
= GSOCK_INVADDR
; 
1613   err 
= _GAddress_translate_to(m_peer
, &addr
, &len
); 
1614   if (err 
!= GSOCK_NOERROR
) 
1624     ret 
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
); 
1626   while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1630   /* Frees memory allocated from _GAddress_translate_to */ 
1636 void GSocket::Detected_Read() 
1640   /* Safeguard against straggling call to Detected_Read */ 
1641   if (m_fd 
== INVALID_SOCKET
) 
1646   /* If we have already detected a LOST event, then don't try 
1647    * to do any further processing. 
1649   if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1651     m_establishing 
= false; 
1653     CALL_CALLBACK(this, GSOCK_LOST
); 
1658   int num 
=  recv(m_fd
, &c
, 1, MSG_PEEK 
| GSOCKET_MSG_NOSIGNAL
); 
1662     CALL_CALLBACK(this, GSOCK_INPUT
); 
1666     if (m_server 
&& m_stream
) 
1668       CALL_CALLBACK(this, GSOCK_CONNECTION
); 
1672       /* Do not throw a lost event in cases where the socket isn't really lost */ 
1673       if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
) || (errno 
== EINTR
)) 
1675         CALL_CALLBACK(this, GSOCK_INPUT
); 
1679         CALL_CALLBACK(this, GSOCK_LOST
); 
1686 void GSocket::Detected_Write() 
1688   /* If we have already detected a LOST event, then don't try 
1689    * to do any further processing. 
1691   if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1693     m_establishing 
= false; 
1695     CALL_CALLBACK(this, GSOCK_LOST
); 
1700   if (m_establishing 
&& !m_server
) 
1703     SOCKOPTLEN_T len 
= sizeof(error
); 
1705     m_establishing 
= false; 
1707     getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
); 
1711       CALL_CALLBACK(this, GSOCK_LOST
); 
1716       CALL_CALLBACK(this, GSOCK_CONNECTION
); 
1717       /* We have to fire this event by hand because CONNECTION (for clients) 
1718        * and OUTPUT are internally the same and we just disabled CONNECTION 
1719        * events with the above macro. 
1721       CALL_CALLBACK(this, GSOCK_OUTPUT
); 
1726     CALL_CALLBACK(this, GSOCK_OUTPUT
); 
1730 /* Compatibility functions for GSocket */ 
1731 GSocket 
*GSocket_new(void) 
1733     GSocket 
*newsocket 
= new GSocket(); 
1734     if (newsocket
->IsOk()) 
1743  * ------------------------------------------------------------------------- 
1745  * ------------------------------------------------------------------------- 
1748 /* CHECK_ADDRESS verifies that the current address family is either 
1749  * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it 
1750  * initalizes it to be a GSOCK_*family*. In other cases, it returns 
1751  * an appropiate error code. 
1753  * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. 
1755 #define CHECK_ADDRESS(address, family)                              \ 
1757   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1758     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1759       return address->m_error;                                      \ 
1760   if (address->m_family != GSOCK_##family)                          \ 
1762     address->m_error = GSOCK_INVADDR;                               \ 
1763     return GSOCK_INVADDR;                                           \ 
1767 #define CHECK_ADDRESS_RETVAL(address, family, retval)               \ 
1769   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1770     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1772   if (address->m_family != GSOCK_##family)                          \ 
1774     address->m_error = GSOCK_INVADDR;                               \ 
1780 GAddress 
*GAddress_new(void) 
1784   if ((address 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1787   address
->m_family  
= GSOCK_NOFAMILY
; 
1788   address
->m_addr    
= NULL
; 
1794 GAddress 
*GAddress_copy(GAddress 
*address
) 
1798   assert(address 
!= NULL
); 
1800   if ((addr2 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1803   memcpy(addr2
, address
, sizeof(GAddress
)); 
1805   if (address
->m_addr 
&& address
->m_len 
> 0) 
1807     addr2
->m_addr 
= (struct sockaddr 
*)malloc(addr2
->m_len
); 
1808     if (addr2
->m_addr 
== NULL
) 
1813     memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
); 
1819 void GAddress_destroy(GAddress 
*address
) 
1821   assert(address 
!= NULL
); 
1823   if (address
->m_addr
) 
1824     free(address
->m_addr
); 
1829 void GAddress_SetFamily(GAddress 
*address
, GAddressType type
) 
1831   assert(address 
!= NULL
); 
1833   address
->m_family 
= type
; 
1836 GAddressType 
GAddress_GetFamily(GAddress 
*address
) 
1838   assert(address 
!= NULL
); 
1840   return address
->m_family
; 
1843 GSocketError 
_GAddress_translate_from(GAddress 
*address
, 
1844                                       struct sockaddr 
*addr
, int len
) 
1846   address
->m_realfamily 
= addr
->sa_family
; 
1847   switch (addr
->sa_family
) 
1850       address
->m_family 
= GSOCK_INET
; 
1853       address
->m_family 
= GSOCK_UNIX
; 
1857       address
->m_family 
= GSOCK_INET6
; 
1862       address
->m_error 
= GSOCK_INVOP
; 
1867   if (address
->m_addr
) 
1868     free(address
->m_addr
); 
1870   address
->m_len  
= len
; 
1871   address
->m_addr 
= (struct sockaddr 
*)malloc(len
); 
1873   if (address
->m_addr 
== NULL
) 
1875     address
->m_error 
= GSOCK_MEMERR
; 
1876     return GSOCK_MEMERR
; 
1879   memcpy(address
->m_addr
, addr
, len
); 
1881   return GSOCK_NOERROR
; 
1884 GSocketError 
_GAddress_translate_to(GAddress 
*address
, 
1885                                     struct sockaddr 
**addr
, int *len
) 
1887   if (!address
->m_addr
) 
1889     address
->m_error 
= GSOCK_INVADDR
; 
1890     return GSOCK_INVADDR
; 
1893   *len 
= address
->m_len
; 
1894   *addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1897     address
->m_error 
= GSOCK_MEMERR
; 
1898     return GSOCK_MEMERR
; 
1901   memcpy(*addr
, address
->m_addr
, address
->m_len
); 
1902   return GSOCK_NOERROR
; 
1906  * ------------------------------------------------------------------------- 
1907  * Internet address family 
1908  * ------------------------------------------------------------------------- 
1911 GSocketError 
_GAddress_Init_INET(GAddress 
*address
) 
1913   address
->m_len  
= sizeof(struct sockaddr_in
); 
1914   address
->m_addr 
= (struct sockaddr 
*) malloc(address
->m_len
); 
1915   if (address
->m_addr 
== NULL
) 
1917     address
->m_error 
= GSOCK_MEMERR
; 
1918     return GSOCK_MEMERR
; 
1921   address
->m_family 
= GSOCK_INET
; 
1922   address
->m_realfamily 
= PF_INET
; 
1923   ((struct sockaddr_in 
*)address
->m_addr
)->sin_family 
= AF_INET
; 
1924   ((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
.s_addr 
= INADDR_ANY
; 
1926   return GSOCK_NOERROR
; 
1929 GSocketError 
GAddress_INET_SetHostName(GAddress 
*address
, const char *hostname
) 
1932   struct in_addr 
*addr
; 
1934   assert(address 
!= NULL
); 
1936   CHECK_ADDRESS(address
, INET
); 
1938   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1940   /* If it is a numeric host name, convert it now */ 
1941 #if defined(HAVE_INET_ATON) 
1942   if (inet_aton(hostname
, addr
) == 0) 
1944 #elif defined(HAVE_INET_ADDR) 
1945   if ( (addr
->s_addr 
= inet_addr(hostname
)) == (unsigned)-1 ) 
1948   /* Use gethostbyname by default */ 
1950   int val 
= 1;  /* VA doesn't like constants in conditional expressions */ 
1955     struct in_addr 
*array_addr
; 
1957     /* It is a real name, we solve it */ 
1959 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) 
1960     struct hostent_data buffer
; 
1965     he 
= wxGethostbyname_r(hostname
, &h
, (void*)&buffer
, sizeof(buffer
), &err
); 
1968       /* Reset to invalid address */ 
1969       addr
->s_addr 
= INADDR_NONE
; 
1970       address
->m_error 
= GSOCK_NOHOST
; 
1971       return GSOCK_NOHOST
; 
1974     array_addr 
= (struct in_addr 
*) *(he
->h_addr_list
); 
1975     addr
->s_addr 
= array_addr
[0].s_addr
; 
1978   return GSOCK_NOERROR
; 
1981 GSocketError 
GAddress_INET_SetAnyAddress(GAddress 
*address
) 
1983   return GAddress_INET_SetHostAddress(address
, INADDR_ANY
); 
1986 GSocketError 
GAddress_INET_SetHostAddress(GAddress 
*address
, 
1987                                           unsigned long hostaddr
) 
1989   struct in_addr 
*addr
; 
1991   assert(address 
!= NULL
); 
1993   CHECK_ADDRESS(address
, INET
); 
1995   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1996   addr
->s_addr 
= htonl(hostaddr
); 
1998   return GSOCK_NOERROR
; 
2001 GSocketError 
GAddress_INET_SetPortName(GAddress 
*address
, const char *port
, 
2002                                        const char *protocol
) 
2005   struct sockaddr_in 
*addr
; 
2007   assert(address 
!= NULL
); 
2008   CHECK_ADDRESS(address
, INET
); 
2012     address
->m_error 
= GSOCK_INVPORT
; 
2013     return GSOCK_INVPORT
; 
2016 #if defined(HAVE_FUNC_GETSERVBYNAME_R_4) 
2017     struct servent_data buffer
; 
2021   struct servent serv
; 
2022   se 
= wxGetservbyname_r(port
, protocol
, &serv
, 
2023                          (void*)&buffer
, sizeof(buffer
)); 
2026     /* the cast to int suppresses compiler warnings about subscript having the 
2028     if (isdigit((int)port
[0])) 
2032       port_int 
= atoi(port
); 
2033       addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2034       addr
->sin_port 
= htons(port_int
); 
2035       return GSOCK_NOERROR
; 
2038     address
->m_error 
= GSOCK_INVPORT
; 
2039     return GSOCK_INVPORT
; 
2042   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2043   addr
->sin_port 
= se
->s_port
; 
2045   return GSOCK_NOERROR
; 
2048 GSocketError 
GAddress_INET_SetPort(GAddress 
*address
, unsigned short port
) 
2050   struct sockaddr_in 
*addr
; 
2052   assert(address 
!= NULL
); 
2053   CHECK_ADDRESS(address
, INET
); 
2055   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2056   addr
->sin_port 
= htons(port
); 
2058   return GSOCK_NOERROR
; 
2061 GSocketError 
GAddress_INET_GetHostName(GAddress 
*address
, char *hostname
, size_t sbuf
) 
2065   struct sockaddr_in 
*addr
; 
2067   assert(address 
!= NULL
); 
2068   CHECK_ADDRESS(address
, INET
); 
2070   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2071   addr_buf 
= (char *)&(addr
->sin_addr
); 
2073   struct hostent temphost
; 
2074 #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) 
2075   struct hostent_data buffer
; 
2080   he 
= wxGethostbyaddr_r(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
, &temphost
, 
2081                          (void*)&buffer
, sizeof(buffer
), &err
); 
2084     address
->m_error 
= GSOCK_NOHOST
; 
2085     return GSOCK_NOHOST
; 
2088   strncpy(hostname
, he
->h_name
, sbuf
); 
2090   return GSOCK_NOERROR
; 
2093 unsigned long GAddress_INET_GetHostAddress(GAddress 
*address
) 
2095   struct sockaddr_in 
*addr
; 
2097   assert(address 
!= NULL
); 
2098   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
2100   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2102   return ntohl(addr
->sin_addr
.s_addr
); 
2105 unsigned short GAddress_INET_GetPort(GAddress 
*address
) 
2107   struct sockaddr_in 
*addr
; 
2109   assert(address 
!= NULL
); 
2110   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
2112   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
2113   return ntohs(addr
->sin_port
); 
2117  * ------------------------------------------------------------------------- 
2118  * Unix address family 
2119  * ------------------------------------------------------------------------- 
2122 #ifndef __VISAGECPP__ 
2123 GSocketError 
_GAddress_Init_UNIX(GAddress 
*address
) 
2125   address
->m_len  
= sizeof(struct sockaddr_un
); 
2126   address
->m_addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
2127   if (address
->m_addr 
== NULL
) 
2129     address
->m_error 
= GSOCK_MEMERR
; 
2130     return GSOCK_MEMERR
; 
2133   address
->m_family 
= GSOCK_UNIX
; 
2134   address
->m_realfamily 
= PF_UNIX
; 
2135   ((struct sockaddr_un 
*)address
->m_addr
)->sun_family 
= AF_UNIX
; 
2136   ((struct sockaddr_un 
*)address
->m_addr
)->sun_path
[0] = 0; 
2138   return GSOCK_NOERROR
; 
2141 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) 
2143 GSocketError 
GAddress_UNIX_SetPath(GAddress 
*address
, const char *path
) 
2145   struct sockaddr_un 
*addr
; 
2147   assert(address 
!= NULL
); 
2149   CHECK_ADDRESS(address
, UNIX
); 
2151   addr 
= ((struct sockaddr_un 
*)address
->m_addr
); 
2152   strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
); 
2153   addr
->sun_path
[UNIX_SOCK_PATHLEN 
- 1] = '\0'; 
2155   return GSOCK_NOERROR
; 
2158 GSocketError 
GAddress_UNIX_GetPath(GAddress 
*address
, char *path
, size_t sbuf
) 
2160   struct sockaddr_un 
*addr
; 
2162   assert(address 
!= NULL
); 
2163   CHECK_ADDRESS(address
, UNIX
); 
2165   addr 
= (struct sockaddr_un 
*)address
->m_addr
; 
2167   strncpy(path
, addr
->sun_path
, sbuf
); 
2169   return GSOCK_NOERROR
; 
2171 #endif  /* !defined(__VISAGECPP__) */ 
2172 #endif  /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */