]>
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 * -------------------------------------------------------------------------
14 #define BSD_SELECT // use Berkley Sockets select
17 #include <sys\types.h>
23 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
28 #include <sys\socket.h>
29 #include <sys\ioctl.h>
30 #include <sys\select.h>
41 #include "wx/gsocket.h"
42 #include "wx/os2/gsockos2.h"
48 # define SOCKLEN_T socklen_t
51 # define SOCKLEN_T int
56 /* Global initialisers */
63 void GSocket_Cleanup()
67 /* Constructors / Destructors */
69 GSocket
*GSocket_new()
74 socket
= (GSocket
*)malloc(sizeof(GSocket
));
80 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
82 socket
->m_cbacks
[i
] = NULL
;
84 socket
->m_detected
= 0;
85 socket
->m_local
= NULL
;
86 socket
->m_peer
= NULL
;
87 socket
->m_error
= GSOCK_NOERROR
;
88 socket
->m_server
= FALSE
;
89 socket
->m_stream
= TRUE
;
90 socket
->m_gui_dependent
= NULL
;
91 socket
->m_non_blocking
= FALSE
;
92 socket
->m_timeout
= 10*60*1000;
93 /* 10 minutes * 60 sec * 1000 millisec */
94 socket
->m_establishing
= FALSE
;
96 /* We initialize the GUI specific entries here */
97 _GSocket_GUI_Init(socket
);
102 void GSocket_destroy(GSocket
*socket
)
104 assert(socket
!= NULL
);
106 /* First, we check that the socket is really shutdowned */
107 if (socket
->m_fd
!= -1)
108 GSocket_Shutdown(socket
);
110 /* We destroy GUI specific variables */
111 _GSocket_GUI_Destroy(socket
);
113 /* We destroy private addresses */
115 GAddress_destroy(socket
->m_local
);
118 GAddress_destroy(socket
->m_peer
);
120 /* We destroy socket itself */
124 void GSocket_Shutdown(GSocket
*socket
)
128 assert(socket
!= NULL
);
130 /* If socket has been created, we shutdown it */
131 if (socket
->m_fd
!= -1)
133 shutdown(socket
->m_fd
, 2);
134 soclose(socket
->m_fd
);
138 /* We also disable GUI callbacks */
139 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
140 socket
->m_cbacks
[evt
] = NULL
;
142 socket
->m_detected
= 0;
143 _GSocket_Disable_Events(socket
);
146 /* Address handling */
148 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
150 assert(socket
!= NULL
);
152 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
153 socket
->m_error
= GSOCK_INVSOCK
;
154 return GSOCK_INVSOCK
;
157 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
158 socket
->m_error
= GSOCK_INVADDR
;
159 return GSOCK_INVADDR
;
163 GAddress_destroy(socket
->m_local
);
165 socket
->m_local
= GAddress_copy(address
);
167 return GSOCK_NOERROR
;
170 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
172 assert(socket
!= NULL
);
174 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
175 socket
->m_error
= GSOCK_INVADDR
;
176 return GSOCK_INVADDR
;
180 GAddress_destroy(socket
->m_peer
);
182 socket
->m_peer
= GAddress_copy(address
);
184 return GSOCK_NOERROR
;
187 GAddress
*GSocket_GetLocal(GSocket
*socket
)
190 struct sockaddr addr
;
194 assert(socket
!= NULL
);
197 return GAddress_copy(socket
->m_local
);
199 if (socket
->m_fd
== -1) {
200 socket
->m_error
= GSOCK_INVSOCK
;
206 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
207 socket
->m_error
= GSOCK_IOERR
;
211 address
= GAddress_new();
212 if (address
== NULL
) {
213 socket
->m_error
= GSOCK_MEMERR
;
216 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
217 if (socket
->m_error
!= GSOCK_NOERROR
) {
218 GAddress_destroy(address
);
225 GAddress
*GSocket_GetPeer(GSocket
*socket
)
227 assert(socket
!= NULL
);
230 return GAddress_copy(socket
->m_peer
);
235 /* Server specific parts */
237 /* GSocket_SetServer:
238 * Sets up the socket as a server. It uses the "Local" field of GSocket.
239 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
240 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
241 * been initialized, GSOCK_INVADDR if the local address has not been
242 * defined and GSOCK_IOERR for other internal errors.
244 GSocketError
GSocket_SetServer(GSocket
*sck
)
251 if (sck
->m_fd
!= -1) {
252 sck
->m_error
= GSOCK_INVSOCK
;
253 return GSOCK_INVSOCK
;
257 sck
->m_error
= GSOCK_INVADDR
;
258 return GSOCK_INVADDR
;
261 /* We always have a stream here */
262 sck
->m_stream
= TRUE
;
263 sck
->m_server
= TRUE
;
265 /* Create the socket */
266 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
268 if (sck
->m_fd
== -1) {
269 sck
->m_error
= GSOCK_IOERR
;
273 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
274 _GSocket_Enable_Events(sck
);
276 /* Bind the socket to the LOCAL address */
277 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
280 sck
->m_error
= GSOCK_IOERR
;
284 /* Enable listening up to 5 connections */
285 if (listen(sck
->m_fd
, 5) < 0) {
288 sck
->m_error
= GSOCK_IOERR
;
292 return GSOCK_NOERROR
;
295 /* GSocket_WaitConnection:
296 * Waits for an incoming client connection.
298 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
303 assert(socket
!= NULL
);
305 /* Reenable CONNECTION events */
306 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
308 /* If the socket has already been created, we exit immediately */
309 if (socket
->m_fd
== -1 || !socket
->m_server
)
311 socket
->m_error
= GSOCK_INVSOCK
;
315 /* Create a GSocket object for the new connection */
316 connection
= GSocket_new();
319 connection
->m_error
= GSOCK_MEMERR
;
323 /* Accept the incoming connection */
324 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
326 GSocket_destroy(connection
);
327 /* socket->m_error set by _GSocket_Input_Timeout */
331 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
333 if (connection
->m_fd
== -1)
335 if (errno
== EWOULDBLOCK
)
336 socket
->m_error
= GSOCK_WOULDBLOCK
;
338 socket
->m_error
= GSOCK_IOERR
;
340 GSocket_destroy(connection
);
344 /* Initialize all fields */
345 connection
->m_server
= FALSE
;
346 connection
->m_stream
= TRUE
;
347 connection
->m_oriented
= TRUE
;
349 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
350 _GSocket_Enable_Events(connection
);
355 /* Non oriented connections */
357 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
363 if (sck
->m_fd
!= -1) {
364 sck
->m_error
= GSOCK_INVSOCK
;
365 return GSOCK_INVSOCK
;
369 sck
->m_error
= GSOCK_INVADDR
;
370 return GSOCK_INVADDR
;
373 sck
->m_stream
= FALSE
;
374 sck
->m_server
= FALSE
;
375 sck
->m_oriented
= FALSE
;
377 /* Create the socket */
378 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
381 sck
->m_error
= GSOCK_IOERR
;
385 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
386 _GSocket_Enable_Events(sck
);
388 /* Bind it to the LOCAL address */
389 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
392 sck
->m_error
= GSOCK_IOERR
;
396 return GSOCK_NOERROR
;
399 /* Client specific parts */
402 * Establishes a client connection to a server using the "Peer"
403 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
404 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
405 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
406 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
407 * the connection request can be completed later. Use GSocket_Select()
408 * to check or wait for a GSOCK_CONNECTION event.
410 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
417 /* Enable CONNECTION events (needed for nonblocking connections) */
418 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
422 sck
->m_error
= GSOCK_INVSOCK
;
423 return GSOCK_INVSOCK
;
428 sck
->m_error
= GSOCK_INVADDR
;
429 return GSOCK_INVADDR
;
432 /* Test whether we want the socket to be a stream (e.g. TCP) */
433 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
434 sck
->m_oriented
= TRUE
;
435 sck
->m_server
= FALSE
;
436 sck
->m_establishing
= FALSE
;
443 /* Create the socket */
444 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
446 if (sck
->m_fd
== -1) {
447 sck
->m_error
= GSOCK_IOERR
;
451 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
452 _GSocket_Enable_Events(sck
);
454 /* Connect it to the PEER address */
455 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
461 /* If connect failed with EINPROGRESS and the GSocket object
462 * is in blocking mode, we select() for the specified timeout
463 * checking for writability to see if the connection request
466 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
468 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
472 /* sck->m_error is set in _GSocket_Output_Timeout */
473 fprintf(stderr
, "Blocking connect timeouts\n");
474 return GSOCK_TIMEDOUT
;
478 fprintf(stderr
, "Blocking connect OK\n");
479 return GSOCK_NOERROR
;
483 /* If connect failed with EINPROGRESS and the GSocket object
484 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
485 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
486 * this way if the connection completes, a GSOCK_CONNECTION
487 * event will be generated, if enabled.
489 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
491 sck
->m_error
= GSOCK_WOULDBLOCK
;
492 sck
->m_establishing
= TRUE
;
493 fprintf(stderr
, "Nonblocking connect in progress\n");
495 return GSOCK_WOULDBLOCK
;
498 /* If connect failed with an error other than EINPROGRESS,
499 * then the call to GSocket_Connect has failed.
503 sck
->m_error
= GSOCK_IOERR
;
505 fprintf(stderr
, "Connect failed (generic err)\n");
509 fprintf(stderr
, "Connect OK\n");
510 return GSOCK_NOERROR
;
515 /* Like recv(), send(), ... */
516 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
520 assert(socket
!= NULL
);
522 /* Reenable INPUT events */
523 _GSocket_Enable(socket
, GSOCK_INPUT
);
525 if (socket
->m_fd
== -1 || socket
->m_server
)
527 socket
->m_error
= GSOCK_INVSOCK
;
531 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
534 if (socket
->m_stream
)
535 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
537 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
541 if (errno
== EWOULDBLOCK
)
542 socket
->m_error
= GSOCK_WOULDBLOCK
;
544 socket
->m_error
= GSOCK_IOERR
;
550 int GSocket_Write(GSocket
*socket
, const char *buffer
,
555 assert(socket
!= NULL
);
557 if (socket
->m_fd
== -1 || socket
->m_server
)
559 socket
->m_error
= GSOCK_INVSOCK
;
563 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
566 if (socket
->m_stream
)
567 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
569 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
573 if (errno
== EWOULDBLOCK
)
574 socket
->m_error
= GSOCK_WOULDBLOCK
;
576 socket
->m_error
= GSOCK_IOERR
;
578 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
579 * in MSW). Once the first OUTPUT event is received, users can assume
580 * that the socket is writable until a read operation fails. Only then
581 * will further OUTPUT events be posted.
583 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
590 * Polls the socket to determine its status. This function will
591 * check for the events specified in the 'flags' parameter, and
592 * it will return a mask indicating which operations can be
593 * performed. This function won't block, regardless of the
594 * mode (blocking|nonblocking) of the socket.
596 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
598 assert(socket
!= NULL
);
600 return (flags
& socket
->m_detected
);
605 /* GSocket_SetNonBlocking:
606 * Sets the socket to non-blocking mode. This is useful if
607 * we don't want to wait.
609 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
611 assert(socket
!= NULL
);
613 socket
->m_non_blocking
= non_block
;
616 /* GSocket_SetTimeout:
617 * Sets the timeout for blocking calls. Time is
618 * expressed in milliseconds.
620 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
622 assert(socket
!= NULL
);
624 socket
->m_timeout
= millisec
;
628 * Returns the last error occured for this socket.
630 GSocketError
GSocket_GetError(GSocket
*socket
)
632 assert(socket
!= NULL
);
634 return socket
->m_error
;
639 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
640 * and LOST). The callbacks are called in the following situations:
642 * INPUT: There is at least one byte in the input buffer
643 * OUTPUT: The system is sure that the next write call will not block
644 * CONNECTION: Two cases are possible:
645 * Client socket -> the connection is established
646 * Server socket -> a client requests a connection
647 * LOST: The connection is lost
649 * An event is generated only once and its state is reseted when the
650 * relative IO call is requested.
651 * For example: INPUT -> GSocket_Read()
652 * CONNECTION -> GSocket_Accept()
655 /* GSocket_SetCallback:
656 * Enables the callbacks specified by 'flags'. Note that 'flags'
657 * may be a combination of flags OR'ed toghether, so the same
658 * callback function can be made to accept different events.
659 * The callback function must have the following prototype:
661 * void function(GSocket *socket, GSocketEvent event, char *cdata)
663 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
664 GSocketCallback callback
, char *cdata
)
668 assert(socket
!= NULL
);
670 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
672 if ((flags
& (1 << count
)) != 0)
674 socket
->m_cbacks
[count
] = callback
;
675 socket
->m_data
[count
] = cdata
;
680 /* GSocket_UnsetCallback:
681 * Disables all callbacks specified by 'flags', which may be a
682 * combination of flags OR'ed toghether.
684 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
688 assert(socket
!= NULL
);
690 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
692 if ((flags
& (1 << count
)) != 0)
694 socket
->m_cbacks
[count
] = NULL
;
695 socket
->m_data
[count
] = NULL
;
700 #define CALL_CALLBACK(socket, event) { \
701 _GSocket_Disable(socket, event); \
702 if (socket->m_cbacks[event]) \
703 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
707 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
709 socket
->m_detected
&= ~(1 << event
);
710 _GSocket_Install_Callback(socket
, event
);
713 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
715 socket
->m_detected
|= (1 << event
);
716 _GSocket_Uninstall_Callback(socket
, event
);
719 /* _GSocket_Input_Timeout:
720 * For blocking sockets, wait until data is available or
721 * until timeout ellapses.
723 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
728 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
729 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
731 if (!socket
->m_non_blocking
)
734 FD_SET(socket
->m_fd
, &readfds
);
735 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
737 socket
->m_error
= GSOCK_TIMEDOUT
;
738 return GSOCK_TIMEDOUT
;
741 return GSOCK_NOERROR
;
744 /* _GSocket_Output_Timeout:
745 * For blocking sockets, wait until data can be sent without
746 * blocking or until timeout ellapses.
748 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
753 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
754 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
756 if (!socket
->m_non_blocking
)
759 FD_SET(socket
->m_fd
, &writefds
);
760 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
762 socket
->m_error
= GSOCK_TIMEDOUT
;
763 return GSOCK_TIMEDOUT
;
766 return GSOCK_NOERROR
;
769 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
773 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
778 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
780 struct sockaddr from
;
785 fromlen
= sizeof(from
);
787 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
792 /* Translate a system address into a GSocket address */
795 socket
->m_peer
= GAddress_new();
798 socket
->m_error
= GSOCK_MEMERR
;
802 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
803 if (err
!= GSOCK_NOERROR
)
805 GAddress_destroy(socket
->m_peer
);
806 socket
->m_peer
= NULL
;
807 socket
->m_error
= err
;
814 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
819 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
824 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
826 struct sockaddr
*addr
;
830 if (!socket
->m_peer
) {
831 socket
->m_error
= GSOCK_INVADDR
;
835 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
836 if (err
!= GSOCK_NOERROR
) {
837 socket
->m_error
= err
;
841 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
843 /* Frees memory allocated from _GAddress_translate_to */
849 void _GSocket_Detected_Read(GSocket
*socket
)
854 if (socket
->m_stream
)
856 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
858 if (ret
< 0 && socket
->m_server
)
860 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
866 CALL_CALLBACK(socket
, GSOCK_INPUT
);
870 CALL_CALLBACK(socket
, GSOCK_LOST
);
875 void _GSocket_Detected_Write(GSocket
*socket
)
877 if (socket
->m_establishing
&& !socket
->m_server
)
881 socket
->m_establishing
= FALSE
;
884 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
888 CALL_CALLBACK(socket
, GSOCK_LOST
);
892 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
893 /* We have to fire this event by hand because CONNECTION (for clients)
894 * and OUTPUT are internally the same and we just disabled CONNECTION
895 * events with the above macro.
897 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
902 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
907 * -------------------------------------------------------------------------
909 * -------------------------------------------------------------------------
912 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
913 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
914 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
916 #define CHECK_ADDRESS(address, family, retval) \
918 if (address->m_family == GSOCK_NOFAMILY) \
919 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
920 return address->m_error; \
922 if (address->m_family != GSOCK_##family) {\
923 address->m_error = GSOCK_INVADDR; \
928 GAddress
*GAddress_new()
932 address
= (GAddress
*)malloc(sizeof(GAddress
));
937 address
->m_family
= GSOCK_NOFAMILY
;
938 address
->m_addr
= NULL
;
944 GAddress
*GAddress_copy(GAddress
*address
)
948 assert(address
!= NULL
);
950 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
955 memcpy(addr2
, address
, sizeof(GAddress
));
957 if (address
->m_addr
) {
958 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
959 if (addr2
->m_addr
== NULL
) {
963 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
969 void GAddress_destroy(GAddress
*address
)
971 assert(address
!= NULL
);
976 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
978 assert(address
!= NULL
);
980 address
->m_family
= type
;
983 GAddressType
GAddress_GetFamily(GAddress
*address
)
985 assert(address
!= NULL
);
987 return address
->m_family
;
990 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
991 address
->m_realfamily
= addr
->sa_family
;
992 switch (addr
->sa_family
) {
994 address
->m_family
= GSOCK_INET
;
997 address
->m_family
= GSOCK_UNIX
;
1001 address
->m_family
= GSOCK_INET6
;
1006 address
->m_error
= GSOCK_INVOP
;
1011 if (address
->m_addr
)
1012 free(address
->m_addr
);
1014 address
->m_len
= len
;
1015 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1016 if (address
->m_addr
== NULL
) {
1017 address
->m_error
= GSOCK_MEMERR
;
1018 return GSOCK_MEMERR
;
1020 memcpy(address
->m_addr
, addr
, len
);
1022 return GSOCK_NOERROR
;
1025 GSocketError
_GAddress_translate_to(GAddress
*address
,
1026 struct sockaddr
**addr
, int *len
)
1028 if (!address
->m_addr
) {
1029 address
->m_error
= GSOCK_INVADDR
;
1030 return GSOCK_INVADDR
;
1033 *len
= address
->m_len
;
1034 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1035 if (*addr
== NULL
) {
1036 address
->m_error
= GSOCK_MEMERR
;
1037 return GSOCK_MEMERR
;
1040 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1041 return GSOCK_NOERROR
;
1045 * -------------------------------------------------------------------------
1046 * Internet address family
1047 * -------------------------------------------------------------------------
1050 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1052 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1053 if (address
->m_addr
== NULL
) {
1054 address
->m_error
= GSOCK_MEMERR
;
1055 return GSOCK_MEMERR
;
1058 address
->m_len
= sizeof(struct sockaddr_in
);
1060 address
->m_family
= GSOCK_INET
;
1061 address
->m_realfamily
= PF_INET
;
1062 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1063 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1065 return GSOCK_NOERROR
;
1068 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1071 struct in_addr
*addr
;
1073 assert(address
!= NULL
);
1075 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1077 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1079 /* If it is a numeric host name, convert it now */
1080 #if defined(HAVE_INET_ATON)
1081 if (inet_aton(hostname
, addr
) == 0) {
1082 #elif defined(HAVE_INET_ADDR)
1083 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1084 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1086 struct in_addr
*array_addr
;
1088 /* It is a real name, we solve it */
1089 he
= gethostbyname((char*)hostname
);
1091 address
->m_error
= GSOCK_NOHOST
;
1092 return GSOCK_NOHOST
;
1094 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1095 addr
->s_addr
= array_addr
[0].s_addr
;
1096 #if defined(HAVE_INET_ATON)
1098 #elif defined(HAVE_INET_ADDR)
1101 return GSOCK_NOERROR
;
1104 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1105 unsigned long hostaddr
)
1107 struct in_addr
*addr
;
1109 assert(address
!= NULL
);
1111 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1113 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1114 addr
->s_addr
= hostaddr
;
1116 return GSOCK_NOERROR
;
1119 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1120 const char *protocol
)
1123 struct sockaddr_in
*addr
;
1125 assert(address
!= NULL
);
1126 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1129 address
->m_error
= GSOCK_INVPORT
;
1130 return GSOCK_INVPORT
;
1133 se
= getservbyname((char*)port
, (char*)protocol
);
1135 if (isdigit(port
[0])) {
1138 port_int
= atoi(port
);
1139 addr
= (struct sockaddr_in
*)address
->m_addr
;
1140 addr
->sin_port
= htons(port_int
);
1141 return GSOCK_NOERROR
;
1144 address
->m_error
= GSOCK_INVPORT
;
1145 return GSOCK_INVPORT
;
1148 addr
= (struct sockaddr_in
*)address
->m_addr
;
1149 addr
->sin_port
= se
->s_port
;
1151 return GSOCK_NOERROR
;
1154 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1156 struct sockaddr_in
*addr
;
1158 assert(address
!= NULL
);
1159 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1161 addr
= (struct sockaddr_in
*)address
->m_addr
;
1162 addr
->sin_port
= htons(port
);
1164 return GSOCK_NOERROR
;
1167 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1171 struct sockaddr_in
*addr
;
1173 assert(address
!= NULL
);
1174 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1176 addr
= (struct sockaddr_in
*)address
->m_addr
;
1177 addr_buf
= (char *)&(addr
->sin_addr
);
1179 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1181 address
->m_error
= GSOCK_NOHOST
;
1182 return GSOCK_NOHOST
;
1185 strncpy(hostname
, he
->h_name
, sbuf
);
1187 return GSOCK_NOERROR
;
1190 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1192 struct sockaddr_in
*addr
;
1194 assert(address
!= NULL
);
1195 CHECK_ADDRESS(address
, INET
, 0);
1197 addr
= (struct sockaddr_in
*)address
->m_addr
;
1199 return addr
->sin_addr
.s_addr
;
1202 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1204 struct sockaddr_in
*addr
;
1206 assert(address
!= NULL
);
1207 CHECK_ADDRESS(address
, INET
, 0);
1209 addr
= (struct sockaddr_in
*)address
->m_addr
;
1210 return ntohs(addr
->sin_port
);