]>
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
7 * Log: Revision 1.1 1999/07/22 17:51:54 GL
8 * Log: Added GSocket for Unix (only GTK for the moment)
9 * Log: Updated wxSocket to use GSocket API
10 * Log: Added a progress bar to client.cpp
11 * Log: Added CopyTo to wxMemoryOutputStream to copy the internal buffer to a specified buffer.
12 * Log: Various changes/fixes to the high-level protocols FTP/HTTP
13 * Log: Various Unicode fixes
14 * Log: Removed sckint.*
16 * Log: Revision 1.2 1999/07/18 15:52:34 guilhem
17 * Log: * Copyright, etc.
19 * -------------------------------------------------------------------------
23 #include <sys/ioctl.h>
27 #include <sys/socket.h>
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
42 #include <sys/filio.h>
51 #include <wx/gsocket.h>
54 /* Constructors / Destructors */
56 GSocket
*GSocket_new()
61 socket
= (GSocket
*)malloc(sizeof(GSocket
));
64 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
65 socket
->m_fbacks
[i
] = NULL
;
66 socket
->m_iocalls
[i
] = FALSE
;
68 socket
->m_local
= NULL
;
69 socket
->m_peer
= NULL
;
70 socket
->m_error
= GSOCK_NOERROR
;
71 socket
->m_server
= FALSE
;
72 socket
->m_stream
= TRUE
;
73 socket
->m_gui_dependent
= NULL
;
74 socket
->m_blocking
= FALSE
;
76 _GSocket_GUI_Init(socket
);
81 void GSocket_destroy(GSocket
*socket
)
83 assert(socket
!= NULL
);
85 _GSocket_GUI_Destroy(socket
);
87 if (socket
->m_fd
!= -1)
88 GSocket_Shutdown(socket
);
91 GAddress_destroy(socket
->m_local
);
94 GAddress_destroy(socket
->m_peer
);
99 void GSocket_Shutdown(GSocket
*socket
)
103 assert(socket
!= NULL
);
105 if (socket
->m_fd
!= -1) {
106 shutdown(socket
->m_fd
, 2);
111 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
112 _GSocket_Uninstall_Fallback(socket
, evt
);
115 /* Address handling */
117 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
119 if (socket
== NULL
|| (socket
->m_fd
!= -1 && !socket
->m_server
))
120 return GSOCK_INVSOCK
;
122 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
123 return GSOCK_INVADDR
;
126 GAddress_destroy(socket
->m_local
);
128 socket
->m_local
= GAddress_copy(address
);
130 return GSOCK_NOERROR
;
133 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
136 return GSOCK_INVSOCK
;
138 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
139 socket
->m_error
= GSOCK_INVADDR
;
140 return GSOCK_INVADDR
;
144 GAddress_destroy(socket
->m_peer
);
146 socket
->m_peer
= GAddress_copy(address
);
148 return GSOCK_NOERROR
;
151 GAddress
*GSocket_GetLocal(GSocket
*socket
)
154 struct sockaddr addr
;
157 assert(socket
!= NULL
);
160 return GAddress_copy(socket
->m_local
);
162 if (socket
->m_fd
== -1) {
163 socket
->m_error
= GSOCK_INVSOCK
;
169 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
170 socket
->m_error
= GSOCK_IOERR
;
174 address
= GAddress_new();
175 _GAddress_translate_from(address
, &addr
, size
);
180 GAddress
*GSocket_GetPeer(GSocket
*socket
)
182 assert(socket
!= NULL
);
185 return GAddress_copy(socket
->m_peer
);
190 /* Server specific parts */
193 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
194 of GSocket. "Local" must be set by GSocket_SetLocal() before
195 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
196 In the other cases, it returns GSOCK_INVADDR.
198 GSocketError
GSocket_SetServer(GSocket
*sck
)
204 if (sck
->m_fd
!= -1) {
205 sck
->m_error
= GSOCK_INVSOCK
;
206 return GSOCK_INVSOCK
;
210 sck
->m_error
= GSOCK_INVADDR
;
211 return GSOCK_INVADDR
;
219 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, type
, 0);
221 if (sck
->m_fd
== -1) {
222 sck
->m_error
= GSOCK_IOERR
;
226 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
229 sck
->m_error
= GSOCK_IOERR
;
233 if (listen(sck
->m_fd
, 5) < 0) {
236 sck
->m_error
= GSOCK_IOERR
;
240 sck
->m_server
= TRUE
;
242 return GSOCK_NOERROR
;
247 GSocket_WaitConnection() waits for an incoming client connection.
249 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
253 assert(socket
!= NULL
);
255 if (socket
->m_fd
== -1 || !socket
->m_server
) {
256 socket
->m_error
= GSOCK_INVSOCK
;
260 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
262 connection
= GSocket_new();
264 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
265 if (connection
->m_fd
== -1) {
266 GSocket_destroy(connection
);
267 socket
->m_error
= GSOCK_IOERR
;
271 connection
->m_stream
= TRUE
;
272 connection
->m_server
= FALSE
;
273 connection
->m_oriented
= TRUE
;
278 /* Non oriented connections */
280 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
284 if (sck
->m_fd
!= -1) {
285 sck
->m_error
= GSOCK_INVSOCK
;
286 return GSOCK_INVSOCK
;
290 sck
->m_error
= GSOCK_INVADDR
;
291 return GSOCK_INVADDR
;
294 sck
->m_stream
= FALSE
;
295 sck
->m_server
= FALSE
;
296 sck
->m_oriented
= FALSE
;
298 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
300 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
303 sck
->m_error
= GSOCK_IOERR
;
307 return GSOCK_NOERROR
;
310 /* Client specific parts */
313 GSocket_Connect() establishes a client connection to a server using the "Peer"
314 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
315 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
317 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
323 if (sck
->m_fd
!= -1) {
324 sck
->m_error
= GSOCK_INVSOCK
;
325 return GSOCK_INVSOCK
;
329 sck
->m_error
= GSOCK_INVADDR
;
330 return GSOCK_INVADDR
;
333 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
334 sck
->m_oriented
= TRUE
;
341 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
343 if (sck
->m_fd
== -1) {
344 sck
->m_error
= GSOCK_IOERR
;
348 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
349 sck
->m_peer
->m_len
) != 0) {
352 sck
->m_error
= GSOCK_IOERR
;
356 sck
->m_server
= FALSE
;
358 return GSOCK_NOERROR
;
363 /* Like recv(), send(), ... */
364 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
366 assert(socket
!= NULL
);
368 if (socket
->m_fd
== -1 || socket
->m_server
) {
369 socket
->m_error
= GSOCK_INVSOCK
;
373 _GSocket_Enable(socket
, GSOCK_INPUT
);
375 if (socket
->m_oriented
)
376 return _GSocket_Recv_Stream(socket
, buffer
, size
);
378 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
381 int GSocket_Write(GSocket
*socket
, const char *buffer
,
384 assert(socket
!= NULL
);
386 if (socket
->m_fd
== -1 || socket
->m_server
) {
387 socket
->m_error
= GSOCK_INVSOCK
;
391 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
393 if (socket
->m_oriented
)
394 return _GSocket_Send_Stream(socket
, buffer
, size
);
396 return _GSocket_Send_Dgram(socket
, buffer
, size
);
399 bool GSocket_DataAvailable(GSocket
*socket
)
404 assert(socket
!= NULL
);
406 if (socket
->m_fd
== -1 || socket
->m_server
) {
407 socket
->m_error
= GSOCK_INVSOCK
;
412 FD_SET(socket
->m_fd
, &read_set
);
417 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
419 return FD_ISSET(socket
->m_fd
, &read_set
);
425 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
426 if we don't want to wait.
428 void GSocket_SetBlocking(GSocket
*socket
, bool block
)
430 assert(socket
!= NULL
);
432 socket
->m_blocking
= block
;
434 if (socket
->m_fd
!= -1)
435 ioctl(socket
->m_fd
, FIONBIO
, &block
);
439 GSocket_GetError() returns the last error occured on the socket stream.
442 GSocketError
GSocket_GetError(GSocket
*socket
)
444 assert(socket
!= NULL
);
446 return socket
->m_error
;
452 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
453 INPUT: The function is called when there is at least a byte in the
455 OUTPUT: The function is called when the system is sure the next write call
457 CONNECTION: Two cases is possible:
458 Client socket -> the connection is established
459 Server socket -> a client request a connection
460 LOST: the connection is lost
462 SetFallback accepts a combination of these flags so a same callback can
463 receive different events.
465 An event is generated only once and its state is reseted when the relative
466 IO call is requested.
467 For example: INPUT -> GSocket_Read()
468 CONNECTION -> GSocket_Accept()
470 void GSocket_SetFallback(GSocket
*socket
, GSocketEventFlags event
,
471 GSocketFallback fallback
, char *cdata
)
475 assert (socket
!= NULL
);
477 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
478 if ((event
& (1 << count
)) != 0) {
479 socket
->m_fbacks
[count
] = fallback
;
480 socket
->m_data
[count
] = cdata
;
482 _GSocket_Install_Fallback(socket
, count
);
483 _GSocket_Enable(socket
, count
);
489 UnsetFallback will disables all fallbacks specified by "event".
490 NOTE: event may be a combination of flags
492 void GSocket_UnsetFallback(GSocket
*socket
, GSocketEventFlags event
)
496 assert(socket
!= NULL
);
498 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
499 if ((event
& (1 << count
)) != 0) {
500 _GSocket_Disable(socket
, count
);
501 socket
->m_fbacks
[count
] = NULL
;
502 _GSocket_Uninstall_Fallback(socket
, count
);
507 #define CALL_FALLBACK(socket, event) \
508 if (socket->m_iocalls[event] && \
509 socket->m_fbacks[event]) {\
510 _GSocket_Disable(socket, event); \
511 socket->m_fbacks[event](socket, event, \
512 socket->m_data[event]); \
515 #define MASK_SIGNAL() \
517 void (*old_handler)(int); \
519 old_handler = signal(SIGPIPE, SIG_IGN);
521 #define UNMASK_SIGNAL() \
522 signal(SIGPIPE, old_handler); \
525 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
527 socket
->m_iocalls
[event
] = TRUE
;
528 if (socket
->m_fbacks
[event
])
529 _GSocket_Install_Fallback(socket
, event
);
532 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
534 socket
->m_iocalls
[event
] = FALSE
;
535 if (socket
->m_fbacks
[event
])
536 _GSocket_Uninstall_Fallback(socket
, event
);
539 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
544 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
547 socket
->m_error
= GSOCK_IOERR
;
553 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
555 struct sockaddr from
;
558 fromlen
= sizeof(from
);
561 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
564 socket
->m_error
= GSOCK_IOERR
;
569 socket
->m_peer
= GAddress_new();
570 _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
575 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
580 ret
= send(socket
->m_fd
, buffer
, size
, 0);
583 socket
->m_error
= GSOCK_IOERR
;
589 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
591 struct sockaddr
*addr
;
594 if (!socket
->m_peer
) {
595 socket
->m_error
= GSOCK_INVADDR
;
599 _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
602 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
605 socket
->m_error
= GSOCK_IOERR
;
614 void _GSocket_Detected_Read(GSocket
*socket
)
619 if (socket
->m_stream
) {
620 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
622 if (ret
< 0 && socket
->m_server
) {
623 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
628 CALL_FALLBACK(socket
, GSOCK_INPUT
);
630 CALL_FALLBACK(socket
, GSOCK_LOST
);
635 void _GSocket_Detected_Write(GSocket
*socket
)
637 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
645 * -------------------------------------------------------------------------
647 * -------------------------------------------------------------------------
650 #define CHECK_ADDRESS(address, family, retval) \
652 if (address->m_family == GSOCK_NOFAMILY) \
653 _GAddress_Init_##family(address); \
654 if (address->m_family != GSOCK_##family) {\
655 address->m_error = GSOCK_INVADDR; \
660 GAddress
*GAddress_new()
664 address
= (GAddress
*)malloc(sizeof(GAddress
));
666 address
->m_family
= GSOCK_NOFAMILY
;
667 address
->m_addr
= NULL
;
673 GAddress
*GAddress_copy(GAddress
*address
)
677 assert(address
!= NULL
);
679 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
680 memcpy(addr2
, address
, sizeof(GAddress
));
682 if (address
->m_addr
) {
683 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
684 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
690 void GAddress_destroy(GAddress
*address
)
692 assert(address
!= NULL
);
697 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
699 assert(address
!= NULL
);
701 address
->m_family
= type
;
704 GAddressType
GAddress_GetFamily(GAddress
*address
)
706 assert(address
!= NULL
);
708 return address
->m_family
;
711 void _GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
712 address
->m_realfamily
= addr
->sa_family
;
713 switch (addr
->sa_family
) {
715 address
->m_family
= GSOCK_INET
;
718 address
->m_family
= GSOCK_UNIX
;
721 address
->m_family
= GSOCK_INET6
;
728 free(address
->m_addr
);
730 address
->m_len
= len
;
731 address
->m_addr
= (struct sockaddr
*)malloc(len
);
732 memcpy(address
->m_addr
, addr
, len
);
735 void _GAddress_translate_to(GAddress
*address
,
736 struct sockaddr
**addr
, int *len
)
738 if (!address
->m_addr
) {
743 *len
= address
->m_len
;
744 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
745 memcpy(*addr
, address
->m_addr
, address
->m_len
);
749 * -------------------------------------------------------------------------
750 * Internet address family
751 * -------------------------------------------------------------------------
754 void _GAddress_Init_INET(GAddress
*address
)
756 address
->m_len
= sizeof(struct sockaddr_in
);
757 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
758 address
->m_family
= GSOCK_INET
;
759 address
->m_realfamily
= PF_INET
;
760 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
761 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
764 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
767 struct in_addr
*addr
;
769 assert(address
!= NULL
);
771 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
773 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
775 /* only name for the moment */
776 if (inet_aton(hostname
, addr
) == 0) {
777 struct in_addr
*array_addr
;
779 if ((he
= gethostbyname(hostname
)) == NULL
) {
780 address
->m_error
= GSOCK_NOHOST
;
783 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
784 addr
->s_addr
= array_addr
[0].s_addr
;
786 return GSOCK_NOERROR
;
789 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
790 unsigned long hostaddr
)
792 struct in_addr
*addr
;
794 assert(address
!= NULL
);
796 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
798 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
799 addr
->s_addr
= hostaddr
;
801 return GSOCK_NOERROR
;
804 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
)
807 struct sockaddr_in
*addr
;
809 assert(address
!= NULL
);
810 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
813 address
->m_error
= GSOCK_INVPORT
;
817 /* TODO: TCP or UDP */
818 se
= getservbyname(port
, "tcp");
820 if (isdigit(port
[0])) {
823 port_int
= atoi(port
);
824 addr
= (struct sockaddr_in
*)address
->m_addr
;
825 addr
->sin_port
= htons(port_int
);
826 return GSOCK_NOERROR
;
829 address
->m_error
= GSOCK_INVPORT
;
830 return GSOCK_INVPORT
;
833 addr
= (struct sockaddr_in
*)address
->m_addr
;
834 addr
->sin_port
= se
->s_port
;
836 return GSOCK_NOERROR
;
839 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
841 struct sockaddr_in
*addr
;
843 assert(address
!= NULL
);
844 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
846 addr
= (struct sockaddr_in
*)address
->m_addr
;
847 addr
->sin_port
= htons(port
);
849 return GSOCK_NOERROR
;
852 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
856 struct sockaddr_in
*addr
;
858 assert(address
!= NULL
);
859 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
861 addr
= (struct sockaddr_in
*)address
->m_addr
;
862 addr_buf
= (char *)&(addr
->sin_addr
);
864 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
866 address
->m_error
= GSOCK_NOHOST
;
870 strncpy(hostname
, he
->h_name
, sbuf
);
872 return GSOCK_NOERROR
;
875 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
877 struct sockaddr_in
*addr
;
879 assert(address
!= NULL
);
880 CHECK_ADDRESS(address
, INET
, 0);
882 addr
= (struct sockaddr_in
*)address
->m_addr
;
884 return addr
->sin_addr
.s_addr
;
887 unsigned short GAddress_INET_GetPort(GAddress
*address
)
889 struct sockaddr_in
*addr
;
891 assert(address
!= NULL
);
892 CHECK_ADDRESS(address
, INET
, 0);
894 addr
= (struct sockaddr_in
*)address
->m_addr
;
895 return ntohs(addr
->sin_port
);
899 * -------------------------------------------------------------------------
900 * Unix address family
901 * -------------------------------------------------------------------------
904 void _GAddress_Init_UNIX(GAddress
*address
)
906 address
->m_len
= sizeof(struct sockaddr_un
);
907 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
908 address
->m_family
= GSOCK_UNIX
;
909 address
->m_realfamily
= PF_UNIX
;
910 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
911 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
914 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
916 struct sockaddr_un
*addr
;
918 assert(address
!= NULL
);
920 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
922 addr
= ((struct sockaddr_un
*)address
->m_addr
);
923 memcpy(addr
->sun_path
, path
, strlen(path
));
925 return GSOCK_NOERROR
;
928 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
930 struct sockaddr_un
*addr
;
932 assert(address
!= NULL
);
933 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
935 addr
= (struct sockaddr_un
*)address
->m_addr
;
937 strncpy(path
, addr
->sun_path
, sbuf
);
939 return GSOCK_NOERROR
;