]>
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"
46 #include "wx/unix/gsockunx.h"
52 # define SOCKLEN_T socklen_t
55 # define SOCKLEN_T int
60 #define MASK_SIGNAL() \
62 void (*old_handler)(int); \
64 old_handler = signal(SIGPIPE, SIG_IGN);
66 #define UNMASK_SIGNAL() \
67 signal(SIGPIPE, old_handler); \
70 #define ENABLE_TIMEOUT(socket) \
72 struct itimerval old_ival, new_ival; \
73 void (*old_timer_sig)(int); \
75 old_timer_sig = signal(SIGALRM, SIG_DFL); \
76 siginterrupt(SIGALRM, 1); \
77 new_ival.it_value.tv_sec = socket->m_timeout / 1000; \
78 new_ival.it_value.tv_usec = (socket->m_timeout % 1000) * 1000; \
79 new_ival.it_interval.tv_sec = 0; \
80 new_ival.it_interval.tv_usec = 0; \
81 setitimer(ITIMER_REAL, &new_ival, &old_ival);
83 #define DISABLE_TIMEOUT(socket) \
84 signal(SIGALRM, old_timer_sig); \
85 siginterrupt(SIGALRM, 0); \
86 setitimer(ITIMER_REAL, &old_ival, NULL); \
89 /* Global initialisers */
96 void GSocket_Cleanup()
100 /* Constructors / Destructors */
102 GSocket
*GSocket_new()
107 socket
= (GSocket
*)malloc(sizeof(GSocket
));
113 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++) {
114 socket
->m_cbacks
[i
] = NULL
;
115 socket
->m_iocalls
[i
] = FALSE
;
117 socket
->m_local
= NULL
;
118 socket
->m_peer
= NULL
;
119 socket
->m_error
= GSOCK_NOERROR
;
120 socket
->m_server
= FALSE
;
121 socket
->m_stream
= TRUE
;
122 socket
->m_gui_dependent
= NULL
;
123 socket
->m_non_blocking
= FALSE
;
124 socket
->m_timeout
= 10*60*1000;
125 /* 10 minutes * 60 sec * 1000 millisec */
126 socket
->m_establishing
= FALSE
;
128 /* We initialize the GUI specific entries here */
129 _GSocket_GUI_Init(socket
);
134 void GSocket_destroy(GSocket
*socket
)
136 assert(socket
!= NULL
);
138 /* First, we check that the socket is really shutdowned */
139 if (socket
->m_fd
!= -1)
140 GSocket_Shutdown(socket
);
142 /* We destroy GUI specific variables */
143 _GSocket_GUI_Destroy(socket
);
145 /* We destroy private addresses */
147 GAddress_destroy(socket
->m_local
);
150 GAddress_destroy(socket
->m_peer
);
152 /* We destroy socket itself */
156 void GSocket_Shutdown(GSocket
*socket
)
160 assert(socket
!= NULL
);
162 /* If socket has been created, we shutdown it */
163 if (socket
->m_fd
!= -1) {
164 shutdown(socket
->m_fd
, 2);
169 /* We also disable GUI callbacks */
170 for (evt
=0;evt
<GSOCK_MAX_EVENT
;evt
++)
171 _GSocket_Uninstall_Callback(socket
, evt
);
174 /* Address handling */
176 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
178 assert(socket
!= NULL
);
180 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
181 socket
->m_error
= GSOCK_INVSOCK
;
182 return GSOCK_INVSOCK
;
185 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
186 socket
->m_error
= GSOCK_INVADDR
;
187 return GSOCK_INVADDR
;
191 GAddress_destroy(socket
->m_local
);
193 socket
->m_local
= GAddress_copy(address
);
195 return GSOCK_NOERROR
;
198 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
200 assert(socket
!= NULL
);
202 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
203 socket
->m_error
= GSOCK_INVADDR
;
204 return GSOCK_INVADDR
;
208 GAddress_destroy(socket
->m_peer
);
210 socket
->m_peer
= GAddress_copy(address
);
212 return GSOCK_NOERROR
;
215 GAddress
*GSocket_GetLocal(GSocket
*socket
)
218 struct sockaddr addr
;
222 assert(socket
!= NULL
);
225 return GAddress_copy(socket
->m_local
);
227 if (socket
->m_fd
== -1) {
228 socket
->m_error
= GSOCK_INVSOCK
;
234 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
235 socket
->m_error
= GSOCK_IOERR
;
239 address
= GAddress_new();
240 if (address
== NULL
) {
241 socket
->m_error
= GSOCK_MEMERR
;
244 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
245 if (socket
->m_error
!= GSOCK_NOERROR
) {
246 GAddress_destroy(address
);
253 GAddress
*GSocket_GetPeer(GSocket
*socket
)
255 assert(socket
!= NULL
);
258 return GAddress_copy(socket
->m_peer
);
263 /* Server specific parts */
266 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
267 of GSocket. "Local" must be set by GSocket_SetLocal() before
268 GSocket_SetServer() is called. GSOCK_INVSOCK if socket has been initialized.
269 In case, you haven't yet defined the local address, it returns GSOCK_INVADDR.
270 In the other cases it returns GSOCK_IOERR.
272 GSocketError
GSocket_SetServer(GSocket
*sck
)
278 if (sck
->m_fd
!= -1) {
279 sck
->m_error
= GSOCK_INVSOCK
;
280 return GSOCK_INVSOCK
;
284 sck
->m_error
= GSOCK_INVADDR
;
285 return GSOCK_INVADDR
;
288 /* We always have a stream here */
289 sck
->m_stream
= TRUE
;
290 sck
->m_server
= TRUE
;
292 /* Create the socket */
293 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
295 if (sck
->m_fd
== -1) {
296 sck
->m_error
= GSOCK_IOERR
;
300 /* Bind the socket to the LOCAL address */
301 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
304 sck
->m_error
= GSOCK_IOERR
;
308 /* Enable listening up to 5 connections */
309 if (listen(sck
->m_fd
, 5) < 0) {
312 sck
->m_error
= GSOCK_IOERR
;
316 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
317 GSocket_SetTimeout(sck
, sck
->m_timeout
);
319 return GSOCK_NOERROR
;
323 GSocket_WaitConnection() waits for an incoming client connection.
325 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
329 assert(socket
!= NULL
);
331 /* If the socket has already been created, we exit immediately */
332 if (socket
->m_fd
== -1 || !socket
->m_server
) {
333 socket
->m_error
= GSOCK_INVSOCK
;
337 /* Reenable GSOCK_CONNECTION event */
338 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
340 /* Create a GSocket object for the new connection */
341 connection
= GSocket_new();
343 connection
->m_error
= GSOCK_MEMERR
;
347 /* Accept the incoming connection */
348 ENABLE_TIMEOUT(connection
);
349 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
350 DISABLE_TIMEOUT(connection
);
351 if (connection
->m_fd
== -1) {
352 GSocket_destroy(connection
);
356 connection
->m_error
= GSOCK_TIMEDOUT
;
359 connection
->m_error
= GSOCK_WOULDBLOCK
;
362 connection
->m_error
= GSOCK_IOERR
;
368 /* Initialize all fields */
369 connection
->m_stream
= TRUE
;
370 connection
->m_server
= FALSE
;
371 connection
->m_oriented
= TRUE
;
376 /* Non oriented connections */
378 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
382 if (sck
->m_fd
!= -1) {
383 sck
->m_error
= GSOCK_INVSOCK
;
384 return GSOCK_INVSOCK
;
388 sck
->m_error
= GSOCK_INVADDR
;
389 return GSOCK_INVADDR
;
392 sck
->m_stream
= FALSE
;
393 sck
->m_server
= FALSE
;
394 sck
->m_oriented
= FALSE
;
396 /* Create the socket */
397 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
400 sck
->m_error
= GSOCK_IOERR
;
404 /* Bind it to the LOCAL address */
405 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
408 sck
->m_error
= GSOCK_IOERR
;
412 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
413 GSocket_SetTimeout(sck
, sck
->m_timeout
);
415 return GSOCK_NOERROR
;
418 /* Client specific parts */
421 GSocket_Connect() establishes a client connection to a server using the "Peer"
422 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
423 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
425 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
431 if (sck
->m_fd
!= -1) {
432 sck
->m_error
= GSOCK_INVSOCK
;
433 return GSOCK_INVSOCK
;
437 sck
->m_error
= GSOCK_INVADDR
;
438 return GSOCK_INVADDR
;
441 /* Test whether we want the socket to be a stream (e.g. TCP) */
442 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
443 sck
->m_oriented
= TRUE
;
450 /* Create the socket */
451 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
453 if (sck
->m_fd
== -1) {
454 sck
->m_error
= GSOCK_IOERR
;
458 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
459 GSocket_SetTimeout(sck
, sck
->m_timeout
);
461 /* Connect it to the PEER address */
463 err
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
464 DISABLE_TIMEOUT(sck
);
466 if (err
!= 0 && errno
!= EINPROGRESS
) {
472 sck
->m_error
= GSOCK_TIMEDOUT
;
475 sck
->m_error
= GSOCK_IOERR
;
481 /* It is not a server */
482 sck
->m_server
= FALSE
;
483 sck
->m_establishing
= (errno
== EINPROGRESS
);
485 return (sck
->m_establishing
) ? GSOCK_WOULDBLOCK
: GSOCK_NOERROR
;
490 /* Like recv(), send(), ... */
491 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
493 assert(socket
!= NULL
);
495 if (socket
->m_fd
== -1 || socket
->m_server
) {
496 socket
->m_error
= GSOCK_INVSOCK
;
500 /* Reenable GSOCK_INPUT event */
501 _GSocket_Enable(socket
, GSOCK_INPUT
);
503 if (socket
->m_stream
)
504 return _GSocket_Recv_Stream(socket
, buffer
, size
);
506 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
509 int GSocket_Write(GSocket
*socket
, const char *buffer
,
512 assert(socket
!= NULL
);
514 if (socket
->m_fd
== -1 || socket
->m_server
) {
515 socket
->m_error
= GSOCK_INVSOCK
;
519 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
521 if (socket
->m_stream
)
522 return _GSocket_Send_Stream(socket
, buffer
, size
);
524 return _GSocket_Send_Dgram(socket
, buffer
, size
);
528 * Polls the socket to determine its status. This function will
529 * check for the events specified in the 'flags' parameter, and
530 * it will return a mask indicating which operations can be
531 * performed. This function won't block, regardless of the
532 * mode (blocking|nonblocking) of the socket.
534 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
536 fd_set readfds
, writefds
, exceptfds
;
538 GSocketEventFlags mask
;
541 assert(socket
!= NULL
);
543 if (socket
->m_fd
== -1)
545 socket
->m_error
= GSOCK_INVSOCK
;
552 FD_SET(socket
->m_fd
, &readfds
);
553 FD_SET(socket
->m_fd
, &writefds
);
554 FD_SET(socket
->m_fd
, &exceptfds
);
558 select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
);
562 /* If select() says that the socket is readable, then we have
563 * no way to distinguish if that means 'data available' (to
564 * recv) or 'incoming connection' (to accept). The same goes
565 * for writability: we cannot distinguish between 'you can
566 * send data' and 'connection request completed'. So we will
567 * assume the following: if the flag was set upon entry,
568 * that means that the event was possible.
570 if (FD_ISSET(socket
->m_fd
, &readfds
))
572 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
573 mask
|= (flags
& GSOCK_INPUT_FLAG
);
575 if (FD_ISSET(socket
->m_fd
, &writefds
))
577 if (socket
->m_establishing
)
579 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
582 mask
|= (flags
& GSOCK_LOST_FLAG
);
584 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
586 mask
|= (flags
& GSOCK_OUTPUT_FLAG
);
588 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
589 mask
|= (flags
& GSOCK_LOST_FLAG
);
597 GSocket_SetNonBlocking() puts the socket in non-blocking mode. This is useful
598 if we don't want to wait.
600 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
602 assert(socket
!= NULL
);
604 socket
->m_non_blocking
= non_block
;
606 if (socket
->m_fd
!= -1)
607 ioctl(socket
->m_fd
, FIONBIO
, &non_block
);
611 * GSocket_SetTimeout()
614 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
616 assert(socket
!= NULL
);
618 socket
->m_timeout
= millisec
;
622 GSocket_GetError() returns the last error occured on the socket stream.
625 GSocketError
GSocket_GetError(GSocket
*socket
)
627 assert(socket
!= NULL
);
629 return socket
->m_error
;
635 Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION)
636 INPUT: The function is called when there is at least a byte in the
638 OUTPUT: The function is called when the system is sure the next write call
640 CONNECTION: Two cases is possible:
641 Client socket -> the connection is established
642 Server socket -> a client request a connection
643 LOST: the connection is lost
645 SetCallback accepts a combination of these flags so a same callback can
646 receive different events.
648 An event is generated only once and its state is reseted when the relative
649 IO call is requested.
650 For example: INPUT -> GSocket_Read()
651 CONNECTION -> GSocket_Accept()
653 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
654 GSocketCallback callback
, char *cdata
)
658 assert (socket
!= NULL
);
660 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
661 /* We test each flag and, if it is enabled, we enable the corresponding
663 if ((flags
& (1 << count
)) != 0) {
664 socket
->m_cbacks
[count
] = callback
;
665 socket
->m_data
[count
] = cdata
;
666 _GSocket_Enable(socket
, count
);
672 UnsetCallback will disables all callbacks specified by "flags".
673 NOTE: "flags" may be a combination of flags
675 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
679 assert(socket
!= NULL
);
681 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
682 if ((flags
& (1 << count
)) != 0) {
683 _GSocket_Disable(socket
, count
);
684 socket
->m_cbacks
[count
] = NULL
;
689 #define CALL_CALLBACK(socket, event) \
690 if (socket->m_iocalls[event] && \
691 socket->m_cbacks[event]) {\
692 _GSocket_Disable(socket, event); \
693 socket->m_cbacks[event](socket, event, \
694 socket->m_data[event]); \
697 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
699 socket
->m_iocalls
[event
] = TRUE
;
700 if (socket
->m_cbacks
[event
])
701 _GSocket_Install_Callback(socket
, event
);
704 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
706 socket
->m_iocalls
[event
] = FALSE
;
707 if (socket
->m_cbacks
[event
])
708 _GSocket_Uninstall_Callback(socket
, event
);
711 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
716 ENABLE_TIMEOUT(socket
);
717 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
718 DISABLE_TIMEOUT(socket
);
722 errno
!= ETIMEDOUT
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
) {
723 socket
->m_error
= GSOCK_IOERR
;
726 if (errno
== EWOULDBLOCK
) {
727 socket
->m_error
= GSOCK_WOULDBLOCK
;
730 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
731 socket
->m_error
= GSOCK_TIMEDOUT
;
737 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
739 struct sockaddr from
;
744 fromlen
= sizeof(from
);
747 ENABLE_TIMEOUT(socket
);
748 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
749 DISABLE_TIMEOUT(socket
);
751 if (ret
== -1 && errno
!= EWOULDBLOCK
&& errno
!= EINTR
&& errno
!= ETIMEDOUT
) {
752 socket
->m_error
= GSOCK_IOERR
;
755 if (errno
== EWOULDBLOCK
) {
756 socket
->m_error
= GSOCK_WOULDBLOCK
;
759 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
760 socket
->m_error
= GSOCK_TIMEDOUT
;
764 /* Translate a system address into a GSocket address */
765 if (!socket
->m_peer
) {
766 socket
->m_peer
= GAddress_new();
767 if (!socket
->m_peer
) {
768 socket
->m_error
= GSOCK_MEMERR
;
772 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
773 if (err
!= GSOCK_NOERROR
) {
774 GAddress_destroy(socket
->m_peer
);
775 socket
->m_peer
= NULL
;
776 socket
->m_error
= err
;
783 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
789 ENABLE_TIMEOUT(socket
);
790 ret
= send(socket
->m_fd
, buffer
, size
, 0);
791 DISABLE_TIMEOUT(socket
);
793 if (ret
== -1 && errno
!= EWOULDBLOCK
&& errno
!= ETIMEDOUT
&& errno
!= EINTR
) {
794 socket
->m_error
= GSOCK_IOERR
;
797 if (errno
== EWOULDBLOCK
) {
798 socket
->m_error
= GSOCK_WOULDBLOCK
;
801 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
802 socket
->m_error
= GSOCK_TIMEDOUT
;
808 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
810 struct sockaddr
*addr
;
814 if (!socket
->m_peer
) {
815 socket
->m_error
= GSOCK_INVADDR
;
819 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
820 if (err
!= GSOCK_NOERROR
) {
821 socket
->m_error
= err
;
826 ENABLE_TIMEOUT(socket
);
827 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
828 DISABLE_TIMEOUT(socket
);
831 /* Frees memory allocated from _GAddress_translate_to */
834 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
835 socket
->m_error
= GSOCK_IOERR
;
838 if (errno
== EWOULDBLOCK
) {
839 socket
->m_error
= GSOCK_WOULDBLOCK
;
846 void _GSocket_Detected_Read(GSocket
*socket
)
851 if (socket
->m_stream
) {
852 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
854 if (ret
< 0 && socket
->m_server
) {
855 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
860 CALL_CALLBACK(socket
, GSOCK_INPUT
);
862 CALL_CALLBACK(socket
, GSOCK_LOST
);
867 void _GSocket_Detected_Write(GSocket
*socket
)
869 if (socket
->m_establishing
) {
874 socket
->m_establishing
= FALSE
;
875 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
878 socket
->m_error
= GSOCK_IOERR
;
879 GSocket_Shutdown(socket
);
881 socket
->m_error
= GSOCK_NOERROR
;
883 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
885 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
889 * -------------------------------------------------------------------------
891 * -------------------------------------------------------------------------
894 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
895 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
896 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
898 #define CHECK_ADDRESS(address, family, retval) \
900 if (address->m_family == GSOCK_NOFAMILY) \
901 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
902 return address->m_error; \
904 if (address->m_family != GSOCK_##family) {\
905 address->m_error = GSOCK_INVADDR; \
910 GAddress
*GAddress_new()
914 address
= (GAddress
*)malloc(sizeof(GAddress
));
919 address
->m_family
= GSOCK_NOFAMILY
;
920 address
->m_addr
= NULL
;
926 GAddress
*GAddress_copy(GAddress
*address
)
930 assert(address
!= NULL
);
932 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
937 memcpy(addr2
, address
, sizeof(GAddress
));
939 if (address
->m_addr
) {
940 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
941 if (addr2
->m_addr
== NULL
) {
945 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
951 void GAddress_destroy(GAddress
*address
)
953 assert(address
!= NULL
);
958 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
960 assert(address
!= NULL
);
962 address
->m_family
= type
;
965 GAddressType
GAddress_GetFamily(GAddress
*address
)
967 assert(address
!= NULL
);
969 return address
->m_family
;
972 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
973 address
->m_realfamily
= addr
->sa_family
;
974 switch (addr
->sa_family
) {
976 address
->m_family
= GSOCK_INET
;
979 address
->m_family
= GSOCK_UNIX
;
983 address
->m_family
= GSOCK_INET6
;
988 address
->m_error
= GSOCK_INVOP
;
994 free(address
->m_addr
);
996 address
->m_len
= len
;
997 address
->m_addr
= (struct sockaddr
*)malloc(len
);
998 if (address
->m_addr
== NULL
) {
999 address
->m_error
= GSOCK_MEMERR
;
1000 return GSOCK_MEMERR
;
1002 memcpy(address
->m_addr
, addr
, len
);
1004 return GSOCK_NOERROR
;
1007 GSocketError
_GAddress_translate_to(GAddress
*address
,
1008 struct sockaddr
**addr
, int *len
)
1010 if (!address
->m_addr
) {
1011 address
->m_error
= GSOCK_INVADDR
;
1012 return GSOCK_INVADDR
;
1015 *len
= address
->m_len
;
1016 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1017 if (*addr
== NULL
) {
1018 address
->m_error
= GSOCK_MEMERR
;
1019 return GSOCK_MEMERR
;
1022 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1023 return GSOCK_NOERROR
;
1027 * -------------------------------------------------------------------------
1028 * Internet address family
1029 * -------------------------------------------------------------------------
1032 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1034 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1035 if (address
->m_addr
== NULL
) {
1036 address
->m_error
= GSOCK_MEMERR
;
1037 return GSOCK_MEMERR
;
1040 address
->m_len
= sizeof(struct sockaddr_in
);
1042 address
->m_family
= GSOCK_INET
;
1043 address
->m_realfamily
= PF_INET
;
1044 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1045 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1047 return GSOCK_NOERROR
;
1050 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1053 struct in_addr
*addr
;
1055 assert(address
!= NULL
);
1057 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1059 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1061 /* If it is a numeric host name, convert it now */
1062 #if defined(HAVE_INET_ATON)
1063 if (inet_aton(hostname
, addr
) == 0) {
1064 #elif defined(HAVE_INET_ADDR)
1065 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1066 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1068 /* Use gethostbyname by default */
1071 struct in_addr
*array_addr
;
1073 /* It is a real name, we solve it */
1074 if ((he
= gethostbyname(hostname
)) == NULL
) {
1075 address
->m_error
= GSOCK_NOHOST
;
1076 return GSOCK_NOHOST
;
1078 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1079 addr
->s_addr
= array_addr
[0].s_addr
;
1081 return GSOCK_NOERROR
;
1084 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1085 unsigned long hostaddr
)
1087 struct in_addr
*addr
;
1089 assert(address
!= NULL
);
1091 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1093 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1094 addr
->s_addr
= hostaddr
;
1096 return GSOCK_NOERROR
;
1099 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1100 const char *protocol
)
1103 struct sockaddr_in
*addr
;
1105 assert(address
!= NULL
);
1106 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1109 address
->m_error
= GSOCK_INVPORT
;
1110 return GSOCK_INVPORT
;
1113 se
= getservbyname(port
, protocol
);
1115 if (isdigit(port
[0])) {
1118 port_int
= atoi(port
);
1119 addr
= (struct sockaddr_in
*)address
->m_addr
;
1120 addr
->sin_port
= htons(port_int
);
1121 return GSOCK_NOERROR
;
1124 address
->m_error
= GSOCK_INVPORT
;
1125 return GSOCK_INVPORT
;
1128 addr
= (struct sockaddr_in
*)address
->m_addr
;
1129 addr
->sin_port
= se
->s_port
;
1131 return GSOCK_NOERROR
;
1134 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1136 struct sockaddr_in
*addr
;
1138 assert(address
!= NULL
);
1139 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1141 addr
= (struct sockaddr_in
*)address
->m_addr
;
1142 addr
->sin_port
= htons(port
);
1144 return GSOCK_NOERROR
;
1147 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1151 struct sockaddr_in
*addr
;
1153 assert(address
!= NULL
);
1154 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1156 addr
= (struct sockaddr_in
*)address
->m_addr
;
1157 addr_buf
= (char *)&(addr
->sin_addr
);
1159 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1161 address
->m_error
= GSOCK_NOHOST
;
1162 return GSOCK_NOHOST
;
1165 strncpy(hostname
, he
->h_name
, sbuf
);
1167 return GSOCK_NOERROR
;
1170 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1172 struct sockaddr_in
*addr
;
1174 assert(address
!= NULL
);
1175 CHECK_ADDRESS(address
, INET
, 0);
1177 addr
= (struct sockaddr_in
*)address
->m_addr
;
1179 return addr
->sin_addr
.s_addr
;
1182 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1184 struct sockaddr_in
*addr
;
1186 assert(address
!= NULL
);
1187 CHECK_ADDRESS(address
, INET
, 0);
1189 addr
= (struct sockaddr_in
*)address
->m_addr
;
1190 return ntohs(addr
->sin_port
);
1194 * -------------------------------------------------------------------------
1195 * Unix address family
1196 * -------------------------------------------------------------------------
1199 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1201 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1202 if (address
->m_addr
== NULL
) {
1203 address
->m_error
= GSOCK_MEMERR
;
1204 return GSOCK_MEMERR
;
1207 address
->m_len
= sizeof(struct sockaddr_un
);
1208 address
->m_family
= GSOCK_UNIX
;
1209 address
->m_realfamily
= PF_UNIX
;
1210 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1211 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1216 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1218 struct sockaddr_un
*addr
;
1220 assert(address
!= NULL
);
1222 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1224 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1225 memcpy(addr
->sun_path
, path
, strlen(path
));
1227 return GSOCK_NOERROR
;
1230 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1232 struct sockaddr_un
*addr
;
1234 assert(address
!= NULL
);
1235 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1237 addr
= (struct sockaddr_un
*)address
->m_addr
;
1239 strncpy(path
, addr
->sun_path
, sbuf
);
1241 return GSOCK_NOERROR
;