1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
7 * Purpose: GSocket main MSW file
8 * Licence: The wxWindows licence
10 * -------------------------------------------------------------------------
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
21 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
22 * warning: conditional expression is constant.
24 # pragma warning(disable:4115)
26 * warning: named type definition in parentheses.
28 # pragma warning(disable:4127)
29 /* GAddress_UNIX_GetPath,
30 * warning: unreferenced formal parameter.
32 # pragma warning(disable:4100)
35 /* windows.h results in tons of warnings at max warning level */
37 # pragma warning(push, 1)
42 # pragma warning(disable:4514)
48 #if defined(__CYGWIN__)
49 //CYGWIN gives annoying warning about runtime stuff if we don't do this
50 # define USE_SYS_TYPES_FD_SET
51 # include <sys/types.h>
56 #ifndef __GSOCKET_STANDALONE__
57 # include "wx/platform.h"
58 # include "wx/setup.h"
61 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
63 #ifndef __GSOCKET_STANDALONE__
64 # include "wx/msw/gsockmsw.h"
65 # include "wx/gsocket.h"
67 # include "gsockmsw.h"
69 #endif /* __GSOCKET_STANDALONE__ */
76 #define isdigit(x) (x > 47 && x < 58)
78 #include "wx/msw/wince/net.h"
87 /* if we use configure for MSW WX_SOCKLEN_T will be already defined */
89 # define WX_SOCKLEN_T int
92 /* Table of GUI-related functions. We must call them indirectly because
93 * of wxBase and GUI separation: */
95 static GSocketGUIFunctionsTable
*gs_gui_functions
;
97 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
100 virtual bool OnInit();
101 virtual void OnExit();
102 virtual bool CanUseEventLoop();
103 virtual bool Init_Socket(GSocket
*socket
);
104 virtual void Destroy_Socket(GSocket
*socket
);
105 virtual void Enable_Events(GSocket
*socket
);
106 virtual void Disable_Events(GSocket
*socket
);
109 bool GSocketGUIFunctionsTableNull::OnInit()
111 void GSocketGUIFunctionsTableNull::OnExit()
113 bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
115 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket
*WXUNUSED(socket
))
117 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket
*WXUNUSED(socket
))
119 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket
*WXUNUSED(socket
))
121 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket
*WXUNUSED(socket
))
123 /* Global initialisers */
125 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable
*guifunc
)
127 gs_gui_functions
= guifunc
;
130 int GSocket_Init(void)
134 if (!gs_gui_functions
)
136 static GSocketGUIFunctionsTableNull table
;
137 gs_gui_functions
= &table
;
139 if ( !gs_gui_functions
->OnInit() )
144 /* Initialize WinSocket */
145 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
148 void GSocket_Cleanup(void)
150 if (gs_gui_functions
)
152 gs_gui_functions
->OnExit();
155 /* Cleanup WinSocket */
159 /* Constructors / Destructors for GSocket */
165 m_fd
= INVALID_SOCKET
;
166 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
173 m_error
= GSOCK_NOERROR
;
176 m_non_blocking
= false;
177 m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
178 m_timeout
.tv_usec
= 0;
179 m_establishing
= false;
182 assert(gs_gui_functions
);
183 /* Per-socket GUI-specific initialization */
184 m_ok
= gs_gui_functions
->Init_Socket(this);
187 void GSocket::Close()
189 gs_gui_functions
->Disable_Events(this);
191 m_fd
= INVALID_SOCKET
;
198 /* Per-socket GUI-specific cleanup */
199 gs_gui_functions
->Destroy_Socket(this);
201 /* Check that the socket is really shutdowned */
202 if (m_fd
!= INVALID_SOCKET
)
205 /* Destroy private addresses */
207 GAddress_destroy(m_local
);
210 GAddress_destroy(m_peer
);
214 * Disallow further read/write operations on this socket, close
215 * the fd and disable all callbacks.
217 void GSocket::Shutdown()
223 /* If socket has been created, shutdown it */
224 if (m_fd
!= INVALID_SOCKET
)
230 /* Disable GUI callbacks */
231 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
232 m_cbacks
[evt
] = NULL
;
234 m_detected
= GSOCK_LOST_FLAG
;
237 /* Address handling */
243 * Set or get the local or peer address for this socket. The 'set'
244 * functions return GSOCK_NOERROR on success, an error code otherwise.
245 * The 'get' functions return a pointer to a GAddress object on success,
246 * or NULL otherwise, in which case they set the error code of the
247 * corresponding GSocket.
250 * GSOCK_INVSOCK - the socket is not valid.
251 * GSOCK_INVADDR - the address is not valid.
253 GSocketError
GSocket::SetLocal(GAddress
*address
)
257 /* the socket must be initialized, or it must be a server */
258 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
260 m_error
= GSOCK_INVSOCK
;
261 return GSOCK_INVSOCK
;
265 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
267 m_error
= GSOCK_INVADDR
;
268 return GSOCK_INVADDR
;
272 GAddress_destroy(m_local
);
274 m_local
= GAddress_copy(address
);
276 return GSOCK_NOERROR
;
279 GSocketError
GSocket::SetPeer(GAddress
*address
)
284 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
286 m_error
= GSOCK_INVADDR
;
287 return GSOCK_INVADDR
;
291 GAddress_destroy(m_peer
);
293 m_peer
= GAddress_copy(address
);
295 return GSOCK_NOERROR
;
298 GAddress
*GSocket::GetLocal()
301 struct sockaddr addr
;
302 WX_SOCKLEN_T size
= sizeof(addr
);
307 /* try to get it from the m_local var first */
309 return GAddress_copy(m_local
);
311 /* else, if the socket is initialized, try getsockname */
312 if (m_fd
== INVALID_SOCKET
)
314 m_error
= GSOCK_INVSOCK
;
318 if (getsockname(m_fd
, &addr
, &size
) == SOCKET_ERROR
)
320 m_error
= GSOCK_IOERR
;
324 /* got a valid address from getsockname, create a GAddress object */
325 if ((address
= GAddress_new()) == NULL
)
327 m_error
= GSOCK_MEMERR
;
331 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
333 GAddress_destroy(address
);
341 GAddress
*GSocket::GetPeer()
345 /* try to get it from the m_peer var */
347 return GAddress_copy(m_peer
);
352 /* Server specific parts */
354 /* GSocket_SetServer:
355 * Sets up this socket as a server. The local address must have been
356 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
357 * Returns GSOCK_NOERROR on success, one of the following otherwise:
360 * GSOCK_INVSOCK - the socket is in use.
361 * GSOCK_INVADDR - the local address has not been set.
362 * GSOCK_IOERR - low-level error.
364 GSocketError
GSocket::SetServer()
370 /* must not be in use */
371 if (m_fd
!= INVALID_SOCKET
)
373 m_error
= GSOCK_INVSOCK
;
374 return GSOCK_INVSOCK
;
377 /* the local addr must have been set */
380 m_error
= GSOCK_INVADDR
;
381 return GSOCK_INVADDR
;
384 /* Initialize all fields */
388 /* Create the socket */
389 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
391 if (m_fd
== INVALID_SOCKET
)
393 m_error
= GSOCK_IOERR
;
397 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
398 gs_gui_functions
->Enable_Events(this);
400 /* allow a socket to re-bind if the socket is in the TIME_WAIT
401 state after being previously closed.
404 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
407 /* Bind to the local address,
408 * retrieve the actual address bound,
409 * and listen up to 5 connections.
411 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
414 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
415 (listen(m_fd
, 5) != 0))
418 m_error
= GSOCK_IOERR
;
422 return GSOCK_NOERROR
;
425 /* GSocket_WaitConnection:
426 * Waits for an incoming client connection. Returns a pointer to
427 * a GSocket object, or NULL if there was an error, in which case
428 * the last error field will be updated for the calling GSocket.
430 * Error codes (set in the calling GSocket)
431 * GSOCK_INVSOCK - the socket is not valid or not a server.
432 * GSOCK_TIMEDOUT - timeout, no incoming connections.
433 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
434 * GSOCK_MEMERR - couldn't allocate memory.
435 * GSOCK_IOERR - low-level error.
437 GSocket
*GSocket::WaitConnection()
440 struct sockaddr from
;
441 WX_SOCKLEN_T fromlen
= sizeof(from
);
447 /* Reenable CONNECTION events */
448 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
450 /* If the socket has already been created, we exit immediately */
451 if (m_fd
== INVALID_SOCKET
|| !m_server
)
453 m_error
= GSOCK_INVSOCK
;
457 /* Create a GSocket object for the new connection */
458 connection
= GSocket_new();
462 m_error
= GSOCK_MEMERR
;
466 /* Wait for a connection (with timeout) */
467 if (Input_Timeout() == GSOCK_TIMEDOUT
)
470 /* m_error set by _GSocket_Input_Timeout */
474 connection
->m_fd
= accept(m_fd
, &from
, &fromlen
);
476 if (connection
->m_fd
== INVALID_SOCKET
)
478 if (WSAGetLastError() == WSAEWOULDBLOCK
)
479 m_error
= GSOCK_WOULDBLOCK
;
481 m_error
= GSOCK_IOERR
;
487 /* Initialize all fields */
488 connection
->m_server
= false;
489 connection
->m_stream
= true;
491 /* Setup the peer address field */
492 connection
->m_peer
= GAddress_new();
493 if (!connection
->m_peer
)
496 m_error
= GSOCK_MEMERR
;
499 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
500 if (err
!= GSOCK_NOERROR
)
502 GAddress_destroy(connection
->m_peer
);
508 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
509 gs_gui_functions
->Enable_Events(connection
);
514 /* GSocket_SetReusable:
515 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
516 * make the appropriate setsockopt() call.
517 * Implemented as a GSocket function because clients (ie, wxSocketServer)
518 * don't have access to the GSocket struct information.
519 * Returns true if the flag was set correctly, false if an error occurred
520 * (ie, if the parameter was NULL)
522 bool GSocket::SetReusable()
524 /* socket must not be null, and must not be in use/already bound */
525 if (this && m_fd
== INVALID_SOCKET
) {
532 /* Client specific parts */
535 * For stream (connection oriented) sockets, GSocket_Connect() tries
536 * to establish a client connection to a server using the peer address
537 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
538 * connection has been successfully established, or one of the error
539 * codes listed below. Note that for nonblocking sockets, a return
540 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
541 * request can be completed later; you should use GSocket_Select()
542 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
543 * corresponding asynchronous events.
545 * For datagram (non connection oriented) sockets, GSocket_Connect()
546 * just sets the peer address established with GSocket_SetPeer() as
547 * default destination.
550 * GSOCK_INVSOCK - the socket is in use or not valid.
551 * GSOCK_INVADDR - the peer address has not been established.
552 * GSOCK_TIMEDOUT - timeout, the connection failed.
553 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
554 * GSOCK_MEMERR - couldn't allocate memory.
555 * GSOCK_IOERR - low-level error.
557 GSocketError
GSocket::Connect(GSocketStream stream
)
564 /* Enable CONNECTION events (needed for nonblocking connections) */
565 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
567 if (m_fd
!= INVALID_SOCKET
)
569 m_error
= GSOCK_INVSOCK
;
570 return GSOCK_INVSOCK
;
575 m_error
= GSOCK_INVADDR
;
576 return GSOCK_INVADDR
;
579 /* Streamed or dgram socket? */
580 m_stream
= (stream
== GSOCK_STREAMED
);
582 m_establishing
= false;
584 /* Create the socket */
585 m_fd
= socket(m_peer
->m_realfamily
,
586 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
588 if (m_fd
== INVALID_SOCKET
)
590 m_error
= GSOCK_IOERR
;
594 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
595 gs_gui_functions
->Enable_Events(this);
597 /* Connect it to the peer address, with a timeout (see below) */
598 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
600 if (ret
== SOCKET_ERROR
)
602 err
= WSAGetLastError();
604 /* If connect failed with EWOULDBLOCK and the GSocket object
605 * is in blocking mode, we select() for the specified timeout
606 * checking for writability to see if the connection request
609 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
611 err
= Connect_Timeout();
613 if (err
!= GSOCK_NOERROR
)
616 /* m_error is set in _GSocket_Connect_Timeout */
619 return (GSocketError
) err
;
622 /* If connect failed with EWOULDBLOCK and the GSocket object
623 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
624 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
625 * this way if the connection completes, a GSOCK_CONNECTION
626 * event will be generated, if enabled.
628 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
630 m_establishing
= true;
631 m_error
= GSOCK_WOULDBLOCK
;
632 return GSOCK_WOULDBLOCK
;
635 /* If connect failed with an error other than EWOULDBLOCK,
636 * then the call to GSocket_Connect() has failed.
639 m_error
= GSOCK_IOERR
;
643 return GSOCK_NOERROR
;
646 /* Datagram sockets */
648 /* GSocket_SetNonOriented:
649 * Sets up this socket as a non-connection oriented (datagram) socket.
650 * Before using this function, the local address must have been set
651 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
652 * on success, or one of the following otherwise.
655 * GSOCK_INVSOCK - the socket is in use.
656 * GSOCK_INVADDR - the local address has not been set.
657 * GSOCK_IOERR - low-level error.
659 GSocketError
GSocket::SetNonOriented()
665 if (m_fd
!= INVALID_SOCKET
)
667 m_error
= GSOCK_INVSOCK
;
668 return GSOCK_INVSOCK
;
673 m_error
= GSOCK_INVADDR
;
674 return GSOCK_INVADDR
;
677 /* Initialize all fields */
681 /* Create the socket */
682 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
684 if (m_fd
== INVALID_SOCKET
)
686 m_error
= GSOCK_IOERR
;
690 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
691 gs_gui_functions
->Enable_Events(this);
693 /* Bind to the local address,
694 * and retrieve the actual address bound.
696 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
699 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
702 m_error
= GSOCK_IOERR
;
706 return GSOCK_NOERROR
;
711 /* Like recv(), send(), ... */
712 int GSocket::Read(char *buffer
, int size
)
718 /* Reenable INPUT events */
719 m_detected
&= ~GSOCK_INPUT_FLAG
;
721 if (m_fd
== INVALID_SOCKET
|| m_server
)
723 m_error
= GSOCK_INVSOCK
;
727 /* If the socket is blocking, wait for data (with a timeout) */
728 if (Input_Timeout() == GSOCK_TIMEDOUT
)
730 m_error
= GSOCK_TIMEDOUT
;
736 ret
= Recv_Stream(buffer
, size
);
738 ret
= Recv_Dgram(buffer
, size
);
740 if (ret
== SOCKET_ERROR
)
742 if (WSAGetLastError() != WSAEWOULDBLOCK
)
743 m_error
= GSOCK_IOERR
;
745 m_error
= GSOCK_WOULDBLOCK
;
752 int GSocket::Write(const char *buffer
, int size
)
758 if (m_fd
== INVALID_SOCKET
|| m_server
)
760 m_error
= GSOCK_INVSOCK
;
764 /* If the socket is blocking, wait for writability (with a timeout) */
765 if (Output_Timeout() == GSOCK_TIMEDOUT
)
770 ret
= Send_Stream(buffer
, size
);
772 ret
= Send_Dgram(buffer
, size
);
774 if (ret
== SOCKET_ERROR
)
776 if (WSAGetLastError() != WSAEWOULDBLOCK
)
777 m_error
= GSOCK_IOERR
;
779 m_error
= GSOCK_WOULDBLOCK
;
781 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
782 * does). Once the first OUTPUT event is received, users can assume
783 * that the socket is writable until a read operation fails. Only then
784 * will further OUTPUT events be posted.
786 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
794 * Polls the socket to determine its status. This function will
795 * check for the events specified in the 'flags' parameter, and
796 * it will return a mask indicating which operations can be
797 * performed. This function won't block, regardless of the
798 * mode (blocking | nonblocking) of the socket.
800 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
802 if (!gs_gui_functions
->CanUseEventLoop())
804 GSocketEventFlags result
= 0;
814 FD_SET(m_fd
, &readfds
);
815 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
816 FD_SET(m_fd
, &writefds
);
817 FD_SET(m_fd
, &exceptfds
);
819 /* Check 'sticky' CONNECTION flag first */
820 result
|= (GSOCK_CONNECTION_FLAG
& m_detected
);
822 /* If we have already detected a LOST event, then don't try
823 * to do any further processing.
825 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
827 m_establishing
= false;
829 return (GSOCK_LOST_FLAG
& flags
);
833 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
836 /* What to do here? */
837 return (result
& flags
);
840 /* Check for readability */
841 if (FD_ISSET(m_fd
, &readfds
))
845 if (!m_stream
|| recv(m_fd
, &c
, 1, MSG_PEEK
) > 0)
847 result
|= GSOCK_INPUT_FLAG
;
851 if (m_server
&& m_stream
)
853 result
|= GSOCK_CONNECTION_FLAG
;
854 m_detected
|= GSOCK_CONNECTION_FLAG
;
858 m_detected
= GSOCK_LOST_FLAG
;
859 m_establishing
= false;
861 /* LOST event: Abort any further processing */
862 return (GSOCK_LOST_FLAG
& flags
);
867 /* Check for writability */
868 if (FD_ISSET(m_fd
, &writefds
))
870 if (m_establishing
&& !m_server
)
873 WX_SOCKLEN_T len
= sizeof(error
);
875 m_establishing
= false;
877 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
881 m_detected
= GSOCK_LOST_FLAG
;
883 /* LOST event: Abort any further processing */
884 return (GSOCK_LOST_FLAG
& flags
);
888 result
|= GSOCK_CONNECTION_FLAG
;
889 m_detected
|= GSOCK_CONNECTION_FLAG
;
894 result
|= GSOCK_OUTPUT_FLAG
;
898 /* Check for exceptions and errors (is this useful in Unices?) */
899 if (FD_ISSET(m_fd
, &exceptfds
))
901 m_establishing
= false;
902 m_detected
= GSOCK_LOST_FLAG
;
904 /* LOST event: Abort any further processing */
905 return (GSOCK_LOST_FLAG
& flags
);
908 return (result
& flags
);
913 return flags
& m_detected
;
919 /* GSocket_SetNonBlocking:
920 * Sets the socket to non-blocking mode. All IO calls will return
923 void GSocket::SetNonBlocking(bool non_block
)
927 m_non_blocking
= non_block
;
930 /* GSocket_SetTimeout:
931 * Sets the timeout for blocking calls. Time is expressed in
934 void GSocket::SetTimeout(unsigned long millis
)
938 m_timeout
.tv_sec
= (millis
/ 1000);
939 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
943 * Returns the last error occurred for this socket. Note that successful
944 * operations do not clear this back to GSOCK_NOERROR, so use it only
947 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
957 * There is data to be read in the input buffer. If, after a read
958 * operation, there is still data available, the callback function will
961 * The socket is available for writing. That is, the next write call
962 * won't block. This event is generated only once, when the connection is
963 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
964 * when the output buffer empties again. This means that the app should
965 * assume that it can write since the first OUTPUT event, and no more
966 * OUTPUT events will be generated unless an error occurs.
968 * Connection successfully established, for client sockets, or incoming
969 * client connection, for server sockets. Wait for this event (also watch
970 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
972 * The connection is lost (or a connection request failed); this could
973 * be due to a failure, or due to the peer closing it gracefully.
976 /* GSocket_SetCallback:
977 * Enables the callbacks specified by 'flags'. Note that 'flags'
978 * may be a combination of flags OR'ed toghether, so the same
979 * callback function can be made to accept different events.
980 * The callback function must have the following prototype:
982 * void function(GSocket *socket, GSocketEvent event, char *cdata)
984 void GSocket::SetCallback(GSocketEventFlags flags
,
985 GSocketCallback callback
, char *cdata
)
991 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
993 if ((flags
& (1 << count
)) != 0)
995 m_cbacks
[count
] = callback
;
996 m_data
[count
] = cdata
;
1001 /* GSocket_UnsetCallback:
1002 * Disables all callbacks specified by 'flags', which may be a
1003 * combination of flags OR'ed toghether.
1005 void GSocket::UnsetCallback(GSocketEventFlags flags
)
1011 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1013 if ((flags
& (1 << count
)) != 0)
1015 m_cbacks
[count
] = NULL
;
1016 m_data
[count
] = NULL
;
1021 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1022 void *optval
, int *optlen
)
1024 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
1026 return GSOCK_NOERROR
;
1028 return GSOCK_OPTERR
;
1031 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1032 const void *optval
, int optlen
)
1034 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
1036 return GSOCK_NOERROR
;
1038 return GSOCK_OPTERR
;
1041 /* Internals (IO) */
1043 /* _GSocket_Input_Timeout:
1044 * For blocking sockets, wait until data is available or
1045 * until timeout ellapses.
1047 GSocketError
GSocket::Input_Timeout()
1051 if (!m_non_blocking
)
1054 FD_SET(m_fd
, &readfds
);
1055 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
1057 m_error
= GSOCK_TIMEDOUT
;
1058 return GSOCK_TIMEDOUT
;
1061 return GSOCK_NOERROR
;
1064 /* _GSocket_Output_Timeout:
1065 * For blocking sockets, wait until data can be sent without
1066 * blocking or until timeout ellapses.
1068 GSocketError
GSocket::Output_Timeout()
1072 if (!m_non_blocking
)
1075 FD_SET(m_fd
, &writefds
);
1076 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
1078 m_error
= GSOCK_TIMEDOUT
;
1079 return GSOCK_TIMEDOUT
;
1082 return GSOCK_NOERROR
;
1085 /* _GSocket_Connect_Timeout:
1086 * For blocking sockets, wait until the connection is
1087 * established or fails, or until timeout ellapses.
1089 GSocketError
GSocket::Connect_Timeout()
1095 FD_ZERO(&exceptfds
);
1096 FD_SET(m_fd
, &writefds
);
1097 FD_SET(m_fd
, &exceptfds
);
1098 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
1100 m_error
= GSOCK_TIMEDOUT
;
1101 return GSOCK_TIMEDOUT
;
1103 if (!FD_ISSET(m_fd
, &writefds
))
1105 m_error
= GSOCK_IOERR
;
1109 return GSOCK_NOERROR
;
1112 int GSocket::Recv_Stream(char *buffer
, int size
)
1114 return recv(m_fd
, buffer
, size
, 0);
1117 int GSocket::Recv_Dgram(char *buffer
, int size
)
1119 struct sockaddr from
;
1120 WX_SOCKLEN_T fromlen
= sizeof(from
);
1124 ret
= recvfrom(m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1126 if (ret
== SOCKET_ERROR
)
1127 return SOCKET_ERROR
;
1129 /* Translate a system address into a GSocket address */
1132 m_peer
= GAddress_new();
1135 m_error
= GSOCK_MEMERR
;
1139 err
= _GAddress_translate_from(m_peer
, &from
, fromlen
);
1140 if (err
!= GSOCK_NOERROR
)
1142 GAddress_destroy(m_peer
);
1151 int GSocket::Send_Stream(const char *buffer
, int size
)
1153 return send(m_fd
, buffer
, size
, 0);
1156 int GSocket::Send_Dgram(const char *buffer
, int size
)
1158 struct sockaddr
*addr
;
1164 m_error
= GSOCK_INVADDR
;
1168 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1169 if (err
!= GSOCK_NOERROR
)
1175 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
1177 /* Frees memory allocated by _GAddress_translate_to */
1183 /* Compatibility functions for GSocket */
1184 GSocket
*GSocket_new(void)
1186 GSocket
*newsocket
= new GSocket();
1187 if(newsocket
->IsOk())
1195 * -------------------------------------------------------------------------
1197 * -------------------------------------------------------------------------
1200 /* CHECK_ADDRESS verifies that the current address family is either
1201 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1202 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1203 * an appropiate error code.
1205 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1207 #define CHECK_ADDRESS(address, family) \
1209 if (address->m_family == GSOCK_NOFAMILY) \
1210 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1211 return address->m_error; \
1212 if (address->m_family != GSOCK_##family) \
1214 address->m_error = GSOCK_INVADDR; \
1215 return GSOCK_INVADDR; \
1219 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1221 if (address->m_family == GSOCK_NOFAMILY) \
1222 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1224 if (address->m_family != GSOCK_##family) \
1226 address->m_error = GSOCK_INVADDR; \
1232 GAddress
*GAddress_new(void)
1236 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1239 address
->m_family
= GSOCK_NOFAMILY
;
1240 address
->m_addr
= NULL
;
1246 GAddress
*GAddress_copy(GAddress
*address
)
1250 assert(address
!= NULL
);
1252 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1255 memcpy(addr2
, address
, sizeof(GAddress
));
1257 if (address
->m_addr
)
1259 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1260 if (addr2
->m_addr
== NULL
)
1265 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1271 void GAddress_destroy(GAddress
*address
)
1273 assert(address
!= NULL
);
1275 if (address
->m_addr
)
1276 free(address
->m_addr
);
1281 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1283 assert(address
!= NULL
);
1285 address
->m_family
= type
;
1288 GAddressType
GAddress_GetFamily(GAddress
*address
)
1290 assert(address
!= NULL
);
1292 return address
->m_family
;
1295 GSocketError
_GAddress_translate_from(GAddress
*address
,
1296 struct sockaddr
*addr
, int len
)
1298 address
->m_realfamily
= addr
->sa_family
;
1299 switch (addr
->sa_family
)
1302 address
->m_family
= GSOCK_INET
;
1305 address
->m_family
= GSOCK_UNIX
;
1309 address
->m_family
= GSOCK_INET6
;
1314 address
->m_error
= GSOCK_INVOP
;
1319 if (address
->m_addr
)
1320 free(address
->m_addr
);
1322 address
->m_len
= len
;
1323 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1325 if (address
->m_addr
== NULL
)
1327 address
->m_error
= GSOCK_MEMERR
;
1328 return GSOCK_MEMERR
;
1330 memcpy(address
->m_addr
, addr
, len
);
1332 return GSOCK_NOERROR
;
1335 GSocketError
_GAddress_translate_to(GAddress
*address
,
1336 struct sockaddr
**addr
, int *len
)
1338 if (!address
->m_addr
)
1340 address
->m_error
= GSOCK_INVADDR
;
1341 return GSOCK_INVADDR
;
1344 *len
= address
->m_len
;
1345 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1348 address
->m_error
= GSOCK_MEMERR
;
1349 return GSOCK_MEMERR
;
1352 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1353 return GSOCK_NOERROR
;
1357 * -------------------------------------------------------------------------
1358 * Internet address family
1359 * -------------------------------------------------------------------------
1362 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1364 address
->m_len
= sizeof(struct sockaddr_in
);
1365 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1366 if (address
->m_addr
== NULL
)
1368 address
->m_error
= GSOCK_MEMERR
;
1369 return GSOCK_MEMERR
;
1372 address
->m_family
= GSOCK_INET
;
1373 address
->m_realfamily
= AF_INET
;
1374 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1375 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1377 return GSOCK_NOERROR
;
1380 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1383 struct in_addr
*addr
;
1385 assert(address
!= NULL
);
1387 CHECK_ADDRESS(address
, INET
);
1389 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1391 addr
->s_addr
= inet_addr(hostname
);
1393 /* If it is a numeric host name, convert it now */
1394 if (addr
->s_addr
== INADDR_NONE
)
1396 struct in_addr
*array_addr
;
1398 /* It is a real name, we solve it */
1399 if ((he
= gethostbyname(hostname
)) == NULL
)
1401 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1402 address
->m_error
= GSOCK_NOHOST
;
1403 return GSOCK_NOHOST
;
1405 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1406 addr
->s_addr
= array_addr
[0].s_addr
;
1408 return GSOCK_NOERROR
;
1411 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1413 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1416 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1417 unsigned long hostaddr
)
1419 struct in_addr
*addr
;
1421 assert(address
!= NULL
);
1423 CHECK_ADDRESS(address
, INET
);
1425 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1426 addr
->s_addr
= htonl(hostaddr
);
1428 return GSOCK_NOERROR
;
1431 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1432 const char *protocol
)
1435 struct sockaddr_in
*addr
;
1437 assert(address
!= NULL
);
1438 CHECK_ADDRESS(address
, INET
);
1442 address
->m_error
= GSOCK_INVPORT
;
1443 return GSOCK_INVPORT
;
1446 se
= getservbyname(port
, protocol
);
1449 if (isdigit(port
[0]))
1453 port_int
= atoi(port
);
1454 addr
= (struct sockaddr_in
*)address
->m_addr
;
1455 addr
->sin_port
= htons((u_short
) port_int
);
1456 return GSOCK_NOERROR
;
1459 address
->m_error
= GSOCK_INVPORT
;
1460 return GSOCK_INVPORT
;
1463 addr
= (struct sockaddr_in
*)address
->m_addr
;
1464 addr
->sin_port
= se
->s_port
;
1466 return GSOCK_NOERROR
;
1469 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1471 struct sockaddr_in
*addr
;
1473 assert(address
!= NULL
);
1474 CHECK_ADDRESS(address
, INET
);
1476 addr
= (struct sockaddr_in
*)address
->m_addr
;
1477 addr
->sin_port
= htons(port
);
1479 return GSOCK_NOERROR
;
1482 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1486 struct sockaddr_in
*addr
;
1488 assert(address
!= NULL
);
1489 CHECK_ADDRESS(address
, INET
);
1491 addr
= (struct sockaddr_in
*)address
->m_addr
;
1492 addr_buf
= (char *)&(addr
->sin_addr
);
1494 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1497 address
->m_error
= GSOCK_NOHOST
;
1498 return GSOCK_NOHOST
;
1501 strncpy(hostname
, he
->h_name
, sbuf
);
1503 return GSOCK_NOERROR
;
1506 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1508 struct sockaddr_in
*addr
;
1510 assert(address
!= NULL
);
1511 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1513 addr
= (struct sockaddr_in
*)address
->m_addr
;
1515 return ntohl(addr
->sin_addr
.s_addr
);
1518 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1520 struct sockaddr_in
*addr
;
1522 assert(address
!= NULL
);
1523 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1525 addr
= (struct sockaddr_in
*)address
->m_addr
;
1526 return ntohs(addr
->sin_port
);
1530 * -------------------------------------------------------------------------
1531 * Unix address family
1532 * -------------------------------------------------------------------------
1535 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1537 assert (address
!= NULL
);
1538 address
->m_error
= GSOCK_INVADDR
;
1539 return GSOCK_INVADDR
;
1542 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1544 assert (address
!= NULL
);
1545 address
->m_error
= GSOCK_INVADDR
;
1546 return GSOCK_INVADDR
;
1549 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1551 assert (address
!= NULL
);
1552 address
->m_error
= GSOCK_INVADDR
;
1553 return GSOCK_INVADDR
;
1556 #else /* !wxUSE_SOCKETS */
1559 * Translation unit shouldn't be empty, so include this typedef to make the
1560 * compiler (VC++ 6.0, for example) happy
1562 typedef void (*wxDummy
)();
1564 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */