]>
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>
36 # include <sys/filio.h>
45 #include "wx/gsocket.h"
52 # define SOCKLEN_T socklen_t
55 # define SOCKLEN_T int
60 /* Global initialisers */
67 void GSocket_Cleanup()
71 /* Constructors / Destructors */
73 GSocket
*GSocket_new()
78 socket
= (GSocket
*)malloc(sizeof(GSocket
));
84 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
85 socket
->m_fbacks
[i
] = NULL
;
86 socket
->m_iocalls
[i
] = FALSE
;
88 socket
->m_local
= NULL
;
89 socket
->m_peer
= NULL
;
90 socket
->m_error
= GSOCK_NOERROR
;
91 socket
->m_server
= FALSE
;
92 socket
->m_stream
= TRUE
;
93 socket
->m_gui_dependent
= NULL
;
94 socket
->m_blocking
= FALSE
;
95 socket
->m_timeout
= 10*60*1000;
96 // 10 minutes * 60 sec * 1000 millisec
98 /* We initialize the GUI specific entries here */
99 _GSocket_GUI_Init(socket
);
104 void GSocket_destroy(GSocket
*socket
)
106 assert(socket
!= NULL
);
108 /* First, we check that the socket is really shutdowned */
109 if (socket
->m_fd
!= -1)
110 GSocket_Shutdown(socket
);
112 /* We destroy GUI specific variables */
113 _GSocket_GUI_Destroy(socket
);
115 /* We destroy private addresses */
117 GAddress_destroy(socket
->m_local
);
120 GAddress_destroy(socket
->m_peer
);
122 /* We destroy socket itself */
126 void GSocket_Shutdown(GSocket
*socket
)
130 assert(socket
!= NULL
);
132 /* If socket has been created, we shutdown it */
133 if (socket
->m_fd
!= -1) {
134 shutdown(socket
->m_fd
, 2);
139 /* We also disable GUI callbacks */
140 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
141 _GSocket_Uninstall_Callback(socket
, evt
);
144 /* Address handling */
146 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
148 assert(socket
!= NULL
);
150 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
151 socket
->m_error
= GSOCK_INVSOCK
;
152 return GSOCK_INVSOCK
;
155 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
156 socket
->m_error
= GSOCK_INVADDR
;
157 return GSOCK_INVADDR
;
161 GAddress_destroy(socket
->m_local
);
163 socket
->m_local
= GAddress_copy(address
);
165 return GSOCK_NOERROR
;
168 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
170 assert(socket
!= NULL
);
172 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
173 socket
->m_error
= GSOCK_INVADDR
;
174 return GSOCK_INVADDR
;
178 GAddress_destroy(socket
->m_peer
);
180 socket
->m_peer
= GAddress_copy(address
);
182 return GSOCK_NOERROR
;
185 GAddress
*GSocket_GetLocal(GSocket
*socket
)
188 struct sockaddr addr
;
191 assert(socket
!= NULL
);
194 return GAddress_copy(socket
->m_local
);
196 if (socket
->m_fd
== -1) {
197 socket
->m_error
= GSOCK_INVSOCK
;
203 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
204 socket
->m_error
= GSOCK_IOERR
;
208 address
= GAddress_new();
209 if (address
== NULL
) {
210 socket
->m_error
= GSOCK_MEMERR
;
213 if (!_GAddress_translate_from(address
, &addr
, size
)) {
214 socket
->m_error
= GSOCK_MEMERR
;
215 GAddress_destroy(address
);
222 GAddress
*GSocket_GetPeer(GSocket
*socket
)
224 assert(socket
!= NULL
);
227 return GAddress_copy(socket
->m_peer
);
232 /* Server specific parts */
235 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
236 of GSocket. "Local" must be set by GSocket_SetLocal() before
237 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
238 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
239 In the other cases it returns GSOCK_IOERR.
241 GSocketError
GSocket_SetServer(GSocket
*sck
)
247 if (sck
->m_fd
!= -1) {
248 sck
->m_error
= GSOCK_INVSOCK
;
249 return GSOCK_INVSOCK
;
253 sck
->m_error
= GSOCK_INVADDR
;
254 return GSOCK_INVADDR
;
257 /* We always have a stream here */
258 sck
->m_stream
= TRUE
;
260 /* Create the socket */
261 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
263 if (sck
->m_fd
== -1) {
264 sck
->m_error
= GSOCK_IOERR
;
268 /* Bind the socket to the LOCAL address */
269 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
272 sck
->m_error
= GSOCK_IOERR
;
276 /* Enable listening up to 5 connections */
277 if (listen(sck
->m_fd
, 5) < 0) {
280 sck
->m_error
= GSOCK_IOERR
;
284 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
285 GSocket_SetTimeout(sck
, sck
->m_timeout
);
287 return GSOCK_NOERROR
;
291 GSocket_WaitConnection() waits for an incoming client connection.
293 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
297 assert(socket
!= NULL
);
299 /* If the socket has already been created, we exit immediately */
300 if (socket
->m_fd
== -1 || !socket
->m_server
) {
301 socket
->m_error
= GSOCK_INVSOCK
;
305 /* Reenable GSOCK_CONNECTION event */
306 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
308 /* Create a GSocket object for the new connection */
309 connection
= GSocket_new();
311 /* Accept the incoming connection */
312 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
313 if (connection
->m_fd
== -1) {
314 GSocket_destroy(connection
);
315 socket
->m_error
= GSOCK_IOERR
;
319 /* Initialize all fields */
320 connection
->m_stream
= TRUE
;
321 connection
->m_server
= FALSE
;
322 connection
->m_oriented
= TRUE
;
327 /* Non oriented connections */
329 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
333 if (sck
->m_fd
!= -1) {
334 sck
->m_error
= GSOCK_INVSOCK
;
335 return GSOCK_INVSOCK
;
339 sck
->m_error
= GSOCK_INVADDR
;
340 return GSOCK_INVADDR
;
343 sck
->m_stream
= FALSE
;
344 sck
->m_server
= FALSE
;
345 sck
->m_oriented
= FALSE
;
347 /* Create the socket */
348 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
350 /* Bind it to the LOCAL address */
351 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
354 sck
->m_error
= GSOCK_IOERR
;
358 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
359 GSocket_SetTimeout(sck
, sck
->m_timeout
);
361 return GSOCK_NOERROR
;
364 /* Client specific parts */
367 GSocket_Connect() establishes a client connection to a server using the "Peer"
368 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
369 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
371 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
377 if (sck
->m_fd
!= -1) {
378 sck
->m_error
= GSOCK_INVSOCK
;
379 return GSOCK_INVSOCK
;
383 sck
->m_error
= GSOCK_INVADDR
;
384 return GSOCK_INVADDR
;
387 /* Test whether we want the socket to be a stream (e.g. TCP) */
388 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
389 sck
->m_oriented
= TRUE
;
396 /* Create the socket */
397 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
399 if (sck
->m_fd
== -1) {
400 sck
->m_error
= GSOCK_IOERR
;
404 /* Connect it to the PEER address */
405 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
406 sck
->m_peer
->m_len
) != 0) {
409 sck
->m_error
= GSOCK_IOERR
;
413 /* It is not a server */
414 sck
->m_server
= FALSE
;
416 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
417 GSocket_SetTimeout(sck
, sck
->m_timeout
);
419 return GSOCK_NOERROR
;
424 /* Like recv(), send(), ... */
425 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
427 assert(socket
!= NULL
);
429 if (socket
->m_fd
== -1 || socket
->m_server
) {
430 socket
->m_error
= GSOCK_INVSOCK
;
434 /* Reenable GSOCK_INPUT event */
435 _GSocket_Enable(socket
, GSOCK_INPUT
);
437 if (socket
->m_stream
)
438 return _GSocket_Recv_Stream(socket
, buffer
, size
);
440 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
443 int GSocket_Write(GSocket
*socket
, const char *buffer
,
446 assert(socket
!= NULL
);
448 if (socket
->m_fd
== -1 || socket
->m_server
) {
449 socket
->m_error
= GSOCK_INVSOCK
;
453 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
455 if (socket
->m_stream
)
456 return _GSocket_Send_Stream(socket
, buffer
, size
);
458 return _GSocket_Send_Dgram(socket
, buffer
, size
);
461 bool GSocket_DataAvailable(GSocket
*socket
)
466 assert(socket
!= NULL
);
468 if (socket
->m_fd
== -1 || socket
->m_server
) {
469 socket
->m_error
= GSOCK_INVSOCK
;
474 FD_SET(socket
->m_fd
, &read_set
);
479 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
481 return FD_ISSET(socket
->m_fd
, &read_set
);
487 GSocket_SetNonBlocking() puts the socket in non-blocking mode. This is useful
488 if we don't want to wait.
490 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
492 assert(socket
!= NULL
);
494 socket
->m_blocking
= block
;
496 if (socket
->m_fd
!= -1)
497 ioctl(socket
->m_fd
, FIONBIO
, &block
);
501 * GSocket_SetTimeout()
505 # define CAN_USE_TIMEOUT
506 #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__)
507 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 1)
508 # define CAN_USE_TIMEOUT
512 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
514 assert(socket
!= NULL
);
516 /* Neither GLIBC 2.0 nor the kernel 2.0.36 define SO_SNDTIMEO or
517 SO_RCVTIMEO. The man pages, that these flags should exist but
518 are read only. RR. */
519 /* OK, restrict this to GLIBC 2.1. GL. */
520 #ifdef CAN_USE_TIMEOUT
521 socket
->m_timeout
= millisec
;
522 if (socket
->m_fd
!= -1) {
525 tval
.tv_sec
= millisec
/ 1000;
526 tval
.tv_usec
= (millisec
% 1000) * 1000;
527 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tval
, sizeof(tval
));
528 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tval
, sizeof(tval
));
534 GSocket_GetError() returns the last error occured on the socket stream.
537 GSocketError
GSocket_GetError(GSocket
*socket
)
539 assert(socket
!= NULL
);
541 return socket
->m_error
;
547 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
548 INPUT: The function is called when there is at least a byte in the
550 OUTPUT: The function is called when the system is sure the next write call
552 CONNECTION: Two cases is possible:
553 Client socket -> the connection is established
554 Server socket -> a client request a connection
555 LOST: the connection is lost
557 SetCallback accepts a combination of these flags so a same callback can
558 receive different events.
560 An event is generated only once and its state is reseted when the relative
561 IO call is requested.
562 For example: INPUT -> GSocket_Read()
563 CONNECTION -> GSocket_Accept()
565 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
566 GSocketCallback fallback
, char *cdata
)
570 assert (socket
!= NULL
);
572 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
573 /* We test each flag and, if it is enabled, we enable the corresponding
575 if ((event
& (1 << count
)) != 0) {
576 socket
->m_fbacks
[count
] = fallback
;
577 socket
->m_data
[count
] = cdata
;
578 _GSocket_Enable(socket
, count
);
584 UnsetCallback will disables all fallbacks specified by "event".
585 NOTE: event may be a combination of flags
587 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
591 assert(socket
!= NULL
);
593 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
594 if ((event
& (1 << count
)) != 0) {
595 _GSocket_Disable(socket
, count
);
596 socket
->m_fbacks
[count
] = NULL
;
601 #define CALL_FALLBACK(socket, event) \
602 if (socket->m_iocalls[event] && \
603 socket->m_fbacks[event]) {\
604 _GSocket_Disable(socket, event); \
605 socket->m_fbacks[event](socket, event, \
606 socket->m_data[event]); \
609 #define MASK_SIGNAL() \
611 void (*old_handler)(int); \
613 old_handler = signal(SIGPIPE, SIG_IGN);
615 #define UNMASK_SIGNAL() \
616 signal(SIGPIPE, old_handler); \
619 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
621 socket
->m_iocalls
[event
] = TRUE
;
622 if (socket
->m_fbacks
[event
])
623 _GSocket_Install_Callback(socket
, event
);
626 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
628 socket
->m_iocalls
[event
] = FALSE
;
629 if (socket
->m_fbacks
[event
])
630 _GSocket_Uninstall_Callback(socket
, event
);
633 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
638 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
640 if (ret
== -1 && errno
!= EAGAIN
) {
641 socket
->m_error
= GSOCK_IOERR
;
644 if (errno
== EAGAIN
) {
645 socket
->m_error
= GSOCK_TRYAGAIN
;
651 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
653 struct sockaddr from
;
657 fromlen
= sizeof(from
);
660 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
662 if (ret
== -1 && errno
!= EAGAIN
) {
663 socket
->m_error
= GSOCK_IOERR
;
666 if (errno
== EAGAIN
) {
667 socket
->m_error
= GSOCK_TRYAGAIN
;
671 /* Translate a system address into a GSocket address */
672 if (!socket
->m_peer
) {
673 socket
->m_peer
= GAddress_new();
674 if (!socket
->m_peer
) {
675 socket
->m_error
= GSOCK_MEMERR
;
679 if (!_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
)) {
680 socket
->m_error
= GSOCK_MEMERR
;
687 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
692 ret
= send(socket
->m_fd
, buffer
, size
, 0);
694 if (ret
== -1 && errno
!= EAGAIN
) {
695 socket
->m_error
= GSOCK_IOERR
;
698 if (errno
== EAGAIN
) {
699 socket
->m_error
= GSOCK_TRYAGAIN
;
705 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
707 struct sockaddr
*addr
;
710 if (!socket
->m_peer
) {
711 socket
->m_error
= GSOCK_INVADDR
;
715 if (!_GAddress_translate_to(socket
->m_peer
, &addr
, &len
)) {
716 socket
->m_error
= GSOCK_MEMERR
;
721 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
724 /* Frees memory allocated from _GAddress_translate_to */
727 if (ret
== -1 && errno
!= EAGAIN
) {
728 socket
->m_error
= GSOCK_IOERR
;
731 if (errno
== EAGAIN
) {
732 socket
->m_error
= GSOCK_TRYAGAIN
;
739 void _GSocket_Detected_Read(GSocket
*socket
)
744 if (socket
->m_stream
) {
745 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
747 if (ret
< 0 && socket
->m_server
) {
748 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
753 CALL_FALLBACK(socket
, GSOCK_INPUT
);
755 CALL_FALLBACK(socket
, GSOCK_LOST
);
760 void _GSocket_Detected_Write(GSocket
*socket
)
762 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
770 * -------------------------------------------------------------------------
772 * -------------------------------------------------------------------------
775 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
776 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
777 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
779 #define CHECK_ADDRESS(address, family, retval) \
781 if (address->m_family == GSOCK_NOFAMILY) \
782 if (!_GAddress_Init_##family(address)) {\
783 address->m_error = GSOCK_MEMERR; \
786 if (address->m_family != GSOCK_##family) {\
787 address->m_error = GSOCK_INVADDR; \
792 GAddress
*GAddress_new()
796 address
= (GAddress
*)malloc(sizeof(GAddress
));
801 address
->m_family
= GSOCK_NOFAMILY
;
802 address
->m_addr
= NULL
;
808 GAddress
*GAddress_copy(GAddress
*address
)
812 assert(address
!= NULL
);
814 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
819 memcpy(addr2
, address
, sizeof(GAddress
));
821 if (address
->m_addr
) {
822 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
823 if (addr2
->m_addr
== NULL
) {
827 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
833 void GAddress_destroy(GAddress
*address
)
835 assert(address
!= NULL
);
840 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
842 assert(address
!= NULL
);
844 address
->m_family
= type
;
847 GAddressType
GAddress_GetFamily(GAddress
*address
)
849 assert(address
!= NULL
);
851 return address
->m_family
;
854 bool _GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
855 address
->m_realfamily
= addr
->sa_family
;
856 switch (addr
->sa_family
) {
858 address
->m_family
= GSOCK_INET
;
861 address
->m_family
= GSOCK_UNIX
;
865 address
->m_family
= GSOCK_INET6
;
875 free(address
->m_addr
);
877 address
->m_len
= len
;
878 address
->m_addr
= (struct sockaddr
*)malloc(len
);
879 if (address
->m_addr
== NULL
)
881 memcpy(address
->m_addr
, addr
, len
);
886 bool _GAddress_translate_to(GAddress
*address
,
887 struct sockaddr
**addr
, int *len
)
889 if (!address
->m_addr
) {
894 *len
= address
->m_len
;
895 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
899 memcpy(*addr
, address
->m_addr
, address
->m_len
);
904 * -------------------------------------------------------------------------
905 * Internet address family
906 * -------------------------------------------------------------------------
909 bool _GAddress_Init_INET(GAddress
*address
)
911 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
912 if (address
->m_addr
== NULL
)
915 address
->m_len
= sizeof(struct sockaddr_in
);
917 address
->m_family
= GSOCK_INET
;
918 address
->m_realfamily
= PF_INET
;
919 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
920 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
925 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
928 struct in_addr
*addr
;
930 assert(address
!= NULL
);
932 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
934 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
936 /* If it is a numeric host name, convert it now */
937 if (inet_aton(hostname
, addr
) == 0) {
938 struct in_addr
*array_addr
;
940 /* It is a real name, we solve it */
941 if ((he
= gethostbyname(hostname
)) == NULL
) {
942 address
->m_error
= GSOCK_NOHOST
;
945 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
946 addr
->s_addr
= array_addr
[0].s_addr
;
948 return GSOCK_NOERROR
;
951 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
952 unsigned long hostaddr
)
954 struct in_addr
*addr
;
956 assert(address
!= NULL
);
958 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
960 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
961 addr
->s_addr
= hostaddr
;
963 return GSOCK_NOERROR
;
966 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
967 const char *protocol
)
970 struct sockaddr_in
*addr
;
972 assert(address
!= NULL
);
973 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
976 address
->m_error
= GSOCK_INVPORT
;
980 se
= getservbyname(port
, protocol
);
982 if (isdigit(port
[0])) {
985 port_int
= atoi(port
);
986 addr
= (struct sockaddr_in
*)address
->m_addr
;
987 addr
->sin_port
= htons(port_int
);
988 return GSOCK_NOERROR
;
991 address
->m_error
= GSOCK_INVPORT
;
992 return GSOCK_INVPORT
;
995 addr
= (struct sockaddr_in
*)address
->m_addr
;
996 addr
->sin_port
= se
->s_port
;
998 return GSOCK_NOERROR
;
1001 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1003 struct sockaddr_in
*addr
;
1005 assert(address
!= NULL
);
1006 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1008 addr
= (struct sockaddr_in
*)address
->m_addr
;
1009 addr
->sin_port
= htons(port
);
1011 return GSOCK_NOERROR
;
1014 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1018 struct sockaddr_in
*addr
;
1020 assert(address
!= NULL
);
1021 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1023 addr
= (struct sockaddr_in
*)address
->m_addr
;
1024 addr_buf
= (char *)&(addr
->sin_addr
);
1026 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1028 address
->m_error
= GSOCK_NOHOST
;
1029 return GSOCK_NOHOST
;
1032 strncpy(hostname
, he
->h_name
, sbuf
);
1034 return GSOCK_NOERROR
;
1037 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1039 struct sockaddr_in
*addr
;
1041 assert(address
!= NULL
);
1042 CHECK_ADDRESS(address
, INET
, 0);
1044 addr
= (struct sockaddr_in
*)address
->m_addr
;
1046 return addr
->sin_addr
.s_addr
;
1049 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1051 struct sockaddr_in
*addr
;
1053 assert(address
!= NULL
);
1054 CHECK_ADDRESS(address
, INET
, 0);
1056 addr
= (struct sockaddr_in
*)address
->m_addr
;
1057 return ntohs(addr
->sin_port
);
1061 * -------------------------------------------------------------------------
1062 * Unix address family
1063 * -------------------------------------------------------------------------
1066 bool _GAddress_Init_UNIX(GAddress
*address
)
1068 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1069 if (address
->m_addr
== NULL
)
1072 address
->m_len
= sizeof(struct sockaddr_un
);
1073 address
->m_family
= GSOCK_UNIX
;
1074 address
->m_realfamily
= PF_UNIX
;
1075 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1076 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1081 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1083 struct sockaddr_un
*addr
;
1085 assert(address
!= NULL
);
1087 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1089 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1090 memcpy(addr
->sun_path
, path
, strlen(path
));
1092 return GSOCK_NOERROR
;
1095 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1097 struct sockaddr_un
*addr
;
1099 assert(address
!= NULL
);
1100 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1102 addr
= (struct sockaddr_un
*)address
->m_addr
;
1104 strncpy(path
, addr
->sun_path
, sbuf
);
1106 return GSOCK_NOERROR
;
1109 #endif // wxUSE_SOCKETS