]>
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
67 #endif /* SOCKLEN_T */
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
);
331 assert(socket
!= NULL
);
333 /* Reenable CONNECTION events */
334 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
336 /* If the socket has already been created, we exit immediately */
337 if (socket
->m_fd
== -1 || !socket
->m_server
)
339 socket
->m_error
= GSOCK_INVSOCK
;
343 /* Create a GSocket object for the new connection */
344 connection
= GSocket_new();
347 socket
->m_error
= GSOCK_MEMERR
;
351 /* Accept the incoming connection */
352 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
354 GSocket_destroy(connection
);
355 /* socket->m_error set by _GSocket_Input_Timeout */
359 connection
->m_fd
= accept(socket
->m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
361 if (connection
->m_fd
== -1)
363 if (errno
== EWOULDBLOCK
)
364 socket
->m_error
= GSOCK_WOULDBLOCK
;
366 socket
->m_error
= GSOCK_IOERR
;
368 GSocket_destroy(connection
);
372 /* Initialize all fields */
373 connection
->m_server
= FALSE
;
374 connection
->m_stream
= TRUE
;
375 connection
->m_oriented
= TRUE
;
377 /* Setup the peer address field */ /*xxx*/
378 connection
->m_peer
= GAddress_new();
379 if (!connection
->m_peer
)
381 GSocket_destroy(connection
);
382 socket
->m_error
= GSOCK_MEMERR
;
385 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
386 if (err
!= GSOCK_NOERROR
)
388 GAddress_destroy(connection
->m_peer
);
389 GSocket_destroy(connection
);
390 socket
->m_error
= err
;
394 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
395 _GSocket_Enable_Events(connection
);
400 /* Non oriented connections */
402 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
408 if (sck
->m_fd
!= -1) {
409 sck
->m_error
= GSOCK_INVSOCK
;
410 return GSOCK_INVSOCK
;
414 sck
->m_error
= GSOCK_INVADDR
;
415 return GSOCK_INVADDR
;
418 sck
->m_stream
= FALSE
;
419 sck
->m_server
= FALSE
;
420 sck
->m_oriented
= FALSE
;
422 /* Create the socket */
423 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
426 sck
->m_error
= GSOCK_IOERR
;
430 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
431 _GSocket_Enable_Events(sck
);
433 /* Bind it to the LOCAL address */
434 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
437 sck
->m_error
= GSOCK_IOERR
;
441 return GSOCK_NOERROR
;
444 /* Client specific parts */
447 * Establishes a client connection to a server using the "Peer"
448 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
449 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
450 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
451 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
452 * the connection request can be completed later. Use GSocket_Select()
453 * to check or wait for a GSOCK_CONNECTION event.
455 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
462 /* Enable CONNECTION events (needed for nonblocking connections) */
463 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
467 sck
->m_error
= GSOCK_INVSOCK
;
468 return GSOCK_INVSOCK
;
473 sck
->m_error
= GSOCK_INVADDR
;
474 return GSOCK_INVADDR
;
477 /* Test whether we want the socket to be a stream (e.g. TCP) */
478 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
479 sck
->m_oriented
= TRUE
;
480 sck
->m_server
= FALSE
;
481 sck
->m_establishing
= FALSE
;
488 /* Create the socket */
489 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
491 if (sck
->m_fd
== -1) {
492 sck
->m_error
= GSOCK_IOERR
;
496 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
497 _GSocket_Enable_Events(sck
);
499 /* Connect it to the PEER address */
500 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
506 /* If connect failed with EINPROGRESS and the GSocket object
507 * is in blocking mode, we select() for the specified timeout
508 * checking for writability to see if the connection request
511 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
513 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
517 /* sck->m_error is set in _GSocket_Output_Timeout */
518 return GSOCK_TIMEDOUT
;
522 return GSOCK_NOERROR
;
526 /* If connect failed with EINPROGRESS and the GSocket object
527 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
528 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
529 * this way if the connection completes, a GSOCK_CONNECTION
530 * event will be generated, if enabled.
532 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
534 sck
->m_error
= GSOCK_WOULDBLOCK
;
535 sck
->m_establishing
= TRUE
;
537 return GSOCK_WOULDBLOCK
;
540 /* If connect failed with an error other than EINPROGRESS,
541 * then the call to GSocket_Connect has failed.
545 sck
->m_error
= GSOCK_IOERR
;
550 return GSOCK_NOERROR
;
555 /* Like recv(), send(), ... */
556 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
560 assert(socket
!= NULL
);
562 /* Reenable INPUT events */
563 _GSocket_Enable(socket
, GSOCK_INPUT
);
565 if (socket
->m_fd
== -1 || socket
->m_server
)
567 socket
->m_error
= GSOCK_INVSOCK
;
571 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
574 if (socket
->m_stream
)
575 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
577 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
581 if (errno
== EWOULDBLOCK
)
582 socket
->m_error
= GSOCK_WOULDBLOCK
;
584 socket
->m_error
= GSOCK_IOERR
;
590 int GSocket_Write(GSocket
*socket
, const char *buffer
,
595 assert(socket
!= NULL
);
597 if (socket
->m_fd
== -1 || socket
->m_server
)
599 socket
->m_error
= GSOCK_INVSOCK
;
603 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
606 if (socket
->m_stream
)
607 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
609 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
613 if (errno
== EWOULDBLOCK
)
614 socket
->m_error
= GSOCK_WOULDBLOCK
;
616 socket
->m_error
= GSOCK_IOERR
;
618 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
619 * in MSW). Once the first OUTPUT event is received, users can assume
620 * that the socket is writable until a read operation fails. Only then
621 * will further OUTPUT events be posted.
623 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
630 * Polls the socket to determine its status. This function will
631 * check for the events specified in the 'flags' parameter, and
632 * it will return a mask indicating which operations can be
633 * performed. This function won't block, regardless of the
634 * mode (blocking|nonblocking) of the socket.
636 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
638 assert(socket
!= NULL
);
640 return (flags
& socket
->m_detected
);
645 /* GSocket_SetNonBlocking:
646 * Sets the socket to non-blocking mode. This is useful if
647 * we don't want to wait.
649 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
651 assert(socket
!= NULL
);
653 socket
->m_non_blocking
= non_block
;
656 /* GSocket_SetTimeout:
657 * Sets the timeout for blocking calls. Time is
658 * expressed in milliseconds.
660 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
662 assert(socket
!= NULL
);
664 socket
->m_timeout
= millisec
;
668 * Returns the last error occured for this socket.
670 GSocketError
GSocket_GetError(GSocket
*socket
)
672 assert(socket
!= NULL
);
674 return socket
->m_error
;
679 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
680 * and LOST). The callbacks are called in the following situations:
682 * INPUT: There is at least one byte in the input buffer
683 * OUTPUT: The system is sure that the next write call will not block
684 * CONNECTION: Two cases are possible:
685 * Client socket -> the connection is established
686 * Server socket -> a client requests a connection
687 * LOST: The connection is lost
689 * An event is generated only once and its state is reseted when the
690 * relative IO call is requested.
691 * For example: INPUT -> GSocket_Read()
692 * CONNECTION -> GSocket_Accept()
695 /* GSocket_SetCallback:
696 * Enables the callbacks specified by 'flags'. Note that 'flags'
697 * may be a combination of flags OR'ed toghether, so the same
698 * callback function can be made to accept different events.
699 * The callback function must have the following prototype:
701 * void function(GSocket *socket, GSocketEvent event, char *cdata)
703 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
704 GSocketCallback callback
, char *cdata
)
708 assert(socket
!= NULL
);
710 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
712 if ((flags
& (1 << count
)) != 0)
714 socket
->m_cbacks
[count
] = callback
;
715 socket
->m_data
[count
] = cdata
;
720 /* GSocket_UnsetCallback:
721 * Disables all callbacks specified by 'flags', which may be a
722 * combination of flags OR'ed toghether.
724 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
728 assert(socket
!= NULL
);
730 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
732 if ((flags
& (1 << count
)) != 0)
734 socket
->m_cbacks
[count
] = NULL
;
735 socket
->m_data
[count
] = NULL
;
740 #define CALL_CALLBACK(socket, event) { \
741 _GSocket_Disable(socket, event); \
742 if (socket->m_cbacks[event]) \
743 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
747 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
749 socket
->m_detected
&= ~(1 << event
);
750 _GSocket_Install_Callback(socket
, event
);
753 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
755 socket
->m_detected
|= (1 << event
);
756 _GSocket_Uninstall_Callback(socket
, event
);
759 /* _GSocket_Input_Timeout:
760 * For blocking sockets, wait until data is available or
761 * until timeout ellapses.
763 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
768 /* Linux select() will overwrite the struct on return */
769 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
770 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
772 if (!socket
->m_non_blocking
)
775 FD_SET(socket
->m_fd
, &readfds
);
776 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
778 socket
->m_error
= GSOCK_TIMEDOUT
;
779 return GSOCK_TIMEDOUT
;
782 return GSOCK_NOERROR
;
785 /* _GSocket_Output_Timeout:
786 * For blocking sockets, wait until data can be sent without
787 * blocking or until timeout ellapses.
789 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
794 /* Linux select() will overwrite the struct on return */
795 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
796 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
798 if (!socket
->m_non_blocking
)
801 FD_SET(socket
->m_fd
, &writefds
);
802 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
804 socket
->m_error
= GSOCK_TIMEDOUT
;
805 return GSOCK_TIMEDOUT
;
808 return GSOCK_NOERROR
;
811 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
816 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
822 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
824 struct sockaddr from
;
825 SOCKLEN_T fromlen
= sizeof(from
);
829 fromlen
= sizeof(from
);
832 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
838 /* Translate a system address into a GSocket address */
841 socket
->m_peer
= GAddress_new();
844 socket
->m_error
= GSOCK_MEMERR
;
848 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
849 if (err
!= GSOCK_NOERROR
)
851 GAddress_destroy(socket
->m_peer
);
852 socket
->m_peer
= NULL
;
853 socket
->m_error
= err
;
860 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
865 ret
= send(socket
->m_fd
, buffer
, size
, 0);
871 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
873 struct sockaddr
*addr
;
877 if (!socket
->m_peer
) {
878 socket
->m_error
= GSOCK_INVADDR
;
882 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
883 if (err
!= GSOCK_NOERROR
) {
884 socket
->m_error
= err
;
889 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
892 /* Frees memory allocated from _GAddress_translate_to */
898 void _GSocket_Detected_Read(GSocket
*socket
)
903 if (socket
->m_stream
)
905 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
907 if (ret
< 0 && socket
->m_server
)
909 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
915 CALL_CALLBACK(socket
, GSOCK_INPUT
);
919 CALL_CALLBACK(socket
, GSOCK_LOST
);
924 void _GSocket_Detected_Write(GSocket
*socket
)
926 if (socket
->m_establishing
&& !socket
->m_server
)
930 socket
->m_establishing
= FALSE
;
933 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
,
938 CALL_CALLBACK(socket
, GSOCK_LOST
);
942 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
943 /* We have to fire this event by hand because CONNECTION (for clients)
944 * and OUTPUT are internally the same and we just disabled CONNECTION
945 * events with the above macro.
947 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
952 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
957 * -------------------------------------------------------------------------
959 * -------------------------------------------------------------------------
962 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
963 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
964 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
966 #define CHECK_ADDRESS(address, family, retval) \
968 if (address->m_family == GSOCK_NOFAMILY) \
969 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
970 return address->m_error; \
972 if (address->m_family != GSOCK_##family) {\
973 address->m_error = GSOCK_INVADDR; \
978 GAddress
*GAddress_new()
982 address
= (GAddress
*)malloc(sizeof(GAddress
));
987 address
->m_family
= GSOCK_NOFAMILY
;
988 address
->m_addr
= NULL
;
994 GAddress
*GAddress_copy(GAddress
*address
)
998 assert(address
!= NULL
);
1000 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
1005 memcpy(addr2
, address
, sizeof(GAddress
));
1007 if (address
->m_addr
) {
1008 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1009 if (addr2
->m_addr
== NULL
) {
1013 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1019 void GAddress_destroy(GAddress
*address
)
1021 assert(address
!= NULL
);
1026 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1028 assert(address
!= NULL
);
1030 address
->m_family
= type
;
1033 GAddressType
GAddress_GetFamily(GAddress
*address
)
1035 assert(address
!= NULL
);
1037 return address
->m_family
;
1040 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1041 address
->m_realfamily
= addr
->sa_family
;
1042 switch (addr
->sa_family
) {
1044 address
->m_family
= GSOCK_INET
;
1047 address
->m_family
= GSOCK_UNIX
;
1051 address
->m_family
= GSOCK_INET6
;
1056 address
->m_error
= GSOCK_INVOP
;
1061 if (address
->m_addr
)
1062 free(address
->m_addr
);
1064 address
->m_len
= len
;
1065 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1066 if (address
->m_addr
== NULL
) {
1067 address
->m_error
= GSOCK_MEMERR
;
1068 return GSOCK_MEMERR
;
1070 memcpy(address
->m_addr
, addr
, len
);
1072 return GSOCK_NOERROR
;
1075 GSocketError
_GAddress_translate_to(GAddress
*address
,
1076 struct sockaddr
**addr
, int *len
)
1078 if (!address
->m_addr
) {
1079 address
->m_error
= GSOCK_INVADDR
;
1080 return GSOCK_INVADDR
;
1083 *len
= address
->m_len
;
1084 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1085 if (*addr
== NULL
) {
1086 address
->m_error
= GSOCK_MEMERR
;
1087 return GSOCK_MEMERR
;
1090 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1091 return GSOCK_NOERROR
;
1095 * -------------------------------------------------------------------------
1096 * Internet address family
1097 * -------------------------------------------------------------------------
1100 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1102 address
->m_len
= sizeof(struct sockaddr_in
);
1103 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1104 if (address
->m_addr
== NULL
)
1106 address
->m_error
= GSOCK_MEMERR
;
1107 return GSOCK_MEMERR
;
1110 address
->m_family
= GSOCK_INET
;
1111 address
->m_realfamily
= PF_INET
;
1112 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1114 INADDR_BROADCAST is identical to INADDR_NONE which is not defined
1115 on all unices. INADDR_BROADCAST should be fine to indicate an error.
1117 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_BROADCAST
;
1119 return GSOCK_NOERROR
;
1122 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1125 struct in_addr
*addr
;
1127 assert(address
!= NULL
);
1129 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1131 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1133 /* If it is a numeric host name, convert it now */
1134 #if defined(HAVE_INET_ATON)
1135 if (inet_aton(hostname
, addr
) == 0)
1137 #elif defined(HAVE_INET_ADDR)
1138 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1139 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1142 /* Use gethostbyname by default */
1146 struct in_addr
*array_addr
;
1148 /* It is a real name, we solve it */
1149 if ((he
= gethostbyname(hostname
)) == NULL
)
1151 address
->m_error
= GSOCK_NOHOST
;
1152 return GSOCK_NOHOST
;
1154 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1155 addr
->s_addr
= array_addr
[0].s_addr
;
1157 return GSOCK_NOERROR
;
1160 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1161 unsigned long hostaddr
)
1163 struct in_addr
*addr
;
1165 assert(address
!= NULL
);
1167 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1169 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1170 addr
->s_addr
= hostaddr
;
1172 return GSOCK_NOERROR
;
1175 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1176 const char *protocol
)
1179 struct sockaddr_in
*addr
;
1181 assert(address
!= NULL
);
1182 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1186 address
->m_error
= GSOCK_INVPORT
;
1187 return GSOCK_INVPORT
;
1190 se
= getservbyname(port
, protocol
);
1192 if (isdigit(port
[0]))
1196 port_int
= atoi(port
);
1197 addr
= (struct sockaddr_in
*)address
->m_addr
;
1198 addr
->sin_port
= htons(port_int
);
1199 return GSOCK_NOERROR
;
1202 address
->m_error
= GSOCK_INVPORT
;
1203 return GSOCK_INVPORT
;
1206 addr
= (struct sockaddr_in
*)address
->m_addr
;
1207 addr
->sin_port
= se
->s_port
;
1209 return GSOCK_NOERROR
;
1212 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1214 struct sockaddr_in
*addr
;
1216 assert(address
!= NULL
);
1217 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1219 addr
= (struct sockaddr_in
*)address
->m_addr
;
1220 addr
->sin_port
= htons(port
);
1222 return GSOCK_NOERROR
;
1225 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1229 struct sockaddr_in
*addr
;
1231 assert(address
!= NULL
);
1232 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1234 addr
= (struct sockaddr_in
*)address
->m_addr
;
1235 addr_buf
= (char *)&(addr
->sin_addr
);
1237 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1240 address
->m_error
= GSOCK_NOHOST
;
1241 return GSOCK_NOHOST
;
1244 strncpy(hostname
, he
->h_name
, sbuf
);
1246 return GSOCK_NOERROR
;
1249 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1251 struct sockaddr_in
*addr
;
1253 assert(address
!= NULL
);
1254 CHECK_ADDRESS(address
, INET
, 0);
1256 addr
= (struct sockaddr_in
*)address
->m_addr
;
1258 return addr
->sin_addr
.s_addr
;
1261 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1263 struct sockaddr_in
*addr
;
1265 assert(address
!= NULL
);
1266 CHECK_ADDRESS(address
, INET
, 0);
1268 addr
= (struct sockaddr_in
*)address
->m_addr
;
1269 return ntohs(addr
->sin_port
);
1273 * -------------------------------------------------------------------------
1274 * Unix address family
1275 * -------------------------------------------------------------------------
1278 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1280 address
->m_len
= sizeof(struct sockaddr_un
);
1281 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1282 if (address
->m_addr
== NULL
)
1284 address
->m_error
= GSOCK_MEMERR
;
1285 return GSOCK_MEMERR
;
1288 address
->m_family
= GSOCK_UNIX
;
1289 address
->m_realfamily
= PF_UNIX
;
1290 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1291 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1296 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1298 struct sockaddr_un
*addr
;
1300 assert(address
!= NULL
);
1302 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1304 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1305 memcpy(addr
->sun_path
, path
, strlen(path
));
1307 return GSOCK_NOERROR
;
1310 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1312 struct sockaddr_un
*addr
;
1314 assert(address
!= NULL
);
1315 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1317 addr
= (struct sockaddr_un
*)address
->m_addr
;
1319 strncpy(path
, addr
->sun_path
, sbuf
);
1321 return GSOCK_NOERROR
;