]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/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-style file 
   8  * ------------------------------------------------------------------------- 
  12  * PLEASE don't put C++ comments here - this is a C source file. 
  15 #ifndef __GSOCKET_STANDALONE__ 
  20 /* I don't see, why this include is needed, but it seems to be necessary 
  21    sometimes. For EMX, including C++ headers into plain C source breaks 
  22    compilation, so don't do it there.                                   */ 
  26 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) 
  28 #define BSD_SELECT /* use Berkley Sockets select */ 
  31 #include <sys\types.h> 
  35 #include <netinet/in.h> 
  36 #include <arpa/inet.h> 
  41 #define HAVE_INET_ADDR 
  54 #if defined(__VISAGECPP__) && __IBMCPP__ < 400 
  56 #include <machine\endian.h> 
  62 #define EBADF   SOCEBADF 
  66 #include <sys\socket.h> 
  67 #include <sys\ioctl.h> 
  68 #include <sys\select.h> 
  71 #define soclose(a) close(a) 
  73 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e) 
  74 int _System 
bsdselect(int, 
  79 int _System 
soclose(int); 
  85 #if (defined(__VISAGECPP__) && __IBMCPP__ < 400) || defined(__EMX__) 
 100 #         define SOCKLEN_T socklen_t 
 103 #      define SOCKLEN_T int 
 109  * MSW defines this, Unices don't. 
 111 #ifndef INVALID_SOCKET 
 112 #define INVALID_SOCKET -1 
 116  * INADDR_BROADCAST is identical to INADDR_NONE which is not defined 
 117  * on all systems. INADDR_BROADCAST should be fine to indicate an error. 
 120 #define INADDR_NONE INADDR_BROADCAST 
 123 #define MASK_SIGNAL()                       \ 
 125   void (*old_handler)(int);                 \ 
 127   old_handler = signal(SIGPIPE, SIG_IGN); 
 129 #define UNMASK_SIGNAL()                     \ 
 130   signal(SIGPIPE, old_handler);             \ 
 133 #ifndef __GSOCKET_STANDALONE__ 
 134 #  include "wx/unix/gsockunx.h" 
 135 #  include "wx/gsocket.h" 
 137 #  include "gsockunx.h" 
 138 #  include "gsocket.h" 
 139 #endif /* __GSOCKET_STANDALONE__ */ 
 141 /* redefine some GUI-only functions to do nothing in console mode */ 
 142 #if defined(wxUSE_GUI) && !wxUSE_GUI 
 143 #  define _GSocket_GUI_Init(socket) (1) 
 144 #  define _GSocket_GUI_Destroy(socket) 
 145 #  define _GSocket_Enable_Events(socket) 
 146 #  define _GSocket_Disable_Events(socket) 
 147 #  define _GSocket_Install_Callback(socket, event) 
 148 #  define _GSocket_Uninstall_Callback(socket, event) 
 149 #endif /* wxUSE_GUI */ 
 151 /* debugging helpers */ 
 152 #define  __GSOCKET_DEBUG__ 
 153 #ifdef __GSOCKET_DEBUG__ 
 154 #  define GSocket_Debug(args) printf args 
 156 #  define GSocket_Debug(args) 
 157 #endif /* __GSOCKET_DEBUG__ */ 
 159 /* Global initialisers */ 
 161 int GSocket_Init(void) 
 166 void GSocket_Cleanup(void) 
 170 /* Constructors / Destructors for GSocket */ 
 172 GSocket 
*GSocket_new(void) 
 177   socket 
= (GSocket 
*)malloc(sizeof(GSocket
)); 
 182   socket
->m_fd                  
= INVALID_SOCKET
; 
 183   for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) 
 185     socket
->m_cbacks
[i
]         = NULL
; 
 187   socket
->m_detected            
= 0; 
 188   socket
->m_local               
= NULL
; 
 189   socket
->m_peer                
= NULL
; 
 190   socket
->m_error               
= GSOCK_NOERROR
; 
 191   socket
->m_server              
= FALSE
; 
 192   socket
->m_stream              
= TRUE
; 
 193   socket
->m_gui_dependent       
= NULL
; 
 194   socket
->m_non_blocking        
= FALSE
; 
 195   socket
->m_timeout             
= 10*60*1000; 
 196                                 /* 10 minutes * 60 sec * 1000 millisec */ 
 197   socket
->m_establishing        
= FALSE
; 
 199   /* Per-socket GUI-specific initialization */ 
 200   success 
= _GSocket_GUI_Init(socket
); 
 210 void GSocket_destroy(GSocket 
*socket
) 
 212   assert(socket 
!= NULL
); 
 214   /* Check that the socket is really shutdowned */ 
 215   if (socket
->m_fd 
!= INVALID_SOCKET
) 
 216     GSocket_Shutdown(socket
); 
 218   /* Per-socket GUI-specific cleanup */ 
 219   _GSocket_GUI_Destroy(socket
); 
 221   /* Destroy private addresses */ 
 223     GAddress_destroy(socket
->m_local
); 
 226     GAddress_destroy(socket
->m_peer
); 
 228   /* Destroy the socket itself */ 
 233  *  Disallow further read/write operations on this socket, close 
 234  *  the fd and disable all callbacks. 
 236 void GSocket_Shutdown(GSocket 
*socket
) 
 240   assert(socket 
!= NULL
); 
 242   /* If socket has been created, shutdown it */ 
 243   if (socket
->m_fd 
!= INVALID_SOCKET
) 
 245     shutdown(socket
->m_fd
, 2); 
 246     soclose(socket
->m_fd
); 
 247     socket
