]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/gsocket.cpp
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 * Purpose: GSocket main MSW file
6 * Licence: The wxWindows licence
8 * -------------------------------------------------------------------------
12 * PLEASE don't put C++ comments here - this is a C source file.
16 /* RPCNOTIFICATION_ROUTINE in rasasync.h (included from winsock.h),
17 * warning: conditional expression is constant.
19 # pragma warning(disable:4115)
21 * warning: named type definition in parentheses.
23 # pragma warning(disable:4127)
24 /* GAddress_UNIX_GetPath,
25 * warning: unreferenced formal parameter.
27 # pragma warning(disable:4100)
30 /* windows.h results in tons of warnings at max warning level */
32 # pragma warning(push, 1)
37 # pragma warning(disable:4514)
45 #ifndef __GSOCKET_STANDALONE__
46 # include "wx/platform.h"
47 # include "wx/setup.h"
50 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
52 #ifndef __GSOCKET_STANDALONE__
53 # include "wx/msw/gsockmsw.h"
54 # include "wx/gsocket.h"
56 # include "gsockmsw.h"
58 #endif /* __GSOCKET_STANDALONE__ */
65 #define isdigit(x) (x > 47 && x < 58)
67 #include "wx/msw/wince/net.h"
76 /* if we use configure for MSW SOCKLEN_T will be already defined */
78 # define SOCKLEN_T int
81 /* Table of GUI-related functions. We must call them indirectly because
82 * of wxBase and GUI separation: */
84 static class GSocketGUIFunctionsTable
*gs_gui_functions
;
86 /* Global initialisers */
88 void GSocket_SetGUIFunctions(struct GSocketGUIFunctionsTable
*guifunc
)
90 gs_gui_functions
= guifunc
;
93 int GSocket_Init(void)
99 if ( !gs_gui_functions
->OnInit() )
105 /* Initialize WinSocket */
106 return (WSAStartup((1 << 8) | 1, &wsaData
) == 0);
109 void GSocket_Cleanup(void)
111 if (gs_gui_functions
)
113 gs_gui_functions
->OnExit();
116 /* Cleanup WinSocket */
120 /* Constructors / Destructors for GSocket */
126 m_fd
= INVALID_SOCKET
;
127 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
134 m_error
= GSOCK_NOERROR
;
137 m_non_blocking
= false;
138 m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
139 m_timeout
.tv_usec
= 0;
140 m_establishing
= FALSE
;
143 assert(gs_gui_functions
);
144 /* Per-socket GUI-specific initialization */
145 m_ok
= gs_gui_functions
->Init_Socket(this);
148 void GSocket::Close()
150 gs_gui_functions
->Disable_Events(this);
152 m_fd
= INVALID_SOCKET
;
159 /* Per-socket GUI-specific cleanup */
160 gs_gui_functions
->Destroy_Socket(this);
162 /* Check that the socket is really shutdowned */
163 if (m_fd
!= INVALID_SOCKET
)
166 /* Destroy private addresses */
168 GAddress_destroy(m_local
);
171 GAddress_destroy(m_peer
);
175 * Disallow further read/write operations on this socket, close
176 * the fd and disable all callbacks.
178 void GSocket::Shutdown()
184 /* If socket has been created, shutdown it */
185 if (m_fd
!= INVALID_SOCKET
)
191 /* Disable GUI callbacks */
192 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
193 m_cbacks
[evt
] = NULL
;
195 m_detected
= GSOCK_LOST_FLAG
;
198 /* Address handling */
204 * Set or get the local or peer address for this socket. The 'set'
205 * functions return GSOCK_NOERROR on success, an error code otherwise.
206 * The 'get' functions return a pointer to a GAddress object on success,
207 * or NULL otherwise, in which case they set the error code of the
208 * corresponding GSocket.
211 * GSOCK_INVSOCK - the socket is not valid.
212 * GSOCK_INVADDR - the address is not valid.
214 GSocketError
GSocket::SetLocal(GAddress
*address
)
218 /* the socket must be initialized, or it must be a server */
219 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
221 m_error
= GSOCK_INVSOCK
;
222 return GSOCK_INVSOCK
;
226 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
228 m_error
= GSOCK_INVADDR
;
229 return GSOCK_INVADDR
;
233 GAddress_destroy(m_local
);
235 m_local
= GAddress_copy(address
);
237 return GSOCK_NOERROR
;
240 GSocketError
GSocket::SetPeer(GAddress
*address
)
245 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
247 m_error
= GSOCK_INVADDR
;
248 return GSOCK_INVADDR
;
252 GAddress_destroy(m_peer
);
254 m_peer
= GAddress_copy(address
);
256 return GSOCK_NOERROR
;
259 GAddress
*GSocket::GetLocal()
262 struct sockaddr addr
;
263 SOCKLEN_T size
= sizeof(addr
);
268 /* try to get it from the m_local var first */
270 return GAddress_copy(m_local
);
272 /* else, if the socket is initialized, try getsockname */
273 if (m_fd
== INVALID_SOCKET
)
275 m_error
= GSOCK_INVSOCK
;
279 if (getsockname(m_fd
, &addr
, &size
) == SOCKET_ERROR
)
281 m_error
= GSOCK_IOERR
;
285 /* got a valid address from getsockname, create a GAddress object */
286 if ((address
= GAddress_new()) == NULL
)
288 m_error
= GSOCK_MEMERR
;
292 if ((err
= _GAddress_translate_from(address
, &addr
, size
)) != GSOCK_NOERROR
)
294 GAddress_destroy(address
);
302 GAddress
*GSocket::GetPeer()
306 /* try to get it from the m_peer var */
308 return GAddress_copy(m_peer
);
313 /* Server specific parts */
315 /* GSocket_SetServer:
316 * Sets up this socket as a server. The local address must have been
317 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
318 * Returns GSOCK_NOERROR on success, one of the following otherwise:
321 * GSOCK_INVSOCK - the socket is in use.
322 * GSOCK_INVADDR - the local address has not been set.
323 * GSOCK_IOERR - low-level error.
325 GSocketError
GSocket::SetServer()
331 /* must not be in use */
332 if (m_fd
!= INVALID_SOCKET
)
334 m_error
= GSOCK_INVSOCK
;
335 return GSOCK_INVSOCK
;
338 /* the local addr must have been set */
341 m_error
= GSOCK_INVADDR
;
342 return GSOCK_INVADDR
;
345 /* Initialize all fields */
349 /* Create the socket */
350 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
352 if (m_fd
== INVALID_SOCKET
)
354 m_error
= GSOCK_IOERR
;
358 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
359 gs_gui_functions
->Enable_Events(this);
361 /* allow a socket to re-bind if the socket is in the TIME_WAIT
362 state after being previously closed.
365 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
368 /* Bind to the local address,
369 * retrieve the actual address bound,
370 * and listen up to 5 connections.
372 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
375 (SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
376 (listen(m_fd
, 5) != 0))
379 m_error
= GSOCK_IOERR
;
383 return GSOCK_NOERROR
;
386 /* GSocket_WaitConnection:
387 * Waits for an incoming client connection. Returns a pointer to
388 * a GSocket object, or NULL if there was an error, in which case
389 * the last error field will be updated for the calling GSocket.
391 * Error codes (set in the calling GSocket)
392 * GSOCK_INVSOCK - the socket is not valid or not a server.
393 * GSOCK_TIMEDOUT - timeout, no incoming connections.
394 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
395 * GSOCK_MEMERR - couldn't allocate memory.
396 * GSOCK_IOERR - low-level error.
398 GSocket
*GSocket::WaitConnection()
401 struct sockaddr from
;
402 SOCKLEN_T fromlen
= sizeof(from
);
408 /* Reenable CONNECTION events */
409 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
411 /* If the socket has already been created, we exit immediately */
412 if (m_fd
== INVALID_SOCKET
|| !m_server
)
414 m_error
= GSOCK_INVSOCK
;
418 /* Create a GSocket object for the new connection */
419 connection
= GSocket_new();
423 m_error
= GSOCK_MEMERR
;
427 /* Wait for a connection (with timeout) */
428 if (Input_Timeout() == GSOCK_TIMEDOUT
)
430 GSocket_destroy(connection
);
431 /* m_error set by _GSocket_Input_Timeout */
435 connection
->m_fd
= accept(m_fd
, &from
, &fromlen
);
437 if (connection
->m_fd
== INVALID_SOCKET
)
439 if (WSAGetLastError() == WSAEWOULDBLOCK
)
440 m_error
= GSOCK_WOULDBLOCK
;
442 m_error
= GSOCK_IOERR
;
444 GSocket_destroy(connection
);
448 /* Initialize all fields */
449 connection
->m_server
= FALSE
;
450 connection
->m_stream
= TRUE
;
452 /* Setup the peer address field */
453 connection
->m_peer
= GAddress_new();
454 if (!connection
->m_peer
)
456 GSocket_destroy(connection
);
457 m_error
= GSOCK_MEMERR
;
460 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
461 if (err
!= GSOCK_NOERROR
)
463 GAddress_destroy(connection
->m_peer
);
464 GSocket_destroy(connection
);
469 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
470 gs_gui_functions
->Enable_Events(connection
);
475 /* GSocket_SetReusable:
476 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
477 * make the appropriate setsockopt() call.
478 * Implemented as a GSocket function because clients (ie, wxSocketServer)
479 * don't have access to the GSocket struct information.
480 * Returns TRUE if the flag was set correctly, FALSE if an error occured
481 * (ie, if the parameter was NULL)
483 int GSocket::SetReusable()
485 /* socket must not be null, and must not be in use/already bound */
486 if (this && m_fd
== INVALID_SOCKET
) {
493 /* Client specific parts */
496 * For stream (connection oriented) sockets, GSocket_Connect() tries
497 * to establish a client connection to a server using the peer address
498 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
499 * connection has been succesfully established, or one of the error
500 * codes listed below. Note that for nonblocking sockets, a return
501 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
502 * request can be completed later; you should use GSocket_Select()
503 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
504 * corresponding asynchronous events.
506 * For datagram (non connection oriented) sockets, GSocket_Connect()
507 * just sets the peer address established with GSocket_SetPeer() as
508 * default destination.
511 * GSOCK_INVSOCK - the socket is in use or not valid.
512 * GSOCK_INVADDR - the peer address has not been established.
513 * GSOCK_TIMEDOUT - timeout, the connection failed.
514 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
515 * GSOCK_MEMERR - couldn't allocate memory.
516 * GSOCK_IOERR - low-level error.
518 GSocketError
GSocket::Connect(GSocketStream stream
)
525 /* Enable CONNECTION events (needed for nonblocking connections) */
526 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
528 if (m_fd
!= INVALID_SOCKET
)
530 m_error
= GSOCK_INVSOCK
;
531 return GSOCK_INVSOCK
;
536 m_error
= GSOCK_INVADDR
;
537 return GSOCK_INVADDR
;
540 /* Streamed or dgram socket? */
541 m_stream
= (stream
== GSOCK_STREAMED
);
543 m_establishing
= FALSE
;
545 /* Create the socket */
546 m_fd
= socket(m_peer
->m_realfamily
,
547 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
549 if (m_fd
== INVALID_SOCKET
)
551 m_error
= GSOCK_IOERR
;
555 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
556 gs_gui_functions
->Enable_Events(this);
558 /* Connect it to the peer address, with a timeout (see below) */
559 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
561 if (ret
== SOCKET_ERROR
)
563 err
= WSAGetLastError();
565 /* If connect failed with EWOULDBLOCK and the GSocket object
566 * is in blocking mode, we select() for the specified timeout
567 * checking for writability to see if the connection request
570 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
572 err
= Connect_Timeout();
574 if (err
!= GSOCK_NOERROR
)
577 /* m_error is set in _GSocket_Connect_Timeout */
580 return (GSocketError
) err
;
583 /* If connect failed with EWOULDBLOCK and the GSocket object
584 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
585 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
586 * this way if the connection completes, a GSOCK_CONNECTION
587 * event will be generated, if enabled.
589 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
591 m_establishing
= TRUE
;
592 m_error
= GSOCK_WOULDBLOCK
;
593 return GSOCK_WOULDBLOCK
;
596 /* If connect failed with an error other than EWOULDBLOCK,
597 * then the call to GSocket_Connect() has failed.
600 m_error
= GSOCK_IOERR
;
604 return GSOCK_NOERROR
;
607 /* Datagram sockets */
609 /* GSocket_SetNonOriented:
610 * Sets up this socket as a non-connection oriented (datagram) socket.
611 * Before using this function, the local address must have been set
612 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
613 * on success, or one of the following otherwise.
616 * GSOCK_INVSOCK - the socket is in use.
617 * GSOCK_INVADDR - the local address has not been set.
618 * GSOCK_IOERR - low-level error.
620 GSocketError
GSocket::SetNonOriented()
626 if (m_fd
!= INVALID_SOCKET
)
628 m_error
= GSOCK_INVSOCK
;
629 return GSOCK_INVSOCK
;
634 m_error
= GSOCK_INVADDR
;
635 return GSOCK_INVADDR
;
638 /* Initialize all fields */
642 /* Create the socket */
643 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
645 if (m_fd
== INVALID_SOCKET
)
647 m_error
= GSOCK_IOERR
;
651 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
652 gs_gui_functions
->Enable_Events(this);
654 /* Bind to the local address,
655 * and retrieve the actual address bound.
657 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
660 (SOCKLEN_T
*)&m_local
->m_len
) != 0))
663 m_error
= GSOCK_IOERR
;
667 return GSOCK_NOERROR
;
672 /* Like recv(), send(), ... */
673 int GSocket::Read(char *buffer
, int size
)
679 /* Reenable INPUT events */
680 m_detected
&= ~GSOCK_INPUT_FLAG
;
682 if (m_fd
== INVALID_SOCKET
|| m_server
)
684 m_error
= GSOCK_INVSOCK
;
688 /* If the socket is blocking, wait for data (with a timeout) */
689 if (Input_Timeout() == GSOCK_TIMEDOUT
)
694 ret
= Recv_Stream(buffer
, size
);
696 ret
= Recv_Dgram(buffer
, size
);
698 if (ret
== SOCKET_ERROR
)
700 if (WSAGetLastError() != WSAEWOULDBLOCK
)
701 m_error
= GSOCK_IOERR
;
703 m_error
= GSOCK_WOULDBLOCK
;
710 int GSocket::Write(const char *buffer
, int size
)
716 if (m_fd
== INVALID_SOCKET
|| m_server
)
718 m_error
= GSOCK_INVSOCK
;
722 /* If the socket is blocking, wait for writability (with a timeout) */
723 if (Output_Timeout() == GSOCK_TIMEDOUT
)
728 ret
= Send_Stream(buffer
, size
);
730 ret
= Send_Dgram(buffer
, size
);
732 if (ret
== SOCKET_ERROR
)
734 if (WSAGetLastError() != WSAEWOULDBLOCK
)
735 m_error
= GSOCK_IOERR
;
737 m_error
= GSOCK_WOULDBLOCK
;
739 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
740 * does). Once the first OUTPUT event is received, users can assume
741 * that the socket is writable until a read operation fails. Only then
742 * will further OUTPUT events be posted.
744 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
752 * Polls the socket to determine its status. This function will
753 * check for the events specified in the 'flags' parameter, and
754 * it will return a mask indicating which operations can be
755 * performed. This function won't block, regardless of the
756 * mode (blocking | nonblocking) of the socket.
758 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
760 if (!gs_gui_functions
->CanUseEventLoop())
762 GSocketEventFlags result
= 0;
772 FD_SET(m_fd
, &readfds
);
773 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
774 FD_SET(m_fd
, &writefds
);
775 FD_SET(m_fd
, &exceptfds
);
777 /* Check 'sticky' CONNECTION flag first */
778 result
|= (GSOCK_CONNECTION_FLAG
& m_detected
);
780 /* If we have already detected a LOST event, then don't try
781 * to do any further processing.
783 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
785 m_establishing
= FALSE
;
787 return (GSOCK_LOST_FLAG
& flags
);
791 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
,
794 /* What to do here? */
795 return (result
& flags
);
798 /* Check for readability */
799 if (FD_ISSET(m_fd
, &readfds
))
803 if (!m_stream
|| recv(m_fd
, &c
, 1, MSG_PEEK
) > 0)
805 result
|= GSOCK_INPUT_FLAG
;
809 if (m_server
&& m_stream
)
811 result
|= GSOCK_CONNECTION_FLAG
;
812 m_detected
|= GSOCK_CONNECTION_FLAG
;
816 m_detected
= GSOCK_LOST_FLAG
;
817 m_establishing
= FALSE
;
819 /* LOST event: Abort any further processing */
820 return (GSOCK_LOST_FLAG
& flags
);
825 /* Check for writability */
826 if (FD_ISSET(m_fd
, &writefds
))
828 if (m_establishing
&& !m_server
)
831 SOCKLEN_T len
= sizeof(error
);
833 m_establishing
= FALSE
;
835 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
839 m_detected
= GSOCK_LOST_FLAG
;
841 /* LOST event: Abort any further processing */
842 return (GSOCK_LOST_FLAG
& flags
);
846 result
|= GSOCK_CONNECTION_FLAG
;
847 m_detected
|= GSOCK_CONNECTION_FLAG
;
852 result
|= GSOCK_OUTPUT_FLAG
;
856 /* Check for exceptions and errors (is this useful in Unices?) */
857 if (FD_ISSET(m_fd
, &exceptfds
))
859 m_establishing
= FALSE
;
860 m_detected
= GSOCK_LOST_FLAG
;
862 /* LOST event: Abort any further processing */
863 return (GSOCK_LOST_FLAG
& flags
);
866 return (result
& flags
);
871 return flags
& m_detected
;
877 /* GSocket_SetNonBlocking:
878 * Sets the socket to non-blocking mode. All IO calls will return
881 void GSocket::SetNonBlocking(bool non_block
)
885 m_non_blocking
= non_block
;
888 /* GSocket_SetTimeout:
889 * Sets the timeout for blocking calls. Time is expressed in
892 void GSocket::SetTimeout(unsigned long millis
)
896 m_timeout
.tv_sec
= (millis
/ 1000);
897 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
901 * Returns the last error occured for this socket. Note that successful
902 * operations do not clear this back to GSOCK_NOERROR, so use it only
905 GSocketError
GSocket::GetError()
915 * There is data to be read in the input buffer. If, after a read
916 * operation, there is still data available, the callback function will
919 * The socket is available for writing. That is, the next write call
920 * won't block. This event is generated only once, when the connection is
921 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
922 * when the output buffer empties again. This means that the app should
923 * assume that it can write since the first OUTPUT event, and no more
924 * OUTPUT events will be generated unless an error occurs.
926 * Connection succesfully established, for client sockets, or incoming
927 * client connection, for server sockets. Wait for this event (also watch
928 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
930 * The connection is lost (or a connection request failed); this could
931 * be due to a failure, or due to the peer closing it gracefully.
934 /* GSocket_SetCallback:
935 * Enables the callbacks specified by 'flags'. Note that 'flags'
936 * may be a combination of flags OR'ed toghether, so the same
937 * callback function can be made to accept different events.
938 * The callback function must have the following prototype:
940 * void function(GSocket *socket, GSocketEvent event, char *cdata)
942 void GSocket::SetCallback(GSocketEventFlags flags
,
943 GSocketCallback callback
, char *cdata
)
949 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
951 if ((flags
& (1 << count
)) != 0)
953 m_cbacks
[count
] = callback
;
954 m_data
[count
] = cdata
;
959 /* GSocket_UnsetCallback:
960 * Disables all callbacks specified by 'flags', which may be a
961 * combination of flags OR'ed toghether.
963 void GSocket::UnsetCallback(GSocketEventFlags flags
)
969 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
971 if ((flags
& (1 << count
)) != 0)
973 m_cbacks
[count
] = NULL
;
974 m_data
[count
] = NULL
;
979 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
980 void *optval
, int *optlen
)
982 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
984 return GSOCK_NOERROR
;
989 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
990 const void *optval
, int optlen
)
992 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
994 return GSOCK_NOERROR
;
1001 /* _GSocket_Input_Timeout:
1002 * For blocking sockets, wait until data is available or
1003 * until timeout ellapses.
1005 GSocketError
GSocket::Input_Timeout()
1009 if (!m_non_blocking
)
1012 FD_SET(m_fd
, &readfds
);
1013 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
1015 m_error
= GSOCK_TIMEDOUT
;
1016 return GSOCK_TIMEDOUT
;
1019 return GSOCK_NOERROR
;
1022 /* _GSocket_Output_Timeout:
1023 * For blocking sockets, wait until data can be sent without
1024 * blocking or until timeout ellapses.
1026 GSocketError
GSocket::Output_Timeout()
1030 if (!m_non_blocking
)
1033 FD_SET(m_fd
, &writefds
);
1034 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
1036 m_error
= GSOCK_TIMEDOUT
;
1037 return GSOCK_TIMEDOUT
;
1040 return GSOCK_NOERROR
;
1043 /* _GSocket_Connect_Timeout:
1044 * For blocking sockets, wait until the connection is
1045 * established or fails, or until timeout ellapses.
1047 GSocketError
GSocket::Connect_Timeout()
1053 FD_ZERO(&exceptfds
);
1054 FD_SET(m_fd
, &writefds
);
1055 FD_SET(m_fd
, &exceptfds
);
1056 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
1058 m_error
= GSOCK_TIMEDOUT
;
1059 return GSOCK_TIMEDOUT
;
1061 if (!FD_ISSET(m_fd
, &writefds
))
1063 m_error
= GSOCK_IOERR
;
1067 return GSOCK_NOERROR
;
1070 int GSocket::Recv_Stream(char *buffer
, int size
)
1072 return recv(m_fd
, buffer
, size
, 0);
1075 int GSocket::Recv_Dgram(char *buffer
, int size
)
1077 struct sockaddr from
;
1078 SOCKLEN_T fromlen
= sizeof(from
);
1082 ret
= recvfrom(m_fd
, buffer
, size
, 0, &from
, &fromlen
);
1084 if (ret
== SOCKET_ERROR
)
1085 return SOCKET_ERROR
;
1087 /* Translate a system address into a GSocket address */
1090 m_peer
= GAddress_new();
1093 m_error
= GSOCK_MEMERR
;
1097 err
= _GAddress_translate_from(m_peer
, &from
, fromlen
);
1098 if (err
!= GSOCK_NOERROR
)
1100 GAddress_destroy(m_peer
);
1109 int GSocket::Send_Stream(const char *buffer
, int size
)
1111 return send(m_fd
, buffer
, size
, 0);
1114 int GSocket::Send_Dgram(const char *buffer
, int size
)
1116 struct sockaddr
*addr
;
1122 m_error
= GSOCK_INVADDR
;
1126 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1127 if (err
!= GSOCK_NOERROR
)
1133 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
1135 /* Frees memory allocated by _GAddress_translate_to */
1141 /* Compatibility functions for GSocket */
1142 GSocket
*GSocket_new(void)
1144 GSocket
*newsocket
= new GSocket();
1145 if(newsocket
->IsOk())
1153 * -------------------------------------------------------------------------
1155 * -------------------------------------------------------------------------
1158 /* CHECK_ADDRESS verifies that the current address family is either
1159 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1160 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1161 * an appropiate error code.
1163 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1165 #define CHECK_ADDRESS(address, family) \
1167 if (address->m_family == GSOCK_NOFAMILY) \
1168 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1169 return address->m_error; \
1170 if (address->m_family != GSOCK_##family) \
1172 address->m_error = GSOCK_INVADDR; \
1173 return GSOCK_INVADDR; \
1177 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1179 if (address->m_family == GSOCK_NOFAMILY) \
1180 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1182 if (address->m_family != GSOCK_##family) \
1184 address->m_error = GSOCK_INVADDR; \
1190 GAddress
*GAddress_new(void)
1194 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1197 address
->m_family
= GSOCK_NOFAMILY
;
1198 address
->m_addr
= NULL
;
1204 GAddress
*GAddress_copy(GAddress
*address
)
1208 assert(address
!= NULL
);
1210 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1213 memcpy(addr2
, address
, sizeof(GAddress
));
1215 if (address
->m_addr
)
1217 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1218 if (addr2
->m_addr
== NULL
)
1223 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1229 void GAddress_destroy(GAddress
*address
)
1231 assert(address
!= NULL
);
1233 if (address
->m_addr
)
1234 free(address
->m_addr
);
1239 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1241 assert(address
!= NULL
);
1243 address
->m_family
= type
;
1246 GAddressType
GAddress_GetFamily(GAddress
*address
)
1248 assert(address
!= NULL
);
1250 return address
->m_family
;
1253 GSocketError
_GAddress_translate_from(GAddress
*address
,
1254 struct sockaddr
*addr
, int len
)
1256 address
->m_realfamily
= addr
->sa_family
;
1257 switch (addr
->sa_family
)
1260 address
->m_family
= GSOCK_INET
;
1263 address
->m_family
= GSOCK_UNIX
;
1267 address
->m_family
= GSOCK_INET6
;
1272 address
->m_error
= GSOCK_INVOP
;
1277 if (address
->m_addr
)
1278 free(address
->m_addr
);
1280 address
->m_len
= len
;
1281 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1283 if (address
->m_addr
== NULL
)
1285 address
->m_error
= GSOCK_MEMERR
;
1286 return GSOCK_MEMERR
;
1288 memcpy(address
->m_addr
, addr
, len
);
1290 return GSOCK_NOERROR
;
1293 GSocketError
_GAddress_translate_to(GAddress
*address
,
1294 struct sockaddr
**addr
, int *len
)
1296 if (!address
->m_addr
)
1298 address
->m_error
= GSOCK_INVADDR
;
1299 return GSOCK_INVADDR
;
1302 *len
= address
->m_len
;
1303 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1306 address
->m_error
= GSOCK_MEMERR
;
1307 return GSOCK_MEMERR
;
1310 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1311 return GSOCK_NOERROR
;
1315 * -------------------------------------------------------------------------
1316 * Internet address family
1317 * -------------------------------------------------------------------------
1320 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1322 address
->m_len
= sizeof(struct sockaddr_in
);
1323 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1324 if (address
->m_addr
== NULL
)
1326 address
->m_error
= GSOCK_MEMERR
;
1327 return GSOCK_MEMERR
;
1330 address
->m_family
= GSOCK_INET
;
1331 address
->m_realfamily
= AF_INET
;
1332 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1333 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1335 return GSOCK_NOERROR
;
1338 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1341 struct in_addr
*addr
;
1343 assert(address
!= NULL
);
1345 CHECK_ADDRESS(address
, INET
);
1347 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1349 addr
->s_addr
= inet_addr(hostname
);
1351 /* If it is a numeric host name, convert it now */
1352 if (addr
->s_addr
== INADDR_NONE
)
1354 struct in_addr
*array_addr
;
1356 /* It is a real name, we solve it */
1357 if ((he
= gethostbyname(hostname
)) == NULL
)
1359 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1360 address
->m_error
= GSOCK_NOHOST
;
1361 return GSOCK_NOHOST
;
1363 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1364 addr
->s_addr
= array_addr
[0].s_addr
;
1366 return GSOCK_NOERROR
;
1369 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1371 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1374 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1375 unsigned long hostaddr
)
1377 struct in_addr
*addr
;
1379 assert(address
!= NULL
);
1381 CHECK_ADDRESS(address
, INET
);
1383 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1384 addr
->s_addr
= htonl(hostaddr
);;
1386 return GSOCK_NOERROR
;
1389 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1390 const char *protocol
)
1393 struct sockaddr_in
*addr
;
1395 assert(address
!= NULL
);
1396 CHECK_ADDRESS(address
, INET
);
1400 address
->m_error
= GSOCK_INVPORT
;
1401 return GSOCK_INVPORT
;
1404 se
= getservbyname(port
, protocol
);
1407 if (isdigit(port
[0]))
1411 port_int
= atoi(port
);
1412 addr
= (struct sockaddr_in
*)address
->m_addr
;
1413 addr
->sin_port
= htons((u_short
) port_int
);
1414 return GSOCK_NOERROR
;
1417 address
->m_error
= GSOCK_INVPORT
;
1418 return GSOCK_INVPORT
;
1421 addr
= (struct sockaddr_in
*)address
->m_addr
;
1422 addr
->sin_port
= se
->s_port
;
1424 return GSOCK_NOERROR
;
1427 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1429 struct sockaddr_in
*addr
;
1431 assert(address
!= NULL
);
1432 CHECK_ADDRESS(address
, INET
);
1434 addr
= (struct sockaddr_in
*)address
->m_addr
;
1435 addr
->sin_port
= htons(port
);
1437 return GSOCK_NOERROR
;
1440 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1444 struct sockaddr_in
*addr
;
1446 assert(address
!= NULL
);
1447 CHECK_ADDRESS(address
, INET
);
1449 addr
= (struct sockaddr_in
*)address
->m_addr
;
1450 addr_buf
= (char *)&(addr
->sin_addr
);
1452 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1455 address
->m_error
= GSOCK_NOHOST
;
1456 return GSOCK_NOHOST
;
1459 strncpy(hostname
, he
->h_name
, sbuf
);
1461 return GSOCK_NOERROR
;
1464 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1466 struct sockaddr_in
*addr
;
1468 assert(address
!= NULL
);
1469 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1471 addr
= (struct sockaddr_in
*)address
->m_addr
;
1473 return ntohl(addr
->sin_addr
.s_addr
);
1476 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1478 struct sockaddr_in
*addr
;
1480 assert(address
!= NULL
);
1481 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1483 addr
= (struct sockaddr_in
*)address
->m_addr
;
1484 return ntohs(addr
->sin_port
);
1488 * -------------------------------------------------------------------------
1489 * Unix address family
1490 * -------------------------------------------------------------------------
1493 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1495 assert (address
!= NULL
);
1496 address
->m_error
= GSOCK_INVADDR
;
1497 return GSOCK_INVADDR
;
1500 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1502 #if defined(__BORLANDC__)
1503 /* prevents unused variable message in Borland */
1506 assert (address
!= NULL
);
1507 address
->m_error
= GSOCK_INVADDR
;
1508 return GSOCK_INVADDR
;
1511 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1513 #if defined(__BORLANDC__)
1514 /* prevents unused variable message in Borland */
1518 assert (address
!= NULL
);
1519 address
->m_error
= GSOCK_INVADDR
;
1520 return GSOCK_INVADDR
;
1523 #else /* !wxUSE_SOCKETS */
1526 * Translation unit shouldn't be empty, so include this typedef to make the
1527 * compiler (VC++ 6.0, for example) happy
1529 typedef void (*wxDummy
)();
1531 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */