]>
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_cbacks
[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_non_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 /* Only oriented connection should be shutdowned */
143 if (socket
->m_oriented
)
144 shutdown(socket
->m_fd
, 2);
149 /* We also disable GUI callbacks */
150 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
151 _GSocket_Uninstall_Callback(socket
, evt
);
154 /* Address handling */
156 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
158 assert(socket
!= NULL
);
160 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
161 socket
->m_error
= GSOCK_INVSOCK
;
162 return GSOCK_INVSOCK
;
165 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
166 socket
->m_error
= GSOCK_INVADDR
;
167 return GSOCK_INVADDR
;
171 GAddress_destroy(socket
->m_local
);
173 socket
->m_local
= GAddress_copy(address
);
175 return GSOCK_NOERROR
;
178 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
180 assert(socket
!= NULL
);
182 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
183 socket
->m_error
= GSOCK_INVADDR
;
184 return GSOCK_INVADDR
;
188 GAddress_destroy(socket
->m_peer
);
190 socket
->m_peer
= GAddress_copy(address
);
192 return GSOCK_NOERROR
;
195 GAddress
*GSocket_GetLocal(GSocket
*socket
)
198 struct sockaddr addr
;
201 assert(socket
!= NULL
);
204 return GAddress_copy(socket
->m_local
);
206 if (socket
->m_fd
== -1) {
207 socket
->m_error
= GSOCK_INVSOCK
;
213 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
214 socket
->m_error
= GSOCK_IOERR
;
218 address
= GAddress_new();
219 if (address
== NULL
) {
220 socket
->m_error
= GSOCK_MEMERR
;
223 if (_GAddress_translate_from(address
, &addr
, size
) != GSOCK_NOERROR
) {
224 GAddress_destroy(address
);
231 GAddress
*GSocket_GetPeer(GSocket
*socket
)
233 assert(socket
!= NULL
);
236 return GAddress_copy(socket
->m_peer
);
241 /* Server specific parts */
244 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
245 of GSocket. "Local" must be set by GSocket_SetLocal() before
246 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
247 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
248 In the other cases it returns GSOCK_IOERR.
250 GSocketError
GSocket_SetServer(GSocket
*sck
)
256 if (sck
->m_fd
!= -1) {
257 sck
->m_error
= GSOCK_INVSOCK
;
258 return GSOCK_INVSOCK
;
262 sck
->m_error
= GSOCK_INVADDR
;
263 return GSOCK_INVADDR
;
266 /* We always have a stream here */
267 sck
->m_stream
= TRUE
;
268 sck
->m_server
= TRUE
;
270 /* Create the socket */
271 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
273 if (sck
->m_fd
== -1) {
274 sck
->m_error
= GSOCK_IOERR
;
278 /* Bind the socket to the LOCAL address */
279 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
282 sck
->m_error
= GSOCK_IOERR
;
286 /* Enable listening up to 5 connections */
287 if (listen(sck
->m_fd
, 5) < 0) {
290 sck
->m_error
= GSOCK_IOERR
;
294 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
295 GSocket_SetTimeout(sck
, sck
->m_timeout
);
297 return GSOCK_NOERROR
;
301 GSocket_WaitConnection() waits for an incoming client connection.
303 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
307 assert(socket
!= NULL
);
309 /* If the socket has already been created, we exit immediately */
310 if (socket
->m_fd
== -1 || !socket
->m_server
) {
311 socket
->m_error
= GSOCK_INVSOCK
;
315 /* Reenable GSOCK_CONNECTION event */
316 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
318 /* Create a GSocket object for the new connection */
319 connection
= GSocket_new();
321 /* Accept the incoming connection */
322 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
323 if (connection
->m_fd
== -1) {
324 GSocket_destroy(connection
);
325 socket
->m_error
= GSOCK_IOERR
;
329 /* Initialize all fields */
330 connection
->m_stream
= TRUE
;
331 connection
->m_server
= FALSE
;
332 connection
->m_oriented
= TRUE
;
337 /* Non oriented connections */
339 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
343 if (sck
->m_fd
!= -1) {
344 sck
->m_error
= GSOCK_INVSOCK
;
345 return GSOCK_INVSOCK
;
349 sck
->m_error
= GSOCK_INVADDR
;
350 return GSOCK_INVADDR
;
353 sck
->m_stream
= FALSE
;
354 sck
->m_server
= FALSE
;
355 sck
->m_oriented
= FALSE
;
357 /* Create the socket */
358 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
360 /* Bind it to the LOCAL address */
361 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
364 sck
->m_error
= GSOCK_IOERR
;
368 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
369 GSocket_SetTimeout(sck
, sck
->m_timeout
);
371 return GSOCK_NOERROR
;
374 /* Client specific parts */
377 GSocket_Connect() establishes a client connection to a server using the "Peer"
378 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
379 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
381 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
387 if (sck
->m_fd
!= -1) {
388 sck
->m_error
= GSOCK_INVSOCK
;
389 return GSOCK_INVSOCK
;
393 sck
->m_error
= GSOCK_INVADDR
;
394 return GSOCK_INVADDR
;
397 /* Test whether we want the socket to be a stream (e.g. TCP) */
398 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
399 sck
->m_oriented
= TRUE
;
406 /* Create the socket */
407 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
409 if (sck
->m_fd
== -1) {
410 sck
->m_error
= GSOCK_IOERR
;
414 /* Connect it to the PEER address */
415 if (connect(sck
->m_fd
, sck
->m_peer
->m_addr
,
416 sck
->m_peer
->m_len
) != 0) {
419 sck
->m_error
= GSOCK_IOERR
;
423 /* It is not a server */
424 sck
->m_server
= FALSE
;
426 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
427 GSocket_SetTimeout(sck
, sck
->m_timeout
);
429 return GSOCK_NOERROR
;
434 /* Like recv(), send(), ... */
435 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
437 assert(socket
!= NULL
);
439 if (socket
->m_fd
== -1 || socket
->m_server
) {
440 socket
->m_error
= GSOCK_INVSOCK
;
444 /* Reenable GSOCK_INPUT event */
445 _GSocket_Enable(socket
, GSOCK_INPUT
);
447 if (socket
->m_stream
)
448 return _GSocket_Recv_Stream(socket
, buffer
, size
);
450 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
453 int GSocket_Write(GSocket
*socket
, const char *buffer
,
456 assert(socket
!= NULL
);
458 if (socket
->m_fd
== -1 || socket
->m_server
) {
459 socket
->m_error
= GSOCK_INVSOCK
;
463 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
465 if (socket
->m_stream
)
466 return _GSocket_Send_Stream(socket
, buffer
, size
);
468 return _GSocket_Send_Dgram(socket
, buffer
, size
);
471 bool GSocket_DataAvailable(GSocket
*socket
)
476 assert(socket
!= NULL
);
478 if (socket
->m_fd
== -1 || socket
->m_server
) {
479 socket
->m_error
= GSOCK_INVSOCK
;
484 FD_SET(socket
->m_fd
, &read_set
);
489 select(socket
->m_fd
+1, &read_set
, NULL
, NULL
, &tv
);
491 return FD_ISSET(socket
->m_fd
, &read_set
);
497 GSocket_SetNonBlocking() puts the socket in non-blocking mode. This is useful
498 if we don't want to wait.
500 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
502 assert(socket
!= NULL
);
504 socket
->m_non_blocking
= non_block
;
506 if (socket
->m_fd
!= -1)
507 ioctl(socket
->m_fd
, FIONBIO
, &non_block
);
511 * GSocket_SetTimeout()
514 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
516 assert(socket
!= NULL
);
518 socket
->m_timeout
= millisec
;
519 /* Neither GLIBC 2.0 nor the kernel 2.0.36 define SO_SNDTIMEO or
520 SO_RCVTIMEO. The man pages, that these flags should exist but
521 are read only. RR. */
522 /* OK, restrict this to GLIBC 2.1. GL. */
523 /* Anyway, they seem to pose problems: I need to know the socket level and
524 it varies (may be SOL_TCP, SOL_UDP, ...). I disables this and use the
525 other solution. GL. */
527 #ifdef CAN_USE_TIMEOUT
528 if (socket
->m_fd
!= -1) {
531 tval
.tv_sec
= millisec
/ 1000;
532 tval
.tv_usec
= (millisec
% 1000) * 1000;
533 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tval
, sizeof(tval
));
534 setsockopt(socket
->m_fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tval
, sizeof(tval
));
541 GSocket_GetError() returns the last error occured on the socket stream.
544 GSocketError
GSocket_GetError(GSocket
*socket
)
546 assert(socket
!= NULL
);
548 return socket
->m_error
;
554 Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION)
555 INPUT: The function is called when there is at least a byte in the
557 OUTPUT: The function is called when the system is sure the next write call
559 CONNECTION: Two cases is possible:
560 Client socket -> the connection is established
561 Server socket -> a client request a connection
562 LOST: the connection is lost
564 SetCallback accepts a combination of these flags so a same callback can
565 receive different events.
567 An event is generated only once and its state is reseted when the relative
568 IO call is requested.
569 For example: INPUT -> GSocket_Read()
570 CONNECTION -> GSocket_Accept()
572 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
573 GSocketCallback callback
, char *cdata
)
577 assert (socket
!= NULL
);
579 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
580 /* We test each flag and, if it is enabled, we enable the corresponding
582 if ((event
& (1 << count
)) != 0) {
583 socket
->m_cbacks
[count
] = callback
;
584 socket
->m_data
[count
] = cdata
;
585 _GSocket_Enable(socket
, count
);
591 UnsetCallback will disables all callbacks specified by "event".
592 NOTE: event may be a combination of flags
594 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
)
598 assert(socket
!= NULL
);
600 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
601 if ((event
& (1 << count
)) != 0) {
602 _GSocket_Disable(socket
, count
);
603 socket
->m_cbacks
[count
] = NULL
;
608 #define CALL_FALLBACK(socket, event) \
609 if (socket->m_iocalls[event] && \
610 socket->m_cbacks[event]) {\
611 _GSocket_Disable(socket, event); \
612 socket->m_cbacks[event](socket, event, \
613 socket->m_data[event]); \
616 #define MASK_SIGNAL() \
618 void (*old_handler)(int); \
620 old_handler = signal(SIGPIPE, SIG_IGN);
622 #define UNMASK_SIGNAL() \
623 signal(SIGPIPE, old_handler); \
626 #define ENABLE_TIMEOUT(socket) \
628 struct itimerval old_ival, new_ival; \
629 void (*old_timer_sig)(int); \
631 old_timer_sig = signal(SIGALRM, SIG_DFL); \
632 siginterrupt(SIGALRM, 1); \
633 new_ival.it_value.tv_sec = socket->m_timeout / 1000; \
634 new_ival.it_value.tv_usec = (socket->m_timeout % 1000) * 1000; \
635 new_ival.it_interval.tv_sec = 0; \
636 new_ival.it_interval.tv_usec = 0; \
637 setitimer(ITIMER_REAL, &new_ival, &old_ival);
639 #define DISABLE_TIMEOUT(socket) \
640 signal(SIGALRM, old_timer_sig); \
641 siginterrupt(SIGALRM, 0); \
642 setitimer(ITIMER_REAL, &old_ival, NULL); \
645 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
647 socket
->m_iocalls
[event
] = TRUE
;
648 if (socket
->m_cbacks
[event
])
649 _GSocket_Install_Callback(socket
, event
);
652 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
654 socket
->m_iocalls
[event
] = FALSE
;
655 if (socket
->m_cbacks
[event
])
656 _GSocket_Uninstall_Callback(socket
, event
);
659 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
664 ENABLE_TIMEOUT(socket
);
665 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
666 DISABLE_TIMEOUT(socket
);
669 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
670 socket
->m_error
= GSOCK_IOERR
;
673 if (errno
== EWOULDBLOCK
) {
674 socket
->m_error
= GSOCK_WOULDBLOCK
;
680 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
682 struct sockaddr from
;
686 fromlen
= sizeof(from
);
689 ENABLE_TIMEOUT(socket
);
690 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
691 DISABLE_TIMEOUT(socket
);
693 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
694 socket
->m_error
= GSOCK_IOERR
;
697 if (errno
== EWOULDBLOCK
) {
698 socket
->m_error
= GSOCK_WOULDBLOCK
;
702 /* Translate a system address into a GSocket address */
703 if (!socket
->m_peer
) {
704 socket
->m_peer
= GAddress_new();
705 if (!socket
->m_peer
) {
706 socket
->m_error
= GSOCK_MEMERR
;
710 if (_GAddress_translate_from(socket
->m_peer
, &from
, fromlen
) != GSOCK_NOERROR
)
716 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
721 ENABLE_TIMEOUT(socket
);
722 ret
= send(socket
->m_fd
, buffer
, size
, 0);
723 DISABLE_TIMEOUT(socket
);
725 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
726 socket
->m_error
= GSOCK_IOERR
;
729 if (errno
== EWOULDBLOCK
) {
730 socket
->m_error
= GSOCK_WOULDBLOCK
;
736 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
738 struct sockaddr
*addr
;
741 if (!socket
->m_peer
) {
742 socket
->m_error
= GSOCK_INVADDR
;
746 if (_GAddress_translate_to(socket
->m_peer
, &addr
, &len
) != GSOCK_NOERROR
) {
751 ENABLE_TIMEOUT(socket
);
752 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
753 DISABLE_TIMEOUT(socket
);
756 /* Frees memory allocated from _GAddress_translate_to */
759 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
760 socket
->m_error
= GSOCK_IOERR
;
763 if (errno
== EWOULDBLOCK
) {
764 socket
->m_error
= GSOCK_WOULDBLOCK
;
771 void _GSocket_Detected_Read(GSocket
*socket
)
776 if (socket
->m_stream
) {
777 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
779 if (ret
< 0 && socket
->m_server
) {
780 CALL_FALLBACK(socket
, GSOCK_CONNECTION
);
785 CALL_FALLBACK(socket
, GSOCK_INPUT
);
787 CALL_FALLBACK(socket
, GSOCK_LOST
);
792 void _GSocket_Detected_Write(GSocket
*socket
)
794 CALL_FALLBACK(socket
, GSOCK_OUTPUT
);
802 * -------------------------------------------------------------------------
804 * -------------------------------------------------------------------------
807 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
808 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
809 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
811 #define CHECK_ADDRESS(address, family, retval) \
813 if (address->m_family == GSOCK_NOFAMILY) \
814 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
815 return address->m_error; \
817 if (address->m_family != GSOCK_##family) {\
818 address->m_error = GSOCK_INVADDR; \
823 GAddress
*GAddress_new()
827 address
= (GAddress
*)malloc(sizeof(GAddress
));
832 address
->m_family
= GSOCK_NOFAMILY
;
833 address
->m_addr
= NULL
;
839 GAddress
*GAddress_copy(GAddress
*address
)
843 assert(address
!= NULL
);
845 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
850 memcpy(addr2
, address
, sizeof(GAddress
));
852 if (address
->m_addr
) {
853 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
854 if (addr2
->m_addr
== NULL
) {
858 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
864 void GAddress_destroy(GAddress
*address
)
866 assert(address
!= NULL
);
871 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
873 assert(address
!= NULL
);
875 address
->m_family
= type
;
878 GAddressType
GAddress_GetFamily(GAddress
*address
)
880 assert(address
!= NULL
);
882 return address
->m_family
;
885 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
886 address
->m_realfamily
= addr
->sa_family
;
887 switch (addr
->sa_family
) {
889 address
->m_family
= GSOCK_INET
;
892 address
->m_family
= GSOCK_UNIX
;
896 address
->m_family
= GSOCK_INET6
;
901 address
->m_error
= GSOCK_INVOP
;
907 free(address
->m_addr
);
909 address
->m_len
= len
;
910 address
->m_addr
= (struct sockaddr
*)malloc(len
);
911 if (address
->m_addr
== NULL
) {
912 address
->m_error
= GSOCK_MEMERR
;
915 memcpy(address
->m_addr
, addr
, len
);
917 return GSOCK_NOERROR
;
920 GSocketError
_GAddress_translate_to(GAddress
*address
,
921 struct sockaddr
**addr
, int *len
)
923 if (!address
->m_addr
) {
924 address
->m_error
= GSOCK_INVADDR
;
925 return GSOCK_INVADDR
;
928 *len
= address
->m_len
;
929 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
931 address
->m_error
= GSOCK_MEMERR
;
935 memcpy(*addr
, address
->m_addr
, address
->m_len
);
936 return GSOCK_NOERROR
;
940 * -------------------------------------------------------------------------
941 * Internet address family
942 * -------------------------------------------------------------------------
945 GSocketError
_GAddress_Init_INET(GAddress
*address
)
947 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
948 if (address
->m_addr
== NULL
) {
949 address
->m_error
= GSOCK_MEMERR
;
953 address
->m_len
= sizeof(struct sockaddr_in
);
955 address
->m_family
= GSOCK_INET
;
956 address
->m_realfamily
= PF_INET
;
957 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
958 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
960 return GSOCK_NOERROR
;
963 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
966 struct in_addr
*addr
;
968 assert(address
!= NULL
);
970 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
972 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
974 /* If it is a numeric host name, convert it now */
975 #if defined(HAVE_INET_ATON)
976 if (inet_aton(hostname
, addr
) == 0) {
977 #elif defined(HAVE_INET_ADDR)
978 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
979 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
981 /* Use gethostbyname by default */
984 struct in_addr
*array_addr
;
986 /* It is a real name, we solve it */
987 if ((he
= gethostbyname(hostname
)) == NULL
) {
988 address
->m_error
= GSOCK_NOHOST
;
991 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
992 addr
->s_addr
= array_addr
[0].s_addr
;
994 return GSOCK_NOERROR
;
997 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
998 unsigned long hostaddr
)
1000 struct in_addr
*addr
;
1002 assert(address
!= NULL
);
1004 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1006 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1007 addr
->s_addr
= hostaddr
;
1009 return GSOCK_NOERROR
;
1012 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1013 const char *protocol
)
1016 struct sockaddr_in
*addr
;
1018 assert(address
!= NULL
);
1019 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1022 address
->m_error
= GSOCK_INVPORT
;
1023 return GSOCK_INVPORT
;
1026 se
= getservbyname(port
, protocol
);
1028 if (isdigit(port
[0])) {
1031 port_int
= atoi(port
);
1032 addr
= (struct sockaddr_in
*)address
->m_addr
;
1033 addr
->sin_port
= htons(port_int
);
1034 return GSOCK_NOERROR
;
1037 address
->m_error
= GSOCK_INVPORT
;
1038 return GSOCK_INVPORT
;
1041 addr
= (struct sockaddr_in
*)address
->m_addr
;
1042 addr
->sin_port
= se
->s_port
;
1044 return GSOCK_NOERROR
;
1047 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1049 struct sockaddr_in
*addr
;
1051 assert(address
!= NULL
);
1052 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1054 addr
= (struct sockaddr_in
*)address
->m_addr
;
1055 addr
->sin_port
= htons(port
);
1057 return GSOCK_NOERROR
;
1060 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1064 struct sockaddr_in
*addr
;
1066 assert(address
!= NULL
);
1067 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1069 addr
= (struct sockaddr_in
*)address
->m_addr
;
1070 addr_buf
= (char *)&(addr
->sin_addr
);
1072 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1074 address
->m_error
= GSOCK_NOHOST
;
1075 return GSOCK_NOHOST
;
1078 strncpy(hostname
, he
->h_name
, sbuf
);
1080 return GSOCK_NOERROR
;
1083 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1085 struct sockaddr_in
*addr
;
1087 assert(address
!= NULL
);
1088 CHECK_ADDRESS(address
, INET
, 0);
1090 addr
= (struct sockaddr_in
*)address
->m_addr
;
1092 return addr
->sin_addr
.s_addr
;
1095 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1097 struct sockaddr_in
*addr
;
1099 assert(address
!= NULL
);
1100 CHECK_ADDRESS(address
, INET
, 0);
1102 addr
= (struct sockaddr_in
*)address
->m_addr
;
1103 return ntohs(addr
->sin_port
);
1107 * -------------------------------------------------------------------------
1108 * Unix address family
1109 * -------------------------------------------------------------------------
1112 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1114 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1115 if (address
->m_addr
== NULL
) {
1116 address
->m_error
= GSOCK_MEMERR
;
1117 return GSOCK_MEMERR
;
1120 address
->m_len
= sizeof(struct sockaddr_un
);
1121 address
->m_family
= GSOCK_UNIX
;
1122 address
->m_realfamily
= PF_UNIX
;
1123 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1124 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1129 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1131 struct sockaddr_un
*addr
;
1133 assert(address
!= NULL
);
1135 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1137 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1138 memcpy(addr
->sun_path
, path
, strlen(path
));
1140 return GSOCK_NOERROR
;
1143 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1145 struct sockaddr_un
*addr
;
1147 assert(address
!= NULL
);
1148 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1150 addr
= (struct sockaddr_un
*)address
->m_addr
;
1152 strncpy(path
, addr
->sun_path
, sbuf
);
1154 return GSOCK_NOERROR
;