]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.c
   1 /* ------------------------------------------------------------------------- 
   2  * Project: GSocket (Generic Socket) for WX 
   4  * Authors: Guilhem Lavaux, 
   5  *          Guillermo Rodriguez Garcia <guille@iies.es> (maintainer) 
   6  * Purpose: GSocket main Unix file 
   8  * ------------------------------------------------------------------------- 
  12  * PLEASE don't put C++ comments here - this is a C source file. 
  15 #ifndef __GSOCKET_STANDALONE__ 
  19 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) 
  22 #include <sys/types.h> 
  24 #include <sys/ioctl.h> 
  30     u_char  sun_len
;        /* sockaddr len including null */ 
  31     u_char  sun_family
;     /* AF_UNIX */ 
  32     char    sun_path
[108];  /* path name (gag) */ 
  35 #include <sys/socket.h> 
  40 #include <netinet/in.h> 
  41 #include <arpa/inet.h> 
  50 #  include <sys/filio.h> 
  60 #  define SOCKLEN_T unsigned int 
  64 #      define SOCKLEN_T socklen_t 
  67 #    define SOCKLEN_T int 
  71 #endif /* SOCKLEN_T */ 
  74  * MSW defines this, Unices don't. 
  76 #ifndef INVALID_SOCKET 
  77 #define INVALID_SOCKET -1 
  80 /* UnixWare reportedly needs this for FIONBIO definition */ 
  82 #include <sys/filio.h> 
  86  * INADDR_BROADCAST is identical to INADDR_NONE which is not defined 
  87  * on all systems. INADDR_BROADCAST should be fine to indicate an error. 
  90 #define INADDR_NONE INADDR_BROADCAST 
  93 #define MASK_SIGNAL()                       \ 
  95   void (*old_handler)(int);                 \ 
  97   old_handler = signal(SIGPIPE, SIG_IGN); 
  99 #define UNMASK_SIGNAL()                     \ 
 100   signal(SIGPIPE, old_handler);             \ 
 104 #ifndef __GSOCKET_STANDALONE__ 
 105 #  include "wx/unix/gsockunx.h" 
 106 #  include "wx/gsocket.h" 
 108 #  include "gsockunx.h" 
 109 #  include "gsocket.h" 
 110 #endif /* __GSOCKET_STANDALONE__ */ 
 112 /* redefine some GUI-only functions to do nothing in console mode */ 
 113 #if defined(wxUSE_GUI) && !wxUSE_GUI 
 114 #  define _GSocket_GUI_Init(socket) (1) 
 115 #  define _GSocket_GUI_Destroy(socket) 
 116 #  define _GSocket_Enable_Events(socket) 
 117 #  define _GSocket_Disable_Events(socket) 
 118 #  define _GSocket_Install_Callback(socket, event) 
 119 #  define _GSocket_Uninstall_Callback(socket, event) 
 120 #endif /* wxUSE_GUI */ 
 122 /* debugging helpers */ 
 123 #ifdef __GSOCKET_DEBUG__ 
 124 #  define GSocket_Debug(args) printf args 
 126 #  define GSocket_Debug(args) 
 127 #endif /* __GSOCKET_DEBUG__ */ 
 129 /* Global initialisers */ 
 131 int GSocket_Init(void) 
 136 void GSocket_Cleanup(void) 
 140 /* Constructors / Destructors for GSocket */ 
 142 GSocket 
*GSocket_new(void) 
 147   socket 
= (GSocket 
*)malloc(sizeof(GSocket
)); 
 152   socket
->m_fd                  
= INVALID_SOCKET
; 
 153   for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) 
 155     socket
->m_cbacks
[i
]         = NULL
; 
 157   socket
->m_detected            
= 0; 
 158   socket
->m_local               
= NULL
; 
 159   socket
->m_peer                
= NULL
; 
 160   socket
->m_error               
= GSOCK_NOERROR
; 
 161   socket
->m_server              
= FALSE
; 
 162   socket
->m_stream              
= TRUE
; 
 163   socket
->m_gui_dependent       
= NULL
; 
 164   socket
->m_non_blocking        
= FALSE
; 
 165   socket
->m_timeout             
= 10*60*1000; 
 166                                 /* 10 minutes * 60 sec * 1000 millisec */ 
 167   socket
->m_establishing        
= FALSE
; 
 169   /* Per-socket GUI-specific initialization */ 
 170   success 
= _GSocket_GUI_Init(socket
); 
 180 void GSocket_close(GSocket 
*socket
) 
 182     _GSocket_Disable_Events(socket
); 
 184     socket
->m_fd 
= INVALID_SOCKET
; 
 187 void GSocket_destroy(GSocket 
*socket
) 
 189   assert(socket 
!= NULL
); 
 191   /* Check that the socket is really shutdowned */ 
 192   if (socket
->m_fd 
!= INVALID_SOCKET
) 
 193     GSocket_Shutdown(socket
); 
 195   /* Per-socket GUI-specific cleanup */ 
 196   _GSocket_GUI_Destroy(socket
); 
 198   /* Destroy private addresses */ 
 200     GAddress_destroy(socket
->m_local
); 
 203     GAddress_destroy(socket
->m_peer
); 
 205   /* Destroy the socket itself */ 
 210  *  Disallow further read/write operations on this socket, close 
 211  *  the fd and disable all callbacks. 
 213 void GSocket_Shutdown(GSocket 
*socket
) 
 217   assert(socket 
!= NULL
); 
 219   /* If socket has been created, shutdown it */ 
 220   if (socket
->m_fd 
!= INVALID_SOCKET
) 
 222     shutdown(socket
->m_fd
, 2); 
 223     GSocket_close(socket
); 
 226   /* Disable GUI callbacks */ 
 227   for (evt 
= 0; evt 
< GSOCK_MAX_EVENT
; evt
++) 
 228     socket
->m_cbacks
[evt
] = NULL
; 
 230   socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 233 /* Address handling */ 
 239  *  Set or get the local or peer address for this socket. The 'set' 
 240  *  functions return GSOCK_NOERROR on success, an error code otherwise. 
 241  *  The 'get' functions return a pointer to a GAddress object on success, 
 242  *  or NULL otherwise, in which case they set the error code of the 
 243  *  corresponding GSocket. 
 246  *    GSOCK_INVSOCK - the socket is not valid. 
 247  *    GSOCK_INVADDR - the address is not valid. 
 249 GSocketError 
GSocket_SetLocal(GSocket 
*socket
, GAddress 
*address
) 
 251   assert(socket 
!= NULL
); 
 253   /* the socket must be initialized, or it must be a server */ 
 254   if ((socket
->m_fd 
!= INVALID_SOCKET 
&& !socket
->m_server
)) 
 256     socket
->m_error 
= GSOCK_INVSOCK
; 
 257     return GSOCK_INVSOCK
; 
 261   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 263     socket
->m_error 
= GSOCK_INVADDR
; 
 264     return GSOCK_INVADDR
; 
 268     GAddress_destroy(socket
->m_local
); 
 270   socket
->m_local 
= GAddress_copy(address
); 
 272   return GSOCK_NOERROR
; 
 275 GSocketError 
GSocket_SetPeer(GSocket 
*socket
, GAddress 
*address
) 
 277   assert(socket 
!= NULL
); 
 280   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 282     socket
->m_error 
= GSOCK_INVADDR
; 
 283     return GSOCK_INVADDR
; 
 287     GAddress_destroy(socket
->m_peer
); 
 289   socket
->m_peer 
= GAddress_copy(address
); 
 291   return GSOCK_NOERROR
; 
 294 GAddress 
*GSocket_GetLocal(GSocket 
*socket
) 
 297   struct sockaddr addr
; 
 298   SOCKLEN_T size 
= sizeof(addr
); 
 301   assert(socket 
!= NULL
); 
 303   /* try to get it from the m_local var first */ 
 305     return GAddress_copy(socket
->m_local
); 
 307   /* else, if the socket is initialized, try getsockname */ 
 308   if (socket
->m_fd 
== INVALID_SOCKET
) 
 310     socket
->m_error 
= GSOCK_INVSOCK
; 
 314   if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T 
*) &size
) < 0) 
 316     socket
->m_error 
= GSOCK_IOERR
; 
 320   /* got a valid address from getsockname, create a GAddress object */ 
 321   address 
= GAddress_new(); 
 324     socket
->m_error 
= GSOCK_MEMERR
; 
 328   err 
= _GAddress_translate_from(address
, &addr
, size
); 
 329   if (err 
!= GSOCK_NOERROR
) 
 331     GAddress_destroy(address
); 
 332     socket
->m_error 
= err
; 
 339 GAddress 
*GSocket_GetPeer(GSocket 
*socket
) 
 341   assert(socket 
!= NULL
); 
 343   /* try to get it from the m_peer var */ 
 345     return GAddress_copy(socket
->m_peer
); 
 350 /* Server specific parts */ 
 352 /* GSocket_SetServer: 
 353  *  Sets up this socket as a server. The local address must have been 
 354  *  set with GSocket_SetLocal() before GSocket_SetServer() is called. 
 355  *  Returns GSOCK_NOERROR on success, one of the following otherwise: 
 358  *    GSOCK_INVSOCK - the socket is in use. 
 359  *    GSOCK_INVADDR - the local address has not been set. 
 360  *    GSOCK_IOERR   - low-level error.  
 362 GSocketError 
GSocket_SetServer(GSocket 
*sck
) 
 368   /* must not be in use */ 
 369   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 371     sck
->m_error 
= GSOCK_INVSOCK
; 
 372     return GSOCK_INVSOCK
; 
 375   /* the local addr must have been set */ 
 378     sck
->m_error 
= GSOCK_INVADDR
; 
 379     return GSOCK_INVADDR
; 
 382   /* Initialize all fields */ 
 383   sck
->m_stream   
= TRUE
; 
 384   sck
->m_server   
= TRUE
; 
 385   sck
->m_oriented 
= TRUE
; 
 387   /* Create the socket */ 
 388   sck
->m_fd 
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0); 
 390   if (sck
->m_fd 
== INVALID_SOCKET
) 
 392     sck
->m_error 
= GSOCK_IOERR
; 
 396   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 397   _GSocket_Enable_Events(sck
); 
 399   /* Bind to the local address, 
 400    * retrieve the actual address bound, 
 401    * and listen up to 5 connections. 
 403   if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) || 
 404       (getsockname(sck
->m_fd
, 
 405                    sck
->m_local
->m_addr
, 
 406                    (SOCKLEN_T 
*) &sck
->m_local
->m_len
) != 0) || 
 407       (listen(sck
->m_fd
, 5) != 0)) 
 410     sck
->m_error 
= GSOCK_IOERR
; 
 414   return GSOCK_NOERROR
; 
 417 /* GSocket_WaitConnection: 
 418  *  Waits for an incoming client connection. Returns a pointer to 
 419  *  a GSocket object, or NULL if there was an error, in which case 
 420  *  the last error field will be updated for the calling GSocket. 
 422  *  Error codes (set in the calling GSocket) 
 423  *    GSOCK_INVSOCK    - the socket is not valid or not a server. 
 424  *    GSOCK_TIMEDOUT   - timeout, no incoming connections. 
 425  *    GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. 
 426  *    GSOCK_MEMERR     - couldn't allocate memory. 
 427  *    GSOCK_IOERR      - low-level error.  
 429 GSocket 
*GSocket_WaitConnection(GSocket 
*socket
) 
 431   struct sockaddr from
; 
 432   SOCKLEN_T fromlen 
= sizeof(from
); 
 437   assert(socket 
!= NULL
); 
 439   /* Reenable CONNECTION events */ 
 440   _GSocket_Enable(socket
, GSOCK_CONNECTION
); 
 442   /* If the socket has already been created, we exit immediately */ 
 443   if (socket
->m_fd 
== INVALID_SOCKET 
|| !socket
->m_server
) 
 445     socket
