]>
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 #if !defined(__LINUX__) && !defined(__FREEBSD__)
61 # define CAN_USE_TIMEOUT
62 #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__)
63 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 1)
64 # define CAN_USE_TIMEOUT
68 /* Global initialisers */
75 void GSocket_Cleanup()
79 /* Constructors / Destructors */
81 GSocket
*GSocket_new()
86 socket
= (GSocket
*)malloc(sizeof(GSocket
));
92 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
93 socket
->m_fbacks
[i
] = NULL
;
94 socket
->m_iocalls
[i
] = FALSE
;
96 socket
->m_local
= NULL
;
97 socket
->m_peer
= NULL
;
98 socket
->m_error
= GSOCK_NOERROR
;
99 socket
->m_server
= FALSE
;
100 socket
->m_stream
= TRUE
;
101 socket
->m_gui_dependent
= NULL
;
102 socket
->m_blocking
= FALSE
;
103 socket
->m_timeout
= 10*60*1000;
104 // 10 minutes * 60 sec * 1000 millisec
106 /* We initialize the GUI specific entries here */
107 _GSocket_GUI_Init(socket
);
112 void GSocket_destroy(GSocket
*socket
)
114 assert(socket
!= NULL
);
116 /* First, we check that the socket is really shutdowned */
117 if (socket
->m_fd
!= -1)
118 GSocket_Shutdown(socket
);
120 /* We destroy GUI specific variables */
121 _GSocket_GUI_Destroy(socket
);
123 /* We destroy private addresses */
125 GAddress_destroy(socket
->m_local
);
128 GAddress_destroy(socket
->m_peer
);
130 /* We destroy socket itself */
134 void GSocket_Shutdown(GSocket
*socket
)
138 assert(socket
!= NULL
);
140 /* If socket has been created, we shutdown it */
141 if (socket
->m_fd
!= -1) {
142 shutdown(socket
->m_fd
, 2);
147 /* We also disable GUI callbacks */
148 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
149 _GSocket_Uninstall_Callback(socket
, evt
);
152 /* Address handling */
154 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
156 assert(socket
!= NULL
);
158 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
159 socket
->m_error
= GSOCK_INVSOCK
;
160 return GSOCK_INVSOCK
;
163 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
164 socket
->m_error
= GSOCK_INVADDR
;
165 return GSOCK_INVADDR
;
169 GAddress_destroy(socket
->m_local
);
171 socket
->m_local
= GAddress_copy(address
);
173 return GSOCK_NOERROR
;
176 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
178 assert(socket
!= NULL
);
180 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
181 socket
->m_error
= GSOCK_INVADDR
;
182 return GSOCK_INVADDR
;
186 GAddress_destroy(socket
->m_peer
);
188 socket
->m_peer
= GAddress_copy(address
);
190 return GSOCK_NOERROR
;
193 GAddress
*GSocket_GetLocal(GSocket
*socket
)
196 struct sockaddr addr
;
199 assert(socket
!= NULL
);
202 return GAddress_copy(socket
->m_local
);
204 if (socket
->m_fd
== -1) {
205 socket
->m_error
= GSOCK_INVSOCK
;
211 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
212 socket
->m_error
= GSOCK_IOERR
;
216 address
= GAddress_new();
217 if (address
== NULL
) {
218 socket
->m_error
= GSOCK_MEMERR
;
221 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
) {
222 GAddress_destroy(address
);
229 GAddress
*GSocket_GetPeer(GSocket
*socket
)
231 assert(socket
!= NULL
);
234 return GAddress_copy(socket
->m_peer
);
239 /* Server specific parts */
242 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
243 of GSocket. "Local" must be set by GSocket_SetLocal() before
244 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
245 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
246 In the other cases it returns GSOCK_IOERR.
248 GSocketError
GSocket_SetServer(GSocket
*sck
)
254 if (sck
->m_fd
!= -1) {
255 sck
->m_error
= GSOCK_INVSOCK
;
256 return GSOCK_INVSOCK
;
260 sck
->m_error
= GSOCK_INVADDR
;
261 return GSOCK_INVADDR
;
264 /* We always have a stream here */
265 sck
->m_stream
= TRUE
;
267 /* Create the socket */
268 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
270 if (sck
->m_fd
== -1) {
271 sck
->m_error
= GSOCK_IOERR
;
275 /* Bind the socket to the LOCAL address */
276 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
279 sck
->m_error
= GSOCK_IOERR
;
283 /* Enable listening up to 5 connections */
284 if (listen(sck
->m_fd
, 5) < 0) {
287 sck
->m_error
= GSOCK_IOERR
;
291 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
292 GSocket_SetTimeout(sck
, sck
->m_timeout
);
294 return GSOCK_NOERROR
;
298 GSocket_WaitConnection() waits for an incoming client connection.
300 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
304 assert(socket
!= NULL
);
306 /* If the socket has already been created, we exit immediately */
307 if (socket
->m_fd
== -1 || !socket
->m_server
) {
308 socket
->m_error
= GSOCK_INVSOCK
;
312 /* Reenable GSOCK_CONNECTION event */
313 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
315 /* Create a GSocket object for the new connection */
316 connection
= GSocket_new();
318 /* Accept the incoming connection */
319 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
320 if (connection
->m_fd
== -1) {
321 GSocket_destroy(connection
);
322 socket
->m_error
= GSOCK_IOERR
;
326 /* Initialize all fields */
327 connection
->m_stream
= TRUE
;
328 connection
->m_server
= FALSE
;
329 connection
->m_oriented
= TRUE
;
334 /* Non oriented connections */
336 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
340 if (sck
->m_fd
!= -1) {
341 sck
->m_error
= GSOCK_INVSOCK
;
342 return GSOCK_INVSOCK
;
346 sck
->m_error
= GSOCK_INVADDR
;
347 return GSOCK_INVADDR
;
350 sck
->m_stream
= FALSE
;
351 sck
->m_server
= FALSE
;
352 sck
->m_oriented
= FALSE
;
354 /* Create the socket */
355 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
357 /* Bind it to the LOCAL address */
358 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
361 sck
->m_error
= GSOCK_IOERR
;
365 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
366 GSocket_SetTimeout(sck
, sck
->m_timeout
);
368 return GSOCK_NOERROR
;
371 /* Client specific parts */
374 GSocket_Connect() establishes a client connection to a server using the "Peer"
375 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
376 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
378 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
384 if (sck
->m_fd
!= -1) {
385 sck
->m_error
= GSOCK_INVSOCK
;
386 return GSOCK_INVSOCK
;
390 sck
->m_error
= GSOCK_INVADDR
;
391 return GSOCK_INVADDR
;
394 /* Test whether we want the socket to be a stream (e.g. TCP) */
395 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
396 sck
->m_oriented
= TRUE
;
403 /* Create the socket */
404 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
406 if (sck
->m_fd
== -1) {
407 sck
->m_error
= GSOCK_IOERR
;
411 /* Connect it to the PEER address */
412 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
413 sck
->m_peer
->m_len
) != 0) {
416 sck
->m_error
= GSOCK_IOERR
;
420 /* It is not a server */
421 sck
->m_server
= FALSE
;
423 GSocket_SetNonBlocking(sck
, sck
->m_blocking
);
424 GSocket_SetTimeout(sck
, sck
->m_timeout
);
426 return GSOCK_NOERROR
;
431 /* Like recv(), send(), ... */
432 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
434 assert(socket
!= NULL
);
436 if (socket
->m_fd
== -1 || socket
->m_server
) {
437 socket
->m_error
= GSOCK_INVSOCK
;
441 /* Reenable GSOCK_INPUT event */
442 _GSocket_Enable(socket
, GSOCK_INPUT
);
444 if (socket
->m_stream
)
445 return _GSocket_Recv_Stream(socket
, buffer
, size
);
447 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
450 int GSocket_Write(GSocket
*socket
, const char *buffer
,
453 assert(socket
!= NULL
);
455 if (socket
->m_fd
== -1 || socket
->m_server
) {
456 socket
->m_error
= GSOCK_INVSOCK
;
460 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
462 if (socket
->m_stream
)
463 return _GSocket_Send_Stream(socket
, buffer
, size
);
465 return _GSocket_Send_Dgram(socket
, buffer
, size
);
468 bool GSocket_DataAvailable(GSocket
*socket
)
473 assert(socket
!= NULL
);
475 if (socket
->m_fd
== -1 || socket
->m_server
) {
476 socket
->m_error
= GSOCK_INVSOCK
;
481 FD_SET(socket
->m_fd
, &read_set
);
486 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
488 return FD_ISSET(socket
->m_fd
, &read_set
);
494 GSocket_SetNonBlocking() puts the socket in non-blocking mode. This is useful
495 if we don't want to wait.
497 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
499 assert(socket
!= NULL
);
501 socket
->m_blocking
= non_block
;
503 if (socket
->m_fd
!= -1)
504 ioctl(socket
->m_fd
, FIONBIO
, &non_block
);
508 * GSocket_SetTimeout()
511 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
513 assert(socket
!= NULL
);
515 socket
->m_timeout
= millisec
;
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 if (socket
->m_fd
!= -1) {
524 tval
.tv_sec
= millisec
/ 1000;
525 tval
.tv_usec
= (millisec
% 1000) * 1000;
526 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tval
, sizeof(tval
));
527 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tval
, sizeof(tval
));
533 GSocket_GetError() returns the last error occured on the socket stream.
536 GSocketError
GSocket_GetError(GSocket
*socket
)
538 assert(socket
!= NULL
);
540 return socket
->m_error
;
546 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION)
547 INPUT: The function is called when there is at least a byte in the
549 OUTPUT: The function is called when the system is sure the next write call
551 CONNECTION: Two cases is possible:
552 Client socket -> the connection is established
553 Server socket -> a client request a connection
554 LOST: the connection is lost
556 SetCallback accepts a combination of these flags so a same callback can
557 receive different events.
559 An event is generated only once and its state is reseted when the relative
560 IO call is requested.
561 For example: INPUT -> GSocket_Read()
562 CONNECTION -> GSocket_Accept()
564 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
565 GSocketCallback fallback
, char *cdata
)
569 assert (socket
!= NULL
);
571 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
572 /* We test each flag and, if it is enabled, we enable the corresponding
574 if ((event
& (1 << count
)) != 0) {
575 socket
->m_fbacks
[count
] = fallback
;
576 socket
->m_data
[count
] = cdata
;
577 _GSocket_Enable(socket
, count
);
583 UnsetCallback will disables all fallbacks specified by "event".
584 NOTE: event may be a combination of flags
586 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
590 assert(socket
!= NULL
);
592 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
593 if ((event
& (1 << count
)) != 0) {
594 _GSocket_Disable(socket
, count
);
595 socket
->m_fbacks
[count
] = NULL
;
600 #define CALL_FALLBACK(socket, event) \
601 if (socket->m_iocalls[event] && \
602 socket->m_fbacks[event]) {\
603 _GSocket_Disable(socket, event); \
604 socket->m_fbacks[event](socket, event, \
605 socket->m_data[event]); \
608 #define MASK_SIGNAL() \
610 void (*old_handler)(int); \
612 old_handler = signal(SIGPIPE, SIG_IGN);
614 #define UNMASK_SIGNAL() \
615 signal(SIGPIPE, old_handler); \
619 #ifndef CAN_USE_TIMEOUT
621 #define ENABLE_TIMEOUT(socket) \
623 struct itimerval old_ival, new_ival; \
624 void (*old_timer_sig)(int); \
626 new_ival.it_interval.tv_sec = socket->m_timeout / 1000; \
627 new_ival.it_interval.tv_usec = (socket->m_timeout % 1000) * 1000; \
628 setitimer(ITIMER_REAL, &new_ival, &old_ival); \
629 old_timer_sig = signal(SIGALRM, SIG_DFL);
631 #define DISABLE_TIMEOUT(socket) \
632 signal(SIGALRM, old_timer_sig); \
633 setitimer(ITIMER_REAL, &old_ival, NULL); \
638 #define ENABLE_TIMEOUT(s)
639 #define DISABLE_TIMEOUT(s)
646 #define ENABLE_TIMEOUT(s)
647 #define DISABLE_TIMEOUT(s)
649 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
651 socket
->m_iocalls
[event
] = TRUE
;
652 if (socket
->m_fbacks
[event
])
653 _GSocket_Install_Callback(socket
, event
);
656 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
658 socket
->m_iocalls
[event
] = FALSE
;
659 if (socket
->m_fbacks
[event
])
660 _GSocket_Uninstall_Callback(socket
, event
);
663 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
668 ENABLE_TIMEOUT(socket
);
669 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
670 DISABLE_TIMEOUT(socket
);
673 if (ret
== -1 && errno
!= EAGAIN
) {
674 socket
->m_error
= GSOCK_IOERR
;
677 if (errno
== EAGAIN
) {
678 socket
->m_error
= GSOCK_TRYAGAIN
;
684 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
686 struct sockaddr from
;
690 fromlen
= sizeof(from
);
693 ENABLE_TIMEOUT(socket
);
694 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
695 DISABLE_TIMEOUT(socket
);
697 if (ret
== -1 && errno
!= EAGAIN
) {
698 socket
->m_error
= GSOCK_IOERR
;
701 if (errno
== EAGAIN
) {
702 socket
->m_error
= GSOCK_TRYAGAIN
;
706 /* Translate a system address into a GSocket address */
707 if (!socket
->m_peer
) {
708 socket
->m_peer
= GAddress_new();
709 if (!socket
->m_peer
) {
710 socket
->m_error
= GSOCK_MEMERR
;
714 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
720 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
725 ENABLE_TIMEOUT(socket
);
726 ret
= send(socket
->m_fd
, buffer
, size
, 0);
727 DISABLE_TIMEOUT(socket
);
729 if (ret
== -1 && errno
!= EAGAIN
) {
730 socket
->m_error
= GSOCK_IOERR
;
733 if (errno
== EAGAIN
) {
734 socket
->m_error
= GSOCK_TRYAGAIN
;
740 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
742 struct sockaddr
*addr
;
745 if (!socket
->m_peer
) {
746 socket
->m_error
= GSOCK_INVADDR
;
750 if (_GAddress_translate_to(socket
->m_peer
, &addr
, &len
) != GSOCK_NOERROR
) {
755 ENABLE_TIMEOUT(socket
);
756 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
757 DISABLE_TIMEOUT(socket
);
760 /* Frees memory allocated from _GAddress_translate_to */
763 if (ret
== -1 && errno
!= EAGAIN
) {
764 socket
->m_error
= GSOCK_IOERR
;
767 if (errno
== EAGAIN
) {
768 socket
->m_error
= GSOCK_TRYAGAIN
;
775 void _GSocket_Detected_Read(GSocket
*socket
)
780 if (socket
->m_stream
) {
781 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
783 if (ret
< 0 && socket
->m_server
) {
784 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
789 CALL_FALLBACK(socket
, GSOCK_INPUT
);
791 CALL_FALLBACK(socket
, GSOCK_LOST
);
796 void _GSocket_Detected_Write(GSocket
*socket
)
798 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
806 * -------------------------------------------------------------------------
808 * -------------------------------------------------------------------------
811 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
812 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
813 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
815 #define CHECK_ADDRESS(address, family, retval) \
817 if (address->m_family == GSOCK_NOFAMILY) \
818 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
819 return address->m_error; \
821 if (address->m_family != GSOCK_##family) {\
822 address->m_error = GSOCK_INVADDR; \
827 GAddress
*GAddress_new()
831 address
= (GAddress
*)malloc(sizeof(GAddress
));
836 address
->m_family
= GSOCK_NOFAMILY
;
837 address
->m_addr
= NULL
;
843 GAddress
*GAddress_copy(GAddress
*address
)
847 assert(address
!= NULL
);
849 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
854 memcpy(addr2
, address
, sizeof(GAddress
));
856 if (address
->m_addr
) {
857 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
858 if (addr2
->m_addr
== NULL
) {
862 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
868 void GAddress_destroy(GAddress
*address
)
870 assert(address
!= NULL
);
875 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
877 assert(address
!= NULL
);
879 address
->m_family
= type
;
882 GAddressType
GAddress_GetFamily(GAddress
*address
)
884 assert(address
!= NULL
);
886 return address
->m_family
;
889 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
890 address
->m_realfamily
= addr
->sa_family
;
891 switch (addr
->sa_family
) {
893 address
->m_family
= GSOCK_INET
;
896 address
->m_family
= GSOCK_UNIX
;
900 address
->m_family
= GSOCK_INET6
;
905 address
->m_error
= GSOCK_INVOP
;
911 free(address
->m_addr
);
913 address
->m_len
= len
;
914 address
->m_addr
= (struct sockaddr
*)malloc(len
);
915 if (address
->m_addr
== NULL
) {
916 address
->m_error
= GSOCK_MEMERR
;
919 memcpy(address
->m_addr
, addr
, len
);
921 return GSOCK_NOERROR
;
924 GSocketError
_GAddress_translate_to(GAddress
*address
,
925 struct sockaddr
**addr
, int *len
)
927 if (!address
->m_addr
) {
928 address
->m_error
= GSOCK_INVADDR
;
929 return GSOCK_INVADDR
;
932 *len
= address
->m_len
;
933 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
935 address
->m_error
= GSOCK_MEMERR
;
939 memcpy(*addr
, address
->m_addr
, address
->m_len
);
940 return GSOCK_NOERROR
;
944 * -------------------------------------------------------------------------
945 * Internet address family
946 * -------------------------------------------------------------------------
949 GSocketError
_GAddress_Init_INET(GAddress
*address
)
951 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
952 if (address
->m_addr
== NULL
) {
953 address
->m_error
= GSOCK_MEMERR
;
957 address
->m_len
= sizeof(struct sockaddr_in
);
959 address
->m_family
= GSOCK_INET
;
960 address
->m_realfamily
= PF_INET
;
961 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
962 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
967 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
970 struct in_addr
*addr
;
972 assert(address
!= NULL
);
974 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
976 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
978 /* If it is a numeric host name, convert it now */
979 if (inet_aton(hostname
, addr
) == 0) {
980 struct in_addr
*array_addr
;
982 /* It is a real name, we solve it */
983 if ((he
= gethostbyname(hostname
)) == NULL
) {
984 address
->m_error
= GSOCK_NOHOST
;
987 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
988 addr
->s_addr
= array_addr
[0].s_addr
;
990 return GSOCK_NOERROR
;
993 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
994 unsigned long hostaddr
)
996 struct in_addr
*addr
;
998 assert(address
!= NULL
);
1000 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1002 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1003 addr
->s_addr
= hostaddr
;
1005 return GSOCK_NOERROR
;
1008 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1009 const char *protocol
)
1012 struct sockaddr_in
*addr
;
1014 assert(address
!= NULL
);
1015 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1018 address
->m_error
= GSOCK_INVPORT
;
1019 return GSOCK_INVPORT
;
1022 se
= getservbyname(port
, protocol
);
1024 if (isdigit(port
[0])) {
1027 port_int
= atoi(port
);
1028 addr
= (struct sockaddr_in
*)address
->m_addr
;
1029 addr
->sin_port
= htons(port_int
);
1030 return GSOCK_NOERROR
;
1033 address
->m_error
= GSOCK_INVPORT
;
1034 return GSOCK_INVPORT
;
1037 addr
= (struct sockaddr_in
*)address
->m_addr
;
1038 addr
->sin_port
= se
->s_port
;
1040 return GSOCK_NOERROR
;
1043 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1045 struct sockaddr_in
*addr
;
1047 assert(address
!= NULL
);
1048 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1050 addr
= (struct sockaddr_in
*)address
->m_addr
;
1051 addr
->sin_port
= htons(port
);
1053 return GSOCK_NOERROR
;
1056 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1060 struct sockaddr_in
*addr
;
1062 assert(address
!= NULL
);
1063 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1065 addr
= (struct sockaddr_in
*)address
->m_addr
;
1066 addr_buf
= (char *)&(addr
->sin_addr
);
1068 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1070 address
->m_error
= GSOCK_NOHOST
;
1071 return GSOCK_NOHOST
;
1074 strncpy(hostname
, he
->h_name
, sbuf
);
1076 return GSOCK_NOERROR
;
1079 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1081 struct sockaddr_in
*addr
;
1083 assert(address
!= NULL
);
1084 CHECK_ADDRESS(address
, INET
, 0);
1086 addr
= (struct sockaddr_in
*)address
->m_addr
;
1088 return addr
->sin_addr
.s_addr
;
1091 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1093 struct sockaddr_in
*addr
;
1095 assert(address
!= NULL
);
1096 CHECK_ADDRESS(address
, INET
, 0);
1098 addr
= (struct sockaddr_in
*)address
->m_addr
;
1099 return ntohs(addr
->sin_port
);
1103 * -------------------------------------------------------------------------
1104 * Unix address family
1105 * -------------------------------------------------------------------------
1108 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1110 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1111 if (address
->m_addr
== NULL
) {
1112 address
->m_error
= GSOCK_MEMERR
;
1113 return GSOCK_MEMERR
;
1116 address
->m_len
= sizeof(struct sockaddr_un
);
1117 address
->m_family
= GSOCK_UNIX
;
1118 address
->m_realfamily
= PF_UNIX
;
1119 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1120 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1125 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1127 struct sockaddr_un
*addr
;
1129 assert(address
!= NULL
);
1131 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1133 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1134 memcpy(addr
->sun_path
, path
, strlen(path
));
1136 return GSOCK_NOERROR
;
1139 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1141 struct sockaddr_un
*addr
;
1143 assert(address
!= NULL
);
1144 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1146 addr
= (struct sockaddr_un
*)address
->m_addr
;
1148 strncpy(path
, addr
->sun_path
, sbuf
);
1150 return GSOCK_NOERROR
;
1153 #endif // wxUSE_SOCKETS