1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
3 * Name: src/msw/gsocket.cpp
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 #include "wx/platform.h"
60 #include "wx/gsocket.h"
63 wxFORCE_LINK_MODULE(gsockmsw
)
67 #define isdigit(x) (x > 47 && x < 58)
69 #include "wx/msw/wince/net.h"
78 /* if we use configure for MSW WX_SOCKLEN_T will be already defined */
80 # define WX_SOCKLEN_T int
84 typedef struct sockaddr_storage wxSockAddr
;
86 typedef struct sockaddr wxSockAddr
;
93 GSocketManager
* const manager
= GSocketManager::Get();
94 if ( !manager
|| !manager
->OnInit() )
97 /* Initialize WinSocket */
98 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
101 void GSocket_Cleanup()
103 GSocketManager
* const manager
= GSocketManager::Get();
107 /* Cleanup WinSocket */
111 /* Constructors / Destructors for GSocket */
117 m_fd
= INVALID_SOCKET
;
118 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
125 m_error
= GSOCK_NOERROR
;
128 m_non_blocking
= false;
129 m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
130 m_timeout
.tv_usec
= 0;
131 m_establishing
= false;
135 m_initialRecvBufferSize
= -1;
136 m_initialSendBufferSize
= -1;
138 m_ok
= GSocketManager::Get()->Init_Socket(this);
141 void GSocket::Close()
143 GSocketManager::Get()->Disable_Events(this);
145 m_fd
= INVALID_SOCKET
;
150 GSocketManager::Get()->Destroy_Socket(this);
152 /* Check that the socket is really shutdowned */
153 if (m_fd
!= INVALID_SOCKET
)
156 /* Destroy private addresses */
158 GAddress_destroy(m_local
);
161 GAddress_destroy(m_peer
);
165 * Disallow further read/write operations on this socket, close
166 * the fd and disable all callbacks.
168 void GSocket::Shutdown()
172 /* If socket has been created, shutdown it */
173 if (m_fd
!= INVALID_SOCKET
)
175 shutdown(m_fd
, 1 /* SD_SEND */);
179 /* Disable GUI callbacks */
180 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
181 m_cbacks
[evt
] = NULL
;
183 m_detected
= GSOCK_LOST_FLAG
;
186 /* Address handling */
192 * Set or get the local or peer address for this socket. The 'set'
193 * functions return GSOCK_NOERROR on success, an error code otherwise.
194 * The 'get' functions return a pointer to a GAddress object on success,
195 * or NULL otherwise, in which case they set the error code of the
196 * corresponding GSocket.
199 * GSOCK_INVSOCK - the socket is not valid.
200 * GSOCK_INVADDR - the address is not valid.
202 GSocketError
GSocket::SetLocal(GAddress
*address
)
204 /* the socket must be initialized, or it must be a server */
205 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
207 m_error
= GSOCK_INVSOCK
;
208 return GSOCK_INVSOCK
;
212 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
214 m_error
= GSOCK_INVADDR
;
215 return GSOCK_INVADDR
;
219 GAddress_destroy(m_local
);
221 m_local
= GAddress_copy(address
);
223 return GSOCK_NOERROR
;
226 GSocketError
GSocket::SetPeer(GAddress
*address
)
229 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
231 m_error
= GSOCK_INVADDR
;
232 return GSOCK_INVADDR
;
236 GAddress_destroy(m_peer
);
238 m_peer
= GAddress_copy(address
);
240 return GSOCK_NOERROR
;
243 GAddress
*GSocket::GetLocal()
247 WX_SOCKLEN_T size
= sizeof(addr
);
250 /* try to get it from the m_local var first */
252 return GAddress_copy(m_local
);
254 /* else, if the socket is initialized, try getsockname */
255 if (m_fd
== INVALID_SOCKET
)
257 m_error
= GSOCK_INVSOCK
;
261 if (getsockname(m_fd
, (sockaddr
*)&addr
, &size
) == SOCKET_ERROR
)
263 m_error
= GSOCK_IOERR
;
267 /* got a valid address from getsockname, create a GAddress object */
268 if ((address
= GAddress_new()) == NULL
)
270 m_error
= GSOCK_MEMERR
;
274 if ((err
= _GAddress_translate_from(address
, (sockaddr
*)&addr
, size
)) != GSOCK_NOERROR
)
276 GAddress_destroy(address
);
284 GAddress
*GSocket::GetPeer()
286 /* try to get it from the m_peer var */
288 return GAddress_copy(m_peer
);
293 /* Server specific parts */
295 /* GSocket_SetServer:
296 * Sets up this socket as a server. The local address must have been
297 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
298 * Returns GSOCK_NOERROR on success, one of the following otherwise:
301 * GSOCK_INVSOCK - the socket is in use.
302 * GSOCK_INVADDR - the local address has not been set.
303 * GSOCK_IOERR - low-level error.
305 GSocketError
GSocket::SetServer()
309 /* must not be in use */
310 if (m_fd
!= INVALID_SOCKET
)
312 m_error
= GSOCK_INVSOCK
;
313 return GSOCK_INVSOCK
;
316 /* the local addr must have been set */
319 m_error
= GSOCK_INVADDR
;
320 return GSOCK_INVADDR
;
323 /* Initialize all fields */
327 /* Create the socket */
328 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
330 if (m_fd
== INVALID_SOCKET
)
332 m_error
= GSOCK_IOERR
;
336 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
337 GSocketManager::Get()->Enable_Events(this);
339 /* allow a socket to re-bind if the socket is in the TIME_WAIT
340 state after being previously closed.
344 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
347 /* Bind to the local address,
348 * retrieve the actual address bound,
349 * and listen up to 5 connections.
351 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
354 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
355 (listen(m_fd
, 5) != 0))
358 m_error
= GSOCK_IOERR
;
362 return GSOCK_NOERROR
;
365 /* GSocket_WaitConnection:
366 * Waits for an incoming client connection. Returns a pointer to
367 * a GSocket object, or NULL if there was an error, in which case
368 * the last error field will be updated for the calling GSocket.
370 * Error codes (set in the calling GSocket)
371 * GSOCK_INVSOCK - the socket is not valid or not a server.
372 * GSOCK_TIMEDOUT - timeout, no incoming connections.
373 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
374 * GSOCK_MEMERR - couldn't allocate memory.
375 * GSOCK_IOERR - low-level error.
377 GSocket
*GSocket::WaitConnection()
381 WX_SOCKLEN_T fromlen
= sizeof(from
);
385 /* Reenable CONNECTION events */
386 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
388 /* If the socket has already been created, we exit immediately */
389 if (m_fd
== INVALID_SOCKET
|| !m_server
)
391 m_error
= GSOCK_INVSOCK
;
395 /* Create a GSocket object for the new connection */
396 connection
= GSocket_new();
400 m_error
= GSOCK_MEMERR
;
404 /* Wait for a connection (with timeout) */
405 if (Input_Timeout() == GSOCK_TIMEDOUT
)
408 /* m_error set by _GSocket_Input_Timeout */
412 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, &fromlen
);
414 if (connection
->m_fd
== INVALID_SOCKET
)
416 if (WSAGetLastError() == WSAEWOULDBLOCK
)
417 m_error
= GSOCK_WOULDBLOCK
;
419 m_error
= GSOCK_IOERR
;
425 /* Initialize all fields */
426 connection
->m_server
= false;
427 connection
->m_stream
= true;
429 /* Setup the peer address field */
430 connection
->m_peer
= GAddress_new();
431 if (!connection
->m_peer
)
434 m_error
= GSOCK_MEMERR
;
437 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
438 if (err
!= GSOCK_NOERROR
)
440 GAddress_destroy(connection
->m_peer
);
446 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
447 GSocketManager::Get()->Enable_Events(connection
);
452 /* GSocket_SetReusable:
453 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
454 * make the appropriate setsockopt() call.
455 * Implemented as a GSocket function because clients (ie, wxSocketServer)
456 * don't have access to the GSocket struct information.
457 * Returns true if the flag was set correctly, false if an error occurred
458 * (ie, if the parameter was NULL)
460 bool GSocket::SetReusable()
462 /* socket must not be null, and must not be in use/already bound */
463 if (this && m_fd
== INVALID_SOCKET
) {
470 /* GSocket_SetBroadcast:
471 * Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
472 * make the appropriate setsockopt() call.
473 * Implemented as a GSocket function because clients (ie, wxSocketServer)
474 * don't have access to the GSocket struct information.
475 * Returns true if the flag was set correctly, false if an error occurred
476 * (ie, if the parameter was NULL)
478 bool GSocket::SetBroadcast()
480 /* socket must not be in use/already bound */
481 if (m_fd
== INVALID_SOCKET
) {
488 bool GSocket::DontDoBind()
490 /* socket must not be in use/already bound */
491 if (m_fd
== INVALID_SOCKET
) {
498 /* Client specific parts */
501 * For stream (connection oriented) sockets, GSocket_Connect() tries
502 * to establish a client connection to a server using the peer address
503 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
504 * connection has been successfully established, or one of the error
505 * codes listed below. Note that for nonblocking sockets, a return
506 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
507 * request can be completed later; you should use GSocket_Select()
508 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
509 * corresponding asynchronous events.
511 * For datagram (non connection oriented) sockets, GSocket_Connect()
512 * just sets the peer address established with GSocket_SetPeer() as
513 * default destination.
516 * GSOCK_INVSOCK - the socket is in use or not valid.
517 * GSOCK_INVADDR - the peer address has not been established.
518 * GSOCK_TIMEDOUT - timeout, the connection failed.
519 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
520 * GSOCK_MEMERR - couldn't allocate memory.
521 * GSOCK_IOERR - low-level error.
523 GSocketError
GSocket::Connect(GSocketStream stream
)
528 /* Enable CONNECTION events (needed for nonblocking connections) */
529 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
531 if (m_fd
!= INVALID_SOCKET
)
533 m_error
= GSOCK_INVSOCK
;
534 return GSOCK_INVSOCK
;
539 m_error
= GSOCK_INVADDR
;
540 return GSOCK_INVADDR
;
543 /* Streamed or dgram socket? */
544 m_stream
= (stream
== GSOCK_STREAMED
);
546 m_establishing
= false;
548 /* Create the socket */
549 m_fd
= socket(m_peer
->m_realfamily
,
550 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
552 if (m_fd
== INVALID_SOCKET
)
554 m_error
= GSOCK_IOERR
;
558 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
559 GSocketManager::Get()->Enable_Events(this);
561 // If the reuse flag is set, use the applicable socket reuse flag
564 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
567 if (m_initialRecvBufferSize
>= 0)
568 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
569 if (m_initialSendBufferSize
>= 0)
570 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
572 // If a local address has been set, then we need to bind to it before calling connect
573 if (m_local
&& m_local
->m_addr
)
575 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
578 /* Connect it to the peer address, with a timeout (see below) */
579 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
581 if (ret
== SOCKET_ERROR
)
583 err
= WSAGetLastError();
585 /* If connect failed with EWOULDBLOCK and the GSocket object
586 * is in blocking mode, we select() for the specified timeout
587 * checking for writability to see if the connection request
590 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
592 err
= Connect_Timeout();
594 if (err
!= GSOCK_NOERROR
)
597 /* m_error is set in _GSocket_Connect_Timeout */
600 return (GSocketError
) err
;
603 /* If connect failed with EWOULDBLOCK and the GSocket object
604 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
605 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
606 * this way if the connection completes, a GSOCK_CONNECTION
607 * event will be generated, if enabled.
609 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
611 m_establishing
= true;
612 m_error
= GSOCK_WOULDBLOCK
;
613 return GSOCK_WOULDBLOCK
;
616 /* If connect failed with an error other than EWOULDBLOCK,
617 * then the call to GSocket_Connect() has failed.
620 m_error
= GSOCK_IOERR
;
624 return GSOCK_NOERROR
;
627 /* Datagram sockets */
629 /* GSocket_SetNonOriented:
630 * Sets up this socket as a non-connection oriented (datagram) socket.
631 * Before using this function, the local address must have been set
632 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
633 * on success, or one of the following otherwise.
636 * GSOCK_INVSOCK - the socket is in use.
637 * GSOCK_INVADDR - the local address has not been set.
638 * GSOCK_IOERR - low-level error.
640 GSocketError
GSocket::SetNonOriented()
644 if (m_fd
!= INVALID_SOCKET
)
646 m_error
= GSOCK_INVSOCK
;
647 return GSOCK_INVSOCK
;
652 m_error
= GSOCK_INVADDR
;
653 return GSOCK_INVADDR
;
656 /* Initialize all fields */
660 /* Create the socket */
661 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
663 if (m_fd
== INVALID_SOCKET
)
665 m_error
= GSOCK_IOERR
;
669 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
670 GSocketManager::Get()->Enable_Events(this);
674 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
678 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
682 /* Bind to the local address,
683 * and retrieve the actual address bound.
685 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
688 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
691 m_error
= GSOCK_IOERR
;
696 return GSOCK_NOERROR
;
701 /* Like recv(), send(), ... */
702 int GSocket::Read(char *buffer
, int size
)
706 /* Reenable INPUT events */
707 m_detected
&= ~GSOCK_INPUT_FLAG
;
709 if (m_fd
== INVALID_SOCKET
|| m_server
)
711 m_error
= GSOCK_INVSOCK
;
715 /* If the socket is blocking, wait for data (with a timeout) */
716 if (Input_Timeout() == GSOCK_TIMEDOUT
)
718 m_error
= GSOCK_TIMEDOUT
;
724 ret
= Recv_Stream(buffer
, size
);
726 ret
= Recv_Dgram(buffer
, size
);
728 if (ret
== SOCKET_ERROR
)
730 if (WSAGetLastError() != WSAEWOULDBLOCK
)
731 m_error
= GSOCK_IOERR
;
733 m_error
= GSOCK_WOULDBLOCK
;
740 int GSocket::Write(const char *buffer
, int size
)
744 if (m_fd
== INVALID_SOCKET
|| m_server
)
746 m_error
= GSOCK_INVSOCK
;
750 /* If the socket is blocking, wait for writability (with a timeout) */
751 if (Output_Timeout() == GSOCK_TIMEDOUT
)
756 ret
= Send_Stream(buffer
, size
);
758 ret
= Send_Dgram(buffer
, size
);
760 if (ret
== SOCKET_ERROR
)
762 if (WSAGetLastError() != WSAEWOULDBLOCK
)
763 m_error
= GSOCK_IOERR
;
765 m_error
= GSOCK_WOULDBLOCK
;
767 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
768 * does). Once the first OUTPUT event is received, users can assume
769 * that the socket is writable until a read operation fails. Only then
770 * will further OUTPUT events be posted.
772 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
781 /* GSocket_SetNonBlocking:
782 * Sets the socket to non-blocking mode. All IO calls will return
785 void GSocket::SetNonBlocking(bool non_block
)
787 m_non_blocking
= non_block
;
790 /* GSocket_SetTimeout:
791 * Sets the timeout for blocking calls. Time is expressed in
794 void GSocket::SetTimeout(unsigned long millis
)
796 m_timeout
.tv_sec
= (millis
/ 1000);
797 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
801 * Returns the last error occurred for this socket. Note that successful
802 * operations do not clear this back to GSOCK_NOERROR, so use it only
805 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
813 * There is data to be read in the input buffer. If, after a read
814 * operation, there is still data available, the callback function will
817 * The socket is available for writing. That is, the next write call
818 * won't block. This event is generated only once, when the connection is
819 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
820 * when the output buffer empties again. This means that the app should
821 * assume that it can write since the first OUTPUT event, and no more
822 * OUTPUT events will be generated unless an error occurs.
824 * Connection successfully established, for client sockets, or incoming
825 * client connection, for server sockets. Wait for this event (also watch
826 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
828 * The connection is lost (or a connection request failed); this could
829 * be due to a failure, or due to the peer closing it gracefully.
832 /* GSocket_SetCallback:
833 * Enables the callbacks specified by 'flags'. Note that 'flags'
834 * may be a combination of flags OR'ed toghether, so the same
835 * callback function can be made to accept different events.
836 * The callback function must have the following prototype:
838 * void function(GSocket *socket, GSocketEvent event, char *cdata)
840 void GSocket::SetCallback(GSocketEventFlags flags
,
841 GSocketCallback callback
, char *cdata
)
845 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
847 if ((flags
& (1 << count
)) != 0)
849 m_cbacks
[count
] = callback
;
850 m_data
[count
] = cdata
;
855 /* GSocket_UnsetCallback:
856 * Disables all callbacks specified by 'flags', which may be a
857 * combination of flags OR'ed toghether.
859 void GSocket::UnsetCallback(GSocketEventFlags flags
)
863 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
865 if ((flags
& (1 << count
)) != 0)
867 m_cbacks
[count
] = NULL
;
868 m_data
[count
] = NULL
;
873 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
874 void *optval
, int *optlen
)
876 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
878 return GSOCK_NOERROR
;
883 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
884 const void *optval
, int optlen
)
886 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
888 return GSOCK_NOERROR
;
895 /* _GSocket_Input_Timeout:
896 * For blocking sockets, wait until data is available or
897 * until timeout ellapses.
899 GSocketError
GSocket::Input_Timeout()
906 FD_SET(m_fd
, &readfds
);
907 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
909 m_error
= GSOCK_TIMEDOUT
;
910 return GSOCK_TIMEDOUT
;
913 return GSOCK_NOERROR
;
916 /* _GSocket_Output_Timeout:
917 * For blocking sockets, wait until data can be sent without
918 * blocking or until timeout ellapses.
920 GSocketError
GSocket::Output_Timeout()
927 FD_SET(m_fd
, &writefds
);
928 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
930 m_error
= GSOCK_TIMEDOUT
;
931 return GSOCK_TIMEDOUT
;
934 return GSOCK_NOERROR
;
937 /* _GSocket_Connect_Timeout:
938 * For blocking sockets, wait until the connection is
939 * established or fails, or until timeout ellapses.
941 GSocketError
GSocket::Connect_Timeout()
948 FD_SET(m_fd
, &writefds
);
949 FD_SET(m_fd
, &exceptfds
);
950 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
952 m_error
= GSOCK_TIMEDOUT
;
953 return GSOCK_TIMEDOUT
;
955 if (!FD_ISSET(m_fd
, &writefds
))
957 m_error
= GSOCK_IOERR
;
961 return GSOCK_NOERROR
;
964 int GSocket::Recv_Stream(char *buffer
, int size
)
966 return recv(m_fd
, buffer
, size
, 0);
969 int GSocket::Recv_Dgram(char *buffer
, int size
)
972 WX_SOCKLEN_T fromlen
= sizeof(from
);
976 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
978 if (ret
== SOCKET_ERROR
)
981 /* Translate a system address into a GSocket address */
984 m_peer
= GAddress_new();
987 m_error
= GSOCK_MEMERR
;
991 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
992 if (err
!= GSOCK_NOERROR
)
994 GAddress_destroy(m_peer
);
1003 int GSocket::Send_Stream(const char *buffer
, int size
)
1005 return send(m_fd
, buffer
, size
, 0);
1008 int GSocket::Send_Dgram(const char *buffer
, int size
)
1010 struct sockaddr
*addr
;
1016 m_error
= GSOCK_INVADDR
;
1020 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1021 if (err
!= GSOCK_NOERROR
)
1027 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
1029 /* Frees memory allocated by _GAddress_translate_to */
1035 /* Compatibility functions for GSocket */
1036 GSocket
*GSocket_new()
1038 GSocket
*newsocket
= new GSocket();
1039 if(newsocket
->IsOk())
1047 * -------------------------------------------------------------------------
1049 * -------------------------------------------------------------------------
1052 /* CHECK_ADDRESS verifies that the current address family is either
1053 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1054 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1055 * an appropiate error code.
1057 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1059 #define CHECK_ADDRESS(address, family) \
1061 if (address->m_family == GSOCK_NOFAMILY) \
1062 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1063 return address->m_error; \
1064 if (address->m_family != GSOCK_##family) \
1066 address->m_error = GSOCK_INVADDR; \
1067 return GSOCK_INVADDR; \
1071 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1073 if (address->m_family == GSOCK_NOFAMILY) \
1074 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1076 if (address->m_family != GSOCK_##family) \
1078 address->m_error = GSOCK_INVADDR; \
1084 GAddress
*GAddress_new()
1088 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1091 address
->m_family
= GSOCK_NOFAMILY
;
1092 address
->m_addr
= NULL
;
1098 GAddress
*GAddress_copy(GAddress
*address
)
1102 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1105 memcpy(addr2
, address
, sizeof(GAddress
));
1107 if (address
->m_addr
)
1109 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1110 if (addr2
->m_addr
== NULL
)
1115 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1121 void GAddress_destroy(GAddress
*address
)
1123 if (address
->m_addr
)
1124 free(address
->m_addr
);
1129 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1131 address
->m_family
= type
;
1134 GAddressType
GAddress_GetFamily(GAddress
*address
)
1136 return address
->m_family
;
1139 GSocketError
_GAddress_translate_from(GAddress
*address
,
1140 struct sockaddr
*addr
, int len
)
1142 address
->m_realfamily
= addr
->sa_family
;
1143 switch (addr
->sa_family
)
1146 address
->m_family
= GSOCK_INET
;
1149 address
->m_family
= GSOCK_UNIX
;
1153 address
->m_family
= GSOCK_INET6
;
1158 address
->m_error
= GSOCK_INVOP
;
1163 if (address
->m_addr
)
1164 free(address
->m_addr
);
1166 address
->m_len
= len
;
1167 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1169 if (address
->m_addr
== NULL
)
1171 address
->m_error
= GSOCK_MEMERR
;
1172 return GSOCK_MEMERR
;
1174 memcpy(address
->m_addr
, addr
, len
);
1176 return GSOCK_NOERROR
;
1179 GSocketError
_GAddress_translate_to(GAddress
*address
,
1180 struct sockaddr
**addr
, int *len
)
1182 if (!address
->m_addr
)
1184 address
->m_error
= GSOCK_INVADDR
;
1185 return GSOCK_INVADDR
;
1188 *len
= address
->m_len
;
1189 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1192 address
->m_error
= GSOCK_MEMERR
;
1193 return GSOCK_MEMERR
;
1196 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1197 return GSOCK_NOERROR
;
1201 * -------------------------------------------------------------------------
1202 * Internet address family
1203 * -------------------------------------------------------------------------
1206 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1208 address
->m_len
= sizeof(struct sockaddr_in
);
1209 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1210 if (address
->m_addr
== NULL
)
1212 address
->m_error
= GSOCK_MEMERR
;
1213 return GSOCK_MEMERR
;
1216 address
->m_family
= GSOCK_INET
;
1217 address
->m_realfamily
= AF_INET
;
1218 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1219 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1221 return GSOCK_NOERROR
;
1224 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1227 struct in_addr
*addr
;
1229 CHECK_ADDRESS(address
, INET
);
1231 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1233 addr
->s_addr
= inet_addr(hostname
);
1235 /* If it is a numeric host name, convert it now */
1236 if (addr
->s_addr
== INADDR_NONE
)
1238 struct in_addr
*array_addr
;
1240 /* It is a real name, we solve it */
1241 if ((he
= gethostbyname(hostname
)) == NULL
)
1243 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1244 address
->m_error
= GSOCK_NOHOST
;
1245 return GSOCK_NOHOST
;
1247 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1248 addr
->s_addr
= array_addr
[0].s_addr
;
1250 return GSOCK_NOERROR
;
1253 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1255 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1258 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1260 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1263 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1264 unsigned long hostaddr
)
1266 struct in_addr
*addr
;
1268 CHECK_ADDRESS(address
, INET
);
1270 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1271 addr
->s_addr
= htonl(hostaddr
);
1273 return GSOCK_NOERROR
;
1276 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1277 const char *protocol
)
1280 struct sockaddr_in
*addr
;
1282 CHECK_ADDRESS(address
, INET
);
1286 address
->m_error
= GSOCK_INVPORT
;
1287 return GSOCK_INVPORT
;
1290 se
= getservbyname(port
, protocol
);
1293 if (isdigit(port
[0]))
1297 port_int
= atoi(port
);
1298 addr
= (struct sockaddr_in
*)address
->m_addr
;
1299 addr
->sin_port
= htons((u_short
) port_int
);
1300 return GSOCK_NOERROR
;
1303 address
->m_error
= GSOCK_INVPORT
;
1304 return GSOCK_INVPORT
;
1307 addr
= (struct sockaddr_in
*)address
->m_addr
;
1308 addr
->sin_port
= se
->s_port
;
1310 return GSOCK_NOERROR
;
1313 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1315 struct sockaddr_in
*addr
;
1317 CHECK_ADDRESS(address
, INET
);
1319 addr
= (struct sockaddr_in
*)address
->m_addr
;
1320 addr
->sin_port
= htons(port
);
1322 return GSOCK_NOERROR
;
1325 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1329 struct sockaddr_in
*addr
;
1331 CHECK_ADDRESS(address
, INET
);
1333 addr
= (struct sockaddr_in
*)address
->m_addr
;
1334 addr_buf
= (char *)&(addr
->sin_addr
);
1336 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1339 address
->m_error
= GSOCK_NOHOST
;
1340 return GSOCK_NOHOST
;
1343 strncpy(hostname
, he
->h_name
, sbuf
);
1345 return GSOCK_NOERROR
;
1348 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1350 struct sockaddr_in
*addr
;
1352 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1354 addr
= (struct sockaddr_in
*)address
->m_addr
;
1356 return ntohl(addr
->sin_addr
.s_addr
);
1359 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1361 struct sockaddr_in
*addr
;
1363 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1365 addr
= (struct sockaddr_in
*)address
->m_addr
;
1366 return ntohs(addr
->sin_port
);
1372 * -------------------------------------------------------------------------
1373 * Internet IPv6 address family
1374 * -------------------------------------------------------------------------
1376 #include "ws2tcpip.h"
1379 #pragma comment(lib,"ws2_32")
1380 #endif // __VISUALC__
1382 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1384 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1385 address
->m_len
= sizeof(struct sockaddr_in6
);
1386 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1387 if (address
->m_addr
== NULL
)
1389 address
->m_error
= GSOCK_MEMERR
;
1390 return GSOCK_MEMERR
;
1392 memset(address
->m_addr
,0,address
->m_len
);
1394 address
->m_family
= GSOCK_INET6
;
1395 address
->m_realfamily
= AF_INET6
;
1396 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1397 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1399 return GSOCK_NOERROR
;
1402 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1404 CHECK_ADDRESS(address
, INET6
);
1407 memset( & hints
, 0, sizeof( hints
) );
1408 hints
.ai_family
= AF_INET6
;
1409 addrinfo
* info
= 0;
1410 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1412 address
->m_error
= GSOCK_NOHOST
;
1413 return GSOCK_NOHOST
;
1416 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1417 freeaddrinfo( info
);
1418 return GSOCK_NOERROR
;
1421 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1423 CHECK_ADDRESS(address
, INET6
);
1425 struct in6_addr addr
;
1426 memset( & addr
, 0, sizeof( addr
) );
1427 return GAddress_INET6_SetHostAddress(address
, addr
);
1429 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1430 struct in6_addr hostaddr
)
1432 CHECK_ADDRESS(address
, INET6
);
1434 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1436 return GSOCK_NOERROR
;
1439 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1440 const char *protocol
)
1443 struct sockaddr_in6
*addr
;
1445 CHECK_ADDRESS(address
, INET6
);
1449 address
->m_error
= GSOCK_INVPORT
;
1450 return GSOCK_INVPORT
;
1453 se
= getservbyname(port
, protocol
);
1456 if (isdigit((unsigned char) port
[0]))
1460 port_int
= atoi(port
);
1461 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1462 addr
->sin6_port
= htons((u_short
) port_int
);
1463 return GSOCK_NOERROR
;
1466 address
->m_error
= GSOCK_INVPORT
;
1467 return GSOCK_INVPORT
;
1470 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1471 addr
->sin6_port
= se
->s_port
;
1473 return GSOCK_NOERROR
;
1476 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1478 struct sockaddr_in6
*addr
;
1480 CHECK_ADDRESS(address
, INET6
);
1482 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1483 addr
->sin6_port
= htons(port
);
1485 return GSOCK_NOERROR
;
1488 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1492 struct sockaddr_in6
*addr
;
1494 CHECK_ADDRESS(address
, INET6
);
1496 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1497 addr_buf
= (char *)&(addr
->sin6_addr
);
1499 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1502 address
->m_error
= GSOCK_NOHOST
;
1503 return GSOCK_NOHOST
;
1506 strncpy(hostname
, he
->h_name
, sbuf
);
1508 return GSOCK_NOERROR
;
1511 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1513 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1514 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1515 return GSOCK_NOERROR
;
1518 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1520 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1522 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1525 #endif // wxUSE_IPV6
1528 * -------------------------------------------------------------------------
1529 * Unix address family
1530 * -------------------------------------------------------------------------
1533 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1535 address
->m_error
= GSOCK_INVADDR
;
1536 return GSOCK_INVADDR
;
1539 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1541 address
->m_error
= GSOCK_INVADDR
;
1542 return GSOCK_INVADDR
;
1545 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1547 address
->m_error
= GSOCK_INVADDR
;
1548 return GSOCK_INVADDR
;
1551 #endif // wxUSE_SOCKETS