->m_fd 
= INVALID_SOCKET
; 
 250   /* Disable GUI callbacks */ 
 251   for (evt 
= 0; evt 
< GSOCK_MAX_EVENT
; evt
++) 
 252     socket
->m_cbacks
[evt
] = NULL
; 
 254   socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 255   _GSocket_Disable_Events(socket
); 
 258 /* Address handling */ 
 264  *  Set or get the local or peer address for this socket. The 'set' 
 265  *  functions return GSOCK_NOERROR on success, an error code otherwise. 
 266  *  The 'get' functions return a pointer to a GAddress object on success, 
 267  *  or NULL otherwise, in which case they set the error code of the 
 268  *  corresponding GSocket. 
 271  *    GSOCK_INVSOCK - the socket is not valid. 
 272  *    GSOCK_INVADDR - the address is not valid. 
 274 GSocketError 
GSocket_SetLocal(GSocket 
*socket
, GAddress 
*address
) 
 276   assert(socket 
!= NULL
); 
 278   /* the socket must be initialized, or it must be a server */ 
 279   if ((socket
->m_fd 
!= INVALID_SOCKET 
&& !socket
->m_server
)) 
 281     socket
->m_error 
= GSOCK_INVSOCK
; 
 282     return GSOCK_INVSOCK
; 
 286   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 288     socket
->m_error 
= GSOCK_INVADDR
; 
 289     return GSOCK_INVADDR
; 
 293     GAddress_destroy(socket
->m_local
); 
 295   socket
->m_local 
= GAddress_copy(address
); 
 297   return GSOCK_NOERROR
; 
 300 GSocketError 
GSocket_SetPeer(GSocket 
*socket
, GAddress 
*address
) 
 302   assert(socket 
!= NULL
); 
 305   if (address 
== NULL 
|| address
->m_family 
== GSOCK_NOFAMILY
) 
 307     socket
->m_error 
= GSOCK_INVADDR
; 
 308     return GSOCK_INVADDR
; 
 312     GAddress_destroy(socket
->m_peer
); 
 314   socket
->m_peer 
= GAddress_copy(address
); 
 316   return GSOCK_NOERROR
; 
 319 GAddress 
*GSocket_GetLocal(GSocket 
*socket
) 
 322   struct sockaddr addr
; 
 323   SOCKLEN_T size 
= sizeof(addr
); 
 326   assert(socket 
!= NULL
); 
 328   /* try to get it from the m_local var first */ 
 330     return GAddress_copy(socket
->m_local
); 
 332   /* else, if the socket is initialized, try getsockname */ 
 333   if (socket
->m_fd 
== INVALID_SOCKET
) 
 335     socket
->m_error 
= GSOCK_INVSOCK
; 
 339   if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T 
*) &size
) < 0) 
 341     socket
->m_error 
= GSOCK_IOERR
; 
 345   /* got a valid address from getsockname, create a GAddress object */ 
 346   address 
= GAddress_new(); 
 349     socket
->m_error 
= GSOCK_MEMERR
; 
 353   err 
= _GAddress_translate_from(address
, &addr
, size
); 
 354   if (err 
!= GSOCK_NOERROR
) 
 356     GAddress_destroy(address
); 
 357     socket
->m_error 
= err
; 
 364 GAddress 
*GSocket_GetPeer(GSocket 
*socket
) 
 366   assert(socket 
!= NULL
); 
 368   /* try to get it from the m_peer var */ 
 370     return GAddress_copy(socket
->m_peer
); 
 375 /* Server specific parts */ 
 377 /* GSocket_SetServer: 
 378  *  Sets up this socket as a server. The local address must have been 
 379  *  set with GSocket_SetLocal() before GSocket_SetServer() is called. 
 380  *  Returns GSOCK_NOERROR on success, one of the following otherwise: 
 383  *    GSOCK_INVSOCK - the socket is in use. 
 384  *    GSOCK_INVADDR - the local address has not been set. 
 385  *    GSOCK_IOERR   - low-level error. 
 387 GSocketError 
GSocket_SetServer(GSocket 
*sck
) 
 393   /* must not be in use */ 
 394   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 396     sck
->m_error 
= GSOCK_INVSOCK
; 
 397     return GSOCK_INVSOCK
; 
 400   /* the local addr must have been set */ 
 403     sck
->m_error 
= GSOCK_INVADDR
; 
 404     return GSOCK_INVADDR
; 
 407   /* Initialize all fields */ 
 408   sck
->m_stream   
= TRUE
; 
 409   sck
->m_server   
= TRUE
; 
 410   sck
->m_oriented 
= TRUE
; 
 412   /* Create the socket */ 
 413   sck
->m_fd 
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0); 
 415   if (sck
->m_fd 
== INVALID_SOCKET
) 
 417     sck
->m_error 
= GSOCK_IOERR
; 
 421   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 422   _GSocket_Enable_Events(sck
); 
 424   /* Bind to the local address, 
 425    * retrieve the actual address bound, 
 426    * and listen up to 5 connections. 
 428   if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) || 
 429       (getsockname(sck
->m_fd
, 
 430                    sck
->m_local
->m_addr
, 
 431                    (SOCKLEN_T 
*) &sck
->m_local
->m_len
) != 0) || 
 432       (listen(sck
->m_fd
, 5) != 0)) 
 435     sck
->m_fd 
= INVALID_SOCKET
; 
 436     sck
->m_error 
= GSOCK_IOERR
; 
 440   return GSOCK_NOERROR
; 
 443 /* GSocket_WaitConnection: 
 444  *  Waits for an incoming client connection. Returns a pointer to 
 445  *  a GSocket object, or NULL if there was an error, in which case 
 446  *  the last error field will be updated for the calling GSocket. 
 448  *  Error codes (set in the calling GSocket) 
 449  *    GSOCK_INVSOCK    - the socket is not valid or not a server. 
 450  *    GSOCK_TIMEDOUT   - timeout, no incoming connections. 
 451  *    GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. 
 452  *    GSOCK_MEMERR     - couldn't allocate memory. 
 453  *    GSOCK_IOERR      - low-level error. 
 455 GSocket 
*GSocket_WaitConnection(GSocket 
*socket
) 
 457   struct sockaddr from
; 
 458   SOCKLEN_T fromlen 
= sizeof(from
); 
 463   assert(socket 
!= NULL
); 
 465   /* Reenable CONNECTION events */ 
 466   _GSocket_Enable(socket
, GSOCK_CONNECTION
); 
 468   /* If the socket has already been created, we exit immediately */ 
 469   if (socket
->m_fd 
== INVALID_SOCKET 
|| !socket
->m_server
) 
 471     socket
->m_error 
= GSOCK_INVSOCK
; 
 475   /* Create a GSocket object for the new connection */ 
 476   connection 
= GSocket_new(); 
 480     socket
->m_error 
= GSOCK_MEMERR
; 
 484   /* Wait for a connection (with timeout) */ 
 485   if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 487     GSocket_destroy(connection
); 
 488     /* socket->m_error set by _GSocket_Input_Timeout */ 
 492   connection
->m_fd 
= accept(socket
->m_fd
, &from
, (SOCKLEN_T 
*) &fromlen
); 
 494   if (connection
->m_fd 
== INVALID_SOCKET
) 
 496     if (errno 
== EWOULDBLOCK
) 
 497       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 499       socket
->m_error 
= GSOCK_IOERR
; 
 501     GSocket_destroy(connection
); 
 505   /* Initialize all fields */ 
 506   connection
->m_server   
= FALSE
; 
 507   connection
->m_stream   
= TRUE
; 
 508   connection
->m_oriented 
= TRUE
; 
 510   /* Setup the peer address field */ 
 511   connection
->m_peer 
= GAddress_new(); 
 512   if (!connection
->m_peer
) 
 514     GSocket_destroy(connection
); 
 515     socket
->m_error 
= GSOCK_MEMERR
; 
 518   err 
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
); 
 519   if (err 
!= GSOCK_NOERROR
) 
 521     GAddress_destroy(connection
->m_peer
); 
 522     GSocket_destroy(connection
); 
 523     socket
->m_error 
= err
; 
 527   ioctl(connection
->m_fd
, FIONBIO
, &arg
); 
 528   _GSocket_Enable_Events(connection
); 
 533 /* Client specific parts */ 
 536  *  For stream (connection oriented) sockets, GSocket_Connect() tries 
 537  *  to establish a client connection to a server using the peer address 
 538  *  as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the 
 539  *  connection has been succesfully established, or one of the error 
 540  *  codes listed below. Note that for nonblocking sockets, a return 
 541  *  value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection 
 542  *  request can be completed later; you should use GSocket_Select() 
 543  *  to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the 
 544  *  corresponding asynchronous events. 
 546  *  For datagram (non connection oriented) sockets, GSocket_Connect() 
 547  *  just sets the peer address established with GSocket_SetPeer() as 
 548  *  default destination. 
 551  *    GSOCK_INVSOCK    - the socket is in use or not valid. 
 552  *    GSOCK_INVADDR    - the peer address has not been established. 
 553  *    GSOCK_TIMEDOUT   - timeout, the connection failed. 
 554  *    GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) 
 555  *    GSOCK_MEMERR     - couldn't allocate memory. 
 556  *    GSOCK_IOERR      - low-level error. 
 558 GSocketError 
GSocket_Connect(GSocket 
*sck
, GSocketStream stream
) 
 565   /* Enable CONNECTION events (needed for nonblocking connections) */ 
 566   _GSocket_Enable(sck
, GSOCK_CONNECTION
); 
 568   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 570     sck
->m_error 
= GSOCK_INVSOCK
; 
 571     return GSOCK_INVSOCK
; 
 576     sck
->m_error 
= GSOCK_INVADDR
; 
 577     return GSOCK_INVADDR
; 
 580   /* Streamed or dgram socket? */ 
 581   sck
->m_stream   
= (stream 
== GSOCK_STREAMED
); 
 582   sck
->m_oriented 
= TRUE
; 
 583   sck
->m_server   
= FALSE
; 
 584   sck
->m_establishing 
= FALSE
; 
 586   /* Create the socket */ 
 587   sck
->m_fd 
= socket(sck
->m_peer
->m_realfamily
, 
 588                      sck
->m_stream
? SOCK_STREAM 
: SOCK_DGRAM
, 0); 
 590   if (sck
->m_fd 
== INVALID_SOCKET
) 
 592     sck
->m_error 
= GSOCK_IOERR
; 
 596   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 597   _GSocket_Enable_Events(sck
); 
 599   /* Connect it to the peer address, with a timeout (see below) */ 
 600   ret 
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
); 
 602   printf("connect on %d to %X (%d) returned %d, errno = %d\n", 
 603          sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
, ret
, errno
); 
 608     /* If connect failed with EINPROGRESS and the GSocket object 
 609      * is in blocking mode, we select() for the specified timeout 
 610      * checking for writability to see if the connection request 
 613     if ((err 
== EINPROGRESS
) && (!sck
->m_non_blocking
)) 
 615       if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
) 
 618         sck
->m_fd 
= INVALID_SOCKET
; 
 619         /* sck->m_error is set in _GSocket_Output_Timeout */ 
 620         return GSOCK_TIMEDOUT
; 
 625         SOCKLEN_T len 
= sizeof(error
); 
 627         getsockopt(sck
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
); 
 630           return GSOCK_NOERROR
; 
 634     /* If connect failed with EINPROGRESS and the GSocket object 
 635      * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK 
 636      * (and return GSOCK_WOULDBLOCK) but we don't close the socket; 
 637      * this way if the connection completes, a GSOCK_CONNECTION 
 638      * event will be generated, if enabled. 
 640     if ((err 
== EINPROGRESS
) && (sck
->m_non_blocking
)) 
 642       sck
->m_establishing 
= TRUE
; 
 643       sck
->m_error 
= GSOCK_WOULDBLOCK
; 
 644       return GSOCK_WOULDBLOCK
; 
 647     /* If connect failed with an error other than EINPROGRESS, 
 648      * then the call to GSocket_Connect has failed. 
 651     sck
->m_fd 
= INVALID_SOCKET
; 
 652     sck
->m_error 
= GSOCK_IOERR
; 
 656   return GSOCK_NOERROR
; 
 659 /* Datagram sockets */ 
 661 /* GSocket_SetNonOriented: 
 662  *  Sets up this socket as a non-connection oriented (datagram) socket. 
 663  *  Before using this function, the local address must have been set 
 664  *  with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR 
 665  *  on success, or one of the following otherwise. 
 668  *    GSOCK_INVSOCK - the socket is in use. 
 669  *    GSOCK_INVADDR - the local address has not been set. 
 670  *    GSOCK_IOERR   - low-level error. 
 672 GSocketError 
GSocket_SetNonOriented(GSocket 
*sck
) 
 678   if (sck
->m_fd 
!= INVALID_SOCKET
) 
 680     sck
->m_error 
= GSOCK_INVSOCK
; 
 681     return GSOCK_INVSOCK
; 
 686     sck
->m_error 
= GSOCK_INVADDR
; 
 687     return GSOCK_INVADDR
; 
 690   /* Initialize all fields */ 
 691   sck
->m_stream   
= FALSE
; 
 692   sck
->m_server   
= FALSE
; 
 693   sck
->m_oriented 
= FALSE
; 
 695   /* Create the socket */ 
 696   sck
->m_fd 
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0); 
 698   if (sck
->m_fd 
== INVALID_SOCKET
) 
 700     sck
->m_error 
= GSOCK_IOERR
; 
 704   ioctl(sck
->m_fd
, FIONBIO
, &arg
); 
 705   _GSocket_Enable_Events(sck
); 
 707   /* Bind to the local address, 
 708    * and retrieve the actual address bound. 
 710   if ((bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) != 0) || 
 711       (getsockname(sck
->m_fd
, 
 712                    sck
->m_local
->m_addr
, 
 713                    (SOCKLEN_T 
*) &sck
->m_local
->m_len
) != 0)) 
 716     sck
->m_fd    
= INVALID_SOCKET
; 
 717     sck
->m_error 
= GSOCK_IOERR
; 
 721   return GSOCK_NOERROR
; 
 726 /* Like recv(), send(), ... */ 
 727 int GSocket_Read(GSocket 
*socket
, char *buffer
, int size
) 
 731   assert(socket 
!= NULL
); 
 733   /* Reenable INPUT events */ 
 734   _GSocket_Enable(socket
, GSOCK_INPUT
); 
 736   if (socket
->m_fd 
== INVALID_SOCKET 
|| socket
->m_server
) 
 738     socket
->m_error 
= GSOCK_INVSOCK
; 
 742   /* If the socket is blocking, wait for data (with a timeout) */ 
 743   if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 747   if (socket
->m_stream
) 
 748     ret 
= _GSocket_Recv_Stream(socket
, buffer
, size
); 
 750     ret 
= _GSocket_Recv_Dgram(socket
, buffer
, size
); 
 754     if (errno 
== EWOULDBLOCK
) 
 755       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 757       socket
->m_error 
= GSOCK_IOERR
; 
 763 int GSocket_Write(GSocket 
*socket
, const char *buffer
, int size
) 
 767   assert(socket 
!= NULL
); 
 769   GSocket_Debug(( "GSocket_Write #1, size %d\n", size 
)); 
 771   if (socket
->m_fd 
== INVALID_SOCKET 
|| socket
->m_server
) 
 773     socket
->m_error 
= GSOCK_INVSOCK
; 
 777   GSocket_Debug(( "GSocket_Write #2, size %d\n", size 
)); 
 779   /* If the socket is blocking, wait for writability (with a timeout) */ 
 780   if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
) 
 783   GSocket_Debug(( "GSocket_Write #3, size %d\n", size 
)); 
 786   if (socket
->m_stream
) 
 787     ret 
= _GSocket_Send_Stream(socket
, buffer
, size
); 
 789     ret 
= _GSocket_Send_Dgram(socket
, buffer
, size
); 
 791   GSocket_Debug(( "GSocket_Write #4, size %d\n", size 
)); 
 795     if (errno 
== EWOULDBLOCK
) 
 797       socket
->m_error 
= GSOCK_WOULDBLOCK
; 
 798       GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" )); 
 802       socket
->m_error 
= GSOCK_IOERR
; 
 803       GSocket_Debug(( "GSocket_Write error IOERR\n" )); 
 806     /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect 
 807      * in MSW). Once the first OUTPUT event is received, users can assume 
 808      * that the socket is writable until a read operation fails. Only then 
 809      * will further OUTPUT events be posted. 
 811     _GSocket_Enable(socket
, GSOCK_OUTPUT
); 
 815   GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret 
)); 
 821  *  Polls the socket to determine its status. This function will 
 822  *  check for the events specified in the 'flags' parameter, and 
 823  *  it will return a mask indicating which operations can be 
 824  *  performed. This function won't block, regardless of the 
 825  *  mode (blocking | nonblocking) of the socket. 
 827 GSocketEventFlags 
