]>
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>
38 #include <wx/gsocket.h>
42 # define SOCKLEN_T int
45 /* Constructors / Destructors */
47 GSocket
*GSocket_new()
52 socket
= (GSocket
*)malloc(sizeof(GSocket
));
55 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
56 socket
->m_fbacks
[i
] = NULL
;
57 socket
->m_iocalls
[i
] = FALSE
;
59 socket
->m_local
= NULL
;
60 socket
->m_peer
= NULL
;
61 socket
->m_error
= GSOCK_NOERROR
;
62 socket
->m_server
= FALSE
;
63 socket
->m_stream
= TRUE
;
64 socket
->m_gui_dependent
= NULL
;
65 socket
->m_blocking
= FALSE
;
67 _GSocket_GUI_Init(socket
);
72 void GSocket_destroy(GSocket
*socket
)
74 assert(socket
!= NULL
);
76 if (socket
->m_fd
!= -1)
77 GSocket_Shutdown(socket
);
79 _GSocket_GUI_Destroy(socket
);
82 GAddress_destroy(socket
->m_local
);
85 GAddress_destroy(socket
->m_peer
);
90 void GSocket_Shutdown(GSocket
*socket
)
94 assert(socket
!= NULL
);
96 if (socket
->m_fd
!= -1) {
97 shutdown(socket
->m_fd
, 2);
102 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
103 _GSocket_Uninstall_Fallback(socket
, evt
);
106 /* Address handling */
108 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
110 if (socket
== NULL
|| (socket
->m_fd
!= -1 && !socket
->m_server
))
111 return GSOCK_INVSOCK
;
113 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
114 return GSOCK_INVADDR
;
117 GAddress_destroy(socket
->m_local
);
119 socket
->m_local
= GAddress_copy(address
);
121 return GSOCK_NOERROR
;
124 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
127 return GSOCK_INVSOCK
;
129 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
130 socket
->m_error
= GSOCK_INVADDR
;
131 return GSOCK_INVADDR
;
135 GAddress_destroy(socket
->m_peer
);
137 socket
->m_peer
= GAddress_copy(address
);
139 return GSOCK_NOERROR
;
142 GAddress
*GSocket_GetLocal(GSocket
*socket
)
145 struct sockaddr addr
;
148 assert(socket
!= NULL
);
151 return GAddress_copy(socket
->m_local
);
153 if (socket
->m_fd
== -1) {
154 socket
->m_error
= GSOCK_INVSOCK
;
160 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
161 socket
->m_error
= GSOCK_IOERR
;
165 address
= GAddress_new();
166 _GAddress_translate_from(address
, &addr
, size
);
171 GAddress
*GSocket_GetPeer(GSocket
*socket
)
173 assert(socket
!= NULL
);
176 return GAddress_copy(socket
->m_peer
);
181 /* Server specific parts */
184 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
185 of GSocket. "Local" must be set by GSocket_SetLocal() before
186 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
187 In the other cases, it returns GSOCK_INVADDR.
189 GSocketError
GSocket_SetServer(GSocket
*sck
)
195 if (sck
->m_fd
!= -1) {
196 sck
->m_error
= GSOCK_INVSOCK
;
197 return GSOCK_INVSOCK
;
201 sck
->m_error
= GSOCK_INVADDR
;
202 return GSOCK_INVADDR
;
210 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, type
, 0);
212 if (sck
->m_fd
== -1) {
213 sck
->m_error
= GSOCK_IOERR
;
217 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
220 sck
->m_error
= GSOCK_IOERR
;
224 if (listen(sck
->m_fd
, 5) < 0) {
227 sck
->m_error
= GSOCK_IOERR
;
231 sck
->m_server
= TRUE
;
233 return GSOCK_NOERROR
;
238 GSocket_WaitConnection() waits for an incoming client connection.
240 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
244 assert(socket
!= NULL
);
246 if (socket
->m_fd
== -1 || !socket
->m_server
) {
247 socket
->m_error
= GSOCK_INVSOCK
;
251 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
253 connection
= GSocket_new();
255 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
256 if (connection
->m_fd
== -1) {
257 GSocket_destroy(connection
);
258 socket
->m_error
= GSOCK_IOERR
;
262 connection
->m_stream
= TRUE
;
263 connection
->m_server
= FALSE
;
264 connection
->m_oriented
= TRUE
;
269 /* Non oriented connections */
271 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
275 if (sck
->m_fd
!= -1) {
276 sck
->m_error
= GSOCK_INVSOCK
;
277 return GSOCK_INVSOCK
;
281 sck
->m_error
= GSOCK_INVADDR
;
282 return GSOCK_INVADDR
;
285 sck
->m_stream
= FALSE
;
286 sck
->m_server
= FALSE
;
287 sck
->m_oriented
= FALSE
;
289 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
291 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
294 sck
->m_error
= GSOCK_IOERR
;
298 return GSOCK_NOERROR
;
301 /* Client specific parts */
304 GSocket_Connect() establishes a client connection to a server using the "Peer"
305 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
306 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
308 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
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
= (stream
== GSOCK_STREAMED
);
325 sck
->m_oriented
= TRUE
;
332 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
334 if (sck
->m_fd
== -1) {
335 sck
->m_error
= GSOCK_IOERR
;
339 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
340 sck
->m_peer
->m_len
) != 0) {
343 sck
->m_error
= GSOCK_IOERR
;
347 sck
->m_server
= FALSE
;
349 return GSOCK_NOERROR
;
354 /* Like recv(), send(), ... */
355 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
357 assert(socket
!= NULL
);
359 if (socket
->m_fd
== -1 || socket
->m_server
) {
360 socket
->m_error
= GSOCK_INVSOCK
;
364 _GSocket_Enable(socket
, GSOCK_INPUT
);
366 if (socket
->m_oriented
)
367 return _GSocket_Recv_Stream(socket
, buffer
, size
);
369 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
372 int GSocket_Write(GSocket
*socket
, const char *buffer
,
375 assert(socket
!= NULL
);
377 if (socket
->m_fd
== -1 || socket
->m_server
) {
378 socket
->m_error
= GSOCK_INVSOCK
;
382 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
384 if (socket
->m_oriented
)
385 return _GSocket_Send_Stream(socket
, buffer
, size
);
387 return _GSocket_Send_Dgram(socket
, buffer
, size
);
390 bool GSocket_DataAvailable(GSocket
*socket
)
395 assert(socket
!= NULL
);
397 if (socket
->m_fd
== -1 || socket
->m_server
) {
398 socket
->m_error
= GSOCK_INVSOCK
;
403 FD_SET(socket
->m_fd
, &read_set
);
408 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
410 return FD_ISSET(socket
->m_fd
, &read_set
);
416 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
417 if we don't want to wait.
419 void GSocket_SetBlocking(GSocket
*socket
, bool block
)
421 assert(socket
!= NULL
);
423 socket
->m_blocking
= block
;
425 if (socket
->m_fd
!= -1)
426 ioctl(socket
->m_fd
, FIONBIO
, &block
);
430 GSocket_GetError() returns the last error occured on the socket stream.
433 GSocketError
GSocket_GetError(GSocket
*socket
)
435 assert(socket
!= NULL
);
437 return socket
->m_error
;
443 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
444 INPUT: The function is called when there is at least a byte in the
446 OUTPUT: The function is called when the system is sure the next write call
448 CONNECTION: Two cases is possible:
449 Client socket -> the connection is established
450 Server socket -> a client request a connection
451 LOST: the connection is lost
453 SetFallback accepts a combination of these flags so a same callback can
454 receive different events.
456 An event is generated only once and its state is reseted when the relative
457 IO call is requested.
458 For example: INPUT -> GSocket_Read()
459 CONNECTION -> GSocket_Accept()
461 void GSocket_SetFallback(GSocket
*socket
, GSocketEventFlags event
,
462 GSocketFallback fallback
, char *cdata
)
466 assert (socket
!= NULL
);
468 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
469 if ((event
& (1 << count
)) != 0) {
470 socket
->m_fbacks
[count
] = fallback
;
471 socket
->m_data
[count
] = cdata
;
473 _GSocket_Install_Fallback(socket
, count
);
474 _GSocket_Enable(socket
, count
);
480 UnsetFallback will disables all fallbacks specified by "event".
481 NOTE: event may be a combination of flags
483 void GSocket_UnsetFallback(GSocket
*socket
, GSocketEventFlags event
)
487 assert(socket
!= NULL
);
489 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
490 if ((event
& (1 << count
)) != 0) {
491 _GSocket_Disable(socket
, count
);
492 socket
->m_fbacks
[count
] = NULL
;
493 _GSocket_Uninstall_Fallback(socket
, count
);
498 #define CALL_FALLBACK(socket, event) \
499 if (socket->m_iocalls[event] && \
500 socket->m_fbacks[event]) {\
501 _GSocket_Disable(socket, event); \
502 socket->m_fbacks[event](socket, event, \
503 socket->m_data[event]); \
506 #define MASK_SIGNAL() \
508 void (*old_handler)(int); \
510 old_handler = signal(SIGPIPE, SIG_IGN);
512 #define UNMASK_SIGNAL() \
513 signal(SIGPIPE, old_handler); \
516 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
518 socket
->m_iocalls
[event
] = TRUE
;
519 if (socket
->m_fbacks
[event
])
520 _GSocket_Install_Fallback(socket
, event
);
523 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
525 socket
->m_iocalls
[event
] = FALSE
;
526 if (socket
->m_fbacks
[event
])
527 _GSocket_Uninstall_Fallback(socket
, event
);
530 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
535 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
538 socket
->m_error
= GSOCK_IOERR
;
544 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
546 struct sockaddr from
;
549 fromlen
= sizeof(from
);
552 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
555 socket
->m_error
= GSOCK_IOERR
;
560 socket
->m_peer
= GAddress_new();
561 _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
566 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
571 ret
= send(socket
->m_fd
, buffer
, size
, 0);
574 socket
->m_error
= GSOCK_IOERR
;
580 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
582 struct sockaddr
*addr
;
585 if (!socket
->m_peer
) {
586 socket
->m_error
= GSOCK_INVADDR
;
590 _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
593 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
596 socket
->m_error
= GSOCK_IOERR
;
605 void _GSocket_Detected_Read(GSocket
*socket
)
610 if (socket
->m_stream
) {
611 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
613 if (ret
< 0 && socket
->m_server
) {
614 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
619 CALL_FALLBACK(socket
, GSOCK_INPUT
);
621 CALL_FALLBACK(socket
, GSOCK_LOST
);
626 void _GSocket_Detected_Write(GSocket
*socket
)
628 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
636 * -------------------------------------------------------------------------
638 * -------------------------------------------------------------------------
641 #define CHECK_ADDRESS(address, family, retval) \
643 if (address->m_family == GSOCK_NOFAMILY) \
644 _GAddress_Init_##family(address); \
645 if (address->m_family != GSOCK_##family) {\
646 address->m_error = GSOCK_INVADDR; \
651 GAddress
*GAddress_new()
655 address
= (GAddress
*)malloc(sizeof(GAddress
));
657 address
->m_family
= GSOCK_NOFAMILY
;
658 address
->m_addr
= NULL
;
664 GAddress
*GAddress_copy(GAddress
*address
)
668 assert(address
!= NULL
);
670 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
671 memcpy(addr2
, address
, sizeof(GAddress
));
673 if (address
->m_addr
) {
674 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
675 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
681 void GAddress_destroy(GAddress
*address
)
683 assert(address
!= NULL
);
688 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
690 assert(address
!= NULL
);
692 address
->m_family
= type
;
695 GAddressType
GAddress_GetFamily(GAddress
*address
)
697 assert(address
!= NULL
);
699 return address
->m_family
;
702 void _GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
703 address
->m_realfamily
= addr
->sa_family
;
704 switch (addr
->sa_family
) {
706 address
->m_family
= GSOCK_INET
;
709 address
->m_family
= GSOCK_UNIX
;
713 address
->m_family
= GSOCK_INET6
;
722 free(address
->m_addr
);
724 address
->m_len
= len
;
725 address
->m_addr
= (struct sockaddr
*)malloc(len
);
726 memcpy(address
->m_addr
, addr
, len
);
729 void _GAddress_translate_to(GAddress
*address
,
730 struct sockaddr
**addr
, int *len
)
732 if (!address
->m_addr
) {
737 *len
= address
->m_len
;
738 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
739 memcpy(*addr
, address
->m_addr
, address
->m_len
);
743 * -------------------------------------------------------------------------
744 * Internet address family
745 * -------------------------------------------------------------------------
748 void _GAddress_Init_INET(GAddress
*address
)
750 address
->m_len
= sizeof(struct sockaddr_in
);
751 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
752 address
->m_family
= GSOCK_INET
;
753 address
->m_realfamily
= PF_INET
;
754 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
755 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
758 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
761 struct in_addr
*addr
;
763 assert(address
!= NULL
);
765 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
767 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
769 /* only name for the moment */
770 if (inet_aton(hostname
, addr
) == 0) {
771 struct in_addr
*array_addr
;
773 if ((he
= gethostbyname(hostname
)) == NULL
) {
774 address
->m_error
= GSOCK_NOHOST
;
777 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
778 addr
->s_addr
= array_addr
[0].s_addr
;
780 return GSOCK_NOERROR
;
783 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
784 unsigned long hostaddr
)
786 struct in_addr
*addr
;
788 assert(address
!= NULL
);
790 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
792 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
793 addr
->s_addr
= hostaddr
;
795 return GSOCK_NOERROR
;
798 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
)
801 struct sockaddr_in
*addr
;
803 assert(address
!= NULL
);
804 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
807 address
->m_error
= GSOCK_INVPORT
;
811 /* TODO: TCP or UDP */
812 se
= getservbyname(port
, "tcp");
814 if (isdigit(port
[0])) {
817 port_int
= atoi(port
);
818 addr
= (struct sockaddr_in
*)address
->m_addr
;
819 addr
->sin_port
= htons(port_int
);
820 return GSOCK_NOERROR
;
823 address
->m_error
= GSOCK_INVPORT
;
824 return GSOCK_INVPORT
;
827 addr
= (struct sockaddr_in
*)address
->m_addr
;
828 addr
->sin_port
= se
->s_port
;
830 return GSOCK_NOERROR
;
833 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
835 struct sockaddr_in
*addr
;
837 assert(address
!= NULL
);
838 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
840 addr
= (struct sockaddr_in
*)address
->m_addr
;
841 addr
->sin_port
= htons(port
);
843 return GSOCK_NOERROR
;
846 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
850 struct sockaddr_in
*addr
;
852 assert(address
!= NULL
);
853 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
855 addr
= (struct sockaddr_in
*)address
->m_addr
;
856 addr_buf
= (char *)&(addr
->sin_addr
);
858 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
860 address
->m_error
= GSOCK_NOHOST
;
864 strncpy(hostname
, he
->h_name
, sbuf
);
866 return GSOCK_NOERROR
;
869 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
871 struct sockaddr_in
*addr
;
873 assert(address
!= NULL
);
874 CHECK_ADDRESS(address
, INET
, 0);
876 addr
= (struct sockaddr_in
*)address
->m_addr
;
878 return addr
->sin_addr
.s_addr
;
881 unsigned short GAddress_INET_GetPort(GAddress
*address
)
883 struct sockaddr_in
*addr
;
885 assert(address
!= NULL
);
886 CHECK_ADDRESS(address
, INET
, 0);
888 addr
= (struct sockaddr_in
*)address
->m_addr
;
889 return ntohs(addr
->sin_port
);
893 * -------------------------------------------------------------------------
894 * Unix address family
895 * -------------------------------------------------------------------------
898 void _GAddress_Init_UNIX(GAddress
*address
)
900 address
->m_len
= sizeof(struct sockaddr_un
);
901 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
902 address
->m_family
= GSOCK_UNIX
;
903 address
->m_realfamily
= PF_UNIX
;
904 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
905 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
908 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
910 struct sockaddr_un
*addr
;
912 assert(address
!= NULL
);
914 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
916 addr
= ((struct sockaddr_un
*)address
->m_addr
);
917 memcpy(addr
->sun_path
, path
, strlen(path
));
919 return GSOCK_NOERROR
;
922 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
924 struct sockaddr_un
*addr
;
926 assert(address
!= NULL
);
927 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
929 addr
= (struct sockaddr_un
*)address
->m_addr
;
931 strncpy(path
, addr
->sun_path
, sbuf
);
933 return GSOCK_NOERROR
;