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  * ------------------------------------------------------------------------- 
  16  * PLEASE don't put C++ comments here - this is a C source file. 
  19 #if defined(__WATCOMC__) 
  20 #include "wx/wxprec.h" 
  25 #ifndef __GSOCKET_STANDALONE__ 
  29 #if defined(__VISAGECPP__) 
  30 /* Seems to be needed by Visual Age C++, though I don't see how it manages 
  31    to not break on including a C++ header into a plain C source file      */ 
  33 #define BSD_SELECT /* use Berkley Sockets select */ 
  36 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) 
  39 #include <sys/types.h> 
  44 #include <netinet/in.h> 
  47 #include <sys/ioctl.h> 
  53     u_char  sun_len
;        /* sockaddr len including null */ 
  54     u_char  sun_family
;     /* AF_UNIX */ 
  55     char    sun_path
[108];  /* path name (gag) */ 
  58 #include <sys/socket.h> 
  64 #include <netinet/in.h> 
  65 #include <arpa/inet.h> 
  72 #include <machine/endian.h> 
  78 #define EBADF   SOCEBADF 
  84 #include <sys/socket.h> 
  85 #include <sys/ioctl.h> 
  86 #include <sys/select.h> 
  88 #define close(a) soclose(a) 
  89 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) 
  90 int _System 
bsdselect(int, 
  95 int _System 
soclose(int); 
  99 #include <sys/select.h> 
 107 #  include <sys/filio.h> 
 110 #  include <bstring.h> 
 113 #  include <strings.h> 
 120 #  define SOCKLEN_T unsigned int 
 124 #      define SOCKLEN_T socklen_t 
 126 #  elif defined(__WXMAC__) 
 127 #    define SOCKLEN_T socklen_t 
 129 #    define SOCKLEN_T int 
 133 #endif /* SOCKLEN_T */ 
 136 #define SOCKOPTLEN_T SOCKLEN_T 
 140  * MSW defines this, Unices don't. 
 142 #ifndef INVALID_SOCKET 
 143 #define INVALID_SOCKET -1 
 146 /* UnixWare reportedly needs this for FIONBIO definition */ 
 148 #include <sys/filio.h> 
 152  * INADDR_BROADCAST is identical to INADDR_NONE which is not defined 
 153  * on all systems. INADDR_BROADCAST should be fine to indicate an error. 
 156 #define INADDR_NONE INADDR_BROADCAST 
 159 #if defined(__VISAGECPP__) || defined(__WATCOMC__) 
 161     #define MASK_SIGNAL() { 
 162     #define UNMASK_SIGNAL() } 
 166     #define MASK_SIGNAL()                       \ 
 168         void (*old_handler)(int);               \ 
 170         old_handler = signal(SIGPIPE, SIG_IGN); 
 172     #define UNMASK_SIGNAL()                     \ 
 173         signal(SIGPIPE, old_handler);           \ 
 178 /* If a SIGPIPE is issued by a socket call on a remotely closed socket, 
 179    the program will "crash" unless it explicitly handles the SIGPIPE. 
 180    By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will 
 181    use SO_NOSIGPIPE (if available), the BSD equivalent. */ 
 183 #  define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL 
 184 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */ 
 185 #  define GSOCKET_MSG_NOSIGNAL 0 
 186 #endif /* MSG_NOSIGNAL */ 
 188 #ifndef __GSOCKET_STANDALONE__ 
 189 #  include "wx/unix/gsockunx.h" 
 190 #  include "wx/gsocket.h" 
 192 #  include "gsockunx.h" 
 193 #  include "gsocket.h" 
 194 #endif /* __GSOCKET_STANDALONE__ */ 
 196 /* debugging helpers */ 
 197 #ifdef __GSOCKET_DEBUG__ 
 198 #  define GSocket_Debug(args) printf args 
 200 #  define GSocket_Debug(args) 
 201 #endif /* __GSOCKET_DEBUG__ */ 
 203 /* Table of GUI-related functions. We must call them indirectly because 
 204  * of wxBase and GUI separation: */ 
 206 static GSocketGUIFunctionsTable 
*gs_gui_functions
; 
 208 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
 
 211     virtual bool OnInit(); 
 212     virtual void OnExit(); 
 213     virtual bool CanUseEventLoop(); 
 214     virtual bool Init_Socket(GSocket 
*socket
); 
 215     virtual void Destroy_Socket(GSocket 
*socket
); 
 216     virtual void Install_Callback(GSocket 
*socket
, GSocketEvent event
); 
 217     virtual void Uninstall_Callback(GSocket 
*socket
, GSocketEvent event
); 
 218     virtual void Enable_Events(GSocket 
*socket
); 
 219     virtual void Disable_Events(GSocket 
*socket
); 
 222 bool GSocketGUIFunctionsTableNull::OnInit() 
 224 void GSocketGUIFunctionsTableNull::OnExit() 
 226 bool GSocketGUIFunctionsTableNull::CanUseEventLoop() 
 228 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket 
*socket
) 
 230 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket 
*socket
) 
 232 void GSocketGUIFunctionsTableNull::Install_Callback(GSocket 
*socket
, GSocketEvent event
) 
 234 void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket 
*socket
, GSocketEvent event
) 
 236 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket 
*socket
) 
 238 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket 
*socket
) 
 240 /* Global initialisers */ 
 242 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable 
*guifunc
) 
 244   gs_gui_functions 
= guifunc
; 
 247 int GSocket_Init(void) 
 249   if (!gs_gui_functions
) 
 251     static GSocketGUIFunctionsTableNull table
; 
 252     gs_gui_functions 
= &table
; 
 254   if ( !gs_gui_functions
->OnInit() ) 
 259 void GSocket_Cleanup(void) 
 261   if (gs_gui_functions
) 
 263       gs_gui_functions
->OnExit(); 
 267 /* Constructors / Destructors for GSocket */ 
 273   m_fd                  
= INVALID_SOCKET
; 
 274   for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) 
 281   m_error               
= GSOCK_NOERROR
; 
 284   m_gui_dependent       
= NULL
; 
 285   m_non_blocking        
= false; 
 287   m_timeout             
= 10*60*1000; 
 288                                 /* 10 minutes * 60 sec * 1000 millisec */ 
 289   m_establishing        
= false; 
 291   assert(gs_gui_functions
); 
 292   /* Per-socket GUI-specific initialization */ 
 293   m_ok 
= gs_gui_functions
->Init_Socket(this); 
 296 void GSocket::Close() 
 298     gs_gui_functions
->Disable_Events(this); 
 299     /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */ 
 300 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))) 
 303     m_fd 
= INVALID_SOCKET
; 
 310   /* Check that the socket is really shutdowned */ 
 311   if (m_fd 
!= INVALID_SOCKET
) 
 314   /* Per-socket GUI-specific cleanup */ 
 315   gs_gui_functions
->Destroy_Socket(this); 
 317   /* Destroy private addresses */ 
 319     GAddress_destroy(m_local
); 
 322     GAddress_destroy(m_peer
); 
 326  *  Disallow further read/write operations on this socket, close 
 327  *  the fd and disable all callbacks. 
 329 void GSocket::Shutdown() 
 335   /* If socket has been created, shutdown it */ 
 336   if (m_fd 
!= INVALID_SOCKET
) 
 342   /* Disable GUI callbacks */ 
 343   for (evt 
= 0; evt 
< GSOCK_MAX_EVENT
; evt
++) 
 344     m_cbacks
[evt
] = NULL
; 
 346   m_detected 
= GSOCK_LOST_FLAG
; 
 349 /* Address handling */ 
 355  *  Set or get the local or peer address for this socket. The 'set' 
 356  *  functions return GSOCK_NOERROR on success, an error code otherwise. 
 357  *  The 'get' functions return a pointer to a GAddress object on success, 
 358  *  or NULL otherwise, in which case they set the error code of the 
 359  *  corresponding GSocket. 
 362  *    GSOCK_INVSOCK - the socket is not valid. 
 363  *    GSOCK_INVADDR - the address is not valid. 
 365 GSocketError 
GSocket::SetLocal(GAddress 
*address
) 
 369   /* the socket must be initialized, or it must be a server */ 
 370   if ((m_fd 
!= INVALID_SOCKET 
&& !m_server
)) 
 372     m_error 
= GSOCK_INVSOCK
; 
 373     return GSOCK_INVSOCK
; 
 377   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 379     m_error 
= GSOCK_INVADDR
; 
 380     return GSOCK_INVADDR
; 
 384     GAddress_destroy(m_local
); 
 386   m_local 
= GAddress_copy(address
); 
 388   return GSOCK_NOERROR
; 
 391 GSocketError 
GSocket::SetPeer(GAddress 
*address
) 
 396   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 398     m_error 
= GSOCK_INVADDR
; 
 399     return GSOCK_INVADDR
; 
 403     GAddress_destroy(m_peer
); 
 405   m_peer 
= GAddress_copy(address
); 
 407   return GSOCK_NOERROR
; 
 410 GAddress 
*GSocket::GetLocal() 
 413   struct sockaddr addr
; 
 414   SOCKLEN_T size 
= sizeof(addr
); 
 419   /* try to get it from the m_local var first */ 
 421     return GAddress_copy(m_local
); 
 423   /* else, if the socket is initialized, try getsockname */ 
 424   if (m_fd 
== INVALID_SOCKET
) 
 426     m_error 
= GSOCK_INVSOCK
; 
 430   if (getsockname(m_fd
, &addr
, (SOCKLEN_T 
*) &size
) < 0) 
 432     m_error 
= GSOCK_IOERR
; 
 436   /* got a valid address from getsockname, create a GAddress object */ 
 437   address 
= GAddress_new(); 
 440     m_error 
= GSOCK_MEMERR
; 
 444   err 
= _GAddress_translate_from(address
, &addr
, size
); 
 445   if (err 
!= GSOCK_NOERROR
) 
 447     GAddress_destroy(address
); 
 455 GAddress 
*GSocket::GetPeer() 
 459   /* try to get it from the m_peer var */ 
 461     return GAddress_copy(m_peer
); 
 466 /* Server specific parts */ 
 468 /* GSocket_SetServer: 
 469  *  Sets up this socket as a server. The local address must have been 
 470  *  set with GSocket_SetLocal() before GSocket_SetServer() is called. 
 471  *  Returns GSOCK_NOERROR on success, one of the following otherwise: 
 474  *    GSOCK_INVSOCK - the socket is in use. 
 475  *    GSOCK_INVADDR - the local address has not been set. 
 476  *    GSOCK_IOERR   - low-level error. 
 478 GSocketError 
GSocket::SetServer() 
 484   /* must not be in use */ 
 485   if (m_fd 
!= INVALID_SOCKET
) 
 487     m_error 
= GSOCK_INVSOCK
; 
 488     return GSOCK_INVSOCK
; 
 491   /* the local addr must have been set */ 
 494     m_error 
= GSOCK_INVADDR
; 
 495     return GSOCK_INVADDR
; 
 498   /* Initialize all fields */ 
 502   /* Create the socket */ 
 503   m_fd 
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0); 
 505   if (m_fd 
== INVALID_SOCKET
) 
 507     m_error 
= GSOCK_IOERR
; 
 511   /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ 
 513   setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
)); 
 516   ioctl(m_fd
, FIONBIO
, &arg
); 
 517   gs_gui_functions
