]>
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); \
71 /* Global initialisers */
78 void GSocket_Cleanup()
82 /* Constructors / Destructors */
84 GSocket
*GSocket_new()
89 socket
= (GSocket
*)malloc(sizeof(GSocket
));
95 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
97 socket
->m_cbacks
[i
] = NULL
;
99 socket
->m_detected
= 0;
100 socket
->m_local
= NULL
;
101 socket
->m_peer
= NULL
;
102 socket
->m_error
= GSOCK_NOERROR
;
103 socket
->m_server
= FALSE
;
104 socket
->m_stream
= TRUE
;
105 socket
->m_gui_dependent
= NULL
;
106 socket
->m_non_blocking
= FALSE
;
107 socket
->m_timeout
= 10*60*1000;
108 /* 10 minutes * 60 sec * 1000 millisec */
109 socket
->m_establishing
= FALSE
;
111 /* We initialize the GUI specific entries here */
112 _GSocket_GUI_Init(socket
);
117 void GSocket_destroy(GSocket
*socket
)
119 assert(socket
!= NULL
);
121 /* First, we check that the socket is really shutdowned */
122 if (socket
->m_fd
!= -1)
123 GSocket_Shutdown(socket
);
125 /* We destroy GUI specific variables */
126 _GSocket_GUI_Destroy(socket
);
128 /* We destroy private addresses */
130 GAddress_destroy(socket
->m_local
);
133 GAddress_destroy(socket
->m_peer
);
135 /* We destroy socket itself */
139 void GSocket_Shutdown(GSocket
*socket
)
143 assert(socket
!= NULL
);
145 /* If socket has been created, we shutdown it */
146 if (socket
->m_fd
!= -1)
148 shutdown(socket
->m_fd
, 2);
153 /* We also disable GUI callbacks */
154 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
155 socket
->m_cbacks
[evt
] = NULL
;
157 socket
->m_detected
= 0;
158 _GSocket_Disable_Events(socket
);
161 /* Address handling */
163 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
165 assert(socket
!= NULL
);
167 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
168 socket
->m_error
= GSOCK_INVSOCK
;
169 return GSOCK_INVSOCK
;
172 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
173 socket
->m_error
= GSOCK_INVADDR
;
174 return GSOCK_INVADDR
;
178 GAddress_destroy(socket
->m_local
);
180 socket
->m_local
= GAddress_copy(address
);
182 return GSOCK_NOERROR
;
185 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
187 assert(socket
!= NULL
);
189 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
190 socket
->m_error
= GSOCK_INVADDR
;
191 return GSOCK_INVADDR
;
195 GAddress_destroy(socket
->m_peer
);
197 socket
->m_peer
= GAddress_copy(address
);
199 return GSOCK_NOERROR
;
202 GAddress
*GSocket_GetLocal(GSocket
*socket
)
205 struct sockaddr addr
;
209 assert(socket
!= NULL
);
212 return GAddress_copy(socket
->m_local
);
214 if (socket
->m_fd
== -1) {
215 socket
->m_error
= GSOCK_INVSOCK
;
221 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
222 socket
->m_error
= GSOCK_IOERR
;
226 address
= GAddress_new();
227 if (address
== NULL
) {
228 socket
->m_error
= GSOCK_MEMERR
;
231 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
232 if (socket
->m_error
!= GSOCK_NOERROR
) {
233 GAddress_destroy(address
);
240 GAddress
*GSocket_GetPeer(GSocket
*socket
)
242 assert(socket
!= NULL
);
245 return GAddress_copy(socket
->m_peer
);
250 /* Server specific parts */
252 /* GSocket_SetServer:
253 * Sets up the socket as a server. It uses the "Local" field of GSocket.
254 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
255 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
256 * been initialized, GSOCK_INVADDR if the local address has not been
257 * defined and GSOCK_IOERR for other internal errors.
259 GSocketError
GSocket_SetServer(GSocket
*sck
)
266 if (sck
->m_fd
!= -1) {
267 sck
->m_error
= GSOCK_INVSOCK
;
268 return GSOCK_INVSOCK
;
272 sck
->m_error
= GSOCK_INVADDR
;
273 return GSOCK_INVADDR
;
276 /* We always have a stream here */
277 sck
->m_stream
= TRUE
;
278 sck
->m_server
= TRUE
;
280 /* Create the socket */
281 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
283 if (sck
->m_fd
== -1) {
284 sck
->m_error
= GSOCK_IOERR
;
288 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
289 _GSocket_Enable_Events(sck
);
291 /* Bind the socket to the LOCAL address */
292 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
295 sck
->m_error
= GSOCK_IOERR
;
299 /* Enable listening up to 5 connections */
300 if (listen(sck
->m_fd
, 5) < 0) {
303 sck
->m_error
= GSOCK_IOERR
;
307 return GSOCK_NOERROR
;
310 /* GSocket_WaitConnection:
311 * Waits for an incoming client connection.
313 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
318 assert(socket
!= NULL
);
320 /* Reenable CONNECTION events */
321 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
323 /* If the socket has already been created, we exit immediately */
324 if (socket
->m_fd
== -1 || !socket
->m_server
)
326 socket
->m_error
= GSOCK_INVSOCK
;
330 /* Create a GSocket object for the new connection */
331 connection
= GSocket_new();
334 connection
->m_error
= GSOCK_MEMERR
;
338 /* Accept the incoming connection */
339 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
341 GSocket_destroy(connection
);
342 /* socket->m_error set by _GSocket_Input_Timeout */
346 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
348 if (connection
->m_fd
== -1)
350 if (errno
== EWOULDBLOCK
)
351 socket
->m_error
= GSOCK_WOULDBLOCK
;
353 socket
->m_error
= GSOCK_IOERR
;
355 GSocket_destroy(connection
);
359 /* Initialize all fields */
360 connection
->m_server
= FALSE
;
361 connection
->m_stream
= TRUE
;
362 connection
->m_oriented
= TRUE
;
364 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
365 _GSocket_Enable_Events(connection
);
370 /* Non oriented connections */
372 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
378 if (sck
->m_fd
!= -1) {
379 sck
->m_error
= GSOCK_INVSOCK
;
380 return GSOCK_INVSOCK
;
384 sck
->m_error
= GSOCK_INVADDR
;
385 return GSOCK_INVADDR
;
388 sck
->m_stream
= FALSE
;
389 sck
->m_server
= FALSE
;
390 sck
->m_oriented
= FALSE
;
392 /* Create the socket */
393 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
396 sck
->m_error
= GSOCK_IOERR
;
400 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
401 _GSocket_Enable_Events(sck
);
403 /* Bind it to the LOCAL address */
404 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
407 sck
->m_error
= GSOCK_IOERR
;
411 return GSOCK_NOERROR
;
414 /* Client specific parts */
417 * Establishes a client connection to a server using the "Peer"
418 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
419 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
420 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
421 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
422 * the connection request can be completed later. Use GSocket_Select()
423 * to check or wait for a GSOCK_CONNECTION event.
425 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
432 /* Enable CONNECTION events (needed for nonblocking connections) */
433 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
437 sck
->m_error
= GSOCK_INVSOCK
;
438 return GSOCK_INVSOCK
;
443 sck
->m_error
= GSOCK_INVADDR
;
444 return GSOCK_INVADDR
;
447 /* Test whether we want the socket to be a stream (e.g. TCP) */
448 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
449 sck
->m_oriented
= TRUE
;
450 sck
->m_server
= FALSE
;
451 sck
->m_establishing
= FALSE
;
458 /* Create the socket */
459 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
461 if (sck
->m_fd
== -1) {
462 sck
->m_error
= GSOCK_IOERR
;
466 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
467 _GSocket_Enable_Events(sck
);
469 /* Connect it to the PEER address */
470 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
476 /* If connect failed with EINPROGRESS and the GSocket object
477 * is in blocking mode, we select() for the specified timeout
478 * checking for writability to see if the connection request
481 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
483 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
487 /* sck->m_error is set in _GSocket_Output_Timeout */
488 return GSOCK_TIMEDOUT
;
492 return GSOCK_NOERROR
;
496 /* If connect failed with EINPROGRESS and the GSocket object
497 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
498 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
499 * this way if the connection completes, a GSOCK_CONNECTION
500 * event will be generated, if enabled.
502 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
504 sck
->m_error
= GSOCK_WOULDBLOCK
;
505 sck
->m_establishing
= TRUE
;
507 return GSOCK_WOULDBLOCK
;
510 /* If connect failed with an error other than EINPROGRESS,
511 * then the call to GSocket_Connect has failed.
515 sck
->m_error
= GSOCK_IOERR
;
520 return GSOCK_NOERROR
;
525 /* Like recv(), send(), ... */
526 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
530 assert(socket
!= NULL
);
532 /* Reenable INPUT events */
533 _GSocket_Enable(socket
, GSOCK_INPUT
);
535 if (socket
->m_fd
== -1 || socket
->m_server
)
537 socket
->m_error
= GSOCK_INVSOCK
;
541 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
544 if (socket
->m_stream
)
545 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
547 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
551 if (errno
== EWOULDBLOCK
)
552 socket
->m_error
= GSOCK_WOULDBLOCK
;
554 socket
->m_error
= GSOCK_IOERR
;
560 int GSocket_Write(GSocket
*socket
, const char *buffer
,
565 assert(socket
!= NULL
);
567 if (socket
->m_fd
== -1 || socket
->m_server
)
569 socket
->m_error
= GSOCK_INVSOCK
;
573 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
576 if (socket
->m_stream
)
577 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
579 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
583 if (errno
== EWOULDBLOCK
)
584 socket
->m_error
= GSOCK_WOULDBLOCK
;
586 socket
->m_error
= GSOCK_IOERR
;
588 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
589 * in MSW). Once the first OUTPUT event is received, users can assume
590 * that the socket is writable until a read operation fails. Only then
591 * will further OUTPUT events be posted.
593 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
600 * Polls the socket to determine its status. This function will
601 * check for the events specified in the 'flags' parameter, and
602 * it will return a mask indicating which operations can be
603 * performed. This function won't block, regardless of the
604 * mode (blocking|nonblocking) of the socket.
606 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
608 assert(socket
!= NULL
);
610 return (flags
& socket
->m_detected
);
615 /* GSocket_SetNonBlocking:
616 * Sets the socket to non-blocking mode. This is useful if
617 * we don't want to wait.
619 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
621 assert(socket
!= NULL
);
623 socket
->m_non_blocking
= non_block
;
626 /* GSocket_SetTimeout:
627 * Sets the timeout for blocking calls. Time is
628 * expressed in milliseconds.
630 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
632 assert(socket
!= NULL
);
634 socket
->m_timeout
= millisec
;
638 * Returns the last error occured for this socket.
640 GSocketError
GSocket_GetError(GSocket
*socket
)
642 assert(socket
!= NULL
);
644 return socket
->m_error
;
649 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
650 * and LOST). The callbacks are called in the following situations:
652 * INPUT: There is at least one byte in the input buffer
653 * OUTPUT: The system is sure that the next write call will not block
654 * CONNECTION: Two cases are possible:
655 * Client socket -> the connection is established
656 * Server socket -> a client requests a connection
657 * LOST: The connection is lost
659 * An event is generated only once and its state is reseted when the
660 * relative IO call is requested.
661 * For example: INPUT -> GSocket_Read()
662 * CONNECTION -> GSocket_Accept()
665 /* GSocket_SetCallback:
666 * Enables the callbacks specified by 'flags'. Note that 'flags'
667 * may be a combination of flags OR'ed toghether, so the same
668 * callback function can be made to accept different events.
669 * The callback function must have the following prototype:
671 * void function(GSocket *socket, GSocketEvent event, char *cdata)
673 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
674 GSocketCallback callback
, char *cdata
)
678 assert(socket
!= NULL
);
680 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
682 if ((flags
& (1 << count
)) != 0)
684 socket
->m_cbacks
[count
] = callback
;
685 socket
->m_data
[count
] = cdata
;
690 /* GSocket_UnsetCallback:
691 * Disables all callbacks specified by 'flags', which may be a
692 * combination of flags OR'ed toghether.
694 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
698 assert(socket
!= NULL
);
700 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
702 if ((flags
& (1 << count
)) != 0)
704 socket
->m_cbacks
[count
] = NULL
;
705 socket
->m_data
[count
] = NULL
;
710 #define CALL_CALLBACK(socket, event) { \
711 _GSocket_Disable(socket, event); \
712 if (socket->m_cbacks[event]) \
713 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
717 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
719 socket
->m_detected
&= ~(1 << event
);
720 _GSocket_Install_Callback(socket
, event
);
723 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
725 socket
->m_detected
|= (1 << event
);
726 _GSocket_Uninstall_Callback(socket
, event
);
729 /* _GSocket_Input_Timeout:
730 * For blocking sockets, wait until data is available or
731 * until timeout ellapses.
733 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
738 /* Linux select() will overwrite the struct on return */
739 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
740 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
742 if (!socket
->m_non_blocking
)
745 FD_SET(socket
->m_fd
, &readfds
);
746 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
748 socket
->m_error
= GSOCK_TIMEDOUT
;
749 return GSOCK_TIMEDOUT
;
752 return GSOCK_NOERROR
;
755 /* _GSocket_Output_Timeout:
756 * For blocking sockets, wait until data can be sent without
757 * blocking or until timeout ellapses.
759 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
764 /* Linux select() will overwrite the struct on return */
765 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
766 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
768 if (!socket
->m_non_blocking
)
771 FD_SET(socket
->m_fd
, &writefds
);
772 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
774 socket
->m_error
= GSOCK_TIMEDOUT
;
775 return GSOCK_TIMEDOUT
;
778 return GSOCK_NOERROR
;
781 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
786 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
792 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
794 struct sockaddr from
;
799 fromlen
= sizeof(from
);
802 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
808 /* Translate a system address into a GSocket address */
811 socket
->m_peer
= GAddress_new();
814 socket
->m_error
= GSOCK_MEMERR
;
818 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
819 if (err
!= GSOCK_NOERROR
)
821 GAddress_destroy(socket
->m_peer
);
822 socket
->m_peer
= NULL
;
823 socket
->m_error
= err
;
830 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
836 ret
= send(socket
->m_fd
, buffer
, size
, 0);
842 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
844 struct sockaddr
*addr
;
848 if (!socket
->m_peer
) {
849 socket
->m_error
= GSOCK_INVADDR
;
853 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
854 if (err
!= GSOCK_NOERROR
) {
855 socket
->m_error
= err
;
860 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
863 /* Frees memory allocated from _GAddress_translate_to */
869 void _GSocket_Detected_Read(GSocket
*socket
)
874 if (socket
->m_stream
)
876 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
878 if (ret
< 0 && socket
->m_server
)
880 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
886 CALL_CALLBACK(socket
, GSOCK_INPUT
);
890 CALL_CALLBACK(socket
, GSOCK_LOST
);
895 void _GSocket_Detected_Write(GSocket
*socket
)
897 if (socket
->m_establishing
&& !socket
->m_server
)
901 socket
->m_establishing
= FALSE
;
904 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
, &len
);
908 CALL_CALLBACK(socket
, GSOCK_LOST
);
912 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
913 /* We have to fire this event by hand because CONNECTION (for clients)
914 * and OUTPUT are internally the same and we just disabled CONNECTION
915 * events with the above macro.
917 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
922 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
927 * -------------------------------------------------------------------------
929 * -------------------------------------------------------------------------
932 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
933 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
934 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
936 #define CHECK_ADDRESS(address, family, retval) \
938 if (address->m_family == GSOCK_NOFAMILY) \
939 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
940 return address->m_error; \
942 if (address->m_family != GSOCK_##family) {\
943 address->m_error = GSOCK_INVADDR; \
948 GAddress
*GAddress_new()
952 address
= (GAddress
*)malloc(sizeof(GAddress
));
957 address
->m_family
= GSOCK_NOFAMILY
;
958 address
->m_addr
= NULL
;
964 GAddress
*GAddress_copy(GAddress
*address
)
968 assert(address
!= NULL
);
970 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
975 memcpy(addr2
, address
, sizeof(GAddress
));
977 if (address
->m_addr
) {
978 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
979 if (addr2
->m_addr
== NULL
) {
983 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
989 void GAddress_destroy(GAddress
*address
)
991 assert(address
!= NULL
);
996 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
998 assert(address
!= NULL
);
1000 address
->m_family
= type
;
1003 GAddressType
GAddress_GetFamily(GAddress
*address
)
1005 assert(address
!= NULL
);
1007 return address
->m_family
;
1010 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1011 address
->m_realfamily
= addr
->sa_family
;
1012 switch (addr
->sa_family
) {
1014 address
->m_family
= GSOCK_INET
;
1017 address
->m_family
= GSOCK_UNIX
;
1021 address
->m_family
= GSOCK_INET6
;
1026 address
->m_error
= GSOCK_INVOP
;
1031 if (address
->m_addr
)
1032 free(address
->m_addr
);
1034 address
->m_len
= len
;
1035 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1036 if (address
->m_addr
== NULL
) {
1037 address
->m_error
= GSOCK_MEMERR
;
1038 return GSOCK_MEMERR
;
1040 memcpy(address
->m_addr
, addr
, len
);
1042 return GSOCK_NOERROR
;
1045 GSocketError
_GAddress_translate_to(GAddress
*address
,
1046 struct sockaddr
**addr
, int *len
)
1048 if (!address
->m_addr
) {
1049 address
->m_error
= GSOCK_INVADDR
;
1050 return GSOCK_INVADDR
;
1053 *len
= address
->m_len
;
1054 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1055 if (*addr
== NULL
) {
1056 address
->m_error
= GSOCK_MEMERR
;
1057 return GSOCK_MEMERR
;
1060 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1061 return GSOCK_NOERROR
;
1065 * -------------------------------------------------------------------------
1066 * Internet address family
1067 * -------------------------------------------------------------------------
1070 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1072 address
->m_len
= sizeof(struct sockaddr_in
);
1073 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1074 if (address
->m_addr
== NULL
)
1076 address
->m_error
= GSOCK_MEMERR
;
1077 return GSOCK_MEMERR
;
1080 address
->m_family
= GSOCK_INET
;
1081 address
->m_realfamily
= PF_INET
;
1082 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1083 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1085 return GSOCK_NOERROR
;
1088 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1091 struct in_addr
*addr
;
1093 assert(address
!= NULL
);
1095 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1097 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1099 /* If it is a numeric host name, convert it now */
1100 #if defined(HAVE_INET_ATON)
1101 if (inet_aton(hostname
, addr
) == 0)
1103 #elif defined(HAVE_INET_ADDR)
1104 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1105 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1108 /* Use gethostbyname by default */
1112 struct in_addr
*array_addr
;
1114 /* It is a real name, we solve it */
1115 if ((he
= gethostbyname(hostname
)) == NULL
)
1117 address
->m_error
= GSOCK_NOHOST
;
1118 return GSOCK_NOHOST
;
1120 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1121 addr
->s_addr
= array_addr
[0].s_addr
;
1123 return GSOCK_NOERROR
;
1126 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1127 unsigned long hostaddr
)
1129 struct in_addr
*addr
;
1131 assert(address
!= NULL
);
1133 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1135 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1136 addr
->s_addr
= hostaddr
;
1138 return GSOCK_NOERROR
;
1141 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1142 const char *protocol
)
1145 struct sockaddr_in
*addr
;
1147 assert(address
!= NULL
);
1148 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1152 address
->m_error
= GSOCK_INVPORT
;
1153 return GSOCK_INVPORT
;
1156 se
= getservbyname(port
, protocol
);
1158 if (isdigit(port
[0]))
1162 port_int
= atoi(port
);
1163 addr
= (struct sockaddr_in
*)address
->m_addr
;
1164 addr
->sin_port
= htons(port_int
);
1165 return GSOCK_NOERROR
;
1168 address
->m_error
= GSOCK_INVPORT
;
1169 return GSOCK_INVPORT
;
1172 addr
= (struct sockaddr_in
*)address
->m_addr
;
1173 addr
->sin_port
= se
->s_port
;
1175 return GSOCK_NOERROR
;
1178 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1180 struct sockaddr_in
*addr
;
1182 assert(address
!= NULL
);
1183 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1185 addr
= (struct sockaddr_in
*)address
->m_addr
;
1186 addr
->sin_port
= htons(port
);
1188 return GSOCK_NOERROR
;
1191 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1195 struct sockaddr_in
*addr
;
1197 assert(address
!= NULL
);
1198 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1200 addr
= (struct sockaddr_in
*)address
->m_addr
;
1201 addr_buf
= (char *)&(addr
->sin_addr
);
1203 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1206 address
->m_error
= GSOCK_NOHOST
;
1207 return GSOCK_NOHOST
;
1210 strncpy(hostname
, he
->h_name
, sbuf
);
1212 return GSOCK_NOERROR
;
1215 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1217 struct sockaddr_in
*addr
;
1219 assert(address
!= NULL
);
1220 CHECK_ADDRESS(address
, INET
, 0);
1222 addr
= (struct sockaddr_in
*)address
->m_addr
;
1224 return addr
->sin_addr
.s_addr
;
1227 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1229 struct sockaddr_in
*addr
;
1231 assert(address
!= NULL
);
1232 CHECK_ADDRESS(address
, INET
, 0);
1234 addr
= (struct sockaddr_in
*)address
->m_addr
;
1235 return ntohs(addr
->sin_port
);
1239 * -------------------------------------------------------------------------
1240 * Unix address family
1241 * -------------------------------------------------------------------------
1244 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1246 address
->m_len
= sizeof(struct sockaddr_un
);
1247 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1248 if (address
->m_addr
== NULL
)
1250 address
->m_error
= GSOCK_MEMERR
;
1251 return GSOCK_MEMERR
;
1254 address
->m_family
= GSOCK_UNIX
;
1255 address
->m_realfamily
= PF_UNIX
;
1256 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1257 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1262 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1264 struct sockaddr_un
*addr
;
1266 assert(address
!= NULL
);
1268 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1270 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1271 memcpy(addr
->sun_path
, path
, strlen(path
));
1273 return GSOCK_NOERROR
;
1276 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1278 struct sockaddr_un
*addr
;
1280 assert(address
!= NULL
);
1281 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1283 addr
= (struct sockaddr_un
*)address
->m_addr
;
1285 strncpy(path
, addr
->sun_path
, sbuf
);
1287 return GSOCK_NOERROR
;