]>
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 * -------------------------------------------------------------------------
10 #include <sys/ioctl.h>
11 #include <sys/types.h>
15 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
29 #include <sys/filio.h>
37 //#include <features.h>
40 #include <wx/gsocket.h>
47 # define SOCKLEN_T socklen_t
50 # define SOCKLEN_T int
55 /* Global initialisers */
61 void GSocket_Cleanup()
65 /* Constructors / Destructors */
67 GSocket
*GSocket_new()
72 socket
= (GSocket
*)malloc(sizeof(GSocket
));
75 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
76 socket
->m_fbacks
[i
] = NULL
;
77 socket
->m_iocalls
[i
] = FALSE
;
79 socket
->m_local
= NULL
;
80 socket
->m_peer
= NULL
;
81 socket
->m_error
= GSOCK_NOERROR
;
82 socket
->m_server
= FALSE
;
83 socket
->m_stream
= TRUE
;
84 socket
->m_gui_dependent
= NULL
;
85 socket
->m_blocking
= FALSE
;
87 _GSocket_GUI_Init(socket
);
92 void GSocket_destroy(GSocket
*socket
)
94 assert(socket
!= NULL
);
96 if (socket
->m_fd
!= -1)
97 GSocket_Shutdown(socket
);
99 _GSocket_GUI_Destroy(socket
);
102 GAddress_destroy(socket
->m_local
);
105 GAddress_destroy(socket
->m_peer
);
110 void GSocket_Shutdown(GSocket
*socket
)
114 assert(socket
!= NULL
);
116 if (socket
->m_fd
!= -1) {
117 shutdown(socket
->m_fd
, 2);
122 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
123 _GSocket_Uninstall_Fallback(socket
, evt
);
126 /* Address handling */
128 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
130 assert(socket
!= NULL
);
132 if ((socket
->m_fd
!= -1 && !socket
->m_server
))
133 return GSOCK_INVSOCK
;
135 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
136 return GSOCK_INVADDR
;
139 GAddress_destroy(socket
->m_local
);
141 socket
->m_local
= GAddress_copy(address
);
143 return GSOCK_NOERROR
;
146 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
148 assert(socket
!= NULL
);
150 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
151 socket
->m_error
= GSOCK_INVADDR
;
152 return GSOCK_INVADDR
;
156 GAddress_destroy(socket
->m_peer
);
158 socket
->m_peer
= GAddress_copy(address
);
160 return GSOCK_NOERROR
;
163 GAddress
*GSocket_GetLocal(GSocket
*socket
)
166 struct sockaddr addr
;
169 assert(socket
!= NULL
);
172 return GAddress_copy(socket
->m_local
);
174 if (socket
->m_fd
== -1) {
175 socket
->m_error
= GSOCK_INVSOCK
;
181 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
182 socket
->m_error
= GSOCK_IOERR
;
186 address
= GAddress_new();
187 _GAddress_translate_from(address
, &addr
, size
);
192 GAddress
*GSocket_GetPeer(GSocket
*socket
)
194 assert(socket
!= NULL
);
197 return GAddress_copy(socket
->m_peer
);
202 /* Server specific parts */
205 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
206 of GSocket. "Local" must be set by GSocket_SetLocal() before
207 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
208 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
209 In the other cases it returns GSOCK_IOERR.
211 GSocketError
GSocket_SetServer(GSocket
*sck
)
217 if (sck
->m_fd
!= -1) {
218 sck
->m_error
= GSOCK_INVSOCK
;
219 return GSOCK_INVSOCK
;
223 sck
->m_error
= GSOCK_INVADDR
;
224 return GSOCK_INVADDR
;
232 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, type
, 0);
234 if (sck
->m_fd
== -1) {
235 sck
->m_error
= GSOCK_IOERR
;
239 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
242 sck
->m_error
= GSOCK_IOERR
;
246 if (listen(sck
->m_fd
, 5) < 0) {
249 sck
->m_error
= GSOCK_IOERR
;
253 sck
->m_server
= TRUE
;
255 return GSOCK_NOERROR
;
260 GSocket_WaitConnection() waits for an incoming client connection.
262 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
266 assert(socket
!= NULL
);
268 if (socket
->m_fd
== -1 || !socket
->m_server
) {
269 socket
->m_error
= GSOCK_INVSOCK
;
273 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
275 connection
= GSocket_new();
277 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
278 if (connection
->m_fd
== -1) {
279 GSocket_destroy(connection
);
280 socket
->m_error
= GSOCK_IOERR
;
284 connection
->m_stream
= TRUE
;
285 connection
->m_server
= FALSE
;
286 connection
->m_oriented
= TRUE
;
291 /* Non oriented connections */
293 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
297 if (sck
->m_fd
!= -1) {
298 sck
->m_error
= GSOCK_INVSOCK
;
299 return GSOCK_INVSOCK
;
303 sck
->m_error
= GSOCK_INVADDR
;
304 return GSOCK_INVADDR
;
307 sck
->m_stream
= FALSE
;
308 sck
->m_server
= FALSE
;
309 sck
->m_oriented
= FALSE
;
311 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
313 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
316 sck
->m_error
= GSOCK_IOERR
;
320 return GSOCK_NOERROR
;
323 /* Client specific parts */
326 GSocket_Connect() establishes a client connection to a server using the "Peer"
327 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
328 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
330 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
336 if (sck
->m_fd
!= -1) {
337 sck
->m_error
= GSOCK_INVSOCK
;
338 return GSOCK_INVSOCK
;
342 sck
->m_error
= GSOCK_INVADDR
;
343 return GSOCK_INVADDR
;
346 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
347 sck
->m_oriented
= TRUE
;
354 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
356 if (sck
->m_fd
== -1) {
357 sck
->m_error
= GSOCK_IOERR
;
361 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
362 sck
->m_peer
->m_len
) != 0) {
365 sck
->m_error
= GSOCK_IOERR
;
369 sck
->m_server
= FALSE
;
371 return GSOCK_NOERROR
;
376 /* Like recv(), send(), ... */
377 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
379 assert(socket
!= NULL
);
381 if (socket
->m_fd
== -1 || socket
->m_server
) {
382 socket
->m_error
= GSOCK_INVSOCK
;
386 _GSocket_Enable(socket
, GSOCK_INPUT
);
388 if (socket
->m_oriented
)
389 return _GSocket_Recv_Stream(socket
, buffer
, size
);
391 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
394 int GSocket_Write(GSocket
*socket
, const char *buffer
,
397 assert(socket
!= NULL
);
399 if (socket
->m_fd
== -1 || socket
->m_server
) {
400 socket
->m_error
= GSOCK_INVSOCK
;
404 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
406 if (socket
->m_oriented
)
407 return _GSocket_Send_Stream(socket
, buffer
, size
);
409 return _GSocket_Send_Dgram(socket
, buffer
, size
);
412 bool GSocket_DataAvailable(GSocket
*socket
)
417 assert(socket
!= NULL
);
419 if (socket
->m_fd
== -1 || socket
->m_server
) {
420 socket
->m_error
= GSOCK_INVSOCK
;
425 FD_SET(socket
->m_fd
, &read_set
);
430 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
432 return FD_ISSET(socket
->m_fd
, &read_set
);
438 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
439 if we don't want to wait.
441 void GSocket_SetBlocking(GSocket
*socket
, bool block
)
443 assert(socket
!= NULL
);
445 socket
->m_blocking
= block
;
447 if (socket
->m_fd
!= -1)
448 ioctl(socket
->m_fd
, FIONBIO
, &block
);
452 GSocket_GetError() returns the last error occured on the socket stream.
455 GSocketError
GSocket_GetError(GSocket
*socket
)
457 assert(socket
!= NULL
);
459 return socket
->m_error
;
465 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
466 INPUT: The function is called when there is at least a byte in the
468 OUTPUT: The function is called when the system is sure the next write call
470 CONNECTION: Two cases is possible:
471 Client socket -> the connection is established
472 Server socket -> a client request a connection
473 LOST: the connection is lost
475 SetFallback accepts a combination of these flags so a same callback can
476 receive different events.
478 An event is generated only once and its state is reseted when the relative
479 IO call is requested.
480 For example: INPUT -> GSocket_Read()
481 CONNECTION -> GSocket_Accept()
483 void GSocket_SetFallback(GSocket
*socket
, GSocketEventFlags event
,
484 GSocketFallback fallback
, char *cdata
)
488 assert (socket
!= NULL
);
490 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
491 if ((event
& (1 << count
)) != 0) {
492 socket
->m_fbacks
[count
] = fallback
;
493 socket
->m_data
[count
] = cdata
;
495 _GSocket_Install_Fallback(socket
, count
);
496 _GSocket_Enable(socket
, count
);
502 UnsetFallback will disables all fallbacks specified by "event".
503 NOTE: event may be a combination of flags
505 void GSocket_UnsetFallback(GSocket
*socket
, GSocketEventFlags event
)
509 assert(socket
!= NULL
);
511 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
512 if ((event
& (1 << count
)) != 0) {
513 _GSocket_Disable(socket
, count
);
514 socket
->m_fbacks
[count
] = NULL
;
515 _GSocket_Uninstall_Fallback(socket
, count
);
520 #define CALL_FALLBACK(socket, event) \
521 if (socket->m_iocalls[event] && \
522 socket->m_fbacks[event]) {\
523 _GSocket_Disable(socket, event); \
524 socket->m_fbacks[event](socket, event, \
525 socket->m_data[event]); \
528 #define MASK_SIGNAL() \
530 void (*old_handler)(int); \
532 old_handler = signal(SIGPIPE, SIG_IGN);
534 #define UNMASK_SIGNAL() \
535 signal(SIGPIPE, old_handler); \
538 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
540 socket
->m_iocalls
[event
] = TRUE
;
541 if (socket
->m_fbacks
[event
])
542 _GSocket_Install_Fallback(socket
, event
);
545 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
547 socket
->m_iocalls
[event
] = FALSE
;
548 if (socket
->m_fbacks
[event
])
549 _GSocket_Uninstall_Fallback(socket
, event
);
552 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
557 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
560 socket
->m_error
= GSOCK_IOERR
;
566 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
568 struct sockaddr from
;
571 fromlen
= sizeof(from
);
574 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
577 socket
->m_error
= GSOCK_IOERR
;
582 socket
->m_peer
= GAddress_new();
583 _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
588 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
593 ret
= send(socket
->m_fd
, buffer
, size
, 0);
596 socket
->m_error
= GSOCK_IOERR
;
602 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
604 struct sockaddr
*addr
;
607 if (!socket
->m_peer
) {
608 socket
->m_error
= GSOCK_INVADDR
;
612 _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
615 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
618 socket
->m_error
= GSOCK_IOERR
;
627 void _GSocket_Detected_Read(GSocket
*socket
)
632 if (socket
->m_stream
) {
633 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
635 if (ret
< 0 && socket
->m_server
) {
636 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
641 CALL_FALLBACK(socket
, GSOCK_INPUT
);
643 CALL_FALLBACK(socket
, GSOCK_LOST
);
648 void _GSocket_Detected_Write(GSocket
*socket
)
650 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
658 * -------------------------------------------------------------------------
660 * -------------------------------------------------------------------------
663 #define CHECK_ADDRESS(address, family, retval) \
665 if (address->m_family == GSOCK_NOFAMILY) \
666 _GAddress_Init_##family(address); \
667 if (address->m_family != GSOCK_##family) {\
668 address->m_error = GSOCK_INVADDR; \
673 GAddress
*GAddress_new()
677 address
= (GAddress
*)malloc(sizeof(GAddress
));
679 address
->m_family
= GSOCK_NOFAMILY
;
680 address
->m_addr
= NULL
;
686 GAddress
*GAddress_copy(GAddress
*address
)
690 assert(address
!= NULL
);
692 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
693 memcpy(addr2
, address
, sizeof(GAddress
));
695 if (address
->m_addr
) {
696 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
697 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
703 void GAddress_destroy(GAddress
*address
)
705 assert(address
!= NULL
);
710 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
712 assert(address
!= NULL
);
714 address
->m_family
= type
;
717 GAddressType
GAddress_GetFamily(GAddress
*address
)
719 assert(address
!= NULL
);
721 return address
->m_family
;
724 void _GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
725 address
->m_realfamily
= addr
->sa_family
;
726 switch (addr
->sa_family
) {
728 address
->m_family
= GSOCK_INET
;
731 address
->m_family
= GSOCK_UNIX
;
735 address
->m_family
= GSOCK_INET6
;
744 free(address
->m_addr
);
746 address
->m_len
= len
;
747 address
->m_addr
= (struct sockaddr
*)malloc(len
);
748 memcpy(address
->m_addr
, addr
, len
);
751 void _GAddress_translate_to(GAddress
*address
,
752 struct sockaddr
**addr
, int *len
)
754 if (!address
->m_addr
) {
759 *len
= address
->m_len
;
760 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
761 memcpy(*addr
, address
->m_addr
, address
->m_len
);
765 * -------------------------------------------------------------------------
766 * Internet address family
767 * -------------------------------------------------------------------------
770 void _GAddress_Init_INET(GAddress
*address
)
772 address
->m_len
= sizeof(struct sockaddr_in
);
773 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
774 address
->m_family
= GSOCK_INET
;
775 address
->m_realfamily
= PF_INET
;
776 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
777 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
780 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
783 struct in_addr
*addr
;
785 assert(address
!= NULL
);
787 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
789 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
791 /* only name for the moment */
792 if (inet_aton(hostname
, addr
) == 0) {
793 struct in_addr
*array_addr
;
795 if ((he
= gethostbyname(hostname
)) == NULL
) {
796 address
->m_error
= GSOCK_NOHOST
;
799 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
800 addr
->s_addr
= array_addr
[0].s_addr
;
802 return GSOCK_NOERROR
;
805 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
806 unsigned long hostaddr
)
808 struct in_addr
*addr
;
810 assert(address
!= NULL
);
812 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
814 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
815 addr
->s_addr
= hostaddr
;
817 return GSOCK_NOERROR
;
820 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
821 const char *protocol
)
824 struct sockaddr_in
*addr
;
826 assert(address
!= NULL
);
827 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
830 address
->m_error
= GSOCK_INVPORT
;
834 /* TODO: TCP or UDP */
835 se
= getservbyname(port
, protocol
);
837 if (isdigit(port
[0])) {
840 port_int
= atoi(port
);
841 addr
= (struct sockaddr_in
*)address
->m_addr
;
842 addr
->sin_port
= htons(port_int
);
843 return GSOCK_NOERROR
;
846 address
->m_error
= GSOCK_INVPORT
;
847 return GSOCK_INVPORT
;
850 addr
= (struct sockaddr_in
*)address
->m_addr
;
851 addr
->sin_port
= se
->s_port
;
853 return GSOCK_NOERROR
;
856 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
858 struct sockaddr_in
*addr
;
860 assert(address
!= NULL
);
861 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
863 addr
= (struct sockaddr_in
*)address
->m_addr
;
864 addr
->sin_port
= htons(port
);
866 return GSOCK_NOERROR
;
869 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
873 struct sockaddr_in
*addr
;
875 assert(address
!= NULL
);
876 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
878 addr
= (struct sockaddr_in
*)address
->m_addr
;
879 addr_buf
= (char *)&(addr
->sin_addr
);
881 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
883 address
->m_error
= GSOCK_NOHOST
;
887 strncpy(hostname
, he
->h_name
, sbuf
);
889 return GSOCK_NOERROR
;
892 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
894 struct sockaddr_in
*addr
;
896 assert(address
!= NULL
);
897 CHECK_ADDRESS(address
, INET
, 0);
899 addr
= (struct sockaddr_in
*)address
->m_addr
;
901 return addr
->sin_addr
.s_addr
;
904 unsigned short GAddress_INET_GetPort(GAddress
*address
)
906 struct sockaddr_in
*addr
;
908 assert(address
!= NULL
);
909 CHECK_ADDRESS(address
, INET
, 0);
911 addr
= (struct sockaddr_in
*)address
->m_addr
;
912 return ntohs(addr
->sin_port
);
916 * -------------------------------------------------------------------------
917 * Unix address family
918 * -------------------------------------------------------------------------
921 void _GAddress_Init_UNIX(GAddress
*address
)
923 address
->m_len
= sizeof(struct sockaddr_un
);
924 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
925 address
->m_family
= GSOCK_UNIX
;
926 address
->m_realfamily
= PF_UNIX
;
927 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
928 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
931 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
933 struct sockaddr_un
*addr
;
935 assert(address
!= NULL
);
937 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
939 addr
= ((struct sockaddr_un
*)address
->m_addr
);
940 memcpy(addr
->sun_path
, path
, strlen(path
));
942 return GSOCK_NOERROR
;
945 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
947 struct sockaddr_un
*addr
;
949 assert(address
!= NULL
);
950 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
952 addr
= (struct sockaddr_un
*)address
->m_addr
;
954 strncpy(path
, addr
->sun_path
, sbuf
);
956 return GSOCK_NOERROR
;