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"
64 #define isdigit(x) (x > 47 && x < 58)
66 #include "wx/msw/wince/net.h"
75 /* if we use configure for MSW WX_SOCKLEN_T will be already defined */
77 # define WX_SOCKLEN_T int
81 typedef struct sockaddr_storage wxSockAddr
;
83 typedef struct sockaddr wxSockAddr
;
90 GSocketManager
* const manager
= GSocketManager::Get();
91 if ( !manager
|| !manager
->OnInit() )
94 /* Initialize WinSocket */
95 return WSAStartup((1 << 8) | 1, &wsaData
) == 0;
98 void GSocket_Cleanup()
100 GSocketManager
* const manager
= GSocketManager::Get();
104 /* Cleanup WinSocket */
108 /* Constructors / Destructors for GSocket */
114 m_fd
= INVALID_SOCKET
;
115 for (i
= 0; i
< GSOCK_MAX_EVENT
; i
++)
122 m_error
= GSOCK_NOERROR
;
125 m_non_blocking
= false;
126 m_timeout
.tv_sec
= 10 * 60; /* 10 minutes */
127 m_timeout
.tv_usec
= 0;
128 m_establishing
= false;
132 m_initialRecvBufferSize
= -1;
133 m_initialSendBufferSize
= -1;
135 m_ok
= GSocketManager::Get()->Init_Socket(this);
138 void GSocket::Close()
140 GSocketManager::Get()->Disable_Events(this);
142 m_fd
= INVALID_SOCKET
;
147 GSocketManager::Get()->Destroy_Socket(this);
149 /* Check that the socket is really shutdowned */
150 if (m_fd
!= INVALID_SOCKET
)
153 /* Destroy private addresses */
155 GAddress_destroy(m_local
);
158 GAddress_destroy(m_peer
);
162 * Disallow further read/write operations on this socket, close
163 * the fd and disable all callbacks.
165 void GSocket::Shutdown()
169 /* If socket has been created, shutdown it */
170 if (m_fd
!= INVALID_SOCKET
)
172 shutdown(m_fd
, 1 /* SD_SEND */);
176 /* Disable GUI callbacks */
177 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
178 m_cbacks
[evt
] = NULL
;
180 m_detected
= GSOCK_LOST_FLAG
;
183 /* Address handling */
189 * Set or get the local or peer address for this socket. The 'set'
190 * functions return GSOCK_NOERROR on success, an error code otherwise.
191 * The 'get' functions return a pointer to a GAddress object on success,
192 * or NULL otherwise, in which case they set the error code of the
193 * corresponding GSocket.
196 * GSOCK_INVSOCK - the socket is not valid.
197 * GSOCK_INVADDR - the address is not valid.
199 GSocketError
GSocket::SetLocal(GAddress
*address
)
201 /* the socket must be initialized, or it must be a server */
202 if (m_fd
!= INVALID_SOCKET
&& !m_server
)
204 m_error
= GSOCK_INVSOCK
;
205 return GSOCK_INVSOCK
;
209 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
211 m_error
= GSOCK_INVADDR
;
212 return GSOCK_INVADDR
;
216 GAddress_destroy(m_local
);
218 m_local
= GAddress_copy(address
);
220 return GSOCK_NOERROR
;
223 GSocketError
GSocket::SetPeer(GAddress
*address
)
226 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
228 m_error
= GSOCK_INVADDR
;
229 return GSOCK_INVADDR
;
233 GAddress_destroy(m_peer
);
235 m_peer
= GAddress_copy(address
);
237 return GSOCK_NOERROR
;
240 GAddress
*GSocket::GetLocal()
244 WX_SOCKLEN_T size
= sizeof(addr
);
247 /* try to get it from the m_local var first */
249 return GAddress_copy(m_local
);
251 /* else, if the socket is initialized, try getsockname */
252 if (m_fd
== INVALID_SOCKET
)
254 m_error
= GSOCK_INVSOCK
;
258 if (getsockname(m_fd
, (sockaddr
*)&addr
, &size
) == SOCKET_ERROR
)
260 m_error
= GSOCK_IOERR
;
264 /* got a valid address from getsockname, create a GAddress object */
265 if ((address
= GAddress_new()) == NULL
)
267 m_error
= GSOCK_MEMERR
;
271 if ((err
= _GAddress_translate_from(address
, (sockaddr
*)&addr
, size
)) != GSOCK_NOERROR
)
273 GAddress_destroy(address
);
281 GAddress
*GSocket::GetPeer()
283 /* try to get it from the m_peer var */
285 return GAddress_copy(m_peer
);
290 /* Server specific parts */
292 /* GSocket_SetServer:
293 * Sets up this socket as a server. The local address must have been
294 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
295 * Returns GSOCK_NOERROR on success, one of the following otherwise:
298 * GSOCK_INVSOCK - the socket is in use.
299 * GSOCK_INVADDR - the local address has not been set.
300 * GSOCK_IOERR - low-level error.
302 GSocketError
GSocket::SetServer()
306 /* must not be in use */
307 if (m_fd
!= INVALID_SOCKET
)
309 m_error
= GSOCK_INVSOCK
;
310 return GSOCK_INVSOCK
;
313 /* the local addr must have been set */
316 m_error
= GSOCK_INVADDR
;
317 return GSOCK_INVADDR
;
320 /* Initialize all fields */
324 /* Create the socket */
325 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
327 if (m_fd
== INVALID_SOCKET
)
329 m_error
= GSOCK_IOERR
;
333 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
334 GSocketManager::Get()->Enable_Events(this);
336 /* allow a socket to re-bind if the socket is in the TIME_WAIT
337 state after being previously closed.
341 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
344 /* Bind to the local address,
345 * retrieve the actual address bound,
346 * and listen up to 5 connections.
348 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
351 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0) ||
352 (listen(m_fd
, 5) != 0))
355 m_error
= GSOCK_IOERR
;
359 return GSOCK_NOERROR
;
362 /* GSocket_WaitConnection:
363 * Waits for an incoming client connection. Returns a pointer to
364 * a GSocket object, or NULL if there was an error, in which case
365 * the last error field will be updated for the calling GSocket.
367 * Error codes (set in the calling GSocket)
368 * GSOCK_INVSOCK - the socket is not valid or not a server.
369 * GSOCK_TIMEDOUT - timeout, no incoming connections.
370 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
371 * GSOCK_MEMERR - couldn't allocate memory.
372 * GSOCK_IOERR - low-level error.
374 GSocket
*GSocket::WaitConnection()
378 WX_SOCKLEN_T fromlen
= sizeof(from
);
382 /* Reenable CONNECTION events */
383 m_detected
&= ~GSOCK_CONNECTION_FLAG
;
385 /* If the socket has already been created, we exit immediately */
386 if (m_fd
== INVALID_SOCKET
|| !m_server
)
388 m_error
= GSOCK_INVSOCK
;
392 /* Create a GSocket object for the new connection */
393 connection
= GSocket_new();
397 m_error
= GSOCK_MEMERR
;
401 /* Wait for a connection (with timeout) */
402 if (Input_Timeout() == GSOCK_TIMEDOUT
)
405 /* m_error set by _GSocket_Input_Timeout */
409 connection
->m_fd
= accept(m_fd
, (sockaddr
*)&from
, &fromlen
);
411 if (connection
->m_fd
== INVALID_SOCKET
)
413 if (WSAGetLastError() == WSAEWOULDBLOCK
)
414 m_error
= GSOCK_WOULDBLOCK
;
416 m_error
= GSOCK_IOERR
;
422 /* Initialize all fields */
423 connection
->m_server
= false;
424 connection
->m_stream
= true;
426 /* Setup the peer address field */
427 connection
->m_peer
= GAddress_new();
428 if (!connection
->m_peer
)
431 m_error
= GSOCK_MEMERR
;
434 err
= _GAddress_translate_from(connection
->m_peer
, (sockaddr
*)&from
, fromlen
);
435 if (err
!= GSOCK_NOERROR
)
437 GAddress_destroy(connection
->m_peer
);
443 ioctlsocket(connection
->m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
444 GSocketManager::Get()->Enable_Events(connection
);
449 /* GSocket_SetReusable:
450 * Simply sets the m_resuable flag on the socket. GSocket_SetServer will
451 * make the appropriate setsockopt() call.
452 * Implemented as a GSocket function because clients (ie, wxSocketServer)
453 * don't have access to the GSocket struct information.
454 * Returns true if the flag was set correctly, false if an error occurred
455 * (ie, if the parameter was NULL)
457 bool GSocket::SetReusable()
459 /* socket must not be null, and must not be in use/already bound */
460 if (this && m_fd
== INVALID_SOCKET
) {
467 /* GSocket_SetBroadcast:
468 * Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
469 * make the appropriate setsockopt() call.
470 * Implemented as a GSocket function because clients (ie, wxSocketServer)
471 * don't have access to the GSocket struct information.
472 * Returns true if the flag was set correctly, false if an error occurred
473 * (ie, if the parameter was NULL)
475 bool GSocket::SetBroadcast()
477 /* socket must not be in use/already bound */
478 if (m_fd
== INVALID_SOCKET
) {
485 bool GSocket::DontDoBind()
487 /* socket must not be in use/already bound */
488 if (m_fd
== INVALID_SOCKET
) {
495 /* Client specific parts */
498 * For stream (connection oriented) sockets, GSocket_Connect() tries
499 * to establish a client connection to a server using the peer address
500 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
501 * connection has been successfully established, or one of the error
502 * codes listed below. Note that for nonblocking sockets, a return
503 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
504 * request can be completed later; you should use GSocket_Select()
505 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
506 * corresponding asynchronous events.
508 * For datagram (non connection oriented) sockets, GSocket_Connect()
509 * just sets the peer address established with GSocket_SetPeer() as
510 * default destination.
513 * GSOCK_INVSOCK - the socket is in use or not valid.
514 * GSOCK_INVADDR - the peer address has not been established.
515 * GSOCK_TIMEDOUT - timeout, the connection failed.
516 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
517 * GSOCK_MEMERR - couldn't allocate memory.
518 * GSOCK_IOERR - low-level error.
520 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 GSocketManager::Get()->Enable_Events(this);
558 // If the reuse flag is set, use the applicable socket reuse flag
561 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
564 if (m_initialRecvBufferSize
>= 0)
565 setsockopt(m_fd
, SOL_SOCKET
, SO_RCVBUF
, (const char*)&m_initialRecvBufferSize
, sizeof(m_initialRecvBufferSize
));
566 if (m_initialSendBufferSize
>= 0)
567 setsockopt(m_fd
, SOL_SOCKET
, SO_SNDBUF
, (const char*)&m_initialSendBufferSize
, sizeof(m_initialSendBufferSize
));
569 // If a local address has been set, then we need to bind to it before calling connect
570 if (m_local
&& m_local
->m_addr
)
572 bind(m_fd
, m_local
->m_addr
, m_local
->m_len
);
575 /* Connect it to the peer address, with a timeout (see below) */
576 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
578 if (ret
== SOCKET_ERROR
)
580 err
= WSAGetLastError();
582 /* If connect failed with EWOULDBLOCK and the GSocket object
583 * is in blocking mode, we select() for the specified timeout
584 * checking for writability to see if the connection request
587 if ((err
== WSAEWOULDBLOCK
) && (!m_non_blocking
))
589 err
= Connect_Timeout();
591 if (err
!= GSOCK_NOERROR
)
594 /* m_error is set in _GSocket_Connect_Timeout */
597 return (GSocketError
) err
;
600 /* If connect failed with EWOULDBLOCK and the GSocket object
601 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
602 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
603 * this way if the connection completes, a GSOCK_CONNECTION
604 * event will be generated, if enabled.
606 if ((err
== WSAEWOULDBLOCK
) && (m_non_blocking
))
608 m_establishing
= true;
609 m_error
= GSOCK_WOULDBLOCK
;
610 return GSOCK_WOULDBLOCK
;
613 /* If connect failed with an error other than EWOULDBLOCK,
614 * then the call to GSocket_Connect() has failed.
617 m_error
= GSOCK_IOERR
;
621 return GSOCK_NOERROR
;
624 /* Datagram sockets */
626 /* GSocket_SetNonOriented:
627 * Sets up this socket as a non-connection oriented (datagram) socket.
628 * Before using this function, the local address must have been set
629 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
630 * on success, or one of the following otherwise.
633 * GSOCK_INVSOCK - the socket is in use.
634 * GSOCK_INVADDR - the local address has not been set.
635 * GSOCK_IOERR - low-level error.
637 GSocketError
GSocket::SetNonOriented()
641 if (m_fd
!= INVALID_SOCKET
)
643 m_error
= GSOCK_INVSOCK
;
644 return GSOCK_INVSOCK
;
649 m_error
= GSOCK_INVADDR
;
650 return GSOCK_INVADDR
;
653 /* Initialize all fields */
657 /* Create the socket */
658 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
660 if (m_fd
== INVALID_SOCKET
)
662 m_error
= GSOCK_IOERR
;
666 ioctlsocket(m_fd
, FIONBIO
, (u_long FAR
*) &arg
);
667 GSocketManager::Get()->Enable_Events(this);
671 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(arg
));
675 setsockopt(m_fd
, SOL_SOCKET
, SO_BROADCAST
, (const char*)&arg
, sizeof(arg
));
679 /* Bind to the local address,
680 * and retrieve the actual address bound.
682 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
685 (WX_SOCKLEN_T
*)&m_local
->m_len
) != 0))
688 m_error
= GSOCK_IOERR
;
693 return GSOCK_NOERROR
;
698 /* Like recv(), send(), ... */
699 int GSocket::Read(char *buffer
, int size
)
703 /* Reenable INPUT events */
704 m_detected
&= ~GSOCK_INPUT_FLAG
;
706 if (m_fd
== INVALID_SOCKET
|| m_server
)
708 m_error
= GSOCK_INVSOCK
;
712 /* If the socket is blocking, wait for data (with a timeout) */
713 if (Input_Timeout() == GSOCK_TIMEDOUT
)
715 m_error
= GSOCK_TIMEDOUT
;
721 ret
= Recv_Stream(buffer
, size
);
723 ret
= Recv_Dgram(buffer
, size
);
725 if (ret
== SOCKET_ERROR
)
727 if (WSAGetLastError() != WSAEWOULDBLOCK
)
728 m_error
= GSOCK_IOERR
;
730 m_error
= GSOCK_WOULDBLOCK
;
737 int GSocket::Write(const char *buffer
, int size
)
741 if (m_fd
== INVALID_SOCKET
|| m_server
)
743 m_error
= GSOCK_INVSOCK
;
747 /* If the socket is blocking, wait for writability (with a timeout) */
748 if (Output_Timeout() == GSOCK_TIMEDOUT
)
753 ret
= Send_Stream(buffer
, size
);
755 ret
= Send_Dgram(buffer
, size
);
757 if (ret
== SOCKET_ERROR
)
759 if (WSAGetLastError() != WSAEWOULDBLOCK
)
760 m_error
= GSOCK_IOERR
;
762 m_error
= GSOCK_WOULDBLOCK
;
764 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
765 * does). Once the first OUTPUT event is received, users can assume
766 * that the socket is writable until a read operation fails. Only then
767 * will further OUTPUT events be posted.
769 m_detected
&= ~GSOCK_OUTPUT_FLAG
;
777 * Polls the socket to determine its status. This function will
778 * check for the events specified in the 'flags' parameter, and
779 * it will return a mask indicating which operations can be
780 * performed. This function won't block, regardless of the
781 * mode (blocking | nonblocking) of the socket.
783 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
785 return flags
& m_detected
;
790 /* GSocket_SetNonBlocking:
791 * Sets the socket to non-blocking mode. All IO calls will return
794 void GSocket::SetNonBlocking(bool non_block
)
796 m_non_blocking
= non_block
;
799 /* GSocket_SetTimeout:
800 * Sets the timeout for blocking calls. Time is expressed in
803 void GSocket::SetTimeout(unsigned long millis
)
805 m_timeout
.tv_sec
= (millis
/ 1000);
806 m_timeout
.tv_usec
= (millis
% 1000) * 1000;
810 * Returns the last error occurred for this socket. Note that successful
811 * operations do not clear this back to GSOCK_NOERROR, so use it only
814 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
822 * There is data to be read in the input buffer. If, after a read
823 * operation, there is still data available, the callback function will
826 * The socket is available for writing. That is, the next write call
827 * won't block. This event is generated only once, when the connection is
828 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
829 * when the output buffer empties again. This means that the app should
830 * assume that it can write since the first OUTPUT event, and no more
831 * OUTPUT events will be generated unless an error occurs.
833 * Connection successfully established, for client sockets, or incoming
834 * client connection, for server sockets. Wait for this event (also watch
835 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
837 * The connection is lost (or a connection request failed); this could
838 * be due to a failure, or due to the peer closing it gracefully.
841 /* GSocket_SetCallback:
842 * Enables the callbacks specified by 'flags'. Note that 'flags'
843 * may be a combination of flags OR'ed toghether, so the same
844 * callback function can be made to accept different events.
845 * The callback function must have the following prototype:
847 * void function(GSocket *socket, GSocketEvent event, char *cdata)
849 void GSocket::SetCallback(GSocketEventFlags flags
,
850 GSocketCallback callback
, char *cdata
)
854 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
856 if ((flags
& (1 << count
)) != 0)
858 m_cbacks
[count
] = callback
;
859 m_data
[count
] = cdata
;
864 /* GSocket_UnsetCallback:
865 * Disables all callbacks specified by 'flags', which may be a
866 * combination of flags OR'ed toghether.
868 void GSocket::UnsetCallback(GSocketEventFlags flags
)
872 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
874 if ((flags
& (1 << count
)) != 0)
876 m_cbacks
[count
] = NULL
;
877 m_data
[count
] = NULL
;
882 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
883 void *optval
, int *optlen
)
885 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
887 return GSOCK_NOERROR
;
892 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
893 const void *optval
, int optlen
)
895 if (setsockopt(m_fd
, level
, optname
, (char*)optval
, optlen
) == 0)
897 return GSOCK_NOERROR
;
904 /* _GSocket_Input_Timeout:
905 * For blocking sockets, wait until data is available or
906 * until timeout ellapses.
908 GSocketError
GSocket::Input_Timeout()
915 FD_SET(m_fd
, &readfds
);
916 if (select(0, &readfds
, NULL
, NULL
, &m_timeout
) == 0)
918 m_error
= GSOCK_TIMEDOUT
;
919 return GSOCK_TIMEDOUT
;
922 return GSOCK_NOERROR
;
925 /* _GSocket_Output_Timeout:
926 * For blocking sockets, wait until data can be sent without
927 * blocking or until timeout ellapses.
929 GSocketError
GSocket::Output_Timeout()
936 FD_SET(m_fd
, &writefds
);
937 if (select(0, NULL
, &writefds
, NULL
, &m_timeout
) == 0)
939 m_error
= GSOCK_TIMEDOUT
;
940 return GSOCK_TIMEDOUT
;
943 return GSOCK_NOERROR
;
946 /* _GSocket_Connect_Timeout:
947 * For blocking sockets, wait until the connection is
948 * established or fails, or until timeout ellapses.
950 GSocketError
GSocket::Connect_Timeout()
957 FD_SET(m_fd
, &writefds
);
958 FD_SET(m_fd
, &exceptfds
);
959 if (select(0, NULL
, &writefds
, &exceptfds
, &m_timeout
) == 0)
961 m_error
= GSOCK_TIMEDOUT
;
962 return GSOCK_TIMEDOUT
;
964 if (!FD_ISSET(m_fd
, &writefds
))
966 m_error
= GSOCK_IOERR
;
970 return GSOCK_NOERROR
;
973 int GSocket::Recv_Stream(char *buffer
, int size
)
975 return recv(m_fd
, buffer
, size
, 0);
978 int GSocket::Recv_Dgram(char *buffer
, int size
)
981 WX_SOCKLEN_T fromlen
= sizeof(from
);
985 ret
= recvfrom(m_fd
, buffer
, size
, 0, (sockaddr
*)&from
, &fromlen
);
987 if (ret
== SOCKET_ERROR
)
990 /* Translate a system address into a GSocket address */
993 m_peer
= GAddress_new();
996 m_error
= GSOCK_MEMERR
;
1000 err
= _GAddress_translate_from(m_peer
, (sockaddr
*)&from
, fromlen
);
1001 if (err
!= GSOCK_NOERROR
)
1003 GAddress_destroy(m_peer
);
1012 int GSocket::Send_Stream(const char *buffer
, int size
)
1014 return send(m_fd
, buffer
, size
, 0);
1017 int GSocket::Send_Dgram(const char *buffer
, int size
)
1019 struct sockaddr
*addr
;
1025 m_error
= GSOCK_INVADDR
;
1029 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1030 if (err
!= GSOCK_NOERROR
)
1036 ret
= sendto(m_fd
, buffer
, size
, 0, addr
, len
);
1038 /* Frees memory allocated by _GAddress_translate_to */
1044 /* Compatibility functions for GSocket */
1045 GSocket
*GSocket_new()
1047 GSocket
*newsocket
= new GSocket();
1048 if(newsocket
->IsOk())
1056 * -------------------------------------------------------------------------
1058 * -------------------------------------------------------------------------
1061 /* CHECK_ADDRESS verifies that the current address family is either
1062 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1063 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1064 * an appropiate error code.
1066 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1068 #define CHECK_ADDRESS(address, family) \
1070 if (address->m_family == GSOCK_NOFAMILY) \
1071 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1072 return address->m_error; \
1073 if (address->m_family != GSOCK_##family) \
1075 address->m_error = GSOCK_INVADDR; \
1076 return GSOCK_INVADDR; \
1080 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1082 if (address->m_family == GSOCK_NOFAMILY) \
1083 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1085 if (address->m_family != GSOCK_##family) \
1087 address->m_error = GSOCK_INVADDR; \
1093 GAddress
*GAddress_new()
1097 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1100 address
->m_family
= GSOCK_NOFAMILY
;
1101 address
->m_addr
= NULL
;
1107 GAddress
*GAddress_copy(GAddress
*address
)
1111 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1114 memcpy(addr2
, address
, sizeof(GAddress
));
1116 if (address
->m_addr
)
1118 addr2
->m_addr
= (struct sockaddr
*) malloc(addr2
->m_len
);
1119 if (addr2
->m_addr
== NULL
)
1124 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1130 void GAddress_destroy(GAddress
*address
)
1132 if (address
->m_addr
)
1133 free(address
->m_addr
);
1138 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1140 address
->m_family
= type
;
1143 GAddressType
GAddress_GetFamily(GAddress
*address
)
1145 return address
->m_family
;
1148 GSocketError
_GAddress_translate_from(GAddress
*address
,
1149 struct sockaddr
*addr
, int len
)
1151 address
->m_realfamily
= addr
->sa_family
;
1152 switch (addr
->sa_family
)
1155 address
->m_family
= GSOCK_INET
;
1158 address
->m_family
= GSOCK_UNIX
;
1162 address
->m_family
= GSOCK_INET6
;
1167 address
->m_error
= GSOCK_INVOP
;
1172 if (address
->m_addr
)
1173 free(address
->m_addr
);
1175 address
->m_len
= len
;
1176 address
->m_addr
= (struct sockaddr
*) malloc(len
);
1178 if (address
->m_addr
== NULL
)
1180 address
->m_error
= GSOCK_MEMERR
;
1181 return GSOCK_MEMERR
;
1183 memcpy(address
->m_addr
, addr
, len
);
1185 return GSOCK_NOERROR
;
1188 GSocketError
_GAddress_translate_to(GAddress
*address
,
1189 struct sockaddr
**addr
, int *len
)
1191 if (!address
->m_addr
)
1193 address
->m_error
= GSOCK_INVADDR
;
1194 return GSOCK_INVADDR
;
1197 *len
= address
->m_len
;
1198 *addr
= (struct sockaddr
*) malloc(address
->m_len
);
1201 address
->m_error
= GSOCK_MEMERR
;
1202 return GSOCK_MEMERR
;
1205 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1206 return GSOCK_NOERROR
;
1210 * -------------------------------------------------------------------------
1211 * Internet address family
1212 * -------------------------------------------------------------------------
1215 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1217 address
->m_len
= sizeof(struct sockaddr_in
);
1218 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1219 if (address
->m_addr
== NULL
)
1221 address
->m_error
= GSOCK_MEMERR
;
1222 return GSOCK_MEMERR
;
1225 address
->m_family
= GSOCK_INET
;
1226 address
->m_realfamily
= AF_INET
;
1227 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1228 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1230 return GSOCK_NOERROR
;
1233 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1236 struct in_addr
*addr
;
1238 CHECK_ADDRESS(address
, INET
);
1240 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1242 addr
->s_addr
= inet_addr(hostname
);
1244 /* If it is a numeric host name, convert it now */
1245 if (addr
->s_addr
== INADDR_NONE
)
1247 struct in_addr
*array_addr
;
1249 /* It is a real name, we solve it */
1250 if ((he
= gethostbyname(hostname
)) == NULL
)
1252 /* addr->s_addr = INADDR_NONE just done by inet_addr() above */
1253 address
->m_error
= GSOCK_NOHOST
;
1254 return GSOCK_NOHOST
;
1256 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1257 addr
->s_addr
= array_addr
[0].s_addr
;
1259 return GSOCK_NOERROR
;
1262 GSocketError
GAddress_INET_SetBroadcastAddress(GAddress
*address
)
1264 return GAddress_INET_SetHostAddress(address
, INADDR_BROADCAST
);
1267 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1269 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1272 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1273 unsigned long hostaddr
)
1275 struct in_addr
*addr
;
1277 CHECK_ADDRESS(address
, INET
);
1279 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1280 addr
->s_addr
= htonl(hostaddr
);
1282 return GSOCK_NOERROR
;
1285 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1286 const char *protocol
)
1289 struct sockaddr_in
*addr
;
1291 CHECK_ADDRESS(address
, INET
);
1295 address
->m_error
= GSOCK_INVPORT
;
1296 return GSOCK_INVPORT
;
1299 se
= getservbyname(port
, protocol
);
1302 if (isdigit(port
[0]))
1306 port_int
= atoi(port
);
1307 addr
= (struct sockaddr_in
*)address
->m_addr
;
1308 addr
->sin_port
= htons((u_short
) port_int
);
1309 return GSOCK_NOERROR
;
1312 address
->m_error
= GSOCK_INVPORT
;
1313 return GSOCK_INVPORT
;
1316 addr
= (struct sockaddr_in
*)address
->m_addr
;
1317 addr
->sin_port
= se
->s_port
;
1319 return GSOCK_NOERROR
;
1322 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1324 struct sockaddr_in
*addr
;
1326 CHECK_ADDRESS(address
, INET
);
1328 addr
= (struct sockaddr_in
*)address
->m_addr
;
1329 addr
->sin_port
= htons(port
);
1331 return GSOCK_NOERROR
;
1334 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1338 struct sockaddr_in
*addr
;
1340 CHECK_ADDRESS(address
, INET
);
1342 addr
= (struct sockaddr_in
*)address
->m_addr
;
1343 addr_buf
= (char *)&(addr
->sin_addr
);
1345 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1348 address
->m_error
= GSOCK_NOHOST
;
1349 return GSOCK_NOHOST
;
1352 strncpy(hostname
, he
->h_name
, sbuf
);
1354 return GSOCK_NOERROR
;
1357 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1359 struct sockaddr_in
*addr
;
1361 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1363 addr
= (struct sockaddr_in
*)address
->m_addr
;
1365 return ntohl(addr
->sin_addr
.s_addr
);
1368 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1370 struct sockaddr_in
*addr
;
1372 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1374 addr
= (struct sockaddr_in
*)address
->m_addr
;
1375 return ntohs(addr
->sin_port
);
1381 * -------------------------------------------------------------------------
1382 * Internet IPv6 address family
1383 * -------------------------------------------------------------------------
1385 #include "ws2tcpip.h"
1388 #pragma comment(lib,"ws2_32")
1389 #endif // __VISUALC__
1391 GSocketError
_GAddress_Init_INET6(GAddress
*address
)
1393 struct in6_addr any_address
= IN6ADDR_ANY_INIT
;
1394 address
->m_len
= sizeof(struct sockaddr_in6
);
1395 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1396 if (address
->m_addr
== NULL
)
1398 address
->m_error
= GSOCK_MEMERR
;
1399 return GSOCK_MEMERR
;
1401 memset(address
->m_addr
,0,address
->m_len
);
1403 address
->m_family
= GSOCK_INET6
;
1404 address
->m_realfamily
= AF_INET6
;
1405 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_family
= AF_INET6
;
1406 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= any_address
;
1408 return GSOCK_NOERROR
;
1411 GSocketError
GAddress_INET6_SetHostName(GAddress
*address
, const char *hostname
)
1413 CHECK_ADDRESS(address
, INET6
);
1416 memset( & hints
, 0, sizeof( hints
) );
1417 hints
.ai_family
= AF_INET6
;
1418 addrinfo
* info
= 0;
1419 if ( getaddrinfo( hostname
, "0", & hints
, & info
) || ! info
)
1421 address
->m_error
= GSOCK_NOHOST
;
1422 return GSOCK_NOHOST
;
1425 memcpy( address
->m_addr
, info
->ai_addr
, info
->ai_addrlen
);
1426 freeaddrinfo( info
);
1427 return GSOCK_NOERROR
;
1430 GSocketError
GAddress_INET6_SetAnyAddress(GAddress
*address
)
1432 CHECK_ADDRESS(address
, INET6
);
1434 struct in6_addr addr
;
1435 memset( & addr
, 0, sizeof( addr
) );
1436 return GAddress_INET6_SetHostAddress(address
, addr
);
1438 GSocketError
GAddress_INET6_SetHostAddress(GAddress
*address
,
1439 struct in6_addr hostaddr
)
1441 CHECK_ADDRESS(address
, INET6
);
1443 ((struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
= hostaddr
;
1445 return GSOCK_NOERROR
;
1448 GSocketError
GAddress_INET6_SetPortName(GAddress
*address
, const char *port
,
1449 const char *protocol
)
1452 struct sockaddr_in6
*addr
;
1454 CHECK_ADDRESS(address
, INET6
);
1458 address
->m_error
= GSOCK_INVPORT
;
1459 return GSOCK_INVPORT
;
1462 se
= getservbyname(port
, protocol
);
1465 if (isdigit(port
[0]))
1469 port_int
= atoi(port
);
1470 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1471 addr
->sin6_port
= htons((u_short
) port_int
);
1472 return GSOCK_NOERROR
;
1475 address
->m_error
= GSOCK_INVPORT
;
1476 return GSOCK_INVPORT
;
1479 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1480 addr
->sin6_port
= se
->s_port
;
1482 return GSOCK_NOERROR
;
1485 GSocketError
GAddress_INET6_SetPort(GAddress
*address
, unsigned short port
)
1487 struct sockaddr_in6
*addr
;
1489 CHECK_ADDRESS(address
, INET6
);
1491 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1492 addr
->sin6_port
= htons(port
);
1494 return GSOCK_NOERROR
;
1497 GSocketError
GAddress_INET6_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1501 struct sockaddr_in6
*addr
;
1503 CHECK_ADDRESS(address
, INET6
);
1505 addr
= (struct sockaddr_in6
*)address
->m_addr
;
1506 addr_buf
= (char *)&(addr
->sin6_addr
);
1508 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin6_addr
), AF_INET6
);
1511 address
->m_error
= GSOCK_NOHOST
;
1512 return GSOCK_NOHOST
;
1515 strncpy(hostname
, he
->h_name
, sbuf
);
1517 return GSOCK_NOERROR
;
1520 GSocketError
GAddress_INET6_GetHostAddress(GAddress
*address
,struct in6_addr
*hostaddr
)
1522 CHECK_ADDRESS_RETVAL(address
, INET6
, GSOCK_INVADDR
);
1523 *hostaddr
= ( (struct sockaddr_in6
*)address
->m_addr
)->sin6_addr
;
1524 return GSOCK_NOERROR
;
1527 unsigned short GAddress_INET6_GetPort(GAddress
*address
)
1529 CHECK_ADDRESS_RETVAL(address
, INET6
, 0);
1531 return ntohs( ((struct sockaddr_in6
*)address
->m_addr
)->sin6_port
);
1534 #endif // wxUSE_IPV6
1537 * -------------------------------------------------------------------------
1538 * Unix address family
1539 * -------------------------------------------------------------------------
1542 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1544 address
->m_error
= GSOCK_INVADDR
;
1545 return GSOCK_INVADDR
;
1548 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *WXUNUSED(path
))
1550 address
->m_error
= GSOCK_INVADDR
;
1551 return GSOCK_INVADDR
;
1554 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *WXUNUSED(path
), size_t WXUNUSED(sbuf
))
1556 address
->m_error
= GSOCK_INVADDR
;
1557 return GSOCK_INVADDR
;
1560 #endif // wxUSE_SOCKETS