->m_error 
= GSOCK_INVSOCK
; 
 449   /* Create a GSocket object for the new connection */ 
 450   connection 
= GSocket_new(); 
 454     socket
->m_error 
= GSOCK_MEMERR
; 
 458   /* Wait for a connection (with timeout) */ 
 459   if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 461     GSocket_destroy(connection
); 
 462     /* socket->m_error set by _GSocket_Input_Timeout */ 
 466   connection
->m_fd 
= accept(socket
->m_fd
, &from
, (SOCKLEN_T 
*) &fromlen
); 
 468   if (connection
->m_fd 
== INVALID_SOCKET
) 
 470     if (errno 
== EWOULDBLOCK
) 
 471       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 473       socket
->m_error 
= GSOCK_IOERR
; 
 475     GSocket_destroy(connection
); 
 479   /* Initialize all fields */ 
 480   connection
->m_server   
= FALSE
; 
 481   connection
->m_stream   
= TRUE
; 
 482   connection
->m_oriented 
= TRUE
; 
 484   /* Setup the peer address field */ 
 485   connection
->m_peer 
= GAddress_new(); 
 486   if (!connection
->m_peer
) 
 488     GSocket_destroy(connection
); 
 489     socket
->m_error 
= GSOCK_MEMERR
; 
 492   err 
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
); 
 493   if (err 
!= GSOCK_NOERROR
) 
 495     GAddress_destroy(connection
->m_peer
); 
 496     GSocket_destroy(connection
); 
 497     socket
->m_error 
= err
; 
 501   ioctl(connection
->m_fd
, FIONBIO
, &arg
); 
 502   _GSocket_Enable_Events(connection
); 
 507 /* Client specific parts */ 
 510  *  For stream (connection oriented) sockets, GSocket_Connect() tries 
 511  *  to establish a client connection to a server using the peer address 
 512  *  as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the 
 513  *  connection has been succesfully established, or one of the error 
 514  *  codes listed below. Note that for nonblocking sockets, a return 
 515  *  value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection 
 516  *  request can be completed later; you should use GSocket_Select() 
 517  *  to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the 
 518  *  corresponding asynchronous events. 
 520  *  For datagram (non connection oriented) sockets, GSocket_Connect() 
 521  *  just sets the peer address established with GSocket_SetPeer() as 
 522  *  default destination. 
 525  *    GSOCK_INVSOCK    - the socket is in use or not valid. 
 526  *    GSOCK_INVADDR    - the peer address has not been established. 
 527  *    GSOCK_TIMEDOUT   - timeout, the connection failed. 
 528  *    GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) 
 529  *    GSOCK_MEMERR     - couldn't allocate memory. 
 530  *    GSOCK_IOERR      - low-level error.  
 532 GSocketError 
GSocket_Connect(GSocket 
*sck
, GSocketStream stream
) 
 539   /* Enable CONNECTION events (needed for nonblocking connections) */ 
 540   _GSocket_Enable(sck
, GSOCK_CONNECTION
); 
 542   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 544     sck
->m_error 
= GSOCK_INVSOCK
; 
 545     return GSOCK_INVSOCK
; 
 550     sck
->m_error 
= GSOCK_INVADDR
; 
 551     return GSOCK_INVADDR
; 
 554   /* Streamed or dgram socket? */ 
 555   sck
->m_stream   
= (stream 
== GSOCK_STREAMED
); 
 556   sck
->m_oriented 
= TRUE
; 
 557   sck
->m_server   
= FALSE
; 
 558   sck
->m_establishing 
= FALSE
; 
 560   /* Create the socket */ 
 561   sck
->m_fd 
= socket(sck
->m_peer
->m_realfamily
, 
 562                      sck
->m_stream
? SOCK_STREAM 
: SOCK_DGRAM
, 0); 
 564   if (sck
->m_fd 
== INVALID_SOCKET
) 
 566     sck
->m_error 
= GSOCK_IOERR
; 
 570   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 571   _GSocket_Enable_Events(sck
); 
 573   /* Connect it to the peer address, with a timeout (see below) */ 
 574   ret 
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
); 
 580     /* If connect failed with EINPROGRESS and the GSocket object 
 581      * is in blocking mode, we select() for the specified timeout 
 582      * checking for writability to see if the connection request 
 585     if ((err 
== EINPROGRESS
) && (!sck
->m_non_blocking
)) 
 587       if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
) 
 590         /* sck->m_error is set in _GSocket_Output_Timeout */ 
 591         return GSOCK_TIMEDOUT
; 
 596         SOCKLEN_T len 
= sizeof(error
); 
 598         getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
); 
 601           return GSOCK_NOERROR
