]>
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/types.h>
16 #include <sys/ioctl.h>
20 u_char sun_len
; /* sockaddr len including null */
21 u_char sun_family
; /* AF_UNIX */
22 char sun_path
[108]; /* path name (gag) */
25 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
41 # include <sys/filio.h>
50 #include "wx/gsocket.h"
51 #include "wx/unix/gsockunx.h"
56 # define SOCKLEN_T unsigned int
60 # define SOCKLEN_T socklen_t
63 # define SOCKLEN_T int
69 #define MASK_SIGNAL() \
71 void (*old_handler)(int); \
73 old_handler = signal(SIGPIPE, SIG_IGN);
75 #define UNMASK_SIGNAL() \
76 signal(SIGPIPE, old_handler); \
80 /* Global initialisers */
87 void GSocket_Cleanup()
91 /* Constructors / Destructors */
93 GSocket
*GSocket_new()
98 socket
= (GSocket
*)malloc(sizeof(GSocket
));
104 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
106 socket
->m_cbacks
[i
] = NULL
;
108 socket
->m_detected
= 0;
109 socket
->m_local
= NULL
;
110 socket
->m_peer
= NULL
;
111 socket
->m_error
= GSOCK_NOERROR
;
112 socket
->m_server
= FALSE
;
113 socket
->m_stream
= TRUE
;
114 socket
->m_gui_dependent
= NULL
;
115 socket
->m_non_blocking
= FALSE
;
116 socket
->m_timeout
= 10*60*1000;
117 /* 10 minutes * 60 sec * 1000 millisec */
118 socket
->m_establishing
= FALSE
;
120 /* We initialize the GUI specific entries here */
121 _GSocket_GUI_Init(socket
);
126 void GSocket_destroy(GSocket
*socket
)
128 assert(socket
!= NULL
);
130 /* First, we check that the socket is really shutdowned */
131 if (socket
->m_fd
!= -1)
132 GSocket_Shutdown(socket
);
134 /* We destroy GUI specific variables */
135 _GSocket_GUI_Destroy(socket
);
137 /* We destroy private addresses */
139 GAddress_destroy(socket
->m_local
);
142 GAddress_destroy(socket
->m_peer
);
144 /* We destroy socket itself */
148 void GSocket_Shutdown(GSocket
*socket
)
152 assert(socket
!= NULL
);
154 /* If socket has been created, we shutdown it */
155 if (socket
->m_fd
!= -1)
157 shutdown(socket
->m_fd
, 2);
162 /* We also disable GUI callbacks */
163 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
164 socket
->m_cbacks
[evt
] = NULL
;
166 socket
->m_detected
= 0;
167 _GSocket_Disable_Events(socket
);
170 /* Address handling */
172 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
174 assert(socket
!= NULL
);
176 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
177 socket
->m_error
= GSOCK_INVSOCK
;
178 return GSOCK_INVSOCK
;
181 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
182 socket
->m_error
= GSOCK_INVADDR
;
183 return GSOCK_INVADDR
;
187 GAddress_destroy(socket
->m_local
);
189 socket
->m_local
= GAddress_copy(address
);
191 return GSOCK_NOERROR
;
194 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
196 assert(socket
!= NULL
);
198 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
199 socket
->m_error
= GSOCK_INVADDR
;
200 return GSOCK_INVADDR
;
204 GAddress_destroy(socket
->m_peer
);
206 socket
->m_peer
= GAddress_copy(address
);
208 return GSOCK_NOERROR
;
211 GAddress
*GSocket_GetLocal(GSocket
*socket
)
214 struct sockaddr addr
;
215 SOCKLEN_T size
= sizeof(addr
);
218 assert(socket
!= NULL
);
221 return GAddress_copy(socket
->m_local
);
223 if (socket
->m_fd
== -1) {
224 socket
->m_error
= GSOCK_INVSOCK
;
230 if (getsockname(socket
->m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0) {
231 socket
->m_error
= GSOCK_IOERR
;
235 address
= GAddress_new();
236 if (address
== NULL
) {
237 socket
->m_error
= GSOCK_MEMERR
;
240 err
= _GAddress_translate_from(address
, &addr
, size
); /*xxx*/
241 if (err
!= GSOCK_NOERROR
) {
242 GAddress_destroy(address
);
243 socket
->m_error
= err
;
250 GAddress
*GSocket_GetPeer(GSocket
*socket
)
252 assert(socket
!= NULL
);
255 return GAddress_copy(socket
->m_peer
);
260 /* Server specific parts */
262 /* GSocket_SetServer:
263 * Sets up the socket as a server. It uses the "Local" field of GSocket.
264 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
265 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
266 * been initialized, GSOCK_INVADDR if the local address has not been
267 * defined and GSOCK_IOERR for other internal errors.
269 GSocketError
GSocket_SetServer(GSocket
*sck
)
276 if (sck
->m_fd
!= -1) {
277 sck
->m_error
= GSOCK_INVSOCK
;
278 return GSOCK_INVSOCK
;
282 sck
->m_error
= GSOCK_INVADDR
;
283 return GSOCK_INVADDR
;
286 /* We always have a stream here */
287 sck
->m_stream
= TRUE
;
288 sck
->m_server
= TRUE
;
290 /* Create the socket */
291 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
293 if (sck
->m_fd
== -1) {
294 sck
->m_error
= GSOCK_IOERR
;
298 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
299 _GSocket_Enable_Events(sck
);
301 /* Bind the socket to the LOCAL address */
302 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
305 sck
->m_error
= GSOCK_IOERR
;
309 /* Enable listening up to 5 connections */
310 if (listen(sck
->m_fd
, 5) < 0) {
313 sck
->m_error
= GSOCK_IOERR
;
317 return GSOCK_NOERROR
;
320 /* GSocket_WaitConnection:
321 * Waits for an incoming client connection.
323 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
325 struct sockaddr from
;
326 SOCKLEN_T fromlen
= sizeof(from
);
330 assert(socket
!= NULL
);
332 /* Reenable CONNECTION events */
333 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
335 /* If the socket has already been created, we exit immediately */
336 if (socket
->m_fd
== -1 || !socket
->m_server
)
338 socket
->m_error
= GSOCK_INVSOCK
;
342 /* Create a GSocket object for the new connection */
343 connection
= GSocket_new();
346 socket
->m_error
= GSOCK_MEMERR
;
350 /* Accept the incoming connection */
351 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
353 GSocket_destroy(connection
);
354 /* socket->m_error set by _GSocket_Input_Timeout */
358 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
360 if (connection
->m_fd
== -1)
362 if (errno
== EWOULDBLOCK
)
363 socket
->m_error
= GSOCK_WOULDBLOCK
;
365 socket
->m_error
= GSOCK_IOERR
;
367 GSocket_destroy(connection
);
371 /* Initialize all fields */
372 connection
->m_server
= FALSE
;
373 connection
->m_stream
= TRUE
;
374 connection
->m_oriented
= TRUE
;
376 /* Setup the peer address field */ /*xxx*/
377 connection
->m_peer
= GAddress_new();
378 if (!connection
->m_peer
)
380 GSocket_destroy(connection
);
381 socket
->m_error
= GSOCK_MEMERR
;
384 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
385 if (err
!= GSOCK_NOERROR
)
387 GAddress_destroy(connection
->m_peer
);
388 GSocket_destroy(connection
);
389 socket
->m_error
= err
;
393 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
394 _GSocket_Enable_Events(connection
);
399 /* Non oriented connections */
401 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
407 if (sck
->m_fd
!= -1) {
408 sck
->m_error
= GSOCK_INVSOCK
;
409 return GSOCK_INVSOCK
;
413 sck
->m_error
= GSOCK_INVADDR
;
414 return GSOCK_INVADDR
;
417 sck
->m_stream
= FALSE
;
418 sck
->m_server
= FALSE
;
419 sck
->m_oriented
= FALSE
;
421 /* Create the socket */
422 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
425 sck
->m_error
= GSOCK_IOERR
;
429 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
430 _GSocket_Enable_Events(sck
);
432 /* Bind it to the LOCAL address */
433 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
436 sck
->m_error
= GSOCK_IOERR
;
440 return GSOCK_NOERROR
;
443 /* Client specific parts */
446 * Establishes a client connection to a server using the "Peer"
447 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
448 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
449 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
450 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
451 * the connection request can be completed later. Use GSocket_Select()
452 * to check or wait for a GSOCK_CONNECTION event.
454 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
461 /* Enable CONNECTION events (needed for nonblocking connections) */
462 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
466 sck
->m_error
= GSOCK_INVSOCK
;
467 return GSOCK_INVSOCK
;
472 sck
->m_error
= GSOCK_INVADDR
;
473 return GSOCK_INVADDR
;
476 /* Test whether we want the socket to be a stream (e.g. TCP) */
477 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
478 sck
->m_oriented
= TRUE
;
479 sck
->m_server
= FALSE
;
480 sck
->m_establishing
= FALSE
;
487 /* Create the socket */
488 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
490 if (sck
->m_fd
== -1) {
491 sck
->m_error
= GSOCK_IOERR
;
495 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
496 _GSocket_Enable_Events(sck
);
498 /* Connect it to the PEER address */
499 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
505 /* If connect failed with EINPROGRESS and the GSocket object
506 * is in blocking mode, we select() for the specified timeout
507 * checking for writability to see if the connection request
510 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
512 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
516 /* sck->m_error is set in _GSocket_Output_Timeout */
517 return GSOCK_TIMEDOUT
;
521 return GSOCK_NOERROR
;
525 /* If connect failed with EINPROGRESS and the GSocket object
526 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
527 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
528 * this way if the connection completes, a GSOCK_CONNECTION
529 * event will be generated, if enabled.
531 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
533 sck
->m_error
= GSOCK_WOULDBLOCK
;
534 sck
->m_establishing
= TRUE
;
536 return GSOCK_WOULDBLOCK
;
539 /* If connect failed with an error other than EINPROGRESS,
540 * then the call to GSocket_Connect has failed.
544 sck
->m_error
= GSOCK_IOERR
;
549 return GSOCK_NOERROR
;
554 /* Like recv(), send(), ... */
555 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
559 assert(socket
!= NULL
);
561 /* Reenable INPUT events */
562 _GSocket_Enable(socket
, GSOCK_INPUT
);
564 if (socket
->m_fd
== -1 || socket
->m_server
)
566 socket
->m_error
= GSOCK_INVSOCK
;
570 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
573 if (socket
->m_stream
)
574 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
576 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
580 if (errno
== EWOULDBLOCK
)
581 socket
->m_error
= GSOCK_WOULDBLOCK
;
583 socket
->m_error
= GSOCK_IOERR
;
589 int GSocket_Write(GSocket
*socket
, const char *buffer
,
594 assert(socket
!= NULL
);
596 if (socket
->m_fd
== -1 || socket
->m_server
)
598 socket
->m_error
= GSOCK_INVSOCK
;
602 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
605 if (socket
->m_stream
)
606 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
608 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
612 if (errno
== EWOULDBLOCK
)
613 socket
->m_error
= GSOCK_WOULDBLOCK
;
615 socket
->m_error
= GSOCK_IOERR
;
617 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
618 * in MSW). Once the first OUTPUT event is received, users can assume
619 * that the socket is writable until a read operation fails. Only then
620 * will further OUTPUT events be posted.
622 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
629 * Polls the socket to determine its status. This function will
630 * check for the events specified in the 'flags' parameter, and
631 * it will return a mask indicating which operations can be
632 * performed. This function won't block, regardless of the
633 * mode (blocking|nonblocking) of the socket.
635 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
637 assert(socket
!= NULL
);
639 return (flags
& socket
->m_detected
);
644 /* GSocket_SetNonBlocking:
645 * Sets the socket to non-blocking mode. This is useful if
646 * we don't want to wait.
648 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
650 assert(socket
!= NULL
);
652 socket
->m_non_blocking
= non_block
;
655 /* GSocket_SetTimeout:
656 * Sets the timeout for blocking calls. Time is
657 * expressed in milliseconds.
659 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
661 assert(socket
!= NULL
);
663 socket
->m_timeout
= millisec
;
667 * Returns the last error occured for this socket.
669 GSocketError
GSocket_GetError(GSocket
*socket
)
671 assert(socket
!= NULL
);
673 return socket
->m_error
;
678 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
679 * and LOST). The callbacks are called in the following situations:
681 * INPUT: There is at least one byte in the input buffer
682 * OUTPUT: The system is sure that the next write call will not block
683 * CONNECTION: Two cases are possible:
684 * Client socket -> the connection is established
685 * Server socket -> a client requests a connection
686 * LOST: The connection is lost
688 * An event is generated only once and its state is reseted when the
689 * relative IO call is requested.
690 * For example: INPUT -> GSocket_Read()
691 * CONNECTION -> GSocket_Accept()
694 /* GSocket_SetCallback:
695 * Enables the callbacks specified by 'flags'. Note that 'flags'
696 * may be a combination of flags OR'ed toghether, so the same
697 * callback function can be made to accept different events.
698 * The callback function must have the following prototype:
700 * void function(GSocket *socket, GSocketEvent event, char *cdata)
702 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
703 GSocketCallback callback
, char *cdata
)
707 assert(socket
!= NULL
);
709 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
711 if ((flags
& (1 << count
)) != 0)
713 socket
->m_cbacks
[count
] = callback
;
714 socket
->m_data
[count
] = cdata
;
719 /* GSocket_UnsetCallback:
720 * Disables all callbacks specified by 'flags', which may be a
721 * combination of flags OR'ed toghether.
723 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
727 assert(socket
!= NULL
);
729 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
731 if ((flags
& (1 << count
)) != 0)
733 socket
->m_cbacks
[count
] = NULL
;
734 socket
->m_data
[count
] = NULL
;
739 #define CALL_CALLBACK(socket, event) { \
740 _GSocket_Disable(socket, event); \
741 if (socket->m_cbacks[event]) \
742 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
746 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
748 socket
->m_detected
&= ~(1 << event
);
749 _GSocket_Install_Callback(socket
, event
);
752 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
754 socket
->m_detected
|= (1 << event
);
755 _GSocket_Uninstall_Callback(socket
, event
);
758 /* _GSocket_Input_Timeout:
759 * For blocking sockets, wait until data is available or
760 * until timeout ellapses.
762 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
767 /* Linux select() will overwrite the struct on return */
768 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
769 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
771 if (!socket
->m_non_blocking
)
774 FD_SET(socket
->m_fd
, &readfds
);
775 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
777 socket
->m_error
= GSOCK_TIMEDOUT
;
778 return GSOCK_TIMEDOUT
;
781 return GSOCK_NOERROR
;
784 /* _GSocket_Output_Timeout:
785 * For blocking sockets, wait until data can be sent without
786 * blocking or until timeout ellapses.
788 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
793 /* Linux select() will overwrite the struct on return */
794 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
795 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
797 if (!socket
->m_non_blocking
)
800 FD_SET(socket
->m_fd
, &writefds
);
801 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
803 socket
->m_error
= GSOCK_TIMEDOUT
;
804 return GSOCK_TIMEDOUT
;
807 return GSOCK_NOERROR
;
810 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
815 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
821 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
823 struct sockaddr from
;
824 SOCKLEN_T fromlen
= sizeof(from
);
828 fromlen
= sizeof(from
);
831 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
837 /* Translate a system address into a GSocket address */
840 socket
->m_peer
= GAddress_new();
843 socket
->m_error
= GSOCK_MEMERR
;
847 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
848 if (err
!= GSOCK_NOERROR
)
850 GAddress_destroy(socket
->m_peer
);
851 socket
->m_peer
= NULL
;
852 socket
->m_error
= err
;
859 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
864 ret
= send(socket
->m_fd
, buffer
, size
, 0);
870 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
872 struct sockaddr
*addr
;
876 if (!socket
->m_peer
) {
877 socket
->m_error
= GSOCK_INVADDR
;
881 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
882 if (err
!= GSOCK_NOERROR
) {
883 socket
->m_error
= err
;
888 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
891 /* Frees memory allocated from _GAddress_translate_to */
897 void _GSocket_Detected_Read(GSocket
*socket
)
902 if (socket
->m_stream
)
904 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
906 if (ret
< 0 && socket
->m_server
)
908 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
914 CALL_CALLBACK(socket
, GSOCK_INPUT
);
918 CALL_CALLBACK(socket
, GSOCK_LOST
);
923 void _GSocket_Detected_Write(GSocket
*socket
)
925 if (socket
->m_establishing
&& !socket
->m_server
)
929 socket
->m_establishing
= FALSE
;
932 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
,
937 CALL_CALLBACK(socket
, GSOCK_LOST
);
941 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
942 /* We have to fire this event by hand because CONNECTION (for clients)
943 * and OUTPUT are internally the same and we just disabled CONNECTION
944 * events with the above macro.
946 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
951 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
956 * -------------------------------------------------------------------------
958 * -------------------------------------------------------------------------
961 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
962 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
963 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
965 #define CHECK_ADDRESS(address, family, retval) \
967 if (address->m_family == GSOCK_NOFAMILY) \
968 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
969 return address->m_error; \
971 if (address->m_family != GSOCK_##family) {\
972 address->m_error = GSOCK_INVADDR; \
977 GAddress
*GAddress_new()
981 address
= (GAddress
*)malloc(sizeof(GAddress
));
986 address
->m_family
= GSOCK_NOFAMILY
;
987 address
->m_addr
= NULL
;
993 GAddress
*GAddress_copy(GAddress
*address
)
997 assert(address
!= NULL
);
999 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
1004 memcpy(addr2
, address
, sizeof(GAddress
));
1006 if (address
->m_addr
) {
1007 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1008 if (addr2
->m_addr
== NULL
) {
1012 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1018 void GAddress_destroy(GAddress
*address
)
1020 assert(address
!= NULL
);
1025 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1027 assert(address
!= NULL
);
1029 address
->m_family
= type
;
1032 GAddressType
GAddress_GetFamily(GAddress
*address
)
1034 assert(address
!= NULL
);
1036 return address
->m_family
;
1039 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1040 address
->m_realfamily
= addr
->sa_family
;
1041 switch (addr
->sa_family
) {
1043 address
->m_family
= GSOCK_INET
;
1046 address
->m_family
= GSOCK_UNIX
;
1050 address
->m_family
= GSOCK_INET6
;
1055 address
->m_error
= GSOCK_INVOP
;
1060 if (address
->m_addr
)
1061 free(address
->m_addr
);
1063 address
->m_len
= len
;
1064 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1065 if (address
->m_addr
== NULL
) {
1066 address
->m_error
= GSOCK_MEMERR
;
1067 return GSOCK_MEMERR
;
1069 memcpy(address
->m_addr
, addr
, len
);
1071 return GSOCK_NOERROR
;
1074 GSocketError
_GAddress_translate_to(GAddress
*address
,
1075 struct sockaddr
**addr
, int *len
)
1077 if (!address
->m_addr
) {
1078 address
->m_error
= GSOCK_INVADDR
;
1079 return GSOCK_INVADDR
;
1082 *len
= address
->m_len
;
1083 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1084 if (*addr
== NULL
) {
1085 address
->m_error
= GSOCK_MEMERR
;
1086 return GSOCK_MEMERR
;
1089 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1090 return GSOCK_NOERROR
;
1094 * -------------------------------------------------------------------------
1095 * Internet address family
1096 * -------------------------------------------------------------------------
1099 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1101 address
->m_len
= sizeof(struct sockaddr_in
);
1102 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1103 if (address
->m_addr
== NULL
)
1105 address
->m_error
= GSOCK_MEMERR
;
1106 return GSOCK_MEMERR
;
1109 address
->m_family
= GSOCK_INET
;
1110 address
->m_realfamily
= PF_INET
;
1111 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1113 INADDR_BROADCAST is identical to INADDR_NONE which is not defined
1114 on all unices. INADDR_BROADCAST should be fine to indicate an error.
1116 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_BROADCAST
;
1118 return GSOCK_NOERROR
;
1121 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1124 struct in_addr
*addr
;
1126 assert(address
!= NULL
);
1128 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1130 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1132 /* If it is a numeric host name, convert it now */
1133 #if defined(HAVE_INET_ATON)
1134 if (inet_aton(hostname
, addr
) == 0)
1136 #elif defined(HAVE_INET_ADDR)
1137 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1138 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1141 /* Use gethostbyname by default */
1145 struct in_addr
*array_addr
;
1147 /* It is a real name, we solve it */
1148 if ((he
= gethostbyname(hostname
)) == NULL
)
1150 address
->m_error
= GSOCK_NOHOST
;
1151 return GSOCK_NOHOST
;
1153 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1154 addr
->s_addr
= array_addr
[0].s_addr
;
1156 return GSOCK_NOERROR
;
1159 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1160 unsigned long hostaddr
)
1162 struct in_addr
*addr
;
1164 assert(address
!= NULL
);
1166 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1168 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1169 addr
->s_addr
= hostaddr
;
1171 return GSOCK_NOERROR
;
1174 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1175 const char *protocol
)
1178 struct sockaddr_in
*addr
;
1180 assert(address
!= NULL
);
1181 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1185 address
->m_error
= GSOCK_INVPORT
;
1186 return GSOCK_INVPORT
;
1189 se
= getservbyname(port
, protocol
);
1191 if (isdigit(port
[0]))
1195 port_int
= atoi(port
);
1196 addr
= (struct sockaddr_in
*)address
->m_addr
;
1197 addr
->sin_port
= htons(port_int
);
1198 return GSOCK_NOERROR
;
1201 address
->m_error
= GSOCK_INVPORT
;
1202 return GSOCK_INVPORT
;
1205 addr
= (struct sockaddr_in
*)address
->m_addr
;
1206 addr
->sin_port
= se
->s_port
;
1208 return GSOCK_NOERROR
;
1211 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1213 struct sockaddr_in
*addr
;
1215 assert(address
!= NULL
);
1216 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1218 addr
= (struct sockaddr_in
*)address
->m_addr
;
1219 addr
->sin_port
= htons(port
);
1221 return GSOCK_NOERROR
;
1224 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1228 struct sockaddr_in
*addr
;
1230 assert(address
!= NULL
);
1231 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1233 addr
= (struct sockaddr_in
*)address
->m_addr
;
1234 addr_buf
= (char *)&(addr
->sin_addr
);
1236 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1239 address
->m_error
= GSOCK_NOHOST
;
1240 return GSOCK_NOHOST
;
1243 strncpy(hostname
, he
->h_name
, sbuf
);
1245 return GSOCK_NOERROR
;
1248 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1250 struct sockaddr_in
*addr
;
1252 assert(address
!= NULL
);
1253 CHECK_ADDRESS(address
, INET
, 0);
1255 addr
= (struct sockaddr_in
*)address
->m_addr
;
1257 return addr
->sin_addr
.s_addr
;
1260 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1262 struct sockaddr_in
*addr
;
1264 assert(address
!= NULL
);
1265 CHECK_ADDRESS(address
, INET
, 0);
1267 addr
= (struct sockaddr_in
*)address
->m_addr
;
1268 return ntohs(addr
->sin_port
);
1272 * -------------------------------------------------------------------------
1273 * Unix address family
1274 * -------------------------------------------------------------------------
1277 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1279 address
->m_len
= sizeof(struct sockaddr_un
);
1280 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1281 if (address
->m_addr
== NULL
)
1283 address
->m_error
= GSOCK_MEMERR
;
1284 return GSOCK_MEMERR
;
1287 address
->m_family
= GSOCK_UNIX
;
1288 address
->m_realfamily
= PF_UNIX
;
1289 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1290 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1295 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1297 struct sockaddr_un
*addr
;
1299 assert(address
!= NULL
);
1301 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1303 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1304 memcpy(addr
->sun_path
, path
, strlen(path
));
1306 return GSOCK_NOERROR
;
1309 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1311 struct sockaddr_un
*addr
;
1313 assert(address
!= NULL
);
1314 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1316 addr
= (struct sockaddr_un
*)address
->m_addr
;
1318 strncpy(path
, addr
->sun_path
, sbuf
);
1320 return GSOCK_NOERROR
;