->Enable_Events(this); 
 519   /* allow a socket to re-bind if the socket is in the TIME_WAIT 
 520      state after being previously closed. 
 523     setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
)); 
 525   /* Bind to the local address, 
 526    * retrieve the actual address bound, 
 527    * and listen up to 5 connections. 
 529   if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) || 
 532                    (SOCKLEN_T 
*) &m_local
->m_len
) != 0) || 
 533       (listen(m_fd
, 5) != 0)) 
 536     m_error 
= GSOCK_IOERR
; 
 540   return GSOCK_NOERROR
; 
 543 /* GSocket_WaitConnection: 
 544  *  Waits for an incoming client connection. Returns a pointer to 
 545  *  a GSocket object, or NULL if there was an error, in which case 
 546  *  the last error field will be updated for the calling GSocket. 
 548  *  Error codes (set in the calling GSocket) 
 549  *    GSOCK_INVSOCK    - the socket is not valid or not a server. 
 550  *    GSOCK_TIMEDOUT   - timeout, no incoming connections. 
 551  *    GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. 
 552  *    GSOCK_MEMERR     - couldn't allocate memory. 
 553  *    GSOCK_IOERR      - low-level error. 
 555 GSocket 
*GSocket::WaitConnection() 
 557   struct sockaddr from
; 
 558   SOCKLEN_T fromlen 
= sizeof(from
); 
 565   /* If the socket has already been created, we exit immediately */ 
 566   if (m_fd 
== INVALID_SOCKET 
|| !m_server
) 
 568     m_error 
= GSOCK_INVSOCK
; 
 572   /* Create a GSocket object for the new connection */ 
 573   connection 
= GSocket_new(); 
 577     m_error 
= GSOCK_MEMERR
; 
 581   /* Wait for a connection (with timeout) */ 
 582   if (Input_Timeout() == GSOCK_TIMEDOUT
) 
 585     /* m_error set by _GSocket_Input_Timeout */ 
 589   connection
->m_fd 
= accept(m_fd
, &from
, (SOCKLEN_T 
*) &fromlen
); 
 591   /* Reenable CONNECTION events */ 
 592   Enable(GSOCK_CONNECTION
); 
 594   if (connection
->m_fd 
== INVALID_SOCKET
) 
 596     if (errno 
== EWOULDBLOCK
) 
 597       m_error 
= GSOCK_WOULDBLOCK
; 
 599       m_error 
= GSOCK_IOERR
; 
 605   /* Initialize all fields */ 
 606   connection
->m_server   
= false; 
 607   connection
->m_stream   
= true; 
 609   /* Setup the peer address field */ 
 610   connection
->m_peer 
= GAddress_new(); 
 611   if (!connection
->m_peer
) 
 614     m_error 
= GSOCK_MEMERR
; 
 617   err 
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
); 
 618   if (err 
!= GSOCK_NOERROR
) 
 624 #if defined(__EMX__) || defined(__VISAGECPP__) 
 625   ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
 627   ioctl(connection
->m_fd
, FIONBIO
, &arg
); 
 629   gs_gui_functions
->Enable_Events(connection
); 
 634 bool GSocket::SetReusable() 
 636     /* socket must not be null, and must not be in use/already bound */ 
 637     if (this && m_fd 
== INVALID_SOCKET
) { 
 644 /* Client specific parts */ 
 647  *  For stream (connection oriented) sockets, GSocket_Connect() tries 
 648  *  to establish a client connection to a server using the peer address 
 649  *  as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the 
 650  *  connection has been successfully established, or one of the error 
 651  *  codes listed below. Note that for nonblocking sockets, a return 
 652  *  value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection 
 653  *  request can be completed later; you should use GSocket_Select() 
 654  *  to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the 
 655  *  corresponding asynchronous events. 
 657  *  For datagram (non connection oriented) sockets, GSocket_Connect() 
 658  *  just sets the peer address established with GSocket_SetPeer() as 
 659  *  default destination. 
 662  *    GSOCK_INVSOCK    - the socket is in use or not valid. 
 663  *    GSOCK_INVADDR    - the peer address has not been established. 
 664  *    GSOCK_TIMEDOUT   - timeout, the connection failed. 
 665  *    GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) 
 666  *    GSOCK_MEMERR     - couldn't allocate memory. 
 667  *    GSOCK_IOERR      - low-level error. 
 669 GSocketError 
GSocket::Connect(GSocketStream stream
) 
 676   /* Enable CONNECTION events (needed for nonblocking connections) */ 
 677   Enable(GSOCK_CONNECTION
); 
 679   if (m_fd 
!= INVALID_SOCKET
) 
 681     m_error 
= GSOCK_INVSOCK
; 
 682     return GSOCK_INVSOCK
; 
 687     m_error 
= GSOCK_INVADDR
; 
 688     return GSOCK_INVADDR
; 
 691   /* Streamed or dgram socket? */ 
 692   m_stream   
= (stream 
== GSOCK_STREAMED
); 
 694   m_establishing 
= false; 
 696   /* Create the socket */ 
 697   m_fd 
= socket(m_peer
->m_realfamily
, 
 698                      m_stream
? SOCK_STREAM 
: SOCK_DGRAM
, 0); 
 700   if (m_fd 
== INVALID_SOCKET
) 
 702     m_error 
= GSOCK_IOERR
; 
 706   /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */ 
 708   setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
)); 
 711 #if defined(__EMX__) || defined(__VISAGECPP__) 
 712   ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
 714   ioctl(m_fd
, FIONBIO
, &arg
); 
 717   /* Connect it to the peer address, with a timeout (see below) */ 
 718   ret 
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
); 
 720   /* We only call Enable_Events if we know e aren't shutting down the socket */ 
 724     gs_gui_functions
->Enable_Events(this); 
 731     /* If connect failed with EINPROGRESS and the GSocket object 
 732      * is in blocking mode, we select() for the specified timeout 
 733      * checking for writability to see if the connection request 
 736     if ((err 
== EINPROGRESS
) && (!m_non_blocking
)) 
 738       if (Output_Timeout() == GSOCK_TIMEDOUT
) 
 741         /* m_error is set in _GSocket_Output_Timeout */ 
 742         return GSOCK_TIMEDOUT
; 
 747         SOCKOPTLEN_T len 
= sizeof(error
); 
 749         getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
); 
 751         gs_gui_functions
->Enable_Events(this); 
 754           return GSOCK_NOERROR
; 
 758     /* If connect failed with EINPROGRESS and the GSocket object 
 759      * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK 
 760      * (and return GSOCK_WOULDBLOCK) but we don't close the socket; 
 761      * this way if the connection completes, a GSOCK_CONNECTION 
 762      * event will be generated, if enabled. 
 764     if ((err 
== EINPROGRESS
) && (m_non_blocking
)) 
 766       m_establishing 
= true; 
 767       m_error 
= GSOCK_WOULDBLOCK
; 
 768       return GSOCK_WOULDBLOCK
; 
 771     /* If connect failed with an error other than EINPROGRESS, 
 772      * then the call to GSocket_Connect has failed. 
 775     m_error 
= GSOCK_IOERR
; 
 779   return GSOCK_NOERROR
; 
 782 /* Datagram sockets */ 
 784 /* GSocket_SetNonOriented: 
 785  *  Sets up this socket as a non-connection oriented (datagram) socket. 
 786  *  Before using this function, the local address must have been set 
 787  *  with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR 
 788  *  on success, or one of the following otherwise. 
 791  *    GSOCK_INVSOCK - the socket is in use. 
 792  *    GSOCK_INVADDR - the local address has not been set. 
 793  *    GSOCK_IOERR   - low-level error. 
 795 GSocketError 
GSocket::SetNonOriented() 
 801   if (m_fd 
!= INVALID_SOCKET
) 
 803     m_error 
= GSOCK_INVSOCK
; 
 804     return GSOCK_INVSOCK
; 
 809     m_error 
= GSOCK_INVADDR
; 
 810     return GSOCK_INVADDR
; 
 813   /* Initialize all fields */ 
 817   /* Create the socket */ 
 818   m_fd 
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0); 
 820   if (m_fd 
== INVALID_SOCKET
) 
 822     m_error 
= GSOCK_IOERR
; 
 825 #if defined(__EMX__) || defined(__VISAGECPP__) 
 826   ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
)); 
 828   ioctl(m_fd
, FIONBIO
, &arg
); 
 830   gs_gui_functions
->Enable_Events(this); 
 832   /* Bind to the local address, 
 833    * and retrieve the actual address bound. 
 835   if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) || 
 838                    (SOCKLEN_T 
*) &m_local
->m_len
) != 0)) 
 841     m_error 
= GSOCK_IOERR
; 
 845   return GSOCK_NOERROR
; 
 850 /* Like recv(), send(), ... */ 
 851 int GSocket::Read(char *buffer
, int size
) 
 857   if (m_fd 
== INVALID_SOCKET 
|| m_server
) 
 859     m_error 
= GSOCK_INVSOCK
; 
 863   /* Disable events during query of socket status */ 
 864   Disable(GSOCK_INPUT
); 
 866   /* If the socket is blocking, wait for data (with a timeout) */ 
 867   if (Input_Timeout() == GSOCK_TIMEDOUT
) 
 868     /* We no longer return here immediately, otherwise socket events would not be re-enabled! */ 
 873       ret 
= Recv_Stream(buffer
, size
); 
 875       ret 
= Recv_Dgram(buffer
, size
); 
 880     if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
)) 
 881       m_error 
= GSOCK_WOULDBLOCK
; 
 883       m_error 
= GSOCK_IOERR
; 
 886   /* Enable events again now that we are done processing */ 
 892 int GSocket::Write(const char *buffer
, int size
) 
 898   GSocket_Debug(( "GSocket_Write #1, size %d\n", size 
)); 
 900   if (m_fd 
== INVALID_SOCKET 
|| m_server
) 
 902     m_error 
= GSOCK_INVSOCK
; 
 906   GSocket_Debug(( "GSocket_Write #2, size %d\n", size 
)); 
 908   /* If the socket is blocking, wait for writability (with a timeout) */ 
 909   if (Output_Timeout() == GSOCK_TIMEDOUT
) 
 912   GSocket_Debug(( "GSocket_Write #3, size %d\n", size 
)); 
 916     ret 
= Send_Stream(buffer
, size
); 
 918     ret 
= Send_Dgram(buffer
, size
); 
 920   GSocket_Debug(( "GSocket_Write #4, size %d\n", size 
)); 
 924     if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
)) 
 926       m_error 
= GSOCK_WOULDBLOCK
; 
 927       GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); 
 931       m_error 
= GSOCK_IOERR
; 
 932       GSocket_Debug(( "GSocket_Write error IOERR\n" )); 
 935     /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect 
 936      * in MSW). Once the first OUTPUT event is received, users can assume 
 937      * that the socket is writable until a read operation fails. Only then 
 938      * will further OUTPUT events be posted. 
 940     Enable(GSOCK_OUTPUT
); 
 944   GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret 
)); 
 950  *  Polls the socket to determine its status. This function will 
 951  *  check for the events specified in the 'flags' parameter, and 
 952  *  it will return a mask indicating which operations can be 
 953  *  performed. This function won't block, regardless of the 
 954  *  mode (blocking | nonblocking) of the socket. 
 956 GSocketEventFlags 
GSocket::Select(GSocketEventFlags flags
) 
 958   if (!gs_gui_functions
->CanUseEventLoop()) 
 961     GSocketEventFlags result 
= 0; 
 969     /* Do not use a static struct, Linux can garble it */ 
 970     tv
.tv_sec 
= m_timeout 
/ 1000; 
 971     tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
 976     FD_SET(m_fd
, &readfds
); 
 977     if (flags 
& GSOCK_OUTPUT_FLAG 
|| flags 
& GSOCK_CONNECTION_FLAG
) 
 978       FD_SET(m_fd
, &writefds
); 
 979     FD_SET(m_fd
, &exceptfds
); 
 981     /* Check 'sticky' CONNECTION flag first */ 
 982     result 
|= (GSOCK_CONNECTION_FLAG 
& m_detected
); 
 984     /* If we have already detected a LOST event, then don't try 
 985      * to do any further processing. 
 987     if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
 989       m_establishing 
= false; 
 991       return (GSOCK_LOST_FLAG 
& flags
); 
 995     if (select(m_fd 
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0) 
 997       /* What to do here? */ 
 998       return (result 
& flags
); 
1001     /* Check for readability */ 
1002     if (FD_ISSET(m_fd
, &readfds
)) 
1006       int num 
= recv(m_fd
, &c
, 1, MSG_PEEK 
| GSOCKET_MSG_NOSIGNAL
); 
1010         result 
|= GSOCK_INPUT_FLAG
; 
1014         if (m_server 
&& m_stream
) 
1016           result 
|= GSOCK_CONNECTION_FLAG
; 
1017           m_detected 
|= GSOCK_CONNECTION_FLAG
; 
1019         else if ((errno 
!= EWOULDBLOCK
) && (errno 
!= EAGAIN
) && (errno 
!= EINTR
)) 
1021           m_detected 
= GSOCK_LOST_FLAG
; 
1022           m_establishing 
= false; 
1024           /* LOST event: Abort any further processing */ 
1025           return (GSOCK_LOST_FLAG 
& flags
); 
1030     /* Check for writability */ 
1031     if (FD_ISSET(m_fd
, &writefds
)) 
1033       if (m_establishing 
&& !m_server
) 
1036         SOCKOPTLEN_T len 
= sizeof(error
); 
1038         m_establishing 
= false; 
1040         getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
); 
1044           m_detected 
= GSOCK_LOST_FLAG
; 
1046           /* LOST event: Abort any further processing */ 
1047           return (GSOCK_LOST_FLAG 
& flags
); 
1051           result 
|= GSOCK_CONNECTION_FLAG
; 
1052           m_detected 
|= GSOCK_CONNECTION_FLAG
; 
1057         result 
|= GSOCK_OUTPUT_FLAG
; 
1061     /* Check for exceptions and errors (is this useful in Unices?) */ 
1062     if (FD_ISSET(m_fd
, &exceptfds
)) 
1064       m_establishing 
= false; 
1065       m_detected 
= GSOCK_LOST_FLAG
; 
1067       /* LOST event: Abort any further processing */ 
1068       return (GSOCK_LOST_FLAG 
& flags
); 
1071     return (result 
& flags
); 
1078     return flags 
& m_detected
; 
1085 /* GSocket_SetNonBlocking: 
1086  *  Sets the socket to non-blocking mode. All IO calls will return 
1089 void GSocket::SetNonBlocking(bool non_block
) 
1093   GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) ); 
1095   m_non_blocking 
= non_block
; 
1098 /* GSocket_SetTimeout: 
1099  *  Sets the timeout for blocking calls. Time is expressed in 
1102 void GSocket::SetTimeout(unsigned long millisec
) 
1106   m_timeout 
= millisec
; 
1109 /* GSocket_GetError: 
1110  *  Returns the last error occurred for this socket. Note that successful 
1111  *  operations do not clear this back to GSOCK_NOERROR, so use it only 
1114 GSocketError WXDLLIMPEXP_NET 
GSocket::GetError() 
1124  *   There is data to be read in the input buffer. If, after a read 
1125  *   operation, there is still data available, the callback function will 
1128  *   The socket is available for writing. That is, the next write call 
1129  *   won't block. This event is generated only once, when the connection is 
1130  *   first established, and then only if a call failed with GSOCK_WOULDBLOCK, 
1131  *   when the output buffer empties again. This means that the app should 
1132  *   assume that it can write since the first OUTPUT event, and no more 
1133  *   OUTPUT events will be generated unless an error occurs. 
1135  *   Connection successfully established, for client sockets, or incoming 
1136  *   client connection, for server sockets. Wait for this event (also watch 
1137  *   out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. 
1139  *   The connection is lost (or a connection request failed); this could 
1140  *   be due to a failure, or due to the peer closing it gracefully. 
1143 /* GSocket_SetCallback: 
1144  *  Enables the callbacks specified by 'flags'. Note that 'flags' 
1145  *  may be a combination of flags OR'ed toghether, so the same 
1146  *  callback function can be made to accept different events. 
1147  *  The callback function must have the following prototype: 
1149  *  void function(GSocket *socket, GSocketEvent event, char *cdata) 
1151 void GSocket::SetCallback(GSocketEventFlags flags
, 
1152                          GSocketCallback callback
, char *cdata
) 
1158   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1160     if ((flags 
& (1 << count
)) != 0) 
1162       m_cbacks
[count
] = callback
; 
1163       m_data
[count
] = cdata
; 
1168 /* GSocket_UnsetCallback: 
1169  *  Disables all callbacks specified by 'flags', which may be a 
1170  *  combination of flags OR'ed toghether. 
1172 void GSocket::UnsetCallback(GSocketEventFlags flags
) 
1178   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1180     if ((flags 
& (1 << count
)) != 0) 
1182       m_cbacks
[count
] = NULL
; 
1183       m_data
[count
] = NULL
; 
1188 GSocketError 
GSocket::GetSockOpt(int level
, int optname
, 
1189                                 void *optval
, int *optlen
) 
1191     if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0) 
1193         return GSOCK_NOERROR
; 
1195     return GSOCK_OPTERR
; 
1198 GSocketError 
GSocket::SetSockOpt(int level
, int optname
, 
1199                                 const void *optval
, int optlen
) 
1201     if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0) 
1203         return GSOCK_NOERROR
; 
1205     return GSOCK_OPTERR
; 
1208 #define CALL_CALLBACK(socket, event) {                                  \ 
1209   socket->Disable(event);                                               \ 
1210   if (socket->m_cbacks[event])                                          \ 
1211     socket->m_cbacks[event](socket, event, socket->m_data[event]);      \ 
1215 void GSocket::Enable(GSocketEvent event
) 
1217   m_detected 
&= ~(1 << event
); 
1218   gs_gui_functions
->Install_Callback(this, event
); 
1221 void GSocket::Disable(GSocketEvent event
) 
1223   m_detected 
|= (1 << event
); 
1224   gs_gui_functions
->Uninstall_Callback(this, event
); 
1227 /* _GSocket_Input_Timeout: 
1228  *  For blocking sockets, wait until data is available or 
1229  *  until timeout ellapses. 
1231 GSocketError 
GSocket::Input_Timeout() 
1237   /* Linux select() will overwrite the struct on return */ 
1238   tv
.tv_sec  
= (m_timeout 
/ 1000); 
1239   tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
1241   if (!m_non_blocking
) 
1244     FD_SET(m_fd
, &readfds
); 
1245     ret 
= select(m_fd 
+ 1, &readfds
, NULL
, NULL
, &tv
); 
1248       GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); 
1249       m_error 
= GSOCK_TIMEDOUT
; 
1250       return GSOCK_TIMEDOUT
; 
1254       GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); 
1255       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1256       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1257       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1258       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1259       m_error 
= GSOCK_TIMEDOUT
; 
1260       return GSOCK_TIMEDOUT
; 
1263   return GSOCK_NOERROR
; 
1266 /* _GSocket_Output_Timeout: 
1267  *  For blocking sockets, wait until data can be sent without 
1268  *  blocking or until timeout ellapses. 
1270 GSocketError 
GSocket::Output_Timeout() 
1276   /* Linux select() will overwrite the struct on return */ 
1277   tv
.tv_sec  
= (m_timeout 
/ 1000); 
1278   tv
.tv_usec 
= (m_timeout 
% 1000) * 1000; 
1280   GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) ); 
1282   if (!m_non_blocking
) 
1285     FD_SET(m_fd
, &writefds
); 
1286     ret 
= select(m_fd 
+ 1, NULL
, &writefds
, NULL
, &tv
); 
1289       GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); 
1290       m_error 
= GSOCK_TIMEDOUT
; 
1291       return GSOCK_TIMEDOUT
; 
1295       GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); 
1296       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1297       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1298       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1299       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1300       m_error 
= GSOCK_TIMEDOUT
; 
1301       return GSOCK_TIMEDOUT
; 
1303     if ( ! FD_ISSET(m_fd
, &writefds
) ) { 
1304         GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); 
1307         GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); 
1312     GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); 
1315   return GSOCK_NOERROR
; 
1318 int GSocket::Recv_Stream(char *buffer
, int size
) 
1323     ret 
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
); 
1324   } while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1328 int GSocket::Recv_Dgram(char *buffer
, int size
) 
1330   struct sockaddr from
; 
1331   SOCKLEN_T fromlen 
= sizeof(from
); 
1335   fromlen 
= sizeof(from
); 
1339     ret 
= recvfrom(m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T 
*) &fromlen
); 
1340   } while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1345   /* Translate a system address into a GSocket address */ 
1348     m_peer 
= GAddress_new(); 
1351       m_error 
= GSOCK_MEMERR
; 
1355   err 
= _GAddress_translate_from(m_peer
, &from
, fromlen
); 
1356   if (err 
!= GSOCK_NOERROR
) 
1358     GAddress_destroy(m_peer
); 
1367 int GSocket::Send_Stream(const char *buffer
, int size
) 
1375     ret 
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
); 
1376   } while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1383 int GSocket::Send_Dgram(const char *buffer
, int size
) 
1385   struct sockaddr 
*addr
; 
1391     m_error 
= GSOCK_INVADDR
; 
1395   err 
= _GAddress_translate_to(m_peer
, &addr
, &len
); 
1396   if (err 
!= GSOCK_NOERROR
) 
1406     ret 
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
); 
1407   } while (ret 
== -1 && errno 
== EINTR
); /* Loop until not interrupted */ 
1411   /* Frees memory allocated from _GAddress_translate_to */ 
1417 void GSocket::Detected_Read() 
1421   /* Safeguard against straggling call to Detected_Read */ 
1422   if (m_fd 
== INVALID_SOCKET
) 
1427   /* If we have already detected a LOST event, then don't try 
1428    * to do any further processing. 
1430   if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1432     m_establishing 
= false; 
1434     CALL_CALLBACK(this, GSOCK_LOST
); 
1439   int num 
=  recv(m_fd
, &c
, 1, MSG_PEEK 
| GSOCKET_MSG_NOSIGNAL
); 
1443     CALL_CALLBACK(this, GSOCK_INPUT
); 
1447     if (m_server 
&& m_stream
) 
1449       CALL_CALLBACK(this, GSOCK_CONNECTION
); 
1453       /* Do not throw a lost event in cases where the socket isn't really lost */ 
1454       if ((errno 
== EWOULDBLOCK
) || (errno 
== EAGAIN
) || (errno 
== EINTR
)) 
1456         CALL_CALLBACK(this, GSOCK_INPUT
); 
1460         CALL_CALLBACK(this, GSOCK_LOST
); 
1467 void GSocket::Detected_Write() 
1469   /* If we have already detected a LOST event, then don't try 
1470    * to do any further processing. 
1472   if ((m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1474     m_establishing 
= false; 
1476     CALL_CALLBACK(this, GSOCK_LOST
); 
1481   if (m_establishing 
&& !m_server
) 
1484     SOCKOPTLEN_T len 
= sizeof(error
); 
1486     m_establishing 
= false; 
1488     getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
); 
1492       CALL_CALLBACK(this, GSOCK_LOST
); 
1497       CALL_CALLBACK(this, GSOCK_CONNECTION
); 
1498       /* We have to fire this event by hand because CONNECTION (for clients) 
1499        * and OUTPUT are internally the same and we just disabled CONNECTION 
1500        * events with the above macro. 
1502       CALL_CALLBACK(this, GSOCK_OUTPUT
); 
1507     CALL_CALLBACK(this, GSOCK_OUTPUT
); 
1511 /* Compatibility functions for GSocket */ 
1512 GSocket 
*GSocket_new(void) 
1514     GSocket 
*newsocket 
= new GSocket(); 
1515     if(newsocket
->IsOk()) 
1522  * ------------------------------------------------------------------------- 
1524  * ------------------------------------------------------------------------- 
1527 /* CHECK_ADDRESS verifies that the current address family is either 
1528  * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it 
1529  * initalizes it to be a GSOCK_*family*. In other cases, it returns 
1530  * an appropiate error code. 
1532  * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. 
1534 #define CHECK_ADDRESS(address, family)                              \ 
1536   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1537     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1538       return address->m_error;                                      \ 
1539   if (address->m_family != GSOCK_##family)                          \ 
1541     address->m_error = GSOCK_INVADDR;                               \ 
1542     return GSOCK_INVADDR;                                           \ 
1546 #define CHECK_ADDRESS_RETVAL(address, family, retval)               \ 
1548   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1549     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1551   if (address->m_family != GSOCK_##family)                          \ 
1553     address->m_error = GSOCK_INVADDR;                               \ 
1559 GAddress 
*GAddress_new(void) 
1563   if ((address 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1566   address
->m_family  
= GSOCK_NOFAMILY
; 
1567   address
->m_addr    
= NULL
; 
1573 GAddress 
*GAddress_copy(GAddress 
*address
) 
1577   assert(address 
!= NULL
); 
1579   if ((addr2 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1582   memcpy(addr2
, address
, sizeof(GAddress
)); 
1584   if (address
->m_addr 
&& address
->m_len 
> 0) 
1586     addr2
->m_addr 
= (struct sockaddr 
*)malloc(addr2
->m_len
); 
1587     if (addr2
->m_addr 
== NULL
) 
1592     memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
); 
1598 void GAddress_destroy(GAddress 
*address
) 
1600   assert(address 
!= NULL
); 
1602   if (address
->m_addr
) 
1603     free(address
->m_addr
); 
1608 void GAddress_SetFamily(GAddress 
*address
, GAddressType type
) 
1610   assert(address 
!= NULL
); 
1612   address
->m_family 
= type
; 
1615 GAddressType 
GAddress_GetFamily(GAddress 
*address
) 
1617   assert(address 
!= NULL
); 
1619   return address
->m_family
; 
1622 GSocketError 
_GAddress_translate_from(GAddress 
*address
, 
1623                                       struct sockaddr 
*addr
, int len
) 
1625   address
->m_realfamily 
= addr
->sa_family
; 
1626   switch (addr
->sa_family
) 
1629       address
->m_family 
= GSOCK_INET
; 
1632       address
->m_family 
= GSOCK_UNIX
; 
1636       address
->m_family 
= GSOCK_INET6
; 
1641       address
->m_error 
= GSOCK_INVOP
; 
1646   if (address
->m_addr
) 
1647     free(address
->m_addr
); 
1649   address
->m_len  
= len
; 
1650   address
->m_addr 
= (struct sockaddr 
*)malloc(len
); 
1652   if (address
->m_addr 
== NULL
) 
1654     address
->m_error 
= GSOCK_MEMERR
; 
1655     return GSOCK_MEMERR
; 
1657   memcpy(address
->m_addr
, addr
, len
); 
1659   return GSOCK_NOERROR
; 
1662 GSocketError 
_GAddress_translate_to(GAddress 
*address
, 
1663                                     struct sockaddr 
**addr
, int *len
) 
1665   if (!address
->m_addr
) 
1667     address
->m_error 
= GSOCK_INVADDR
; 
1668     return GSOCK_INVADDR
; 
1671   *len 
= address
->m_len
; 
1672   *addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1675     address
->m_error 
= GSOCK_MEMERR
; 
1676     return GSOCK_MEMERR
; 
1679   memcpy(*addr
, address
->m_addr
, address
->m_len
); 
1680   return GSOCK_NOERROR
; 
1684  * ------------------------------------------------------------------------- 
1685  * Internet address family 
1686  * ------------------------------------------------------------------------- 
1689 GSocketError 
_GAddress_Init_INET(GAddress 
*address
) 
1691   address
->m_len  
= sizeof(struct sockaddr_in
); 
1692   address
->m_addr 
= (struct sockaddr 
*) malloc(address
->m_len
); 
1693   if (address
->m_addr 
== NULL
) 
1695     address
->m_error 
= GSOCK_MEMERR
; 
1696     return GSOCK_MEMERR
; 
1699   address
->m_family 
= GSOCK_INET
; 
1700   address
->m_realfamily 
= PF_INET
; 
1701   ((struct sockaddr_in 
*)address
->m_addr
)->sin_family 
= AF_INET
; 
1702   ((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
.s_addr 
= INADDR_ANY
; 
1704   return GSOCK_NOERROR
; 
1707 GSocketError 
GAddress_INET_SetHostName(GAddress 
*address
, const char *hostname
) 
1710   struct in_addr 
*addr
; 
1712   assert(address 
!= NULL
); 
1714   CHECK_ADDRESS(address
, INET
); 
1716   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1718   /* If it is a numeric host name, convert it now */ 
1719 #if defined(HAVE_INET_ATON) 
1720   if (inet_aton(hostname
, addr
) == 0) 
1722 #elif defined(HAVE_INET_ADDR) 
1723   if ( (addr
->s_addr 
= inet_addr(hostname
)) == (unsigned)-1 ) 
1726   /* Use gethostbyname by default */ 
1728   int val 
= 1;  /* VA doesn't like constants in conditional expressions */ 
1733     struct in_addr 
*array_addr
; 
1735     /* It is a real name, we solve it */ 
1736     if ((he 
= gethostbyname(hostname
)) == NULL
) 
1738       /* Reset to invalid address */ 
1739       addr
->s_addr 
= INADDR_NONE
; 
1740       address
->m_error 
= GSOCK_NOHOST
; 
1741       return GSOCK_NOHOST
; 
1743     array_addr 
= (struct in_addr 
*) *(he
->h_addr_list
); 
1744     addr
->s_addr 
= array_addr
[0].s_addr
; 
1746   return GSOCK_NOERROR
; 
1749 GSocketError 
GAddress_INET_SetAnyAddress(GAddress 
*address
) 
1751   return GAddress_INET_SetHostAddress(address
, INADDR_ANY
); 
1754 GSocketError 
GAddress_INET_SetHostAddress(GAddress 
*address
, 
1755                                           unsigned long hostaddr
) 
1757   struct in_addr 
*addr
; 
1759   assert(address 
!= NULL
); 
1761   CHECK_ADDRESS(address
, INET
); 
1763   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1764   addr
->s_addr 
= htonl(hostaddr
); 
1766   return GSOCK_NOERROR
; 
1769 GSocketError 
GAddress_INET_SetPortName(GAddress 
*address
, const char *port
, 
1770                                        const char *protocol
) 
1773   struct sockaddr_in 
*addr
; 
1775   assert(address 
!= NULL
); 
1776   CHECK_ADDRESS(address
, INET
); 
1780     address
->m_error 
= GSOCK_INVPORT
; 
1781     return GSOCK_INVPORT
; 
1784 #if defined(__WXPM__) && defined(__EMX__) 
1785   se 
= getservbyname(port
, (char*)protocol
); 
1787   se 
= getservbyname(port
, protocol
); 
1791     /* the cast to int suppresses compiler warnings about subscript having the 
1793     if (isdigit((int)port
[0])) 
1797       port_int 
= atoi(port
); 
1798       addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1799       addr
->sin_port 
= htons(port_int
); 
1800       return GSOCK_NOERROR
; 
1803     address
->m_error 
= GSOCK_INVPORT
; 
1804     return GSOCK_INVPORT
; 
1807   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1808   addr
->sin_port 
= se
->s_port
; 
1810   return GSOCK_NOERROR
; 
1813 GSocketError 
GAddress_INET_SetPort(GAddress 
*address
, unsigned short port
) 
1815   struct sockaddr_in 
*addr
; 
1817   assert(address 
!= NULL
); 
1818   CHECK_ADDRESS(address
, INET
); 
1820   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1821   addr
->sin_port 
= htons(port
); 
1823   return GSOCK_NOERROR
; 
1826 GSocketError 
GAddress_INET_GetHostName(GAddress 
*address
, char *hostname
, size_t sbuf
) 
1830   struct sockaddr_in 
*addr
; 
1832   assert(address 
!= NULL
); 
1833   CHECK_ADDRESS(address
, INET
); 
1835   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1836   addr_buf 
= (char *)&(addr
->sin_addr
); 
1838   he 
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
); 
1841     address
->m_error 
= GSOCK_NOHOST
; 
1842     return GSOCK_NOHOST
; 
1845   strncpy(hostname
, he
->h_name
, sbuf
); 
1847   return GSOCK_NOERROR
; 
1850 unsigned long GAddress_INET_GetHostAddress(GAddress 
*address
) 
1852   struct sockaddr_in 
*addr
; 
1854   assert(address 
!= NULL
); 
1855   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
1857   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1859   return ntohl(addr
->sin_addr
.s_addr
); 
1862 unsigned short GAddress_INET_GetPort(GAddress 
*address
) 
1864   struct sockaddr_in 
*addr
; 
1866   assert(address 
!= NULL
); 
1867   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
1869   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1870   return ntohs(addr
->sin_port
); 
1874  * ------------------------------------------------------------------------- 
1875  * Unix address family 
1876  * ------------------------------------------------------------------------- 
1879 #ifndef __VISAGECPP__ 
1880 GSocketError 
_GAddress_Init_UNIX(GAddress 
*address
) 
1882   address
->m_len  
= sizeof(struct sockaddr_un
); 
1883   address
->m_addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1884   if (address
->m_addr 
== NULL
) 
1886     address
->m_error 
= GSOCK_MEMERR
; 
1887     return GSOCK_MEMERR
; 
1890   address
->m_family 
= GSOCK_UNIX
; 
1891   address
->m_realfamily 
= PF_UNIX
; 
1892   ((struct sockaddr_un 
*)address
->m_addr
)->sun_family 
= AF_UNIX
; 
1893   ((struct sockaddr_un 
*)address
->m_addr
)->sun_path
[0] = 0; 
1895   return GSOCK_NOERROR
; 
1898 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) 
1900 GSocketError 
GAddress_UNIX_SetPath(GAddress 
*address
, const char *path
) 
1902   struct sockaddr_un 
*addr
; 
1904   assert(address 
!= NULL
); 
1906   CHECK_ADDRESS(address
, UNIX
); 
1908   addr 
= ((struct sockaddr_un 
*)address
->m_addr
); 
1909   strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
); 
1910   addr
->sun_path
[UNIX_SOCK_PATHLEN 
- 1] = '\0'; 
1912   return GSOCK_NOERROR
; 
1915 GSocketError 
GAddress_UNIX_GetPath(GAddress 
*address
, char *path
, size_t sbuf
) 
1917   struct sockaddr_un 
*addr
; 
1919   assert(address 
!= NULL
); 
1920   CHECK_ADDRESS(address
, UNIX
); 
1922   addr 
= (struct sockaddr_un 
*)address
->m_addr
; 
1924   strncpy(path
, addr
->sun_path
, sbuf
); 
1926   return GSOCK_NOERROR
; 
1928 #endif  /* !defined(__VISAGECPP__) */ 
1929 #endif  /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */