]>
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 */
265 /* GSocket_SetServer:
266 * Sets up the socket as a server. It uses the "Local" field of GSocket.
267 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
268 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
269 * been initialized, GSOCK_INVADDR if the local address has not been
270 * defined and GSOCK_IOERR for other internal errors.
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_Configure_Callbacks(sck
);
317 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
318 GSocket_SetTimeout(sck
, sck
->m_timeout
);
320 return GSOCK_NOERROR
;
323 /* GSocket_WaitConnection:
324 * Waits for an incoming client connection.
326 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
330 assert(socket
!= NULL
);
332 /* If the socket has already been created, we exit immediately */
333 if (socket
->m_fd
== -1 || !socket
->m_server
) {
334 socket
->m_error
= GSOCK_INVSOCK
;
338 /* Reenable GSOCK_CONNECTION event */
339 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
341 /* Create a GSocket object for the new connection */
342 connection
= GSocket_new();
344 connection
->m_error
= GSOCK_MEMERR
;
348 /* Accept the incoming connection */
349 ENABLE_TIMEOUT(connection
);
350 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
351 DISABLE_TIMEOUT(connection
);
352 if (connection
->m_fd
== -1) {
353 GSocket_destroy(connection
);
357 connection
->m_error
= GSOCK_TIMEDOUT
;
360 connection
->m_error
= GSOCK_WOULDBLOCK
;
363 connection
->m_error
= GSOCK_IOERR
;
369 /* Initialize all fields */
370 connection
->m_stream
= TRUE
;
371 connection
->m_server
= FALSE
;
372 connection
->m_oriented
= TRUE
;
374 _GSocket_Configure_Callbacks(connection
);
375 GSocket_SetNonBlocking(connection
, connection
->m_non_blocking
);
376 GSocket_SetTimeout(connection
, connection
->m_timeout
);
381 /* Non oriented connections */
383 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
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 sck
->m_stream
= FALSE
;
398 sck
->m_server
= FALSE
;
399 sck
->m_oriented
= FALSE
;
401 /* Create the socket */
402 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
405 sck
->m_error
= GSOCK_IOERR
;
409 /* Bind it to the LOCAL address */
410 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
413 sck
->m_error
= GSOCK_IOERR
;
417 _GSocket_Configure_Callbacks(sck
);
418 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
419 GSocket_SetTimeout(sck
, sck
->m_timeout
);
421 return GSOCK_NOERROR
;
424 /* Client specific parts */
427 * Establishes a client connection to a server using the "Peer"
428 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
429 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
430 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
431 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
432 * the connection request can be completed later. Use GSocket_Select()
433 * to check or wait for a GSOCK_CONNECTION event.
435 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
441 if (sck
->m_fd
!= -1) {
442 sck
->m_error
= GSOCK_INVSOCK
;
443 return GSOCK_INVSOCK
;
447 sck
->m_error
= GSOCK_INVADDR
;
448 return GSOCK_INVADDR
;
451 /* Test whether we want the socket to be a stream (e.g. TCP) */
452 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
453 sck
->m_oriented
= TRUE
;
460 /* Create the socket */
461 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
463 if (sck
->m_fd
== -1) {
464 sck
->m_error
= GSOCK_IOERR
;
468 _GSocket_Configure_Callbacks(sck
);
469 GSocket_SetNonBlocking(sck
, sck
->m_non_blocking
);
470 GSocket_SetTimeout(sck
, sck
->m_timeout
);
472 /* Connect it to the PEER address */
474 err
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
475 DISABLE_TIMEOUT(sck
);
477 if (err
!= 0 && errno
!= EINPROGRESS
) {
483 sck
->m_error
= GSOCK_TIMEDOUT
;
486 sck
->m_error
= GSOCK_IOERR
;
492 /* It is not a server */
493 sck
->m_server
= FALSE
;
494 sck
->m_establishing
= (errno
== EINPROGRESS
);
496 return (sck
->m_establishing
) ? GSOCK_WOULDBLOCK
: GSOCK_NOERROR
;
501 /* Like recv(), send(), ... */
502 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
504 assert(socket
!= NULL
);
506 if (socket
->m_fd
== -1 || socket
->m_server
) {
507 socket
->m_error
= GSOCK_INVSOCK
;
511 /* Reenable GSOCK_INPUT event */
512 _GSocket_Enable(socket
, GSOCK_INPUT
);
514 if (socket
->m_stream
)
515 return _GSocket_Recv_Stream(socket
, buffer
, size
);
517 return _GSocket_Recv_Dgram(socket
, buffer
, size
);
520 int GSocket_Write(GSocket
*socket
, const char *buffer
,
523 assert(socket
!= NULL
);
525 if (socket
->m_fd
== -1 || socket
->m_server
) {
526 socket
->m_error
= GSOCK_INVSOCK
;
530 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
532 if (socket
->m_stream
)
533 return _GSocket_Send_Stream(socket
, buffer
, size
);
535 return _GSocket_Send_Dgram(socket
, buffer
, size
);
539 * Polls the socket to determine its status. This function will
540 * check for the events specified in the 'flags' parameter, and
541 * it will return a mask indicating which operations can be
542 * performed. This function won't block, regardless of the
543 * mode (blocking|nonblocking) of the socket.
545 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
547 fd_set readfds
, writefds
, exceptfds
;
549 GSocketEventFlags mask
;
552 assert(socket
!= NULL
);
554 if (socket
->m_fd
== -1)
556 socket
->m_error
= GSOCK_INVSOCK
;
563 FD_SET(socket
->m_fd
, &readfds
);
564 FD_SET(socket
->m_fd
, &writefds
);
565 FD_SET(socket
->m_fd
, &exceptfds
);
569 select(socket
->m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
);
573 /* If select() says that the socket is readable, then we have
574 * no way to distinguish if that means 'data available' (to
575 * recv) or 'incoming connection' (to accept). The same goes
576 * for writability: we cannot distinguish between 'you can
577 * send data' and 'connection request completed'. So we will
578 * assume the following: if the flag was set upon entry,
579 * that means that the event was possible.
581 if (FD_ISSET(socket
->m_fd
, &readfds
))
583 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
584 mask
|= (flags
& GSOCK_INPUT_FLAG
);
586 if (FD_ISSET(socket
->m_fd
, &writefds
))
588 if (socket
->m_establishing
)
590 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
593 mask
|= (flags
& GSOCK_LOST_FLAG
);
595 mask
|= (flags
& GSOCK_CONNECTION_FLAG
);
597 mask
|= (flags
& GSOCK_OUTPUT_FLAG
);
599 if (FD_ISSET(socket
->m_fd
, &exceptfds
))
600 mask
|= (flags
& GSOCK_LOST_FLAG
);
607 /* GSocket_SetNonBlocking:
608 * Sets the socket to non-blocking mode. This is useful if
609 * we don't want to wait.
611 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
613 assert(socket
!= NULL
);
615 socket
->m_non_blocking
= non_block
;
617 if (socket
->m_fd
!= -1)
618 ioctl(socket
->m_fd
, FIONBIO
, &non_block
);
621 /* GSocket_SetTimeout:
622 * Sets the timeout for blocking calls. Time is
623 * expressed in milliseconds.
625 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
627 assert(socket
!= NULL
);
629 socket
->m_timeout
= millisec
;
633 * Returns the last error occured for this socket.
635 GSocketError
GSocket_GetError(GSocket
*socket
)
637 assert(socket
!= NULL
);
639 return socket
->m_error
;
644 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
645 * and LOST). The callbacks are called in the following situations:
647 * INPUT: There is at least one byte in the input buffer
648 * OUTPUT: The system is sure that the next write call will not block
649 * CONNECTION: Two cases are possible:
650 * Client socket -> the connection is established
651 * Server socket -> a client requests a connection
652 * LOST: The connection is lost
654 * An event is generated only once and its state is reseted when the
655 * relative IO call is requested.
656 * For example: INPUT -> GSocket_Read()
657 * CONNECTION -> GSocket_Accept()
660 /* GSocket_SetCallback:
661 * Enables the callbacks specified by 'flags'. Note that 'flags'
662 * may be a combination of flags OR'ed toghether, so the same
663 * callback function can be made to accept different events.
664 * The callback function must have the following prototype:
666 * void function(GSocket *socket, GSocketEvent event, char *cdata)
668 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
669 GSocketCallback callback
, char *cdata
)
673 assert (socket
!= NULL
);
675 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
676 /* We test each flag and, if it is enabled, we enable the corresponding
678 if ((flags
& (1 << count
)) != 0) {
679 socket
->m_cbacks
[count
] = callback
;
680 socket
->m_data
[count
] = cdata
;
683 _GSocket_Configure_Callbacks(socket
);
686 /* GSocket_UnsetCallback:
687 * Disables all callbacks specified by 'flags', which may be a
688 * combination of flags OR'ed toghether.
690 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
694 assert(socket
!= NULL
);
696 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
697 if ((flags
& (1 << count
)) != 0) {
698 socket
->m_cbacks
[count
] = NULL
;
701 _GSocket_Configure_Callbacks(socket
);
704 #define CALL_CALLBACK(socket, event) \
705 if (socket->m_iocalls[event] && socket->m_cbacks[event]) { \
706 _GSocket_Disable(socket, event); \
707 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
710 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
712 socket
->m_iocalls
[event
] = TRUE
;
713 if (socket
->m_cbacks
[event
])
714 _GSocket_Install_Callback(socket
, event
);
717 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
719 socket
->m_iocalls
[event
] = FALSE
;
720 if (socket
->m_cbacks
[event
])
721 _GSocket_Uninstall_Callback(socket
, event
);
724 void _GSocket_Configure_Callbacks(GSocket
*socket
)
727 for (count
=0;count
<GSOCK_MAX_EVENT
;count
++) {
728 if ((socket
->m_cbacks
[count
]) != NULL
) {
729 _GSocket_Enable(socket
, count
);
731 _GSocket_Disable(socket
, count
);
736 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
741 ENABLE_TIMEOUT(socket
);
742 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
743 DISABLE_TIMEOUT(socket
);
747 errno
!= ETIMEDOUT
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
) {
748 socket
->m_error
= GSOCK_IOERR
;
751 if (errno
== EWOULDBLOCK
) {
752 socket
->m_error
= GSOCK_WOULDBLOCK
;
755 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
756 socket
->m_error
= GSOCK_TIMEDOUT
;
762 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
764 struct sockaddr from
;
769 fromlen
= sizeof(from
);
772 ENABLE_TIMEOUT(socket
);
773 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
774 DISABLE_TIMEOUT(socket
);
776 if (ret
== -1 && errno
!= EWOULDBLOCK
&& errno
!= EINTR
&& errno
!= ETIMEDOUT
) {
777 socket
->m_error
= GSOCK_IOERR
;
780 if (errno
== EWOULDBLOCK
) {
781 socket
->m_error
= GSOCK_WOULDBLOCK
;
784 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
785 socket
->m_error
= GSOCK_TIMEDOUT
;
789 /* Translate a system address into a GSocket address */
790 if (!socket
->m_peer
) {
791 socket
->m_peer
= GAddress_new();
792 if (!socket
->m_peer
) {
793 socket
->m_error
= GSOCK_MEMERR
;
797 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
798 if (err
!= GSOCK_NOERROR
) {
799 GAddress_destroy(socket
->m_peer
);
800 socket
->m_peer
= NULL
;
801 socket
->m_error
= err
;
808 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
814 ENABLE_TIMEOUT(socket
);
815 ret
= send(socket
->m_fd
, buffer
, size
, 0);
816 DISABLE_TIMEOUT(socket
);
818 if (ret
== -1 && errno
!= EWOULDBLOCK
&& errno
!= ETIMEDOUT
&& errno
!= EINTR
) {
819 socket
->m_error
= GSOCK_IOERR
;
822 if (errno
== EWOULDBLOCK
) {
823 socket
->m_error
= GSOCK_WOULDBLOCK
;
826 if (errno
== ETIMEDOUT
|| errno
== EINTR
) {
827 socket
->m_error
= GSOCK_TIMEDOUT
;
833 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
835 struct sockaddr
*addr
;
839 if (!socket
->m_peer
) {
840 socket
->m_error
= GSOCK_INVADDR
;
844 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
845 if (err
!= GSOCK_NOERROR
) {
846 socket
->m_error
= err
;
851 ENABLE_TIMEOUT(socket
);
852 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
853 DISABLE_TIMEOUT(socket
);
856 /* Frees memory allocated from _GAddress_translate_to */
859 if (ret
== -1 && errno
!= EWOULDBLOCK
) {
860 socket
->m_error
= GSOCK_IOERR
;
863 if (errno
== EWOULDBLOCK
) {
864 socket
->m_error
= GSOCK_WOULDBLOCK
;
871 void _GSocket_Detected_Read(GSocket
*socket
)
876 if (socket
->m_stream
) {
877 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
879 if (ret
< 0 && socket
->m_server
) {
880 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
885 CALL_CALLBACK(socket
, GSOCK_INPUT
);
887 CALL_CALLBACK(socket
, GSOCK_LOST
);
892 void _GSocket_Detected_Write(GSocket
*socket
)
894 if (socket
->m_establishing
) {
899 socket
->m_establishing
= FALSE
;
900 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
903 socket
->m_error
= GSOCK_IOERR
;
904 GSocket_Shutdown(socket
);
906 socket
->m_error
= GSOCK_NOERROR
;
908 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
910 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
914 * -------------------------------------------------------------------------
916 * -------------------------------------------------------------------------
919 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
920 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
921 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
923 #define CHECK_ADDRESS(address, family, retval) \
925 if (address->m_family == GSOCK_NOFAMILY) \
926 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
927 return address->m_error; \
929 if (address->m_family != GSOCK_##family) {\
930 address->m_error = GSOCK_INVADDR; \
935 GAddress
*GAddress_new()
939 address
= (GAddress
*)malloc(sizeof(GAddress
));
944 address
->m_family
= GSOCK_NOFAMILY
;
945 address
->m_addr
= NULL
;
951 GAddress
*GAddress_copy(GAddress
*address
)
955 assert(address
!= NULL
);
957 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
962 memcpy(addr2
, address
, sizeof(GAddress
));
964 if (address
->m_addr
) {
965 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
966 if (addr2
->m_addr
== NULL
) {
970 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
976 void GAddress_destroy(GAddress
*address
)
978 assert(address
!= NULL
);
983 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
985 assert(address
!= NULL
);
987 address
->m_family
= type
;
990 GAddressType
GAddress_GetFamily(GAddress
*address
)
992 assert(address
!= NULL
);
994 return address
->m_family
;
997 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
998 address
->m_realfamily
= addr
->sa_family
;
999 switch (addr
->sa_family
) {
1001 address
->m_family
= GSOCK_INET
;
1004 address
->m_family
= GSOCK_UNIX
;
1008 address
->m_family
= GSOCK_INET6
;
1013 address
->m_error
= GSOCK_INVOP
;
1018 if (address
->m_addr
)
1019 free(address
->m_addr
);
1021 address
->m_len
= len
;
1022 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1023 if (address
->m_addr
== NULL
) {
1024 address
->m_error
= GSOCK_MEMERR
;
1025 return GSOCK_MEMERR
;
1027 memcpy(address
->m_addr
, addr
, len
);
1029 return GSOCK_NOERROR
;
1032 GSocketError
_GAddress_translate_to(GAddress
*address
,
1033 struct sockaddr
**addr
, int *len
)
1035 if (!address
->m_addr
) {
1036 address
->m_error
= GSOCK_INVADDR
;
1037 return GSOCK_INVADDR
;
1040 *len
= address
->m_len
;
1041 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1042 if (*addr
== NULL
) {
1043 address
->m_error
= GSOCK_MEMERR
;
1044 return GSOCK_MEMERR
;
1047 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1048 return GSOCK_NOERROR
;
1052 * -------------------------------------------------------------------------
1053 * Internet address family
1054 * -------------------------------------------------------------------------
1057 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1059 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1060 if (address
->m_addr
== NULL
) {
1061 address
->m_error
= GSOCK_MEMERR
;
1062 return GSOCK_MEMERR
;
1065 address
->m_len
= sizeof(struct sockaddr_in
);
1067 address
->m_family
= GSOCK_INET
;
1068 address
->m_realfamily
= PF_INET
;
1069 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1070 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1072 return GSOCK_NOERROR
;
1075 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1078 struct in_addr
*addr
;
1080 assert(address
!= NULL
);
1082 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1084 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1086 /* If it is a numeric host name, convert it now */
1087 #if defined(HAVE_INET_ATON)
1088 if (inet_aton(hostname
, addr
) == 0) {
1089 #elif defined(HAVE_INET_ADDR)
1090 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1091 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1093 /* Use gethostbyname by default */
1096 struct in_addr
*array_addr
;
1098 /* It is a real name, we solve it */
1099 if ((he
= gethostbyname(hostname
)) == NULL
) {
1100 address
->m_error
= GSOCK_NOHOST
;
1101 return GSOCK_NOHOST
;
1103 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1104 addr
->s_addr
= array_addr
[0].s_addr
;
1106 return GSOCK_NOERROR
;
1109 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1110 unsigned long hostaddr
)
1112 struct in_addr
*addr
;
1114 assert(address
!= NULL
);
1116 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1118 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1119 addr
->s_addr
= hostaddr
;
1121 return GSOCK_NOERROR
;
1124 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1125 const char *protocol
)
1128 struct sockaddr_in
*addr
;
1130 assert(address
!= NULL
);
1131 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1134 address
->m_error
= GSOCK_INVPORT
;
1135 return GSOCK_INVPORT
;
1138 se
= getservbyname(port
, protocol
);
1140 if (isdigit(port
[0])) {
1143 port_int
= atoi(port
);
1144 addr
= (struct sockaddr_in
*)address
->m_addr
;
1145 addr
->sin_port
= htons(port_int
);
1146 return GSOCK_NOERROR
;
1149 address
->m_error
= GSOCK_INVPORT
;
1150 return GSOCK_INVPORT
;
1153 addr
= (struct sockaddr_in
*)address
->m_addr
;
1154 addr
->sin_port
= se
->s_port
;
1156 return GSOCK_NOERROR
;
1159 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1161 struct sockaddr_in
*addr
;
1163 assert(address
!= NULL
);
1164 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1166 addr
= (struct sockaddr_in
*)address
->m_addr
;
1167 addr
->sin_port
= htons(port
);
1169 return GSOCK_NOERROR
;
1172 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1176 struct sockaddr_in
*addr
;
1178 assert(address
!= NULL
);
1179 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1181 addr
= (struct sockaddr_in
*)address
->m_addr
;
1182 addr_buf
= (char *)&(addr
->sin_addr
);
1184 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1186 address
->m_error
= GSOCK_NOHOST
;
1187 return GSOCK_NOHOST
;
1190 strncpy(hostname
, he
->h_name
, sbuf
);
1192 return GSOCK_NOERROR
;
1195 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1197 struct sockaddr_in
*addr
;
1199 assert(address
!= NULL
);
1200 CHECK_ADDRESS(address
, INET
, 0);
1202 addr
= (struct sockaddr_in
*)address
->m_addr
;
1204 return addr
->sin_addr
.s_addr
;
1207 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1209 struct sockaddr_in
*addr
;
1211 assert(address
!= NULL
);
1212 CHECK_ADDRESS(address
, INET
, 0);
1214 addr
= (struct sockaddr_in
*)address
->m_addr
;
1215 return ntohs(addr
->sin_port
);
1219 * -------------------------------------------------------------------------
1220 * Unix address family
1221 * -------------------------------------------------------------------------
1224 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1226 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1227 if (address
->m_addr
== NULL
) {
1228 address
->m_error
= GSOCK_MEMERR
;
1229 return GSOCK_MEMERR
;
1232 address
->m_len
= sizeof(struct sockaddr_un
);
1233 address
->m_family
= GSOCK_UNIX
;
1234 address
->m_realfamily
= PF_UNIX
;
1235 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1236 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1241 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1243 struct sockaddr_un
*addr
;
1245 assert(address
!= NULL
);
1247 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1249 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1250 memcpy(addr
->sun_path
, path
, strlen(path
));
1252 return GSOCK_NOERROR
;
1255 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1257 struct sockaddr_un
*addr
;
1259 assert(address
!= NULL
);
1260 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1262 addr
= (struct sockaddr_un
*)address
->m_addr
;
1264 strncpy(path
, addr
->sun_path
, sbuf
);
1266 return GSOCK_NOERROR
;