]>
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>
18 #define SOCK_LEN_TYP (unsigned int*)
21 u_char sun_len
; /* sockaddr len including null */
22 u_char sun_family
; /* AF_UNIX */
23 char sun_path
[108]; /* path name (gag) */
26 #include <sys/socket.h>
28 #define SOCK_LEN_TYP (int*)
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
43 # include <sys/filio.h>
52 #include "wx/gsocket.h"
53 #include "wx/unix/gsockunx.h"
59 # define SOCKLEN_T socklen_t
62 # define SOCKLEN_T int
67 #define MASK_SIGNAL() \
69 void (*old_handler)(int); \
71 old_handler = signal(SIGPIPE, SIG_IGN);
73 #define UNMASK_SIGNAL() \
74 signal(SIGPIPE, old_handler); \
78 /* Global initialisers */
85 void GSocket_Cleanup()
89 /* Constructors / Destructors */
91 GSocket
*GSocket_new()
96 socket
= (GSocket
*)malloc(sizeof(GSocket
));
102 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
104 socket
->m_cbacks
[i
] = NULL
;
106 socket
->m_detected
= 0;
107 socket
->m_local
= NULL
;
108 socket
->m_peer
= NULL
;
109 socket
->m_error
= GSOCK_NOERROR
;
110 socket
->m_server
= FALSE
;
111 socket
->m_stream
= TRUE
;
112 socket
->m_gui_dependent
= NULL
;
113 socket
->m_non_blocking
= FALSE
;
114 socket
->m_timeout
= 10*60*1000;
115 /* 10 minutes * 60 sec * 1000 millisec */
116 socket
->m_establishing
= FALSE
;
118 /* We initialize the GUI specific entries here */
119 _GSocket_GUI_Init(socket
);
124 void GSocket_destroy(GSocket
*socket
)
126 assert(socket
!= NULL
);
128 /* First, we check that the socket is really shutdowned */
129 if (socket
->m_fd
!= -1)
130 GSocket_Shutdown(socket
);
132 /* We destroy GUI specific variables */
133 _GSocket_GUI_Destroy(socket
);
135 /* We destroy private addresses */
137 GAddress_destroy(socket
->m_local
);
140 GAddress_destroy(socket
->m_peer
);
142 /* We destroy socket itself */
146 void GSocket_Shutdown(GSocket
*socket
)
150 assert(socket
!= NULL
);
152 /* If socket has been created, we shutdown it */
153 if (socket
->m_fd
!= -1)
155 shutdown(socket
->m_fd
, 2);
160 /* We also disable GUI callbacks */
161 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
162 socket
->m_cbacks
[evt
] = NULL
;
164 socket
->m_detected
= 0;
165 _GSocket_Disable_Events(socket
);
168 /* Address handling */
170 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
172 assert(socket
!= NULL
);
174 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
175 socket
->m_error
= GSOCK_INVSOCK
;
176 return GSOCK_INVSOCK
;
179 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
180 socket
->m_error
= GSOCK_INVADDR
;
181 return GSOCK_INVADDR
;
185 GAddress_destroy(socket
->m_local
);
187 socket
->m_local
= GAddress_copy(address
);
189 return GSOCK_NOERROR
;
192 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
194 assert(socket
!= NULL
);
196 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
197 socket
->m_error
= GSOCK_INVADDR
;
198 return GSOCK_INVADDR
;
202 GAddress_destroy(socket
->m_peer
);
204 socket
->m_peer
= GAddress_copy(address
);
206 return GSOCK_NOERROR
;
209 GAddress
*GSocket_GetLocal(GSocket
*socket
)
212 struct sockaddr addr
;
216 assert(socket
!= NULL
);
219 return GAddress_copy(socket
->m_local
);
221 if (socket
->m_fd
== -1) {
222 socket
->m_error
= GSOCK_INVSOCK
;
228 if (getsockname(socket
->m_fd
, &addr
, SOCK_LEN_TYP
&size
) < 0) {
229 socket
->m_error
= GSOCK_IOERR
;
233 address
= GAddress_new();
234 if (address
== NULL
) {
235 socket
->m_error
= GSOCK_MEMERR
;
238 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
239 if (socket
->m_error
!= GSOCK_NOERROR
) {
240 GAddress_destroy(address
);
247 GAddress
*GSocket_GetPeer(GSocket
*socket
)
249 assert(socket
!= NULL
);
252 return GAddress_copy(socket
->m_peer
);
257 /* Server specific parts */
259 /* GSocket_SetServer:
260 * Sets up the socket as a server. It uses the "Local" field of GSocket.
261 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
262 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
263 * been initialized, GSOCK_INVADDR if the local address has not been
264 * defined and GSOCK_IOERR for other internal errors.
266 GSocketError
GSocket_SetServer(GSocket
*sck
)
273 if (sck
->m_fd
!= -1) {
274 sck
->m_error
= GSOCK_INVSOCK
;
275 return GSOCK_INVSOCK
;
279 sck
->m_error
= GSOCK_INVADDR
;
280 return GSOCK_INVADDR
;
283 /* We always have a stream here */
284 sck
->m_stream
= TRUE
;
285 sck
->m_server
= TRUE
;
287 /* Create the socket */
288 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
290 if (sck
->m_fd
== -1) {
291 sck
->m_error
= GSOCK_IOERR
;
295 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
296 _GSocket_Enable_Events(sck
);
298 /* Bind the socket to the LOCAL address */
299 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
302 sck
->m_error
= GSOCK_IOERR
;
306 /* Enable listening up to 5 connections */
307 if (listen(sck
->m_fd
, 5) < 0) {
310 sck
->m_error
= GSOCK_IOERR
;
314 return GSOCK_NOERROR
;
317 /* GSocket_WaitConnection:
318 * Waits for an incoming client connection.
320 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
325 assert(socket
!= NULL
);
327 /* Reenable CONNECTION events */
328 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
330 /* If the socket has already been created, we exit immediately */
331 if (socket
->m_fd
== -1 || !socket
->m_server
)
333 socket
->m_error
= GSOCK_INVSOCK
;
337 /* Create a GSocket object for the new connection */
338 connection
= GSocket_new();
341 connection
->m_error
= GSOCK_MEMERR
;
345 /* Accept the incoming connection */
346 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
348 GSocket_destroy(connection
);
349 /* socket->m_error set by _GSocket_Input_Timeout */
353 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
355 if (connection
->m_fd
== -1)
357 if (errno
== EWOULDBLOCK
)
358 socket
->m_error
= GSOCK_WOULDBLOCK
;
360 socket
->m_error
= GSOCK_IOERR
;
362 GSocket_destroy(connection
);
366 /* Initialize all fields */
367 connection
->m_server
= FALSE
;
368 connection
->m_stream
= TRUE
;
369 connection
->m_oriented
= TRUE
;
371 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
372 _GSocket_Enable_Events(connection
);
377 /* Non oriented connections */
379 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
385 if (sck
->m_fd
!= -1) {
386 sck
->m_error
= GSOCK_INVSOCK
;
387 return GSOCK_INVSOCK
;
391 sck
->m_error
= GSOCK_INVADDR
;
392 return GSOCK_INVADDR
;
395 sck
->m_stream
= FALSE
;
396 sck
->m_server
= FALSE
;
397 sck
->m_oriented
= FALSE
;
399 /* Create the socket */
400 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
403 sck
->m_error
= GSOCK_IOERR
;
407 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
408 _GSocket_Enable_Events(sck
);
410 /* Bind it to the LOCAL address */
411 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
414 sck
->m_error
= GSOCK_IOERR
;
418 return GSOCK_NOERROR
;
421 /* Client specific parts */
424 * Establishes a client connection to a server using the "Peer"
425 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
426 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
427 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
428 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
429 * the connection request can be completed later. Use GSocket_Select()
430 * to check or wait for a GSOCK_CONNECTION event.
432 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
439 /* Enable CONNECTION events (needed for nonblocking connections) */
440 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
444 sck
->m_error
= GSOCK_INVSOCK
;
445 return GSOCK_INVSOCK
;
450 sck
->m_error
= GSOCK_INVADDR
;
451 return GSOCK_INVADDR
;
454 /* Test whether we want the socket to be a stream (e.g. TCP) */
455 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
456 sck
->m_oriented
= TRUE
;
457 sck
->m_server
= FALSE
;
458 sck
->m_establishing
= FALSE
;
465 /* Create the socket */
466 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
468 if (sck
->m_fd
== -1) {
469 sck
->m_error
= GSOCK_IOERR
;
473 ioctl(sck
->m_fd
, FIONBIO
, &arg
);
474 _GSocket_Enable_Events(sck
);
476 /* Connect it to the PEER address */
477 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
483 /* If connect failed with EINPROGRESS and the GSocket object
484 * is in blocking mode, we select() for the specified timeout
485 * checking for writability to see if the connection request
488 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
490 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
494 /* sck->m_error is set in _GSocket_Output_Timeout */
495 return GSOCK_TIMEDOUT
;
499 return GSOCK_NOERROR
;
503 /* If connect failed with EINPROGRESS and the GSocket object
504 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
505 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
506 * this way if the connection completes, a GSOCK_CONNECTION
507 * event will be generated, if enabled.
509 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
511 sck
->m_error
= GSOCK_WOULDBLOCK
;
512 sck
->m_establishing
= TRUE
;
514 return GSOCK_WOULDBLOCK
;
517 /* If connect failed with an error other than EINPROGRESS,
518 * then the call to GSocket_Connect has failed.
522 sck
->m_error
= GSOCK_IOERR
;
527 return GSOCK_NOERROR
;
532 /* Like recv(), send(), ... */
533 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
537 assert(socket
!= NULL
);
539 /* Reenable INPUT events */
540 _GSocket_Enable(socket
, GSOCK_INPUT
);
542 if (socket
->m_fd
== -1 || socket
->m_server
)
544 socket
->m_error
= GSOCK_INVSOCK
;
548 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
551 if (socket
->m_stream
)
552 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
554 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
558 if (errno
== EWOULDBLOCK
)
559 socket
->m_error
= GSOCK_WOULDBLOCK
;
561 socket
->m_error
= GSOCK_IOERR
;
567 int GSocket_Write(GSocket
*socket
, const char *buffer
,
572 assert(socket
!= NULL
);
574 if (socket
->m_fd
== -1 || socket
->m_server
)
576 socket
->m_error
= GSOCK_INVSOCK
;
580 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
583 if (socket
->m_stream
)
584 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
586 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
590 if (errno
== EWOULDBLOCK
)
591 socket
->m_error
= GSOCK_WOULDBLOCK
;
593 socket
->m_error
= GSOCK_IOERR
;
595 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
596 * in MSW). Once the first OUTPUT event is received, users can assume
597 * that the socket is writable until a read operation fails. Only then
598 * will further OUTPUT events be posted.
600 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
607 * Polls the socket to determine its status. This function will
608 * check for the events specified in the 'flags' parameter, and
609 * it will return a mask indicating which operations can be
610 * performed. This function won't block, regardless of the
611 * mode (blocking|nonblocking) of the socket.
613 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
615 assert(socket
!= NULL
);
617 return (flags
& socket
->m_detected
);
622 /* GSocket_SetNonBlocking:
623 * Sets the socket to non-blocking mode. This is useful if
624 * we don't want to wait.
626 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
628 assert(socket
!= NULL
);
630 socket
->m_non_blocking
= non_block
;
633 /* GSocket_SetTimeout:
634 * Sets the timeout for blocking calls. Time is
635 * expressed in milliseconds.
637 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
639 assert(socket
!= NULL
);
641 socket
->m_timeout
= millisec
;
645 * Returns the last error occured for this socket.
647 GSocketError
GSocket_GetError(GSocket
*socket
)
649 assert(socket
!= NULL
);
651 return socket
->m_error
;
656 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
657 * and LOST). The callbacks are called in the following situations:
659 * INPUT: There is at least one byte in the input buffer
660 * OUTPUT: The system is sure that the next write call will not block
661 * CONNECTION: Two cases are possible:
662 * Client socket -> the connection is established
663 * Server socket -> a client requests a connection
664 * LOST: The connection is lost
666 * An event is generated only once and its state is reseted when the
667 * relative IO call is requested.
668 * For example: INPUT -> GSocket_Read()
669 * CONNECTION -> GSocket_Accept()
672 /* GSocket_SetCallback:
673 * Enables the callbacks specified by 'flags'. Note that 'flags'
674 * may be a combination of flags OR'ed toghether, so the same
675 * callback function can be made to accept different events.
676 * The callback function must have the following prototype:
678 * void function(GSocket *socket, GSocketEvent event, char *cdata)
680 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
681 GSocketCallback callback
, char *cdata
)
685 assert(socket
!= NULL
);
687 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
689 if ((flags
& (1 << count
)) != 0)
691 socket
->m_cbacks
[count
] = callback
;
692 socket
->m_data
[count
] = cdata
;
697 /* GSocket_UnsetCallback:
698 * Disables all callbacks specified by 'flags', which may be a
699 * combination of flags OR'ed toghether.
701 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
705 assert(socket
!= NULL
);
707 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
709 if ((flags
& (1 << count
)) != 0)
711 socket
->m_cbacks
[count
] = NULL
;
712 socket
->m_data
[count
] = NULL
;
717 #define CALL_CALLBACK(socket, event) { \
718 _GSocket_Disable(socket, event); \
719 if (socket->m_cbacks[event]) \
720 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
724 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
726 socket
->m_detected
&= ~(1 << event
);
727 _GSocket_Install_Callback(socket
, event
);
730 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
732 socket
->m_detected
|= (1 << event
);
733 _GSocket_Uninstall_Callback(socket
, event
);
736 /* _GSocket_Input_Timeout:
737 * For blocking sockets, wait until data is available or
738 * until timeout ellapses.
740 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
745 /* Linux select() will overwrite the struct on return */
746 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
747 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
749 if (!socket
->m_non_blocking
)
752 FD_SET(socket
->m_fd
, &readfds
);
753 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
755 socket
->m_error
= GSOCK_TIMEDOUT
;
756 return GSOCK_TIMEDOUT
;
759 return GSOCK_NOERROR
;
762 /* _GSocket_Output_Timeout:
763 * For blocking sockets, wait until data can be sent without
764 * blocking or until timeout ellapses.
766 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
771 /* Linux select() will overwrite the struct on return */
772 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
773 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
775 if (!socket
->m_non_blocking
)
778 FD_SET(socket
->m_fd
, &writefds
);
779 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
781 socket
->m_error
= GSOCK_TIMEDOUT
;
782 return GSOCK_TIMEDOUT
;
785 return GSOCK_NOERROR
;
788 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
793 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
799 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
801 struct sockaddr from
;
806 fromlen
= sizeof(from
);
809 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
,
810 SOCK_LEN_TYP
&fromlen
);
816 /* Translate a system address into a GSocket address */
819 socket
->m_peer
= GAddress_new();
822 socket
->m_error
= GSOCK_MEMERR
;
826 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
827 if (err
!= GSOCK_NOERROR
)
829 GAddress_destroy(socket
->m_peer
);
830 socket
->m_peer
= NULL
;
831 socket
->m_error
= err
;
838 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
844 ret
= send(socket
->m_fd
, buffer
, size
, 0);
850 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
852 struct sockaddr
*addr
;
856 if (!socket
->m_peer
) {
857 socket
->m_error
= GSOCK_INVADDR
;
861 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
862 if (err
!= GSOCK_NOERROR
) {
863 socket
->m_error
= err
;
868 ret
= sendto(socket
->m_fd
, buffer
, size
, 0, addr
, len
);
871 /* Frees memory allocated from _GAddress_translate_to */
877 void _GSocket_Detected_Read(GSocket
*socket
)
882 if (socket
->m_stream
)
884 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
886 if (ret
< 0 && socket
->m_server
)
888 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
894 CALL_CALLBACK(socket
, GSOCK_INPUT
);
898 CALL_CALLBACK(socket
, GSOCK_LOST
);
903 void _GSocket_Detected_Write(GSocket
*socket
)
905 if (socket
->m_establishing
&& !socket
->m_server
)
909 socket
->m_establishing
= FALSE
;
912 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (void*) &error
,
917 CALL_CALLBACK(socket
, GSOCK_LOST
);
921 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
922 /* We have to fire this event by hand because CONNECTION (for clients)
923 * and OUTPUT are internally the same and we just disabled CONNECTION
924 * events with the above macro.
926 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
931 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
936 * -------------------------------------------------------------------------
938 * -------------------------------------------------------------------------
941 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
942 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
943 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
945 #define CHECK_ADDRESS(address, family, retval) \
947 if (address->m_family == GSOCK_NOFAMILY) \
948 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
949 return address->m_error; \
951 if (address->m_family != GSOCK_##family) {\
952 address->m_error = GSOCK_INVADDR; \
957 GAddress
*GAddress_new()
961 address
= (GAddress
*)malloc(sizeof(GAddress
));
966 address
->m_family
= GSOCK_NOFAMILY
;
967 address
->m_addr
= NULL
;
973 GAddress
*GAddress_copy(GAddress
*address
)
977 assert(address
!= NULL
);
979 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
984 memcpy(addr2
, address
, sizeof(GAddress
));
986 if (address
->m_addr
) {
987 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
988 if (addr2
->m_addr
== NULL
) {
992 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
998 void GAddress_destroy(GAddress
*address
)
1000 assert(address
!= NULL
);
1005 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1007 assert(address
!= NULL
);
1009 address
->m_family
= type
;
1012 GAddressType
GAddress_GetFamily(GAddress
*address
)
1014 assert(address
!= NULL
);
1016 return address
->m_family
;
1019 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1020 address
->m_realfamily
= addr
->sa_family
;
1021 switch (addr
->sa_family
) {
1023 address
->m_family
= GSOCK_INET
;
1026 address
->m_family
= GSOCK_UNIX
;
1030 address
->m_family
= GSOCK_INET6
;
1035 address
->m_error
= GSOCK_INVOP
;
1040 if (address
->m_addr
)
1041 free(address
->m_addr
);
1043 address
->m_len
= len
;
1044 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1045 if (address
->m_addr
== NULL
) {
1046 address
->m_error
= GSOCK_MEMERR
;
1047 return GSOCK_MEMERR
;
1049 memcpy(address
->m_addr
, addr
, len
);
1051 return GSOCK_NOERROR
;
1054 GSocketError
_GAddress_translate_to(GAddress
*address
,
1055 struct sockaddr
**addr
, int *len
)
1057 if (!address
->m_addr
) {
1058 address
->m_error
= GSOCK_INVADDR
;
1059 return GSOCK_INVADDR
;
1062 *len
= address
->m_len
;
1063 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1064 if (*addr
== NULL
) {
1065 address
->m_error
= GSOCK_MEMERR
;
1066 return GSOCK_MEMERR
;
1069 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1070 return GSOCK_NOERROR
;
1074 * -------------------------------------------------------------------------
1075 * Internet address family
1076 * -------------------------------------------------------------------------
1079 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1081 address
->m_len
= sizeof(struct sockaddr_in
);
1082 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1083 if (address
->m_addr
== NULL
)
1085 address
->m_error
= GSOCK_MEMERR
;
1086 return GSOCK_MEMERR
;
1089 address
->m_family
= GSOCK_INET
;
1090 address
->m_realfamily
= PF_INET
;
1091 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1093 INADDR_BROADCAST is identical to INADDR_NONE which is not defined
1094 on all unices. INADDR_BROADCAST should be fine to indicate an error.
1096 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_BROADCAST
;
1098 return GSOCK_NOERROR
;
1101 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1104 struct in_addr
*addr
;
1106 assert(address
!= NULL
);
1108 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1110 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1112 /* If it is a numeric host name, convert it now */
1113 #if defined(HAVE_INET_ATON)
1114 if (inet_aton(hostname
, addr
) == 0)
1116 #elif defined(HAVE_INET_ADDR)
1117 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1118 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 )
1121 /* Use gethostbyname by default */
1125 struct in_addr
*array_addr
;
1127 /* It is a real name, we solve it */
1128 if ((he
= gethostbyname(hostname
)) == NULL
)
1130 address
->m_error
= GSOCK_NOHOST
;
1131 return GSOCK_NOHOST
;
1133 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1134 addr
->s_addr
= array_addr
[0].s_addr
;
1136 return GSOCK_NOERROR
;
1139 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1140 unsigned long hostaddr
)
1142 struct in_addr
*addr
;
1144 assert(address
!= NULL
);
1146 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1148 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1149 addr
->s_addr
= hostaddr
;
1151 return GSOCK_NOERROR
;
1154 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1155 const char *protocol
)
1158 struct sockaddr_in
*addr
;
1160 assert(address
!= NULL
);
1161 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1165 address
->m_error
= GSOCK_INVPORT
;
1166 return GSOCK_INVPORT
;
1169 se
= getservbyname(port
, protocol
);
1171 if (isdigit(port
[0]))
1175 port_int
= atoi(port
);
1176 addr
= (struct sockaddr_in
*)address
->m_addr
;
1177 addr
->sin_port
= htons(port_int
);
1178 return GSOCK_NOERROR
;
1181 address
->m_error
= GSOCK_INVPORT
;
1182 return GSOCK_INVPORT
;
1185 addr
= (struct sockaddr_in
*)address
->m_addr
;
1186 addr
->sin_port
= se
->s_port
;
1188 return GSOCK_NOERROR
;
1191 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1193 struct sockaddr_in
*addr
;
1195 assert(address
!= NULL
);
1196 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1198 addr
= (struct sockaddr_in
*)address
->m_addr
;
1199 addr
->sin_port
= htons(port
);
1201 return GSOCK_NOERROR
;
1204 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1208 struct sockaddr_in
*addr
;
1210 assert(address
!= NULL
);
1211 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1213 addr
= (struct sockaddr_in
*)address
->m_addr
;
1214 addr_buf
= (char *)&(addr
->sin_addr
);
1216 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1219 address
->m_error
= GSOCK_NOHOST
;
1220 return GSOCK_NOHOST
;
1223 strncpy(hostname
, he
->h_name
, sbuf
);
1225 return GSOCK_NOERROR
;
1228 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1230 struct sockaddr_in
*addr
;
1232 assert(address
!= NULL
);
1233 CHECK_ADDRESS(address
, INET
, 0);
1235 addr
= (struct sockaddr_in
*)address
->m_addr
;
1237 return addr
->sin_addr
.s_addr
;
1240 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1242 struct sockaddr_in
*addr
;
1244 assert(address
!= NULL
);
1245 CHECK_ADDRESS(address
, INET
, 0);
1247 addr
= (struct sockaddr_in
*)address
->m_addr
;
1248 return ntohs(addr
->sin_port
);
1252 * -------------------------------------------------------------------------
1253 * Unix address family
1254 * -------------------------------------------------------------------------
1257 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1259 address
->m_len
= sizeof(struct sockaddr_un
);
1260 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1261 if (address
->m_addr
== NULL
)
1263 address
->m_error
= GSOCK_MEMERR
;
1264 return GSOCK_MEMERR
;
1267 address
->m_family
= GSOCK_UNIX
;
1268 address
->m_realfamily
= PF_UNIX
;
1269 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1270 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1275 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1277 struct sockaddr_un
*addr
;
1279 assert(address
!= NULL
);
1281 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1283 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1284 memcpy(addr
->sun_path
, path
, strlen(path
));
1286 return GSOCK_NOERROR
;
1289 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1291 struct sockaddr_un
*addr
;
1293 assert(address
!= NULL
);
1294 CHECK_ADDRESS(address
, UNIX
, GSOCK_INVADDR
);
1296 addr
= (struct sockaddr_un
*)address
->m_addr
;
1298 strncpy(path
, addr
->sun_path
, sbuf
);
1300 return GSOCK_NOERROR
;