; 
 605     /* If connect failed with EINPROGRESS and the GSocket object 
 606      * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK 
 607      * (and return GSOCK_WOULDBLOCK) but we don't close the socket; 
 608      * this way if the connection completes, a GSOCK_CONNECTION 
 609      * event will be generated, if enabled. 
 611     if ((err 
== EINPROGRESS
) && (sck
->m_non_blocking
)) 
 613       sck
->m_establishing 
= TRUE
; 
 614       sck
->m_error 
= GSOCK_WOULDBLOCK
; 
 615       return GSOCK_WOULDBLOCK
; 
 618     /* If connect failed with an error other than EINPROGRESS, 
 619      * then the call to GSocket_Connect has failed. 
 622     sck
->m_error 
= GSOCK_IOERR
; 
 626   return GSOCK_NOERROR
; 
 629 /* Datagram sockets */ 
 631 /* GSocket_SetNonOriented: 
 632  *  Sets up this socket as a non-connection oriented (datagram) socket. 
 633  *  Before using this function, the local address must have been set 
 634  *  with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR 
 635  *  on success, or one of the following otherwise. 
 638  *    GSOCK_INVSOCK - the socket is in use. 
 639  *    GSOCK_INVADDR - the local address has not been set. 
 640  *    GSOCK_IOERR   - low-level error. 
 642 GSocketError 
GSocket_SetNonOriented(GSocket 
*sck
) 
 648   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 650     sck
->m_error 
= GSOCK_INVSOCK
; 
 651     return GSOCK_INVSOCK
; 
 656     sck
->m_error 
= GSOCK_INVADDR
; 
 657     return GSOCK_INVADDR
; 
 660   /* Initialize all fields */ 
 661   sck
->m_stream   
= FALSE
; 
 662   sck
->m_server   
= FALSE
; 
 663   sck
->m_oriented 
= FALSE
; 
 665   /* Create the socket */ 
 666   sck
->m_fd 
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0); 
 668   if (sck
->m_fd 
== INVALID_SOCKET
) 
 670     sck
->m_error 
= GSOCK_IOERR
; 
 674   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 675   _GSocket_Enable_Events(sck
); 
 677   /* Bind to the local address, 
 678    * and retrieve the actual address bound. 
 680   if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) || 
 681       (getsockname(sck
->m_fd
, 
 682                    sck
->m_local
->m_addr
, 
 683                    (SOCKLEN_T 
*) &sck
->m_local
->m_len
) != 0)) 
 686     sck
->m_error 
= GSOCK_IOERR
; 
 690   return GSOCK_NOERROR
; 
 695 /* Like recv(), send(), ... */ 
 696 int GSocket_Read(GSocket 
*socket
, char *buffer
, int size
) 
 700   assert(socket 
!= NULL
); 
 702   /* Reenable INPUT events */ 
 703   _GSocket_Enable(socket
, GSOCK_INPUT
); 
 705   if (socket
->m_fd 
== INVALID_SOCKET 
|| socket
->m_server
) 
 707     socket
->m_error 
= GSOCK_INVSOCK
; 
 711   /* If the socket is blocking, wait for data (with a timeout) */ 
 712   if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 716   if (socket
->m_stream
) 
 717     ret 
= _GSocket_Recv_Stream(socket
, buffer
, size
); 
 719     ret 
= _GSocket_Recv_Dgram(socket
, buffer
, size
); 
 723     if (errno 
== EWOULDBLOCK
) 
 724       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 726       socket
->m_error 
= GSOCK_IOERR
; 
 732 int GSocket_Write(GSocket 
*socket
, const char *buffer
, int size
) 
 736   assert(socket 
!= NULL
); 
 738   GSocket_Debug(( "GSocket_Write #1, size %d\n", size 
)); 
 740   if (socket
->m_fd 
== INVALID_SOCKET 
|| socket
->m_server
) 
 742     socket
->m_error 
= GSOCK_INVSOCK
; 
 746   GSocket_Debug(( "GSocket_Write #2, size %d\n", size 
)); 
 748   /* If the socket is blocking, wait for writability (with a timeout) */ 
 749   if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 752   GSocket_Debug(( "GSocket_Write #3, size %d\n", size 
)); 
 755   if (socket
->m_stream
) 
 756     ret 
= _GSocket_Send_Stream(socket
, buffer
, size
); 
 758     ret 
= _GSocket_Send_Dgram(socket
, buffer
, size
); 
 760   GSocket_Debug(( "GSocket_Write #4, size %d\n", size 
)); 
 764     if (errno 
== EWOULDBLOCK
) 
 766       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 767       GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); 
 771       socket
->m_error 
= GSOCK_IOERR
; 
 772       GSocket_Debug(( "GSocket_Write error IOERR\n" )); 
 775     /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect 
 776      * in MSW). Once the first OUTPUT event is received, users can assume 
 777      * that the socket is writable until a read operation fails. Only then 
 778      * will further OUTPUT events be posted. 
 780     _GSocket_Enable(socket
, GSOCK_OUTPUT
); 
 784   GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret 
)); 
 790  *  Polls the socket to determine its status. This function will 
 791  *  check for the events specified in the 'flags' parameter, and 
 792  *  it will return a mask indicating which operations can be 
 793  *  performed. This function won't block, regardless of the 
 794  *  mode (blocking | nonblocking) of the socket. 
 796 GSocketEventFlags 
GSocket_Select(GSocket 
*socket
, GSocketEventFlags flags
) 
 798 #if defined(wxUSE_GUI) && !wxUSE_GUI 
 800   GSocketEventFlags result 
= 0; 
 806   /* Do not use a static struct, Linux can garble it */ 
 810   assert(socket 
!= NULL
); 
 815   FD_SET(socket
->m_fd
, &readfds
); 
 816   FD_SET(socket
->m_fd
, &writefds
); 
 817   FD_SET(socket
->m_fd
, &exceptfds
); 
 819   /* Check 'sticky' CONNECTION flag first */ 
 820   result 
|= (GSOCK_CONNECTION_FLAG 
& socket
->m_detected
); 
 822   /* If we have already detected a LOST event, then don't try 
 823    * to do any further processing. 
 825   if ((socket
->m_detected 
& GSOCK_LOST_FLAG
) != 0) 
 827     socket
->m_establishing 
= FALSE
; 
 829     return (GSOCK_LOST_FLAG 
& flags
); 
 833   if (select(socket
->m_fd 
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0) 
 835     /* What to do here? */ 
 836     return (result 
& flags
); 
 839   /* Check for readability */ 
 840   if (FD_ISSET(socket
->m_fd
, &readfds
)) 
 844     if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0) 
 846       result 
|= GSOCK_INPUT_FLAG
; 
 850       if (socket
->m_server 
&& socket
->m_stream
) 
 852         result 
|= GSOCK_CONNECTION_FLAG
; 
 853         socket
->m_detected 
|= GSOCK_CONNECTION_FLAG
; 
 857         socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 858         socket
->m_establishing 
= FALSE
; 
 860         /* LOST event: Abort any further processing */ 
 861         return (GSOCK_LOST_FLAG 
& flags
); 
 866   /* Check for writability */ 
 867   if (FD_ISSET(socket
->m_fd
, &writefds
)) 
 869     if (socket
->m_establishing 
&& !socket
->m_server
) 
 872       SOCKLEN_T len 
= sizeof(error
); 
 874       socket
->m_establishing 
= FALSE
; 
 876       getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
); 
 880         socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 882         /* LOST event: Abort any further processing */ 
 883         return (GSOCK_LOST_FLAG 
& flags
); 
 887         result 
|= GSOCK_CONNECTION_FLAG
; 
 888         socket
->m_detected 
|= GSOCK_CONNECTION_FLAG
; 
 893       result 
|= GSOCK_OUTPUT_FLAG
; 
 897   /* Check for exceptions and errors (is this useful in Unices?) */ 
 898   if (FD_ISSET(socket
->m_fd
, &exceptfds
)) 
 900     socket
->m_establishing 
= FALSE
; 
 901     socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 903     /* LOST event: Abort any further processing */ 
 904     return (GSOCK_LOST_FLAG 
& flags
); 
 907   return (result 
& flags
); 
 911   assert(socket 
!= NULL
); 
 912   return flags 