GSocket_Select(GSocket 
*socket
, GSocketEventFlags flags
) 
 829 #if defined(wxUSE_GUI) && !wxUSE_GUI 
 831   GSocketEventFlags result 
= 0; 
 837   /* Do not use a static struct, Linux can garble it */ 
 841   assert(socket 
!= NULL
); 
 846   FD_SET(socket
->m_fd
, &readfds
); 
 847   FD_SET(socket
->m_fd
, &writefds
); 
 848   FD_SET(socket
->m_fd
, &exceptfds
); 
 850   /* Check known state first */ 
 851   result 
|= (GSOCK_CONNECTION_FLAG 
& socket
->m_detected 
& flags
); 
 852   result 
|= (GSOCK_LOST_FLAG       
& socket
->m_detected 
& flags
); 
 855   if (select(socket
->m_fd 
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0) 
 858   /* Check for readability */ 
 859   if (FD_ISSET(socket
->m_fd
, &readfds
)) 
 863     if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0) 
 865       result 
|= (GSOCK_INPUT_FLAG 
& flags
); 
 869       if (socket
->m_server 
&& socket
->m_stream
) 
 871         result 
|= (GSOCK_CONNECTION_FLAG 
& flags
); 
 872         socket
->m_detected 
|= GSOCK_CONNECTION_FLAG
; 
 876         result 
|= (GSOCK_LOST_FLAG 
& flags
); 
 877         socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 882   /* Check for writability */ 
 883   if (FD_ISSET(socket
->m_fd
, &writefds
)) 
 885     if (socket
->m_establishing 
&& !socket
->m_server
) 
 888       SOCKLEN_T len 
= sizeof(error
); 
 890       socket
->m_establishing 
= FALSE
; 
 892       getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
); 
 896         result 
|= (GSOCK_LOST_FLAG 
& flags
); 
 897         socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 901         result 
|= (GSOCK_CONNECTION_FLAG 
& flags
); 
 902         socket
->m_detected 
|= GSOCK_CONNECTION_FLAG
; 
 907       result 
|= (GSOCK_OUTPUT_FLAG 
& flags
); 
 911   /* Check for exceptions and errors (is this useful in Unices?) */ 
 912   if (FD_ISSET(socket
->m_fd
, &exceptfds
)) 
 914     result 
|= (GSOCK_LOST_FLAG 
& flags
); 
 915     socket
->m_establishing 
= FALSE
; 
 916     socket
->m_detected 
= GSOCK_LOST_FLAG
; 
 923   assert(socket 
!= NULL
); 
 924   return flags 
& socket
->m_detected
; 
 926 #endif /* !wxUSE_GUI */ 
 931 /* GSocket_SetNonBlocking: 
 932  *  Sets the socket to non-blocking mode. All IO calls will return 
 935 void GSocket_SetNonBlocking(GSocket 
*socket
, int non_block
) 
 937   assert(socket 
!= NULL
); 
 939   GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) ); 
 941   socket
->m_non_blocking 
= non_block
; 
 944 /* GSocket_SetTimeout: 
 945  *  Sets the timeout for blocking calls. Time is expressed in 
 948 void GSocket_SetTimeout(GSocket 
*socket
, unsigned long millisec
) 
 950   assert(socket 
!= NULL
); 
 952   socket
->m_timeout 
= millisec
; 
 956  *  Returns the last error occured for this socket. Note that successful 
 957  *  operations do not clear this back to GSOCK_NOERROR, so use it only 
 960 GSocketError 
GSocket_GetError(GSocket 
*socket
) 
 962   assert(socket 
!= NULL
); 
 964   return socket
->m_error
; 
 970  *   There is data to be read in the input buffer. If, after a read 
 971  *   operation, there is still data available, the callback function will 
 974  *   The socket is available for writing. That is, the next write call 
 975  *   won't block. This event is generated only once, when the connection is 
 976  *   first established, and then only if a call failed with GSOCK_WOULDBLOCK, 
 977  *   when the output buffer empties again. This means that the app should 
 978  *   assume that it can write since the first OUTPUT event, and no more 
 979  *   OUTPUT events will be generated unless an error occurs. 
 981  *   Connection succesfully established, for client sockets, or incoming 
 982  *   client connection, for server sockets. Wait for this event (also watch 
 983  *   out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. 
 985  *   The connection is lost (or a connection request failed); this could 
 986  *   be due to a failure, or due to the peer closing it gracefully. 
 989 /* GSocket_SetCallback: 
 990  *  Enables the callbacks specified by 'flags'. Note that 'flags' 
 991  *  may be a combination of flags OR'ed toghether, so the same 
 992  *  callback function can be made to accept different events. 
 993  *  The callback function must have the following prototype: 
 995  *  void function(GSocket *socket, GSocketEvent event, char *cdata) 
 997 void GSocket_SetCallback(GSocket 
*socket
, GSocketEventFlags flags
, 
 998                          GSocketCallback callback
, char *cdata
) 
1002   assert(socket 
!= NULL
); 
1004   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1006     if ((flags 
& (1 << count
)) != 0) 
1008       printf("Setting callback no %d for socket at %X to address %X,data %X\n", 
1009              count
, socket
, callback
, cdata
); 
1010       socket
->m_cbacks
[count
] = callback
; 
1011       socket
->m_data
[count
] = cdata
; 
1016 /* GSocket_UnsetCallback: 
1017  *  Disables all callbacks specified by 'flags', which may be a 
1018  *  combination of flags OR'ed toghether. 
1020 void GSocket_UnsetCallback(GSocket 
*socket
, GSocketEventFlags flags
) 
1024   assert(socket 
!= NULL
); 
1026   for (count 
= 0; count 
< GSOCK_MAX_EVENT
; count
++) 
1028     if ((flags 
& (1 << count
)) != 0) 
1030       printf("Removing callback no %d for socket at %X", 
1032       socket
->m_cbacks
[count
] = NULL
; 
1033       socket
->m_data
[count
] = NULL
; 
1039 #define CALL_CALLBACK(socket, event) {                                  \ 
1040   _GSocket_Disable(socket, event);                                      \ 
1041   if (socket->m_cbacks[event]){                                         \ 
1042     printf("Call callback for event %d, socket %X: %X, %X\n", event,    \ 
1043             socket, socket->m_cbacks[event], socket->m_data[event]);    \ 
1044     socket->m_cbacks[event](socket, event, socket->m_data[event]);      \ 
1049 void _GSocket_Enable(GSocket 
*socket
, GSocketEvent event
) 
1051   socket
->m_detected 
&= ~(1 << event
); 
1052   _GSocket_Install_Callback(socket
, event
); 
1055 void _GSocket_Disable(GSocket 
*socket
, GSocketEvent event
) 
1057   socket
->m_detected 
|= (1 << event
); 
1058   _GSocket_Uninstall_Callback(socket
, event
); 
1061 /* _GSocket_Input_Timeout: 
1062  *  For blocking sockets, wait until data is available or 
1063  *  until timeout ellapses. 
1065 GSocketError 
_GSocket_Input_Timeout(GSocket 
*socket
) 
1071   /* Linux select() will overwrite the struct on return */ 
1072   tv
.tv_sec  
= (socket
->m_timeout 
/ 1000); 
1073   tv
.tv_usec 
= (socket
->m_timeout 
% 1000) * 1000; 
1075   if (!socket
->m_non_blocking
) 
1078     FD_SET(socket
->m_fd
, &readfds
); 
1079     ret 
= select(socket
->m_fd 
+ 1, &readfds
, NULL
, NULL
, &tv
); 
1082       GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" )); 
1083       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1084       return GSOCK_TIMEDOUT
; 
1088       GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" )); 
1089       if (errno 
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" )); 
1090       if (errno 
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" )); 
1091       if (errno 
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" )); 
1092       if (errno 
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" )); 
1093       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1094       return GSOCK_TIMEDOUT
; 
1097   return GSOCK_NOERROR
; 
1100 /* _GSocket_Output_Timeout: 
1101  *  For blocking sockets, wait until data can be sent without 
1102  *  blocking or until timeout ellapses. 
1104 GSocketError 
_GSocket_Output_Timeout(GSocket 
*socket
) 
1110   /* Linux select() will overwrite the struct on return */ 
1111   tv
.tv_sec  
= (socket
->m_timeout 
/ 1000); 
1112   tv
.tv_usec 
= (socket
->m_timeout 
% 1000) * 1000; 
1114   GSocket_Debug( ("m_non_blocking has: %d\n", (int)socket
->m_non_blocking
) ); 
1116   if (!socket
->m_non_blocking
) 
1119     FD_SET(socket
->m_fd
, &writefds
); 
1120     ret 
= select(socket
->m_fd 
+ 1, NULL
, &writefds
, NULL
, &tv
); 
1123       GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" )); 
1124       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1125       return GSOCK_TIMEDOUT
; 
1129       GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" )); 
1130       if (errno 
== EBADF
) GSocket_Debug(( "Invalid file descriptor\n" )); 
1131       if (errno 
== EINTR
) GSocket_Debug(( "A non blocked signal was caught\n" )); 
1132       if (errno 
== EINVAL
) GSocket_Debug(( "The highest number descriptor is negative\n" )); 
1133       if (errno 
== ENOMEM
) GSocket_Debug(( "Not enough memory\n" )); 
1134       socket
->m_error 
= GSOCK_TIMEDOUT
; 
1135       return GSOCK_TIMEDOUT
; 
1137     if ( ! FD_ISSET(socket
->m_fd
, &writefds
) ) 
1138       GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" )); 
1140       GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" )); 
1144     GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" )); 
1147   return GSOCK_NOERROR
; 
1150 int _GSocket_Recv_Stream(GSocket 
*socket
, char *buffer
, int size
) 
1152   return recv(socket
->m_fd
, buffer
, size
, 0); 
1155 int _GSocket_Recv_Dgram(GSocket 
*socket
, char *buffer
, int size
) 
1157   struct sockaddr from
; 
1158   SOCKLEN_T fromlen 
= sizeof(from
); 
1162   fromlen 
= sizeof(from
); 
1164   ret 
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T 
*) &fromlen
); 
1169   /* Translate a system address into a GSocket address */ 
1170   if (!socket
->m_peer
) 
1172     socket
->m_peer 
= GAddress_new(); 
1173     if (!socket
->m_peer
) 
1175       socket
->m_error 
= GSOCK_MEMERR
; 
1179   err 
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
); 
1180   if (err 
!= GSOCK_NOERROR
) 
1182     GAddress_destroy(socket
->m_peer
); 
1183     socket
->m_peer  
= NULL
; 
1184     socket
->m_error 
= err
; 
1191 int _GSocket_Send_Stream(GSocket 
*socket
, const char *buffer
, int size
) 
1197   ret 
= send(socket
->m_fd
, buffer
, size
, 0); 
1200   ret 
= send(socket
->m_fd
, buffer
, size
, 0); 
1205 int _GSocket_Send_Dgram(GSocket 
*socket
, const char *buffer
, int size
) 
1207   struct sockaddr 
*addr
; 
1211   if (!socket
->m_peer
) 
1213     socket
->m_error 
= GSOCK_INVADDR
; 
1217   err 
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
); 
1218   if (err 
!= GSOCK_NOERROR
) 
1220     socket
->m_error 
= err
; 
1226   ret 
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
); 
1229   ret 
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
); 
1232   /* Frees memory allocated from _GAddress_translate_to */ 
1238 void _GSocket_Detected_Read(GSocket 
*socket
) 
1242   if (recv(socket
->m_fd
, &c
, 1, MSG_PEEK
) > 0) 
1244     CALL_CALLBACK(socket
, GSOCK_INPUT
); 
1248     if (socket
->m_server 
&& socket
->m_stream
) 
1250       CALL_CALLBACK(socket
, GSOCK_CONNECTION
); 
1254       CALL_CALLBACK(socket
, GSOCK_LOST
); 
1259 void _GSocket_Detected_Write(GSocket 
*socket
) 
1261   if (socket
->m_establishing 
&& !socket
->m_server
) 
1264     SOCKLEN_T len 
= sizeof(error
); 
1266     socket
->m_establishing 
= FALSE
; 
1268     getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*)&error
, &len
); 
1272       CALL_CALLBACK(socket
, GSOCK_LOST
); 
1276       CALL_CALLBACK(socket
, GSOCK_CONNECTION
); 
1277       /* We have to fire this event by hand because CONNECTION (for clients) 
1278        * and OUTPUT are internally the same and we just disabled CONNECTION 
1279        * events with the above macro. 
1281       CALL_CALLBACK(socket
, GSOCK_OUTPUT
); 
1286     CALL_CALLBACK(socket
, GSOCK_OUTPUT
); 
1291  * ------------------------------------------------------------------------- 
1293  * ------------------------------------------------------------------------- 
1296 /* CHECK_ADDRESS verifies that the current address family is either 
1297  * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it 
1298  * initalizes it to be a GSOCK_*family*. In other cases, it returns 
1299  * an appropiate error code. 
1301  * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error. 
1303 #define CHECK_ADDRESS(address, family)                              \ 
1305   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1306     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1307       return address->m_error;                                      \ 
1308   if (address->m_family != GSOCK_##family)                          \ 
1310     address->m_error = GSOCK_INVADDR;                               \ 
1311     return GSOCK_INVADDR;                                           \ 
1315 #define CHECK_ADDRESS_RETVAL(address, family, retval)               \ 
1317   if (address->m_family == GSOCK_NOFAMILY)                          \ 
1318     if (_GAddress_Init_##family(address) != GSOCK_NOERROR)          \ 
1320   if (address->m_family != GSOCK_##family)                          \ 
1322     address->m_error = GSOCK_INVADDR;                               \ 
1328 GAddress 
*GAddress_new(void) 
1332   if ((address 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1335   address
->m_family  
= GSOCK_NOFAMILY
; 
1336   address
->m_addr    
= NULL
; 
1342 GAddress 
*GAddress_copy(GAddress 
*address
) 
1346   assert(address 
!= NULL
); 
1348   if ((addr2 
= (GAddress 
*) malloc(sizeof(GAddress
))) == NULL
) 
1351   memcpy(addr2
, address
, sizeof(GAddress
)); 
1353   if (address
->m_addr
) 
1355     addr2
->m_addr 
= (struct sockaddr 
*)malloc(addr2
->m_len
); 
1356     if (addr2
->m_addr 
== NULL
) 
1361     memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
); 
1367 void GAddress_destroy(GAddress 
*address
) 
1369   assert(address 
!= NULL
); 
1371   if (address
->m_addr
) 
1372     free(address
->m_addr
); 
1374 /*    free(address); */ 
1377 void GAddress_SetFamily(GAddress 
*address
, GAddressType type
) 
1379   assert(address 
!= NULL
); 
1381   address
->m_family 
= type
; 
1384 GAddressType 
GAddress_GetFamily(GAddress 
*address
) 
1386   assert(address 
!= NULL
); 
1388   return address
->m_family
; 
1391 GSocketError 
_GAddress_translate_from(GAddress 
*address
, 
1392                                       struct sockaddr 
*addr
, int len
) 
1394   address
->m_realfamily 
= addr
->sa_family
; 
1395   switch (addr
->sa_family
) 
1398       address
->m_family 
= GSOCK_INET
; 
1401       address
->m_family 
= GSOCK_UNIX
; 
1405       address
->m_family 
= GSOCK_INET6
; 
1410       address
->m_error 
= GSOCK_INVOP
; 
1415   if (address
->m_addr
) 
1416     free(address
->m_addr
); 
1418   address
->m_len  
= len
; 
1419   address
->m_addr 
= (struct sockaddr 
*)malloc(len
); 
1421   if (address
->m_addr 
== NULL
) 
1423     address
->m_error 
= GSOCK_MEMERR
; 
1424     return GSOCK_MEMERR
; 
1426   memcpy(address
->m_addr
, addr
, len
); 
1428   return GSOCK_NOERROR
; 
1431 GSocketError 
_GAddress_translate_to(GAddress 
*address
, 
1432                                     struct sockaddr 
**addr
, int *len
) 
1434   if (!address
->m_addr
) 
1436     address
->m_error 
= GSOCK_INVADDR
; 
1437     return GSOCK_INVADDR
; 
1440   *len 
= address
->m_len
; 
1441   *addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1444     address
->m_error 
= GSOCK_MEMERR
; 
1445     return GSOCK_MEMERR
; 
1448   memcpy(*addr
, address
->m_addr
, address
->m_len
); 
1449   return GSOCK_NOERROR
; 
1453  * ------------------------------------------------------------------------- 
1454  * Internet address family 
1455  * ------------------------------------------------------------------------- 
1458 GSocketError 
_GAddress_Init_INET(GAddress 
*address
) 
1460   address
->m_len  
= sizeof(struct sockaddr_in
); 
1461   address
->m_addr 
= (struct sockaddr 
*) malloc(address
->m_len
); 
1462   if (address
->m_addr 
== NULL
) 
1464     address
->m_error 
= GSOCK_MEMERR
; 
1465     return GSOCK_MEMERR
; 
1468   address
->m_family 
= GSOCK_INET
; 
1469   address
->m_realfamily 
= PF_INET
; 
1470   ((struct sockaddr_in 
*)address
->m_addr
)->sin_family 
= AF_INET
; 
1471   ((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
.s_addr 
= INADDR_ANY
; 
1473   return GSOCK_NOERROR
; 
1476 GSocketError 
GAddress_INET_SetHostName(GAddress 
*address
, const char *hostname
) 
1479   struct in_addr 
*addr
; 
1481   assert(address 
!= NULL
); 
1483   CHECK_ADDRESS(address
, INET
); 
1485   addr 
= &(((struct sockaddr_in 
*)address
->m_addr
)->sin_addr
); 
1487   /* If it is a numeric host name, convert it now */ 
1488 #if defined(HAVE_INET_ATON) 
1489   if (inet_aton(hostname
, addr
) == 0) 
1491 #elif defined(HAVE_INET_ADDR) 
1492   addr
->s_addr 
= inet_addr(hostname
); 
1493   if ( (addr
->s_addr 
== -1 ) 
1496   /* Use gethostbyname by default */ 
1497   int val 
= 1;  //VA doesn't like constants in conditional expressions at all 
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     if (isdigit((int)port
[0])) 
1559       port_int 
= atoi(port
); 
1560       addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1561       addr
->sin_port 
= htons(port_int
); 
1562       return GSOCK_NOERROR
; 
1565     address
->m_error 
= GSOCK_INVPORT
; 
1566     return GSOCK_INVPORT
; 
1569   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1570   addr
->sin_port 
= se
->s_port
; 
1572   return GSOCK_NOERROR
; 
1575 GSocketError 
GAddress_INET_SetPort(GAddress 
*address
, unsigned short port
) 
1577   struct sockaddr_in 
*addr
; 
1579   assert(address 
!= NULL
); 
1580   CHECK_ADDRESS(address
, INET
); 
1582   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1583   addr
->sin_port 
= htons(port
); 
1585   return GSOCK_NOERROR
; 
1588 GSocketError 
GAddress_INET_GetHostName(GAddress 
*address
, char *hostname
, size_t sbuf
) 
1592   struct sockaddr_in 
*addr
; 
1594   assert(address 
!= NULL
); 
1595   CHECK_ADDRESS(address
, INET
); 
1597   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1598   addr_buf 
= (char *)&(addr
->sin_addr
); 
1600   he 
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
); 
1603     address
->m_error 
= GSOCK_NOHOST
; 
1604     return GSOCK_NOHOST
; 
1607   strncpy(hostname
, he
->h_name
, sbuf
); 
1609   return GSOCK_NOERROR
; 
1612 unsigned long GAddress_INET_GetHostAddress(GAddress 
*address
) 
1614   struct sockaddr_in 
*addr
; 
1616   assert(address 
!= NULL
); 
1617   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
1619   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1621   return addr
->sin_addr
.s_addr
; 
1624 unsigned short GAddress_INET_GetPort(GAddress 
*address
) 
1626   struct sockaddr_in 
*addr
; 
1628   assert(address 
!= NULL
); 
1629   CHECK_ADDRESS_RETVAL(address
, INET
, 0); 
1631   addr 
= (struct sockaddr_in 
*)address
->m_addr
; 
1632   return ntohs(addr
->sin_port
); 
1636  * ------------------------------------------------------------------------- 
1637  * Unix address family 
1638  * ------------------------------------------------------------------------- 
1642 GSocketError 
_GAddress_Init_UNIX(GAddress 
*address
) 
1644   address
->m_len  
= sizeof(struct sockaddr_un
); 
1645   address
->m_addr 
= (struct sockaddr 
*)malloc(address
->m_len
); 
1646   if (address
->m_addr 
== NULL
) 
1648     address
->m_error 
= GSOCK_MEMERR
; 
1649     return GSOCK_MEMERR
; 
1652   address
->m_family 
= GSOCK_UNIX
; 
1653   address
->m_realfamily 
= PF_UNIX
; 
1654   ((struct sockaddr_un 
*)address
->m_addr
)->sun_family 
= AF_UNIX
; 
1655   ((struct sockaddr_un 
*)address
->m_addr
)->sun_path
[0] = 0; 
1657   return GSOCK_NOERROR
; 
1660 GSocketError 
GAddress_UNIX_SetPath(GAddress 
*address
, const char *path
) 
1662   struct sockaddr_un 
*addr
; 
1664   assert(address 
!= NULL
); 
1666   CHECK_ADDRESS(address
, UNIX
); 
1668   addr 
= ((struct sockaddr_un 
*)address
->m_addr
); 
1669   memcpy(addr
->sun_path
, path
, strlen(path
)); 
1671   return GSOCK_NOERROR
; 
1674 GSocketError 
GAddress_UNIX_GetPath(GAddress 
*address
, char *path
, size_t sbuf
) 
1676   struct sockaddr_un 
*addr
; 
1678   assert(address 
!= NULL
); 
1679   CHECK_ADDRESS(address
, UNIX
); 
1681   addr 
= (struct sockaddr_un 
*)address
->m_addr
; 
1683   strncpy(path
, addr
->sun_path
, sbuf
); 
1685   return GSOCK_NOERROR
; 
1690   /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */