]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main Unix-style file
6 * -------------------------------------------------------------------------
11 /* I don't see, why this include is needed, but it seems to be necessary
12 sometimes. For EMX, including C++ headers into plain C source breaks
13 compilation, so don't do it there. */
19 #define BSD_SELECT /* use Berkley Sockets select */
22 #include <sys\types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
29 #define HAVE_INET_ADDR
37 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
42 #include <sys\socket.h>
43 #include <sys\ioctl.h>
44 #include <sys\select.h>
46 #define soclose(a) close(a)
48 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
49 int _System
bsdselect(int,
54 int _System
soclose(int);
60 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
71 #include "wx/gsocket.h"
72 #include "wx/os2/gsockos2.h"
78 # define SOCKLEN_T socklen_t
81 # define SOCKLEN_T int
86 /* Global initialisers */
93 void GSocket_Cleanup()
97 /* Constructors / Destructors */
99 GSocket
*GSocket_new()
104 socket
= (GSocket
*)malloc(sizeof(GSocket
));
110 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
112 socket
->m_cbacks
[i
] = NULL
;
114 socket
->m_detected
= 0;
115 socket
->m_local
= NULL
;
116 socket
->m_peer
= NULL
;
117 socket
->m_error
= GSOCK_NOERROR
;
118 socket
->m_server
= 0;
119 socket
->m_stream
= 1;
120 socket
->m_gui_dependent
= NULL
;
121 socket
->m_non_blocking
= 0;
122 socket
->m_timeout
= 10*60*1000;
123 /* 10 minutes * 60 sec * 1000 millisec */
124 socket
->m_establishing
= 0;
129 void GSocket_destroy(GSocket
*socket
)
131 assert(socket
!= NULL
);
133 /* First, we check that the socket is really shutdowned */
134 if (socket
->m_fd
!= -1)
135 GSocket_Shutdown(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);
158 soclose(socket
->m_fd
);
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;
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
;
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
, &size
) < 0) {
231 socket
->m_error
= GSOCK_IOERR
;
235 address
= GAddress_new();
236 if (address
== NULL
) {
237 socket
->m_error
= GSOCK_MEMERR
;
240 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
241 if (socket
->m_error
!= GSOCK_NOERROR
) {
242 GAddress_destroy(address
);
249 GAddress
*GSocket_GetPeer(GSocket
*socket
)
251 assert(socket
!= NULL
);
254 return GAddress_copy(socket
->m_peer
);
259 /* Server specific parts */
261 /* GSocket_SetServer:
262 * Sets up the socket as a server. It uses the "Local" field of GSocket.
263 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
264 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
265 * been initialized, GSOCK_INVADDR if the local address has not been
266 * defined and GSOCK_IOERR for other internal errors.
268 GSocketError
GSocket_SetServer(GSocket
*sck
)
275 if (sck
->m_fd
!= -1) {
276 sck
->m_error
= GSOCK_INVSOCK
;
277 return GSOCK_INVSOCK
;
281 sck
->m_error
= GSOCK_INVADDR
;
282 return GSOCK_INVADDR
;
285 /* We always have a stream here */
289 /* Create the socket */
290 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
292 if (sck
->m_fd
== -1) {
293 sck
->m_error
= GSOCK_IOERR
;
297 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
299 /* Bind the socket to the LOCAL address */
300 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
303 sck
->m_error
= GSOCK_IOERR
;
307 /* Enable listening up to 5 connections */
308 if (listen(sck
->m_fd
, 5) < 0) {
311 sck
->m_error
= GSOCK_IOERR
;
315 return GSOCK_NOERROR
;
318 /* GSocket_WaitConnection:
319 * Waits for an incoming client connection.
321 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
326 assert(socket
!= NULL
);
328 /* Reenable CONNECTION events */
329 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
331 /* If the socket has already been created, we exit immediately */
332 if (socket
->m_fd
== -1 || !socket
->m_server
)
334 socket
->m_error
= GSOCK_INVSOCK
;
338 /* Create a GSocket object for the new connection */
339 connection
= GSocket_new();
342 connection
->m_error
= GSOCK_MEMERR
;
346 /* Accept the incoming connection */
347 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
349 GSocket_destroy(connection
);
350 /* socket->m_error set by _GSocket_Input_Timeout */
354 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
356 if (connection
->m_fd
== -1)
358 if (errno
== EWOULDBLOCK
)
359 socket
->m_error
= GSOCK_WOULDBLOCK
;
361 socket
->m_error
= GSOCK_IOERR
;
363 GSocket_destroy(connection
);
367 /* Initialize all fields */
368 connection
->m_server
= 0;
369 connection
->m_stream
= 1;
370 connection
->m_oriented
= 1;
372 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
376 /* Non oriented connections */
378 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
384 if (sck
->m_fd
!= -1) {
385 sck
->m_error
= GSOCK_INVSOCK
;
386 return GSOCK_INVSOCK
;
390 sck
->m_error
= GSOCK_INVADDR
;
391 return GSOCK_INVADDR
;
398 /* Create the socket */
399 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
402 sck
->m_error
= GSOCK_IOERR
;
406 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
408 /* Bind it to the LOCAL address */
409 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
412 sck
->m_error
= GSOCK_IOERR
;
416 return GSOCK_NOERROR
;
419 /* Client specific parts */
422 * Establishes a client connection to a server using the "Peer"
423 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
424 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
425 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
426 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
427 * the connection request can be completed later. Use GSocket_Select()
428 * to check or wait for a GSOCK_CONNECTION event.
430 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
437 /* Enable CONNECTION events (needed for nonblocking connections) */
438 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
442 sck
->m_error
= GSOCK_INVSOCK
;
443 return GSOCK_INVSOCK
;
448 sck
->m_error
= GSOCK_INVADDR
;
449 return GSOCK_INVADDR
;
452 /* Test whether we want the socket to be a stream (e.g. TCP) */
453 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
456 sck
->m_establishing
= 0;
463 /* Create the socket */
464 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
466 if (sck
->m_fd
== -1) {
467 sck
->m_error
= GSOCK_IOERR
;
471 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
473 /* Connect it to the PEER address */
474 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
480 /* If connect failed with EINPROGRESS and the GSocket object
481 * is in blocking mode, we select() for the specified timeout
482 * checking for writability to see if the connection request
485 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
487 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
491 /* sck->m_error is set in _GSocket_Output_Timeout */
492 fprintf(stderr
, "Blocking connect timeouts\n");
493 return GSOCK_TIMEDOUT
;
497 fprintf(stderr
, "Blocking connect OK\n");
498 return GSOCK_NOERROR
;
502 /* If connect failed with EINPROGRESS and the GSocket object
503 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
504 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
505 * this way if the connection completes, a GSOCK_CONNECTION
506 * event will be generated, if enabled.
508 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
510 sck
->m_error
= GSOCK_WOULDBLOCK
;
511 sck
->m_establishing
= 1;
512 fprintf(stderr
, "Nonblocking connect in progress\n");
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
;
524 fprintf(stderr
, "Connect failed (generic err)\n");
528 fprintf(stderr
, "Connect OK\n");
529 return GSOCK_NOERROR
;
534 /* Like recv(), send(), ... */
535 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
539 assert(socket
!= NULL
);
541 /* Reenable INPUT events */
542 _GSocket_Enable(socket
, GSOCK_INPUT
);
544 if (socket
->m_fd
== -1 || socket
->m_server
)
546 socket
->m_error
= GSOCK_INVSOCK
;
550 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
553 if (socket
->m_stream
)
554 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
556 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
560 if (errno
== EWOULDBLOCK
)
561 socket
->m_error
= GSOCK_WOULDBLOCK
;
563 socket
->m_error
= GSOCK_IOERR
;
569 int GSocket_Write(GSocket
*socket
, const char *buffer
,
574 assert(socket
!= NULL
);
576 if (socket
->m_fd
== -1 || socket
->m_server
)
578 socket
->m_error
= GSOCK_INVSOCK
;
582 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
585 if (socket
->m_stream
)
586 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
588 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
592 if (errno
== EWOULDBLOCK
)
593 socket
->m_error
= GSOCK_WOULDBLOCK
;
595 socket
->m_error
= GSOCK_IOERR
;
597 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
598 * in MSW). Once the first OUTPUT event is received, users can assume
599 * that the socket is writable until a read operation fails. Only then
600 * will further OUTPUT events be posted.
602 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
609 * Polls the socket to determine its status. This function will
610 * check for the events specified in the 'flags' parameter, and
611 * it will return a mask indicating which operations can be
612 * performed. This function won't block, regardless of the
613 * mode (blocking|nonblocking) of the socket.
615 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
617 assert(socket
!= NULL
);
619 return (flags
& socket
->m_detected
);
624 /* GSocket_SetNonBlocking:
625 * Sets the socket to non-blocking mode. This is useful if
626 * we don't want to wait.
628 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
630 assert(socket
!= NULL
);
632 socket
->m_non_blocking
= non_block
;
635 /* GSocket_SetTimeout:
636 * Sets the timeout for blocking calls. Time is
637 * expressed in milliseconds.
639 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
641 assert(socket
!= NULL
);
643 socket
->m_timeout
= millisec
;
647 * Returns the last error occured for this socket.
649 GSocketError
GSocket_GetError(GSocket
*socket
)
651 assert(socket
!= NULL
);
653 return socket
->m_error
;
658 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
659 * and LOST). The callbacks are called in the following situations:
661 * INPUT: There is at least one byte in the input buffer
662 * OUTPUT: The system is sure that the next write call will not block
663 * CONNECTION: Two cases are possible:
664 * Client socket -> the connection is established
665 * Server socket -> a client requests a connection
666 * LOST: The connection is lost
668 * An event is generated only once and its state is reseted when the
669 * relative IO call is requested.
670 * For example: INPUT -> GSocket_Read()
671 * CONNECTION -> GSocket_Accept()
674 /* GSocket_SetCallback:
675 * Enables the callbacks specified by 'flags'. Note that 'flags'
676 * may be a combination of flags OR'ed toghether, so the same
677 * callback function can be made to accept different events.
678 * The callback function must have the following prototype:
680 * void function(GSocket *socket, GSocketEvent event, char *cdata)
682 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
683 GSocketCallback callback
, char *cdata
)
687 assert(socket
!= NULL
);
689 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
691 if ((flags
& (1 << count
)) != 0)
693 socket
->m_cbacks
[count
] = callback
;
694 socket
->m_data
[count
] = cdata
;
699 /* GSocket_UnsetCallback:
700 * Disables all callbacks specified by 'flags', which may be a
701 * combination of flags OR'ed toghether.
703 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
707 assert(socket
!= NULL
);
709 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
711 if ((flags
& (1 << count
)) != 0)
713 socket
->m_cbacks
[count
] = NULL
;
714 socket
->m_data
[count
] = NULL
;
719 #define CALL_CALLBACK(socket, event) { \
720 _GSocket_Disable(socket, event); \
721 if (socket->m_cbacks[event]) \
722 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
726 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
728 socket
->m_detected
&= ~(1 << event
);
732 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
734 socket
->m_detected
|= (1 << event
);
737 /* _GSocket_Input_Timeout:
738 * For blocking sockets, wait until data is available or
739 * until timeout ellapses.
741 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
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 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
772 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
774 if (!socket
->m_non_blocking
)
777 FD_SET(socket
->m_fd
, &writefds
);
778 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
780 socket
->m_error
= GSOCK_TIMEDOUT
;
781 return GSOCK_TIMEDOUT
;
784 return GSOCK_NOERROR
;
787 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
791 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
796 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
798 struct sockaddr from
;
803 fromlen
= sizeof(from
);
805 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
810 /* Translate a system address into a GSocket address */
813 socket
->m_peer
= GAddress_new();
816 socket
->m_error
= GSOCK_MEMERR
;
820 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
821 if (err
!= GSOCK_NOERROR
)
823 GAddress_destroy(socket
->m_peer
);
824 socket
->m_peer
= NULL
;
825 socket
->m_error
= err
;
832 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
837 ret
= send(socket
->m_fd
, (char*)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
;
859 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
861 /* Frees memory allocated from _GAddress_translate_to */
867 void _GSocket_Detected_Read(GSocket
*socket
)
872 if (socket
->m_stream
)
874 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
876 if (ret
< 0 && socket
->m_server
)
878 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
884 CALL_CALLBACK(socket
, GSOCK_INPUT
);
888 CALL_CALLBACK(socket
, GSOCK_LOST
);
893 void _GSocket_Detected_Write(GSocket
*socket
)
895 if (socket
->m_establishing
&& !socket
->m_server
)
899 socket
->m_establishing
= 0;
902 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
906 CALL_CALLBACK(socket
, GSOCK_LOST
);
910 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
911 /* We have to fire this event by hand because CONNECTION (for clients)
912 * and OUTPUT are internally the same and we just disabled CONNECTION
913 * events with the above macro.
915 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
920 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
925 * -------------------------------------------------------------------------
927 * -------------------------------------------------------------------------
930 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
931 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
932 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
934 #define CHECK_ADDRESS(address, family, retval) \
936 if (address->m_family == GSOCK_NOFAMILY) \
937 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
938 return address->m_error; \
940 if (address->m_family != GSOCK_##family) {\
941 address->m_error = GSOCK_INVADDR; \
946 GAddress
*GAddress_new()
950 address
= (GAddress
*)malloc(sizeof(GAddress
));
955 address
->m_family
= GSOCK_NOFAMILY
;
956 address
->m_addr
= NULL
;
962 GAddress
*GAddress_copy(GAddress
*address
)
966 assert(address
!= NULL
);
968 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
973 memcpy(addr2
, address
, sizeof(GAddress
));
975 if (address
->m_addr
) {
976 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
977 if (addr2
->m_addr
== NULL
) {
981 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
987 void GAddress_destroy(GAddress
*address
)
989 assert(address
!= NULL
);
994 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
996 assert(address
!= NULL
);
998 address
->m_family
= type
;
1001 GAddressType
GAddress_GetFamily(GAddress
*address
)
1003 assert(address
!= NULL
);
1005 return address
->m_family
;
1008 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1009 address
->m_realfamily
= addr
->sa_family
;
1010 switch (addr
->sa_family
) {
1012 address
->m_family
= GSOCK_INET
;
1015 address
->m_family
= GSOCK_UNIX
;
1019 address
->m_family
= GSOCK_INET6
;
1024 address
->m_error
= GSOCK_INVOP
;
1029 if (address
->m_addr
)
1030 free(address
->m_addr
);
1032 address
->m_len
= len
;
1033 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1034 if (address
->m_addr
== NULL
) {
1035 address
->m_error
= GSOCK_MEMERR
;
1036 return GSOCK_MEMERR
;
1038 memcpy(address
->m_addr
, addr
, len
);
1040 return GSOCK_NOERROR
;
1043 GSocketError
_GAddress_translate_to(GAddress
*address
,
1044 struct sockaddr
**addr
, int *len
)
1046 if (!address
->m_addr
) {
1047 address
->m_error
= GSOCK_INVADDR
;
1048 return GSOCK_INVADDR
;
1051 *len
= address
->m_len
;
1052 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1053 if (*addr
== NULL
) {
1054 address
->m_error
= GSOCK_MEMERR
;
1055 return GSOCK_MEMERR
;
1058 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1059 return GSOCK_NOERROR
;
1063 * -------------------------------------------------------------------------
1064 * Internet address family
1065 * -------------------------------------------------------------------------
1068 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1070 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1071 if (address
->m_addr
== NULL
) {
1072 address
->m_error
= GSOCK_MEMERR
;
1073 return GSOCK_MEMERR
;
1076 address
->m_len
= sizeof(struct sockaddr_in
);
1078 address
->m_family
= GSOCK_INET
;
1079 address
->m_realfamily
= PF_INET
;
1080 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1081 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1083 return GSOCK_NOERROR
;
1086 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1089 struct in_addr
*addr
;
1091 assert(address
!= NULL
);
1093 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1095 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1097 /* If it is a numeric host name, convert it now */
1098 #if defined(HAVE_INET_ATON)
1099 if (inet_aton(hostname
, addr
) == 0) {
1100 #elif defined(HAVE_INET_ADDR)
1101 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1102 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1104 struct in_addr
*array_addr
;
1106 /* It is a real name, we solve it */
1107 he
= gethostbyname((char*)hostname
);
1109 address
->m_error
= GSOCK_NOHOST
;
1110 return GSOCK_NOHOST
;
1112 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1113 addr
->s_addr
= array_addr
[0].s_addr
;
1114 #if defined(HAVE_INET_ATON)
1116 #elif defined(HAVE_INET_ADDR)
1119 return GSOCK_NOERROR
;
1122 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1124 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1127 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1128 unsigned long hostaddr
)
1130 struct in_addr
*addr
;
1132 assert(address
!= NULL
);
1134 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1136 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1137 addr
->s_addr
= hostaddr
;
1139 return GSOCK_NOERROR
;
1142 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1143 const char *protocol
)
1146 struct sockaddr_in
*addr
;
1148 assert(address
!= NULL
);
1149 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1152 address
->m_error
= GSOCK_INVPORT
;
1153 return GSOCK_INVPORT
;
1156 se
= getservbyname((char*)port
, (char*)protocol
);
1158 if (isdigit(port
[0])) {
1161 port_int
= atoi(port
);
1162 addr
= (struct sockaddr_in
*)address
->m_addr
;
1163 addr
->sin_port
= htons(port_int
);
1164 return GSOCK_NOERROR
;
1167 address
->m_error
= GSOCK_INVPORT
;
1168 return GSOCK_INVPORT
;
1171 addr
= (struct sockaddr_in
*)address
->m_addr
;
1172 addr
->sin_port
= se
->s_port
;
1174 return GSOCK_NOERROR
;
1177 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1179 struct sockaddr_in
*addr
;
1181 assert(address
!= NULL
);
1182 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1184 addr
= (struct sockaddr_in
*)address
->m_addr
;
1185 addr
->sin_port
= htons(port
);
1187 return GSOCK_NOERROR
;
1190 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1194 struct sockaddr_in
*addr
;
1196 assert(address
!= NULL
);
1197 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1199 addr
= (struct sockaddr_in
*)address
->m_addr
;
1200 addr_buf
= (char *)&(addr
->sin_addr
);
1202 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1204 address
->m_error
= GSOCK_NOHOST
;
1205 return GSOCK_NOHOST
;
1208 strncpy(hostname
, he
->h_name
, sbuf
);
1210 return GSOCK_NOERROR
;
1213 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1215 struct sockaddr_in
*addr
;
1217 assert(address
!= NULL
);
1218 CHECK_ADDRESS(address
, INET
, 0);
1220 addr
= (struct sockaddr_in
*)address
->m_addr
;
1222 return addr
->sin_addr
.s_addr
;
1225 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1227 struct sockaddr_in
*addr
;
1229 assert(address
!= NULL
);
1230 CHECK_ADDRESS(address
, INET
, 0);
1232 addr
= (struct sockaddr_in
*)address
->m_addr
;
1233 return ntohs(addr
->sin_port
);
1237 * -------------------------------------------------------------------------
1238 * Unix address family
1239 * -------------------------------------------------------------------------
1242 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1244 assert (address
!= NULL
);
1245 address
->m_error
= GSOCK_INVADDR
;
1246 return GSOCK_INVADDR
;
1249 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1251 assert (address
!= NULL
);
1252 address
->m_error
= GSOCK_INVADDR
;
1253 return GSOCK_INVADDR
;
1256 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1258 assert (address
!= NULL
);
1259 address
->m_error
= GSOCK_INVADDR
;
1260 return GSOCK_INVADDR
;