& socket
->m_detected
; 
 914 #endif /* !wxUSE_GUI */ 
 919 /* GSocket_SetNonBlocking: 
 920  *  Sets the socket to non-blocking mode. All IO calls will return 
 923 void GSocket_SetNonBlocking(GSocket 
*socket
, int non_block
) 
 925   assert(socket 
!= NULL
); 
 927   GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) ); 
 929   socket
->m_non_blocking 
= non_block
; 
 932 /* GSocket_SetTimeout: 
 933  *  Sets the timeout for blocking calls. Time is expressed in 
 936 void GSocket_SetTimeout(GSocket 
*socket
, unsigned long millisec
) 
 938   assert(socket 
!= NULL
); 
 940   socket
->m_timeout 
= millisec
; 
 944  *  Returns the last error occured for this socket. Note that successful 
 945  *  operations do not clear this back to GSOCK_NOERROR, so use it only 
 948 GSocketError 
GSocket_GetError(GSocket 
*socket
) 
 950   assert(socket 
!= NULL
); 
 952   return socket
->m_error
; 
 958  *   There is data to be read in the input buffer. If, after a read 
 959  *   operation, there is still data available, the callback function will 
 962  *   The socket is available for writing. That is, the next write call  
 963  *   won't block. This event is generated only once, when the connection is 
 964  *   first established, and then only if a call failed with GSOCK_WOULDBLOCK, 
 965  *   when the output buffer empties again. This means that the app should 
 966  *   assume that it can write since the first OUTPUT event, and no more 
 967  *   OUTPUT events will be generated unless an error occurs. 
 969  *   Connection succesfully established, for client sockets, or incoming 
 970  *   client connection, for server sockets. Wait for this event (also watch 
 971  *   out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. 
 973  *   The connection is lost (or a connection request failed); this could 
 974  *   be due to a failure, or due to the peer closing it gracefully. 
 977 /* GSocket_SetCallback: 
 978  *  Enables the callbacks specified by 'flags'. Note that 'flags' 
 979  *  may be a combination of flags OR'ed toghether, so the same 
 980  *  callback function can be made to accept different events. 
 981  *  The callback function must have the following prototype: 
 983  *  void function(GSocket *socket, GSocketEvent event, char *cdata) 
 985 void GSocket_SetCallback(GSocket 
*socket
, GSocketEventFlags flags
, 
 986                          GSocketCallback callback
, char *cdata
) 
 990   assert(socket 
!= NULL
); 
 992   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
 994     if ((flags 
& (1 << count
)) != 0) 
 996       socket
->m_cbacks
[count
] = callback
; 
 997       socket
->m_data
[count
] = cdata
; 
1002 /* GSocket_UnsetCallback: 
1003  *  Disables all callbacks specified by 'flags', which may be a 
1004  *  combination of flags OR'ed toghether. 
1006 void GSocket_UnsetCallback(GSocket 
*socket
, GSocketEventFlags flags
) 
1010   assert(socket 
!= NULL
); 
1012   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1014     if ((flags 
& (1 << count
)) != 0) 
1016       socket
->m_cbacks
[count
] = NULL
; 
1017       socket
->m_data
[count
] = NULL
; 
1023 #define CALL_CALLBACK(socket, event) {                                  \ 
1024   _GSocket_Disable(socket, event);                                      \ 
1025   if (socket->m_cbacks[event])                                          \ 
1026     socket->m_cbacks[event](socket, event, socket->m_data[event]);      \ 
1030 void _GSocket_Enable(GSocket 
*socket
, GSocketEvent event
) 
1032   socket
->m_detected 
&= ~(1 << event
); 
1033   _GSocket_Install_Callback(socket
, event
); 
1036 void _GSocket_Disable(GSocket 
*socket
, GSocketEvent event
) 
1038   socket
->m_detected 
|= (1 << event
); 
1039   _GSocket_Uninstall_Callback(socket
, event
); 
1042 /* _GSocket_Input_Timeout: 
1043  *  For blocking sockets, wait until data is available or 
1044  *  until timeout ellapses. 
1046 GSocketError 
_GSocket_Input_Timeout(GSocket 
*socket
) 
1052   /* Linux select() will overwrite the struct on return */ 
1053   tv
.tv_sec  
= (socket
->m_timeout 
/ 1000); 
1054   tv
.tv_usec 
= (socket
->m_timeout 
% 1000) * 1000; 
1056   if (!socket
->m_non_blocking
) 
1059     FD_SET(socket
->m_fd
, &readfds
); 
1060     ret 
= select(socket
->m_fd 
+ 1, &readfds
, NULL
, NULL
, &tv
); 
1063       GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); 
1064       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1065       return GSOCK_TIMEDOUT
; 
1069       GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); 
1070       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1071       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1072       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1073       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1074       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1075       return GSOCK_TIMEDOUT
; 
1078   return GSOCK_NOERROR
; 
1081 /* _GSocket_Output_Timeout: 
1082  *  For blocking sockets, wait until data can be sent without 
1083  *  blocking or until timeout ellapses. 
1085 GSocketError 
_GSocket_Output_Timeout(GSocket 
*socket
) 
1091   /* Linux select() will overwrite the struct on return */ 
1092   tv
.tv_sec  
= (socket
->m_timeout 
/ 1000); 
1093   tv
.tv_usec 
= (socket
->m_timeout 
% 1000) * 1000; 
1095   GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) ); 
1097   if (!socket
->m_non_blocking
) 
1100     FD_SET(socket
->m_fd
, &writefds
); 
1101     ret 
= select(socket
->m_fd 
+ 1, NULL
, &writefds
, NULL
, &tv
); 
1104       GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); 
1105       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1106       return GSOCK_TIMEDOUT
; 
1110       GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); 
1111       if (errno 
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); } 
1112       if (errno 
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); } 
1113       if (errno 
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); } 
1114       if (errno 
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); } 
1115       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1116       return GSOCK_TIMEDOUT
; 
1118     if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) { 
1119         GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); 
1122         GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); 
1127     GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); 
1130   return GSOCK_NOERROR
; 
1133 int _GSocket_Recv_Stream(GSocket 
*socket
, char *buffer
, int size
) 
1135   return recv(socket
->m_fd
, buffer
, size
, 0); 
1138 int _GSocket_Recv_Dgram(GSocket 
*socket
, char *buffer
, int size
) 
1140   struct sockaddr from
; 
1141   SOCKLEN_T fromlen 
= sizeof(from
); 
1145   fromlen 
= sizeof(from
); 
1147   ret 
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T 
*) &fromlen
); 
1152   /* Translate a system address into a GSocket address */ 
1153   if (!socket
->m_peer
) 
1155     socket
->m_peer 
= GAddress_new(); 
1156     if (!socket
->m_peer
) 
1158       socket
->m_error 
= GSOCK_MEMERR
; 
1162   err 
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
); 
1163   if (err 
!= GSOCK_NOERROR
) 
1165     GAddress_destroy(socket
->m_peer
); 
1166     socket
->m_peer  
= NULL
; 
1167     socket
->m_error 
= err
; 
1174 int _GSocket_Send_Stream(GSocket 
*socket
, const char *buffer
, int size
) 
1179   ret 
= send(socket
->m_fd
, buffer
, size
, 0); 
1185 int _GSocket_Send_Dgram(GSocket 
*socket
, const char *buffer
, int size
) 
1187   struct sockaddr 
*addr
; 
1191   if (!socket
->m_peer
) 
1193     socket
->m_error 
= GSOCK_INVADDR
; 
1197   err 
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
); 
1198   if (err 
!= GSOCK_NOERROR
) 
1200     socket
->m_error 
= err
; 
1205   ret 
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
); 
1208   /* Frees memory allocated from _GAddress_translate_to */ 
1214 void _GSocket_Detected_Read(GSocket 
*socket
) 
1218   /* If we have already detected a LOST event, then don't try 
1219    * to do any further processing. 
1221   if ((socket
->m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1223     socket
->m_establishing 
= FALSE
; 
1225     CALL_CALLBACK(socket
, GSOCK_LOST
); 
1226     GSocket_Shutdown(socket
); 
1230   if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0) 
1232     CALL_CALLBACK(socket
, GSOCK_INPUT
); 
1236     if (socket
->m_server 
&& socket
->m_stream
) 
1238       CALL_CALLBACK(socket
, GSOCK_CONNECTION
); 
1242       CALL_CALLBACK(socket
, GSOCK_LOST
); 
1243       GSocket_Shutdown(socket
); 
1248 void _GSocket_Detected_Write(GSocket 
*socket
) 
1250   /* If we have already detected a LOST event, then don't try 
1251    * to do any further processing. 
1253   if ((socket
->m_detected 
& GSOCK_LOST_FLAG
) != 0) 
1255     socket
->m_establishing 
= FALSE
; 
1257     CALL_CALLBACK(socket
, GSOCK_LOST
); 
1258     GSocket_Shutdown(socket
); 
1262   if (socket
->m_establishing 
&& !socket
->m_server
) 
1265     SOCKLEN_T len 
= sizeof(error
); 
1267     socket
->m_establishing 
= FALSE
; 
1269     getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
); 
1273       CALL_CALLBACK(socket
, GSOCK_LOST
); 
1274       GSocket_Shutdown(socket
); 
1278       CALL_CALLBACK(socket
, GSOCK_CONNECTION
); 
1279       /* We have to fire this event by hand because CONNECTION (for clients) 
1280        * and OUTPUT are internally the same and we just disabled CONNECTION 
1281        * events with the above macro. 
1283       CALL_CALLBACK(socket
, GSOCK_OUTPUT
); 
1288     CALL_CALLBACK(socket
, GSOCK_OUTPUT
); 
1293  * ------------------------------------------------------------------------- 
1295  * ------------------------------------------------------------------------- 
1298 /* CHECK_ADDRESS verifies that the current address family is either 
1299  * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it 
1300  * initalizes it to be a GSOCK_*family*. In other cases, it returns 
1301  * an appropiate error code. 
1303  * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. 
1305 #define CHECK_ADDRESS(address, family)                              \ 
1307   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1308     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1309       return address->m_error;                                      \ 
1310   if (address->m_family != GSOCK_##family)                          \ 
1312     address->m_error = GSOCK_INVADDR;                               \ 
1313     return GSOCK_INVADDR;                                           \ 
1317 #define CHECK_ADDRESS_RETVAL(address, family, retval)               \ 
1319   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1320     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1322   if (address->m_family != GSOCK_##family)                          \ 
1324     address->m_error = GSOCK_INVADDR;                               \ 
1330 GAddress 
*GAddress_new(void) 
1334   if ((address 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1337   address
->m_family  
= GSOCK_NOFAMILY
; 
1338   address
->m_addr    
= NULL
; 
1344 GAddress 
*GAddress_copy(GAddress 
*address
) 
1348   assert(address 
!= NULL
); 
1350   if ((addr2 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1353   memcpy(addr2
, address
, sizeof(GAddress
)); 
1355   if (address
->m_addr 
&& address
->m_len 
> 0) 
1357     addr2
->m_addr 
= (struct sockaddr 
*)malloc(addr2
->m_len
); 
1358     if (addr2
->m_addr 
== NULL
) 
1363     memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
); 
1369 void GAddress_destroy(GAddress 
*address
) 
1371   assert(address 
!= NULL
); 
1373   if (address
->m_addr
) 
1374     free(address
->m_addr
); 
1379 void GAddress_SetFamily(GAddress 
*address
, GAddressType type
) 
1381   assert(address 
!= NULL
); 
1383   address
->m_family 
= type
; 
1386 GAddressType 
GAddress_GetFamily(GAddress 
*address
) 
1388   assert(address 
!= NULL
); 
1390   return address
->m_family
; 
1393 GSocketError 
_GAddress_translate_from(GAddress 
*address
, 
1394                                       struct sockaddr 
*addr
, int len
) 
1396   address
->m_realfamily 
= addr
->sa_family
; 
1397   switch (addr
->sa_family
) 
1400       address
->m_family 
= GSOCK_INET
; 
1403       address
->m_family 
= GSOCK_UNIX
; 
1407       address
->m_family 
= GSOCK_INET6
; 
1412       address
->m_error 
= GSOCK_INVOP
; 
1417   if (address
->m_addr
) 
1418     free(address
->m_addr
); 
1420   address
->m_len  
= len
; 
1421   address
->m_addr 
= (struct sockaddr 
*)malloc(len
); 
1423   if (address
->m_addr 
== NULL
) 
1425     address
->m_error 
= GSOCK_MEMERR
; 
1426     return GSOCK_MEMERR
; 
1428   memcpy(address
->m_addr
, addr
, len
); 
1430   return GSOCK_NOERROR
; 
1433 GSocketError 
_GAddress_translate_to(GAddress 
*address
, 
1434                                     struct sockaddr 
**addr
, int *len
) 
1436   if (!address
->m_addr
) 
1438     address
->m_error 
= GSOCK_INVADDR
; 
1439     return GSOCK_INVADDR
; 
1442   *len 
= address
->m_len
; 
1443   *addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1446     address
->m_error 
= GSOCK_MEMERR
; 
1447     return GSOCK_MEMERR
; 
1450   memcpy(*addr
, address
->m_addr
, address
->m_len
); 
1451   return GSOCK_NOERROR
; 
1455  * ------------------------------------------------------------------------- 
1456  * Internet address family 
1457  * ------------------------------------------------------------------------- 
1460 GSocketError 
_GAddress_Init_INET(GAddress 
*address
) 
1462   address
->m_len  
= sizeof(struct sockaddr_in
); 
1463   address
->m_addr 
= (struct sockaddr 
*) malloc(address
->m_len
); 
1464   if (address
->m_addr 
== NULL
) 
1466     address
->m_error 
= GSOCK_MEMERR
; 
1467     return GSOCK_MEMERR
; 
1470   address
->m_family 
= GSOCK_INET
; 
1471   address
->m_realfamily 
= PF_INET
; 
1472   ((struct sockaddr_in 
*)address
->m_addr
)->sin_family 
= AF_INET
; 
1473   ((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
.s_addr 
= INADDR_ANY
; 
1475   return GSOCK_NOERROR
; 
1478 GSocketError 
GAddress_INET_SetHostName(GAddress 
*address
, const char *hostname
) 
1481   struct in_addr 
*addr
; 
1483   assert(address 
!= NULL
); 
1485   CHECK_ADDRESS(address
, INET
); 
1487   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1489   /* If it is a numeric host name, convert it now */ 
1490 #if defined(HAVE_INET_ATON) 
1491   if (inet_aton(hostname
, addr
) == 0) 
1493 #elif defined(HAVE_INET_ADDR) 
1494   if ( (addr
->s_addr 
= inet_addr(hostname
)) == -1 ) 
1497   /* Use gethostbyname by default */ 
1501     struct in_addr 
*array_addr
; 
1503     /* It is a real name, we solve it */ 
1504     if ((he 
= gethostbyname(hostname
)) == NULL
) 
1506       /* Reset to invalid address */ 
1507       addr
->s_addr 
= INADDR_NONE
; 
1508       address
->m_error 
= GSOCK_NOHOST
; 
1509       return GSOCK_NOHOST
; 
1511     array_addr 
= (struct in_addr 
*) *(he
->h_addr_list
); 
1512     addr
->s_addr 
= array_addr
[0].s_addr
; 
1514   return GSOCK_NOERROR
; 
1517 GSocketError 
GAddress_INET_SetAnyAddress(GAddress 
*address
) 
1519   return GAddress_INET_SetHostAddress(address
, INADDR_ANY
); 
1522 GSocketError 
GAddress_INET_SetHostAddress(GAddress 
*address
, 
1523                                           unsigned long hostaddr
) 
1525   struct in_addr 
*addr
; 
1527   assert(address 
!= NULL
); 
1529   CHECK_ADDRESS(address
, INET
); 
1531   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1532   addr
->s_addr 
= hostaddr
; 
1534   return GSOCK_NOERROR
; 
1537 GSocketError 
GAddress_INET_SetPortName(GAddress 
*address
, const char *port
, 
1538                                        const char *protocol
) 
1541   struct sockaddr_in 
*addr
; 
1543   assert(address 
!= NULL
); 
1544   CHECK_ADDRESS(address
, INET
); 
1548     address
->m_error 
= GSOCK_INVPORT
; 
1549     return GSOCK_INVPORT
; 
1552   se 
= getservbyname(port
, protocol
); 
1555     /* the cast to int suppresses compiler warnings about subscript having the 
1557     if (isdigit((int)port
[0])) 
1561       port_int 
= atoi(port
); 
1562       addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1563       addr
->sin_port 
= htons(port_int
); 
1564       return GSOCK_NOERROR
; 
1567     address
->m_error 
= GSOCK_INVPORT
; 
1568     return GSOCK_INVPORT
; 
1571   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1572   addr
->sin_port 
= se
->s_port
; 
1574   return GSOCK_NOERROR
; 
1577 GSocketError 
GAddress_INET_SetPort(GAddress 
*address
, unsigned short port
) 
1579   struct sockaddr_in 
*addr
; 
1581   assert(address 
!= NULL
); 
1582   CHECK_ADDRESS(address
, INET
); 
1584   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1585   addr
->sin_port 
= htons(port
); 
1587   return GSOCK_NOERROR
; 
1590 GSocketError 
GAddress_INET_GetHostName(GAddress 
*address
, char *hostname
, size_t sbuf
) 
1594   struct sockaddr_in 
*addr
; 
1596   assert(address 
!= NULL
);  
1597   CHECK_ADDRESS(address
, INET
); 
1599   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1600   addr_buf 
= (char *)&(addr
->sin_addr
); 
1602   he 
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
); 
1605     address
->m_error 
= GSOCK_NOHOST
; 
1606     return GSOCK_NOHOST
; 
1609   strncpy(hostname
, he
->h_name
, sbuf
); 
1611   return GSOCK_NOERROR
; 
1614 unsigned long GAddress_INET_GetHostAddress(GAddress 
*address
) 
1616   struct sockaddr_in 
*addr
; 
1618   assert(address 
!= NULL
);  
1619   CHECK_ADDRESS_RETVAL(address
, INET
, 0);  
1621   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1623   return addr
->sin_addr
.s_addr
; 
1626 unsigned short GAddress_INET_GetPort(GAddress 
*address
) 
1628   struct sockaddr_in 
*addr
; 
1630   assert(address 
!= NULL
);  
1631   CHECK_ADDRESS_RETVAL(address
, INET
, 0);  
1633   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1634   return ntohs(addr
->sin_port
); 
1638  * ------------------------------------------------------------------------- 
1639  * Unix address family 
1640  * ------------------------------------------------------------------------- 
1643 GSocketError 
_GAddress_Init_UNIX(GAddress 
*address
) 
1645   address
->m_len  
= sizeof(struct sockaddr_un
); 
1646   address
->m_addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1647   if (address
->m_addr 
== NULL
) 
1649     address
->m_error 
= GSOCK_MEMERR
; 
1650     return GSOCK_MEMERR
; 
1653   address
->m_family 
= GSOCK_UNIX
; 
1654   address
->m_realfamily 
= PF_UNIX
; 
1655   ((struct sockaddr_un 
*)address
->m_addr
)->sun_family 
= AF_UNIX
; 
1656   ((struct sockaddr_un 
*)address
->m_addr
)->sun_path
[0] = 0; 
1658   return GSOCK_NOERROR
; 
1661 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0])) 
1663 GSocketError 
GAddress_UNIX_SetPath(GAddress 
*address
, const char *path
) 
1665   struct sockaddr_un 
*addr
; 
1667   assert(address 
!= NULL
);  
1669   CHECK_ADDRESS(address
, UNIX
);  
1671   addr 
= ((struct sockaddr_un 
*)address
->m_addr
); 
1672   strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
); 
1673   addr
->sun_path
[UNIX_SOCK_PATHLEN 
- 1] = '\0'; 
1675   return GSOCK_NOERROR
; 
1678 GSocketError 
GAddress_UNIX_GetPath(GAddress 
*address
, char *path
, size_t sbuf
) 
1680   struct sockaddr_un 
*addr
; 
1682   assert(address 
!= NULL
); 
1683   CHECK_ADDRESS(address
, UNIX
); 
1685   addr 
= (struct sockaddr_un 
*)address
->m_addr
; 
1687   strncpy(path
, addr
->sun_path
, sbuf
); 
1689   return GSOCK_NOERROR
; 
1692 #endif  /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */