]>
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
38 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
39 #include <machine\endian.h>
45 #include <sys\socket.h>
46 #include <sys\ioctl.h>
47 #include <sys\select.h>
49 #define soclose(a) close(a)
51 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
52 int _System
bsdselect(int,
57 int _System
soclose(int);
63 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
74 #include "wx/gsocket.h"
75 #include "wx/os2/gsockos2.h"
81 # define SOCKLEN_T socklen_t
84 # define SOCKLEN_T int
89 /* Global initialisers */
96 void GSocket_Cleanup()
100 /* Constructors / Destructors */
102 GSocket
*GSocket_new()
107 socket
= (GSocket
*)malloc(sizeof(GSocket
));
113 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
115 socket
->m_cbacks
[i
] = NULL
;
117 socket
->m_detected
= 0;
118 socket
->m_local
= NULL
;
119 socket
->m_peer
= NULL
;
120 socket
->m_error
= GSOCK_NOERROR
;
121 socket
->m_server
= 0;
122 socket
->m_stream
= 1;
123 socket
->m_gui_dependent
= NULL
;
124 socket
->m_non_blocking
= 0;
125 socket
->m_timeout
= 10*60*1000;
126 /* 10 minutes * 60 sec * 1000 millisec */
127 socket
->m_establishing
= 0;
132 void GSocket_destroy(GSocket
*socket
)
134 assert(socket
!= NULL
);
136 /* First, we check that the socket is really shutdowned */
137 if (socket
->m_fd
!= -1)
138 GSocket_Shutdown(socket
);
140 /* We destroy private addresses */
142 GAddress_destroy(socket
->m_local
);
145 GAddress_destroy(socket
->m_peer
);
147 /* We destroy socket itself */
151 void GSocket_Shutdown(GSocket
*socket
)
155 assert(socket
!= NULL
);
157 /* If socket has been created, we shutdown it */
158 if (socket
->m_fd
!= -1)
160 shutdown(socket
->m_fd
, 2);
161 soclose(socket
->m_fd
);
165 /* We also disable GUI callbacks */
166 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
167 socket
->m_cbacks
[evt
] = NULL
;
169 socket
->m_detected
= 0;
173 /* Address handling */
175 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
177 assert(socket
!= NULL
);
179 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
180 socket
->m_error
= GSOCK_INVSOCK
;
181 return GSOCK_INVSOCK
;
184 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
185 socket
->m_error
= GSOCK_INVADDR
;
186 return GSOCK_INVADDR
;
190 GAddress_destroy(socket
->m_local
);
192 socket
->m_local
= GAddress_copy(address
);
194 return GSOCK_NOERROR
;
197 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
199 assert(socket
!= NULL
);
201 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
202 socket
->m_error
= GSOCK_INVADDR
;
203 return GSOCK_INVADDR
;
207 GAddress_destroy(socket
->m_peer
);
209 socket
->m_peer
= GAddress_copy(address
);
211 return GSOCK_NOERROR
;
214 GAddress
*GSocket_GetLocal(GSocket
*socket
)
217 struct sockaddr addr
;
221 assert(socket
!= NULL
);
224 return GAddress_copy(socket
->m_local
);
226 if (socket
->m_fd
== -1) {
227 socket
->m_error
= GSOCK_INVSOCK
;
233 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
234 socket
->m_error
= GSOCK_IOERR
;
238 address
= GAddress_new();
239 if (address
== NULL
) {
240 socket
->m_error
= GSOCK_MEMERR
;
243 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
244 if (socket
->m_error
!= GSOCK_NOERROR
) {
245 GAddress_destroy(address
);
252 GAddress
*GSocket_GetPeer(GSocket
*socket
)
254 assert(socket
!= NULL
);
257 return GAddress_copy(socket
->m_peer
);
262 /* Server specific parts */
264 /* GSocket_SetServer:
265 * Sets up the socket as a server. It uses the "Local" field of GSocket.
266 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
267 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
268 * been initialized, GSOCK_INVADDR if the local address has not been
269 * defined and GSOCK_IOERR for other internal errors.
271 GSocketError
GSocket_SetServer(GSocket
*sck
)
278 if (sck
->m_fd
!= -1) {
279 sck
->m_error
= GSOCK_INVSOCK
;
280 return GSOCK_INVSOCK
;
284 sck
->m_error
= GSOCK_INVADDR
;
285 return GSOCK_INVADDR
;
288 /* We always have a stream here */
292 /* Create the socket */
293 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
295 if (sck
->m_fd
== -1) {
296 sck
->m_error
= GSOCK_IOERR
;
300 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
302 /* Bind the socket to the LOCAL address */
303 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
306 sck
->m_error
= GSOCK_IOERR
;
310 /* Enable listening up to 5 connections */
311 if (listen(sck
->m_fd
, 5) < 0) {
314 sck
->m_error
= GSOCK_IOERR
;
318 return GSOCK_NOERROR
;
321 /* GSocket_WaitConnection:
322 * Waits for an incoming client connection.
324 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
329 assert(socket
!= NULL
);
331 /* Reenable CONNECTION events */
332 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
334 /* If the socket has already been created, we exit immediately */
335 if (socket
->m_fd
== -1 || !socket
->m_server
)
337 socket
->m_error
= GSOCK_INVSOCK
;
341 /* Create a GSocket object for the new connection */
342 connection
= GSocket_new();
345 connection
->m_error
= GSOCK_MEMERR
;
349 /* Accept the incoming connection */
350 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
352 GSocket_destroy(connection
);
353 /* socket->m_error set by _GSocket_Input_Timeout */
357 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
359 if (connection
->m_fd
== -1)
361 if (errno
== EWOULDBLOCK
)
362 socket
->m_error
= GSOCK_WOULDBLOCK
;
364 socket
->m_error
= GSOCK_IOERR
;
366 GSocket_destroy(connection
);
370 /* Initialize all fields */
371 connection
->m_server
= 0;
372 connection
->m_stream
= 1;
373 connection
->m_oriented
= 1;
375 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
379 /* Non oriented connections */
381 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
387 if (sck
->m_fd
!= -1) {
388 sck
->m_error
= GSOCK_INVSOCK
;
389 return GSOCK_INVSOCK
;
393 sck
->m_error
= GSOCK_INVADDR
;
394 return GSOCK_INVADDR
;
401 /* Create the socket */
402 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
405 sck
->m_error
= GSOCK_IOERR
;
409 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
411 /* Bind it to the LOCAL address */
412 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
415 sck
->m_error
= GSOCK_IOERR
;
419 return GSOCK_NOERROR
;
422 /* Client specific parts */
425 * Establishes a client connection to a server using the "Peer"
426 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
427 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
428 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
429 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
430 * the connection request can be completed later. Use GSocket_Select()
431 * to check or wait for a GSOCK_CONNECTION event.
433 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
440 /* Enable CONNECTION events (needed for nonblocking connections) */
441 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
445 sck
->m_error
= GSOCK_INVSOCK
;
446 return GSOCK_INVSOCK
;
451 sck
->m_error
= GSOCK_INVADDR
;
452 return GSOCK_INVADDR
;
455 /* Test whether we want the socket to be a stream (e.g. TCP) */
456 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
459 sck
->m_establishing
= 0;
466 /* Create the socket */
467 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
469 if (sck
->m_fd
== -1) {
470 sck
->m_error
= GSOCK_IOERR
;
474 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
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 fprintf(stderr
, "Blocking connect timeouts\n");
496 return GSOCK_TIMEDOUT
;
500 fprintf(stderr
, "Blocking connect OK\n");
501 return GSOCK_NOERROR
;
505 /* If connect failed with EINPROGRESS and the GSocket object
506 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
507 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
508 * this way if the connection completes, a GSOCK_CONNECTION
509 * event will be generated, if enabled.
511 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
513 sck
->m_error
= GSOCK_WOULDBLOCK
;
514 sck
->m_establishing
= 1;
515 fprintf(stderr
, "Nonblocking connect in progress\n");
517 return GSOCK_WOULDBLOCK
;
520 /* If connect failed with an error other than EINPROGRESS,
521 * then the call to GSocket_Connect has failed.
525 sck
->m_error
= GSOCK_IOERR
;
527 fprintf(stderr
, "Connect failed (generic err)\n");
531 fprintf(stderr
, "Connect OK\n");
532 return GSOCK_NOERROR
;
537 /* Like recv(), send(), ... */
538 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
542 assert(socket
!= NULL
);
544 /* Reenable INPUT events */
545 _GSocket_Enable(socket
, GSOCK_INPUT
);
547 if (socket
->m_fd
== -1 || socket
->m_server
)
549 socket
->m_error
= GSOCK_INVSOCK
;
553 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
556 if (socket
->m_stream
)
557 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
559 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
563 if (errno
== EWOULDBLOCK
)
564 socket
->m_error
= GSOCK_WOULDBLOCK
;
566 socket
->m_error
= GSOCK_IOERR
;
572 int GSocket_Write(GSocket
*socket
, const char *buffer
,
577 assert(socket
!= NULL
);
579 if (socket
->m_fd
== -1 || socket
->m_server
)
581 socket
->m_error
= GSOCK_INVSOCK
;
585 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
588 if (socket
->m_stream
)
589 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
591 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
595 if (errno
== EWOULDBLOCK
)
596 socket
->m_error
= GSOCK_WOULDBLOCK
;
598 socket
->m_error
= GSOCK_IOERR
;
600 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
601 * in MSW). Once the first OUTPUT event is received, users can assume
602 * that the socket is writable until a read operation fails. Only then
603 * will further OUTPUT events be posted.
605 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
612 * Polls the socket to determine its status. This function will
613 * check for the events specified in the 'flags' parameter, and
614 * it will return a mask indicating which operations can be
615 * performed. This function won't block, regardless of the
616 * mode (blocking|nonblocking) of the socket.
618 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
620 assert(socket
!= NULL
);
622 return (flags
& socket
->m_detected
);
627 /* GSocket_SetNonBlocking:
628 * Sets the socket to non-blocking mode. This is useful if
629 * we don't want to wait.
631 void GSocket_SetNonBlocking(GSocket
*socket
, int non_block
)
633 assert(socket
!= NULL
);
635 socket
->m_non_blocking
= non_block
;
638 /* GSocket_SetTimeout:
639 * Sets the timeout for blocking calls. Time is
640 * expressed in milliseconds.
642 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
644 assert(socket
!= NULL
);
646 socket
->m_timeout
= millisec
;
650 * Returns the last error occured for this socket.
652 GSocketError
GSocket_GetError(GSocket
*socket
)
654 assert(socket
!= NULL
);
656 return socket
->m_error
;
661 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
662 * and LOST). The callbacks are called in the following situations:
664 * INPUT: There is at least one byte in the input buffer
665 * OUTPUT: The system is sure that the next write call will not block
666 * CONNECTION: Two cases are possible:
667 * Client socket -> the connection is established
668 * Server socket -> a client requests a connection
669 * LOST: The connection is lost
671 * An event is generated only once and its state is reseted when the
672 * relative IO call is requested.
673 * For example: INPUT -> GSocket_Read()
674 * CONNECTION -> GSocket_Accept()
677 /* GSocket_SetCallback:
678 * Enables the callbacks specified by 'flags'. Note that 'flags'
679 * may be a combination of flags OR'ed toghether, so the same
680 * callback function can be made to accept different events.
681 * The callback function must have the following prototype:
683 * void function(GSocket *socket, GSocketEvent event, char *cdata)
685 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
686 GSocketCallback callback
, char *cdata
)
690 assert(socket
!= NULL
);
692 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
694 if ((flags
& (1 << count
)) != 0)
696 socket
->m_cbacks
[count
] = callback
;
697 socket
->m_data
[count
] = cdata
;
702 /* GSocket_UnsetCallback:
703 * Disables all callbacks specified by 'flags', which may be a
704 * combination of flags OR'ed toghether.
706 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
710 assert(socket
!= NULL
);
712 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
714 if ((flags
& (1 << count
)) != 0)
716 socket
->m_cbacks
[count
] = NULL
;
717 socket
->m_data
[count
] = NULL
;
722 #define CALL_CALLBACK(socket, event) { \
723 _GSocket_Disable(socket, event); \
724 if (socket->m_cbacks[event]) \
725 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
729 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
731 socket
->m_detected
&= ~(1 << event
);
735 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
737 socket
->m_detected
|= (1 << event
);
740 /* _GSocket_Input_Timeout:
741 * For blocking sockets, wait until data is available or
742 * until timeout ellapses.
744 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
749 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
750 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
752 if (!socket
->m_non_blocking
)
755 FD_SET(socket
->m_fd
, &readfds
);
756 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
758 socket
->m_error
= GSOCK_TIMEDOUT
;
759 return GSOCK_TIMEDOUT
;
762 return GSOCK_NOERROR
;
765 /* _GSocket_Output_Timeout:
766 * For blocking sockets, wait until data can be sent without
767 * blocking or until timeout ellapses.
769 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
774 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
775 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
777 if (!socket
->m_non_blocking
)
780 FD_SET(socket
->m_fd
, &writefds
);
781 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
783 socket
->m_error
= GSOCK_TIMEDOUT
;
784 return GSOCK_TIMEDOUT
;
787 return GSOCK_NOERROR
;
790 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
794 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
);
808 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
813 /* Translate a system address into a GSocket address */
816 socket
->m_peer
= GAddress_new();
819 socket
->m_error
= GSOCK_MEMERR
;
823 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
824 if (err
!= GSOCK_NOERROR
)
826 GAddress_destroy(socket
->m_peer
);
827 socket
->m_peer
= NULL
;
828 socket
->m_error
= err
;
835 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
840 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
845 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
847 struct sockaddr
*addr
;
851 if (!socket
->m_peer
) {
852 socket
->m_error
= GSOCK_INVADDR
;
856 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
857 if (err
!= GSOCK_NOERROR
) {
858 socket
->m_error
= err
;
862 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
864 /* Frees memory allocated from _GAddress_translate_to */
870 void _GSocket_Detected_Read(GSocket
*socket
)
875 if (socket
->m_stream
)
877 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
879 if (ret
< 0 && socket
->m_server
)
881 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
887 CALL_CALLBACK(socket
, GSOCK_INPUT
);
891 CALL_CALLBACK(socket
, GSOCK_LOST
);
896 void _GSocket_Detected_Write(GSocket
*socket
)
898 if (socket
->m_establishing
&& !socket
->m_server
)
902 socket
->m_establishing
= 0;
905 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
909 CALL_CALLBACK(socket
, GSOCK_LOST
);
913 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
914 /* We have to fire this event by hand because CONNECTION (for clients)
915 * and OUTPUT are internally the same and we just disabled CONNECTION
916 * events with the above macro.
918 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
923 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
928 * -------------------------------------------------------------------------
930 * -------------------------------------------------------------------------
933 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
934 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
935 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
937 #define CHECK_ADDRESS(address, family, retval) \
939 if (address->m_family == GSOCK_NOFAMILY) \
940 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
941 return address->m_error; \
943 if (address->m_family != GSOCK_##family) {\
944 address->m_error = GSOCK_INVADDR; \
949 GAddress
*GAddress_new()
953 address
= (GAddress
*)malloc(sizeof(GAddress
));
958 address
->m_family
= GSOCK_NOFAMILY
;
959 address
->m_addr
= NULL
;
965 GAddress
*GAddress_copy(GAddress
*address
)
969 assert(address
!= NULL
);
971 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
976 memcpy(addr2
, address
, sizeof(GAddress
));
978 if (address
->m_addr
) {
979 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
980 if (addr2
->m_addr
== NULL
) {
984 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
990 void GAddress_destroy(GAddress
*address
)
992 assert(address
!= NULL
);
997 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
999 assert(address
!= NULL
);
1001 address
->m_family
= type
;
1004 GAddressType
GAddress_GetFamily(GAddress
*address
)
1006 assert(address
!= NULL
);
1008 return address
->m_family
;
1011 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1012 address
->m_realfamily
= addr
->sa_family
;
1013 switch (addr
->sa_family
) {
1015 address
->m_family
= GSOCK_INET
;
1018 address
->m_family
= GSOCK_UNIX
;
1022 address
->m_family
= GSOCK_INET6
;
1027 address
->m_error
= GSOCK_INVOP
;
1032 if (address
->m_addr
)
1033 free(address
->m_addr
);
1035 address
->m_len
= len
;
1036 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1037 if (address
->m_addr
== NULL
) {
1038 address
->m_error
= GSOCK_MEMERR
;
1039 return GSOCK_MEMERR
;
1041 memcpy(address
->m_addr
, addr
, len
);
1043 return GSOCK_NOERROR
;
1046 GSocketError
_GAddress_translate_to(GAddress
*address
,
1047 struct sockaddr
**addr
, int *len
)
1049 if (!address
->m_addr
) {
1050 address
->m_error
= GSOCK_INVADDR
;
1051 return GSOCK_INVADDR
;
1054 *len
= address
->m_len
;
1055 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1056 if (*addr
== NULL
) {
1057 address
->m_error
= GSOCK_MEMERR
;
1058 return GSOCK_MEMERR
;
1061 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1062 return GSOCK_NOERROR
;
1066 * -------------------------------------------------------------------------
1067 * Internet address family
1068 * -------------------------------------------------------------------------
1071 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1073 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1074 if (address
->m_addr
== NULL
) {
1075 address
->m_error
= GSOCK_MEMERR
;
1076 return GSOCK_MEMERR
;
1079 address
->m_len
= sizeof(struct sockaddr_in
);
1081 address
->m_family
= GSOCK_INET
;
1082 address
->m_realfamily
= PF_INET
;
1083 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1084 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1086 return GSOCK_NOERROR
;
1089 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1092 struct in_addr
*addr
;
1094 assert(address
!= NULL
);
1096 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1098 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1100 /* If it is a numeric host name, convert it now */
1101 #if defined(HAVE_INET_ATON)
1102 if (inet_aton(hostname
, addr
) == 0) {
1103 #elif defined(HAVE_INET_ADDR)
1104 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1105 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1107 struct in_addr
*array_addr
;
1109 /* It is a real name, we solve it */
1110 he
= gethostbyname((char*)hostname
);
1112 address
->m_error
= GSOCK_NOHOST
;
1113 return GSOCK_NOHOST
;
1115 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1116 addr
->s_addr
= array_addr
[0].s_addr
;
1117 #if defined(HAVE_INET_ATON)
1119 #elif defined(HAVE_INET_ADDR)
1122 return GSOCK_NOERROR
;
1125 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1127 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1130 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1131 unsigned long hostaddr
)
1133 struct in_addr
*addr
;
1135 assert(address
!= NULL
);
1137 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1139 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1140 addr
->s_addr
= hostaddr
;
1142 return GSOCK_NOERROR
;
1145 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1146 const char *protocol
)
1149 struct sockaddr_in
*addr
;
1151 assert(address
!= NULL
);
1152 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1155 address
->m_error
= GSOCK_INVPORT
;
1156 return GSOCK_INVPORT
;
1159 se
= getservbyname((char*)port
, (char*)protocol
);
1161 if (isdigit(port
[0])) {
1164 port_int
= atoi(port
);
1165 addr
= (struct sockaddr_in
*)address
->m_addr
;
1166 // addr->sin_port = htons(port_int);
1167 return GSOCK_NOERROR
;
1170 address
->m_error
= GSOCK_INVPORT
;
1171 return GSOCK_INVPORT
;
1174 addr
= (struct sockaddr_in
*)address
->m_addr
;
1175 addr
->sin_port
= se
->s_port
;
1177 return GSOCK_NOERROR
;
1180 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1182 struct sockaddr_in
*addr
;
1184 assert(address
!= NULL
);
1185 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1187 addr
= (struct sockaddr_in
*)address
->m_addr
;
1188 // addr->sin_port = htons(port);
1190 return GSOCK_NOERROR
;
1193 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1197 struct sockaddr_in
*addr
;
1199 assert(address
!= NULL
);
1200 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1202 addr
= (struct sockaddr_in
*)address
->m_addr
;
1203 addr_buf
= (char *)&(addr
->sin_addr
);
1205 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1207 address
->m_error
= GSOCK_NOHOST
;
1208 return GSOCK_NOHOST
;
1211 strncpy(hostname
, he
->h_name
, sbuf
);
1213 return GSOCK_NOERROR
;
1216 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1218 struct sockaddr_in
*addr
;
1220 assert(address
!= NULL
);
1221 CHECK_ADDRESS(address
, INET
, 0);
1223 addr
= (struct sockaddr_in
*)address
->m_addr
;
1225 return addr
->sin_addr
.s_addr
;
1228 unsigned short GAddress_INET_GetPort(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
;
1236 return /*ntohs*/(addr
->sin_port
);
1240 * -------------------------------------------------------------------------
1241 * Unix address family
1242 * -------------------------------------------------------------------------
1245 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1247 assert (address
!= NULL
);
1248 address
->m_error
= GSOCK_INVADDR
;
1249 return GSOCK_INVADDR
;
1252 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1254 assert (address
!= NULL
);
1255 address
->m_error
= GSOCK_INVADDR
;
1256 return GSOCK_INVADDR
;
1259 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1261 assert (address
!= NULL
);
1262 address
->m_error
= GSOCK_INVADDR
;
1263 return GSOCK_INVADDR
;