]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main Unix file
6 * -------------------------------------------------------------------------
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
33 # include <sys/filio.h>
42 #include "wx/gsocket.h"
49 # define SOCKLEN_T socklen_t
52 # define SOCKLEN_T int
57 /* Global initialisers */
64 void GSocket_Cleanup()
68 /* Constructors / Destructors */
70 GSocket
*GSocket_new()
75 socket
= (GSocket
*)malloc(sizeof(GSocket
));
78 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
79 socket
->m_fbacks
[i
] = NULL
;
80 socket
->m_iocalls
[i
] = FALSE
;
82 socket
->m_local
= NULL
;
83 socket
->m_peer
= NULL
;
84 socket
->m_error
= GSOCK_NOERROR
;
85 socket
->m_server
= FALSE
;
86 socket
->m_stream
= TRUE
;
87 socket
->m_gui_dependent
= NULL
;
88 socket
->m_blocking
= FALSE
;
90 /* We initialize the GUI specific entries here */
91 _GSocket_GUI_Init(socket
);
96 void GSocket_destroy(GSocket
*socket
)
98 assert(socket
!= NULL
);
100 /* First, we check that the socket is really shutdowned */
101 if (socket
->m_fd
!= -1)
102 GSocket_Shutdown(socket
);
104 /* We destroy GUI specific variables */
105 _GSocket_GUI_Destroy(socket
);
107 /* We destroy private addresses */
109 GAddress_destroy(socket
->m_local
);
112 GAddress_destroy(socket
->m_peer
);
114 /* We destroy socket itself */
118 void GSocket_Shutdown(GSocket
*socket
)
122 assert(socket
!= NULL
);
124 /* If socket has been created, we shutdown it */
125 if (socket
->m_fd
!= -1) {
126 shutdown(socket
->m_fd
, 2);
131 /* We also disable GUI callbacks */
132 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
133 _GSocket_Uninstall_Fallback(socket
, evt
);
136 /* Address handling */
138 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
140 assert(socket
!= NULL
);
142 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
143 socket
->m_error
= GSOCK_INVSOCK
;
144 return GSOCK_INVSOCK
;
147 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
148 socket
->m_error
= GSOCK_INVADDR
;
149 return GSOCK_INVADDR
;
153 GAddress_destroy(socket
->m_local
);
155 socket
->m_local
= GAddress_copy(address
);
157 return GSOCK_NOERROR
;
160 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
162 assert(socket
!= NULL
);
164 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
165 socket
->m_error
= GSOCK_INVADDR
;
166 return GSOCK_INVADDR
;
170 GAddress_destroy(socket
->m_peer
);
172 socket
->m_peer
= GAddress_copy(address
);
174 return GSOCK_NOERROR
;
177 GAddress
*GSocket_GetLocal(GSocket
*socket
)
180 struct sockaddr addr
;
183 assert(socket
!= NULL
);
186 return GAddress_copy(socket
->m_local
);
188 if (socket
->m_fd
== -1) {
189 socket
->m_error
= GSOCK_INVSOCK
;
195 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
196 socket
->m_error
= GSOCK_IOERR
;
200 address
= GAddress_new();
201 _GAddress_translate_from(address
, &addr
, size
);
206 GAddress
*GSocket_GetPeer(GSocket
*socket
)
208 assert(socket
!= NULL
);
211 return GAddress_copy(socket
->m_peer
);
216 /* Server specific parts */
219 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
220 of GSocket. "Local" must be set by GSocket_SetLocal() before
221 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
222 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
223 In the other cases it returns GSOCK_IOERR.
225 GSocketError
GSocket_SetServer(GSocket
*sck
)
231 if (sck
->m_fd
!= -1) {
232 sck
->m_error
= GSOCK_INVSOCK
;
233 return GSOCK_INVSOCK
;
237 sck
->m_error
= GSOCK_INVADDR
;
238 return GSOCK_INVADDR
;
241 /* We always have a stream here */
242 sck
->m_stream
= TRUE
;
244 /* Create the socket */
245 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
247 if (sck
->m_fd
== -1) {
248 sck
->m_error
= GSOCK_IOERR
;
252 /* Bind the socket to the LOCAL address */
253 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
256 sck
->m_error
= GSOCK_IOERR
;
260 /* Enable listening up to 5 connections */
261 if (listen(sck
->m_fd
, 5) < 0) {
264 sck
->m_error
= GSOCK_IOERR
;
268 return GSOCK_NOERROR
;
272 GSocket_WaitConnection() waits for an incoming client connection.
274 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
278 assert(socket
!= NULL
);
280 /* If the socket has already been created, we exit immediately */
281 if (socket
->m_fd
== -1 || !socket
->m_server
) {
282 socket
->m_error
= GSOCK_INVSOCK
;
286 /* Reenable GSOCK_CONNECTION event */
287 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
289 /* Create a GSocket object for the new connection */
290 connection
= GSocket_new();
292 /* Accept the incoming connection */
293 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
294 if (connection
->m_fd
== -1) {
295 GSocket_destroy(connection
);
296 socket
->m_error
= GSOCK_IOERR
;
300 /* Initialize all fields */
301 connection
->m_stream
= TRUE
;
302 connection
->m_server
= FALSE
;
303 connection
->m_oriented
= TRUE
;
308 /* Non oriented connections */
310 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
314 if (sck
->m_fd
!= -1) {
315 sck
->m_error
= GSOCK_INVSOCK
;
316 return GSOCK_INVSOCK
;
320 sck
->m_error
= GSOCK_INVADDR
;
321 return GSOCK_INVADDR
;
324 sck
->m_stream
= FALSE
;
325 sck
->m_server
= FALSE
;
326 sck
->m_oriented
= FALSE
;
328 /* Create the socket */
329 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
331 /* Bind it to the LOCAL address */
332 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
335 sck
->m_error
= GSOCK_IOERR
;
339 return GSOCK_NOERROR
;
342 /* Client specific parts */
345 GSocket_Connect() establishes a client connection to a server using the "Peer"
346 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
347 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
349 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
355 if (sck
->m_fd
!= -1) {
356 sck
->m_error
= GSOCK_INVSOCK
;
357 return GSOCK_INVSOCK
;
361 sck
->m_error
= GSOCK_INVADDR
;
362 return GSOCK_INVADDR
;
365 /* Test whether we want the socket to be a stream (e.g. TCP) */
366 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
367 sck
->m_oriented
= TRUE
;
374 /* Create the socket */
375 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
377 if (sck
->m_fd
== -1) {
378 sck
->m_error
= GSOCK_IOERR
;
382 /* Connect it to the PEER address */
383 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
384 sck
->m_peer
->m_len
) != 0) {
387 sck
->m_error
= GSOCK_IOERR
;
391 /* It is not a server */
392 sck
->m_server
= FALSE
;
394 return GSOCK_NOERROR
;
399 /* Like recv(), send(), ... */
400 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
402 assert(socket
!= NULL
);
404 if (socket
->m_fd
== -1 || socket
->m_server
) {
405 socket
->m_error
= GSOCK_INVSOCK
;
409 /* Reenable GSOCK_INPUT event */
410 _GSocket_Enable(socket
, GSOCK_INPUT
);
412 if (socket
->m_stream
)
413 return _GSocket_Recv_Stream(socket
, buffer
, size
);
415 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
418 int GSocket_Write(GSocket
*socket
, const char *buffer
,
421 assert(socket
!= NULL
);
423 if (socket
->m_fd
== -1 || socket
->m_server
) {
424 socket
->m_error
= GSOCK_INVSOCK
;
428 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
430 if (socket
->m_stream
)
431 return _GSocket_Send_Stream(socket
, buffer
, size
);
433 return _GSocket_Send_Dgram(socket
, buffer
, size
);
436 bool GSocket_DataAvailable(GSocket
*socket
)
441 assert(socket
!= NULL
);
443 if (socket
->m_fd
== -1 || socket
->m_server
) {
444 socket
->m_error
= GSOCK_INVSOCK
;
449 FD_SET(socket
->m_fd
, &read_set
);
454 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
456 return FD_ISSET(socket
->m_fd
, &read_set
);
462 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
463 if we don't want to wait.
465 void GSocket_SetBlocking(GSocket
*socket
, bool block
)
467 assert(socket
!= NULL
);
469 socket
->m_blocking
= block
;
471 if (socket
->m_fd
!= -1)
472 ioctl(socket
->m_fd
, FIONBIO
, &block
);
476 GSocket_GetError() returns the last error occured on the socket stream.
479 GSocketError
GSocket_GetError(GSocket
*socket
)
481 assert(socket
!= NULL
);
483 return socket
->m_error
;
489 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
490 INPUT: The function is called when there is at least a byte in the
492 OUTPUT: The function is called when the system is sure the next write call
494 CONNECTION: Two cases is possible:
495 Client socket -> the connection is established
496 Server socket -> a client request a connection
497 LOST: the connection is lost
499 SetFallback accepts a combination of these flags so a same callback can
500 receive different events.
502 An event is generated only once and its state is reseted when the relative
503 IO call is requested.
504 For example: INPUT -> GSocket_Read()
505 CONNECTION -> GSocket_Accept()
507 void GSocket_SetFallback(GSocket
*socket
, GSocketEventFlags event
,
508 GSocketFallback fallback
, char *cdata
)
512 assert (socket
!= NULL
);
514 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
515 /* We test each flag and, if it is enabled, we enable the corresponding
517 if ((event
& (1 << count
)) != 0) {
518 socket
->m_fbacks
[count
] = fallback
;
519 socket
->m_data
[count
] = cdata
;
521 _GSocket_Install_Fallback(socket
, count
);
522 _GSocket_Enable(socket
, count
);
528 UnsetFallback will disables all fallbacks specified by "event".
529 NOTE: event may be a combination of flags
531 void GSocket_UnsetFallback(GSocket
*socket
, GSocketEventFlags event
)
535 assert(socket
!= NULL
);
537 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
538 if ((event
& (1 << count
)) != 0) {
539 _GSocket_Disable(socket
, count
);
540 socket
->m_fbacks
[count
] = NULL
;
541 _GSocket_Uninstall_Fallback(socket
, count
);
546 #define CALL_FALLBACK(socket, event) \
547 if (socket->m_iocalls[event] && \
548 socket->m_fbacks[event]) {\
549 _GSocket_Disable(socket, event); \
550 socket->m_fbacks[event](socket, event, \
551 socket->m_data[event]); \
554 #define MASK_SIGNAL() \
556 void (*old_handler)(int); \
558 old_handler = signal(SIGPIPE, SIG_IGN);
560 #define UNMASK_SIGNAL() \
561 signal(SIGPIPE, old_handler); \
564 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
566 socket
->m_iocalls
[event
] = TRUE
;
567 if (socket
->m_fbacks
[event
])
568 _GSocket_Install_Fallback(socket
, event
);
571 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
573 socket
->m_iocalls
[event
] = FALSE
;
574 if (socket
->m_fbacks
[event
])
575 _GSocket_Uninstall_Fallback(socket
, event
);
578 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
583 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
586 socket
->m_error
= GSOCK_IOERR
;
592 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
594 struct sockaddr from
;
598 fromlen
= sizeof(from
);
601 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
604 socket
->m_error
= GSOCK_IOERR
;
608 /* Translate a system address into a GSocket address */
610 socket
->m_peer
= GAddress_new();
611 _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
616 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
621 ret
= send(socket
->m_fd
, buffer
, size
, 0);
624 socket
->m_error
= GSOCK_IOERR
;
630 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
632 struct sockaddr
*addr
;
635 if (!socket
->m_peer
) {
636 socket
->m_error
= GSOCK_INVADDR
;
640 _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
643 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
646 socket
->m_error
= GSOCK_IOERR
;
650 /* Frees memory allocated from _GAddress_translate_to */
656 void _GSocket_Detected_Read(GSocket
*socket
)
661 if (socket
->m_stream
) {
662 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
664 if (ret
< 0 && socket
->m_server
) {
665 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
670 CALL_FALLBACK(socket
, GSOCK_INPUT
);
672 CALL_FALLBACK(socket
, GSOCK_LOST
);
677 void _GSocket_Detected_Write(GSocket
*socket
)
679 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
687 * -------------------------------------------------------------------------
689 * -------------------------------------------------------------------------
692 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
693 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
694 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
696 #define CHECK_ADDRESS(address, family, retval) \
698 if (address->m_family == GSOCK_NOFAMILY) \
699 _GAddress_Init_##family(address); \
700 if (address->m_family != GSOCK_##family) {\
701 address->m_error = GSOCK_INVADDR; \
706 GAddress
*GAddress_new()
710 address
= (GAddress
*)malloc(sizeof(GAddress
));
712 address
->m_family
= GSOCK_NOFAMILY
;
713 address
->m_addr
= NULL
;
719 GAddress
*GAddress_copy(GAddress
*address
)
723 assert(address
!= NULL
);
725 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
726 memcpy(addr2
, address
, sizeof(GAddress
));
728 if (address
->m_addr
) {
729 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
730 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
736 void GAddress_destroy(GAddress
*address
)
738 assert(address
!= NULL
);
743 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
745 assert(address
!= NULL
);
747 address
->m_family
= type
;
750 GAddressType
GAddress_GetFamily(GAddress
*address
)
752 assert(address
!= NULL
);
754 return address
->m_family
;
757 void _GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
758 address
->m_realfamily
= addr
->sa_family
;
759 switch (addr
->sa_family
) {
761 address
->m_family
= GSOCK_INET
;
764 address
->m_family
= GSOCK_UNIX
;
768 address
->m_family
= GSOCK_INET6
;
778 free(address
->m_addr
);
780 address
->m_len
= len
;
781 address
->m_addr
= (struct sockaddr
*)malloc(len
);
782 memcpy(address
->m_addr
, addr
, len
);
785 void _GAddress_translate_to(GAddress
*address
,
786 struct sockaddr
**addr
, int *len
)
788 if (!address
->m_addr
) {
793 *len
= address
->m_len
;
794 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
795 memcpy(*addr
, address
->m_addr
, address
->m_len
);
799 * -------------------------------------------------------------------------
800 * Internet address family
801 * -------------------------------------------------------------------------
804 void _GAddress_Init_INET(GAddress
*address
)
806 address
->m_len
= sizeof(struct sockaddr_in
);
807 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
808 address
->m_family
= GSOCK_INET
;
809 address
->m_realfamily
= PF_INET
;
810 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
811 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
814 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
817 struct in_addr
*addr
;
819 assert(address
!= NULL
);
821 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
823 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
825 /* If it is a numeric host name, convert it now */
826 if (inet_aton(hostname
, addr
) == 0) {
827 struct in_addr
*array_addr
;
829 /* It is a real name, we solve it */
830 if ((he
= gethostbyname(hostname
)) == NULL
) {
831 address
->m_error
= GSOCK_NOHOST
;
834 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
835 addr
->s_addr
= array_addr
[0].s_addr
;
837 return GSOCK_NOERROR
;
840 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
841 unsigned long hostaddr
)
843 struct in_addr
*addr
;
845 assert(address
!= NULL
);
847 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
849 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
850 addr
->s_addr
= hostaddr
;
852 return GSOCK_NOERROR
;
855 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
856 const char *protocol
)
859 struct sockaddr_in
*addr
;
861 assert(address
!= NULL
);
862 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
865 address
->m_error
= GSOCK_INVPORT
;
869 se
= getservbyname(port
, protocol
);
871 if (isdigit(port
[0])) {
874 port_int
= atoi(port
);
875 addr
= (struct sockaddr_in
*)address
->m_addr
;
876 addr
->sin_port
= htons(port_int
);
877 return GSOCK_NOERROR
;
880 address
->m_error
= GSOCK_INVPORT
;
881 return GSOCK_INVPORT
;
884 addr
= (struct sockaddr_in
*)address
->m_addr
;
885 addr
->sin_port
= se
->s_port
;
887 return GSOCK_NOERROR
;
890 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
892 struct sockaddr_in
*addr
;
894 assert(address
!= NULL
);
895 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
897 addr
= (struct sockaddr_in
*)address
->m_addr
;
898 addr
->sin_port
= htons(port
);
900 return GSOCK_NOERROR
;
903 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
907 struct sockaddr_in
*addr
;
909 assert(address
!= NULL
);
910 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
912 addr
= (struct sockaddr_in
*)address
->m_addr
;
913 addr_buf
= (char *)&(addr
->sin_addr
);
915 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
917 address
->m_error
= GSOCK_NOHOST
;
921 strncpy(hostname
, he
->h_name
, sbuf
);
923 return GSOCK_NOERROR
;
926 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
928 struct sockaddr_in
*addr
;
930 assert(address
!= NULL
);
931 CHECK_ADDRESS(address
, INET
, 0);
933 addr
= (struct sockaddr_in
*)address
->m_addr
;
935 return addr
->sin_addr
.s_addr
;
938 unsigned short GAddress_INET_GetPort(GAddress
*address
)
940 struct sockaddr_in
*addr
;
942 assert(address
!= NULL
);
943 CHECK_ADDRESS(address
, INET
, 0);
945 addr
= (struct sockaddr_in
*)address
->m_addr
;
946 return ntohs(addr
->sin_port
);
950 * -------------------------------------------------------------------------
951 * Unix address family
952 * -------------------------------------------------------------------------
955 void _GAddress_Init_UNIX(GAddress
*address
)
957 address
->m_len
= sizeof(struct sockaddr_un
);
958 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
959 address
->m_family
= GSOCK_UNIX
;
960 address
->m_realfamily
= PF_UNIX
;
961 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
962 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
965 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
967 struct sockaddr_un
*addr
;
969 assert(address
!= NULL
);
971 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
973 addr
= ((struct sockaddr_un
*)address
->m_addr
);
974 memcpy(addr
->sun_path
, path
, strlen(path
));
976 return GSOCK_NOERROR
;
979 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
981 struct sockaddr_un
*addr
;
983 assert(address
!= NULL
);
984 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
986 addr
= (struct sockaddr_un
*)address
->m_addr
;
988 strncpy(path
, addr
->sun_path
, sbuf
);
990 return GSOCK_NOERROR
;
993 #endif // wxUSE_SOCKETS