]>
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
18 #include <sys\types.h>
35 #include "wx/gsocket.h"
36 #include "wx/os2/gsockos2.h"
42 # define SOCKLEN_T socklen_t
45 # define SOCKLEN_T int
50 /* Global initialisers */
57 void GSocket_Cleanup()
61 /* Constructors / Destructors */
63 GSocket
*GSocket_new()
68 socket
= (GSocket
*)malloc(sizeof(GSocket
));
74 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
76 socket
->m_cbacks
[i
] = NULL
;
78 socket
->m_detected
= 0;
79 socket
->m_local
= NULL
;
80 socket
->m_peer
= NULL
;
81 socket
->m_error
= GSOCK_NOERROR
;
82 socket
->m_server
= FALSE
;
83 socket
->m_stream
= TRUE
;
84 socket
->m_gui_dependent
= NULL
;
85 socket
->m_non_blocking
= FALSE
;
86 socket
->m_timeout
= 10*60*1000;
87 /* 10 minutes * 60 sec * 1000 millisec */
88 socket
->m_establishing
= FALSE
;
90 /* We initialize the GUI specific entries here */
91 _GSocket_GUI_Init(socket
);
96 void GSocket_destroy(GSocket
*socket
)
98 assert(socket
!= NULL
);
100 /* First, we check that the socket is really shutdowned */
101 if (socket
->m_fd
!= -1)
102 GSocket_Shutdown(socket
);
104 /* We destroy GUI specific variables */
105 _GSocket_GUI_Destroy(socket
);
107 /* We destroy private addresses */
109 GAddress_destroy(socket
->m_local
);
112 GAddress_destroy(socket
->m_peer
);
114 /* We destroy socket itself */
118 void GSocket_Shutdown(GSocket
*socket
)
122 assert(socket
!= NULL
);
124 /* If socket has been created, we shutdown it */
125 if (socket
->m_fd
!= -1)
127 shutdown(socket
->m_fd
, 2);
128 soclose(socket
->m_fd
);
132 /* We also disable GUI callbacks */
133 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
134 socket
->m_cbacks
[evt
] = NULL
;
136 socket
->m_detected
= 0;
137 _GSocket_Disable_Events(socket
);
140 /* Address handling */
142 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
144 assert(socket
!= NULL
);
146 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
147 socket
->m_error
= GSOCK_INVSOCK
;
148 return GSOCK_INVSOCK
;
151 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
152 socket
->m_error
= GSOCK_INVADDR
;
153 return GSOCK_INVADDR
;
157 GAddress_destroy(socket
->m_local
);
159 socket
->m_local
= GAddress_copy(address
);
161 return GSOCK_NOERROR
;
164 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
166 assert(socket
!= NULL
);
168 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
169 socket
->m_error
= GSOCK_INVADDR
;
170 return GSOCK_INVADDR
;
174 GAddress_destroy(socket
->m_peer
);
176 socket
->m_peer
= GAddress_copy(address
);
178 return GSOCK_NOERROR
;
181 GAddress
*GSocket_GetLocal(GSocket
*socket
)
184 struct sockaddr addr
;
188 assert(socket
!= NULL
);
191 return GAddress_copy(socket
->m_local
);
193 if (socket
->m_fd
== -1) {
194 socket
->m_error
= GSOCK_INVSOCK
;
200 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
201 socket
->m_error
= GSOCK_IOERR
;
205 address
= GAddress_new();
206 if (address
== NULL
) {
207 socket
->m_error
= GSOCK_MEMERR
;
210 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
211 if (socket
->m_error
!= GSOCK_NOERROR
) {
212 GAddress_destroy(address
);
219 GAddress
*GSocket_GetPeer(GSocket
*socket
)
221 assert(socket
!= NULL
);
224 return GAddress_copy(socket
->m_peer
);
229 /* Server specific parts */
231 /* GSocket_SetServer:
232 * Sets up the socket as a server. It uses the "Local" field of GSocket.
233 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
234 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
235 * been initialized, GSOCK_INVADDR if the local address has not been
236 * defined and GSOCK_IOERR for other internal errors.
238 GSocketError
GSocket_SetServer(GSocket
*sck
)
245 if (sck
->m_fd
!= -1) {
246 sck
->m_error
= GSOCK_INVSOCK
;
247 return GSOCK_INVSOCK
;
251 sck
->m_error
= GSOCK_INVADDR
;
252 return GSOCK_INVADDR
;
255 /* We always have a stream here */
256 sck
->m_stream
= TRUE
;
257 sck
->m_server
= TRUE
;
259 /* Create the socket */
260 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
262 if (sck
->m_fd
== -1) {
263 sck
->m_error
= GSOCK_IOERR
;
267 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
268 _GSocket_Enable_Events(sck
);
270 /* Bind the socket to the LOCAL address */
271 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
274 sck
->m_error
= GSOCK_IOERR
;
278 /* Enable listening up to 5 connections */
279 if (listen(sck
->m_fd
, 5) < 0) {
282 sck
->m_error
= GSOCK_IOERR
;
286 return GSOCK_NOERROR
;
289 /* GSocket_WaitConnection:
290 * Waits for an incoming client connection.
292 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
297 assert(socket
!= NULL
);
299 /* Reenable CONNECTION events */
300 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
302 /* If the socket has already been created, we exit immediately */
303 if (socket
->m_fd
== -1 || !socket
->m_server
)
305 socket
->m_error
= GSOCK_INVSOCK
;
309 /* Create a GSocket object for the new connection */
310 connection
= GSocket_new();
313 connection
->m_error
= GSOCK_MEMERR
;
317 /* Accept the incoming connection */
318 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
320 GSocket_destroy(connection
);
321 /* socket->m_error set by _GSocket_Input_Timeout */
325 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
327 if (connection
->m_fd
== -1)
329 if (errno
== EWOULDBLOCK
)
330 socket
->m_error
= GSOCK_WOULDBLOCK
;
332 socket
->m_error
= GSOCK_IOERR
;
334 GSocket_destroy(connection
);
338 /* Initialize all fields */
339 connection
->m_server
= FALSE
;
340 connection
->m_stream
= TRUE
;
341 connection
->m_oriented
= TRUE
;
343 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
344 _GSocket_Enable_Events(connection
);
349 /* Non oriented connections */
351 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
357 if (sck
->m_fd
!= -1) {
358 sck
->m_error
= GSOCK_INVSOCK
;
359 return GSOCK_INVSOCK
;
363 sck
->m_error
= GSOCK_INVADDR
;
364 return GSOCK_INVADDR
;
367 sck
->m_stream
= FALSE
;
368 sck
->m_server
= FALSE
;
369 sck
->m_oriented
= FALSE
;
371 /* Create the socket */
372 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
375 sck
->m_error
= GSOCK_IOERR
;
379 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
380 _GSocket_Enable_Events(sck
);
382 /* Bind it to the LOCAL address */
383 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
386 sck
->m_error
= GSOCK_IOERR
;
390 return GSOCK_NOERROR
;
393 /* Client specific parts */
396 * Establishes a client connection to a server using the "Peer"
397 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
398 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
399 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
400 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
401 * the connection request can be completed later. Use GSocket_Select()
402 * to check or wait for a GSOCK_CONNECTION event.
404 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
411 /* Enable CONNECTION events (needed for nonblocking connections) */
412 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
416 sck
->m_error
= GSOCK_INVSOCK
;
417 return GSOCK_INVSOCK
;
422 sck
->m_error
= GSOCK_INVADDR
;
423 return GSOCK_INVADDR
;
426 /* Test whether we want the socket to be a stream (e.g. TCP) */
427 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
428 sck
->m_oriented
= TRUE
;
429 sck
->m_server
= FALSE
;
430 sck
->m_establishing
= FALSE
;
437 /* Create the socket */
438 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
440 if (sck
->m_fd
== -1) {
441 sck
->m_error
= GSOCK_IOERR
;
445 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
446 _GSocket_Enable_Events(sck
);
448 /* Connect it to the PEER address */
449 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
455 /* If connect failed with EINPROGRESS and the GSocket object
456 * is in blocking mode, we select() for the specified timeout
457 * checking for writability to see if the connection request
460 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
462 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
466 /* sck->m_error is set in _GSocket_Output_Timeout */
467 fprintf(stderr
, "Blocking connect timeouts\n");
468 return GSOCK_TIMEDOUT
;
472 fprintf(stderr
, "Blocking connect OK\n");
473 return GSOCK_NOERROR
;
477 /* If connect failed with EINPROGRESS and the GSocket object
478 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
479 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
480 * this way if the connection completes, a GSOCK_CONNECTION
481 * event will be generated, if enabled.
483 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
485 sck
->m_error
= GSOCK_WOULDBLOCK
;
486 sck
->m_establishing
= TRUE
;
487 fprintf(stderr
, "Nonblocking connect in progress\n");
489 return GSOCK_WOULDBLOCK
;
492 /* If connect failed with an error other than EINPROGRESS,
493 * then the call to GSocket_Connect has failed.
497 sck
->m_error
= GSOCK_IOERR
;
499 fprintf(stderr
, "Connect failed (generic err)\n");
503 fprintf(stderr
, "Connect OK\n");
504 return GSOCK_NOERROR
;
509 /* Like recv(), send(), ... */
510 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
514 assert(socket
!= NULL
);
516 /* Reenable INPUT events */
517 _GSocket_Enable(socket
, GSOCK_INPUT
);
519 if (socket
->m_fd
== -1 || socket
->m_server
)
521 socket
->m_error
= GSOCK_INVSOCK
;
525 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
528 if (socket
->m_stream
)
529 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
531 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
535 if (errno
== EWOULDBLOCK
)
536 socket
->m_error
= GSOCK_WOULDBLOCK
;
538 socket
->m_error
= GSOCK_IOERR
;
544 int GSocket_Write(GSocket
*socket
, const char *buffer
,
549 assert(socket
!= NULL
);
551 if (socket
->m_fd
== -1 || socket
->m_server
)
553 socket
->m_error
= GSOCK_INVSOCK
;
557 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
560 if (socket
->m_stream
)
561 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
563 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
567 if (errno
== EWOULDBLOCK
)
568 socket
->m_error
= GSOCK_WOULDBLOCK
;
570 socket
->m_error
= GSOCK_IOERR
;
572 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
573 * in MSW). Once the first OUTPUT event is received, users can assume
574 * that the socket is writable until a read operation fails. Only then
575 * will further OUTPUT events be posted.
577 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
584 * Polls the socket to determine its status. This function will
585 * check for the events specified in the 'flags' parameter, and
586 * it will return a mask indicating which operations can be
587 * performed. This function won't block, regardless of the
588 * mode (blocking|nonblocking) of the socket.
590 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
592 assert(socket
!= NULL
);
594 return (flags
& socket
->m_detected
);
599 /* GSocket_SetNonBlocking:
600 * Sets the socket to non-blocking mode. This is useful if
601 * we don't want to wait.
603 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
605 assert(socket
!= NULL
);
607 socket
->m_non_blocking
= non_block
;
610 /* GSocket_SetTimeout:
611 * Sets the timeout for blocking calls. Time is
612 * expressed in milliseconds.
614 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
616 assert(socket
!= NULL
);
618 socket
->m_timeout
= millisec
;
622 * Returns the last error occured for this socket.
624 GSocketError
GSocket_GetError(GSocket
*socket
)
626 assert(socket
!= NULL
);
628 return socket
->m_error
;
633 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
634 * and LOST). The callbacks are called in the following situations:
636 * INPUT: There is at least one byte in the input buffer
637 * OUTPUT: The system is sure that the next write call will not block
638 * CONNECTION: Two cases are possible:
639 * Client socket -> the connection is established
640 * Server socket -> a client requests a connection
641 * LOST: The connection is lost
643 * An event is generated only once and its state is reseted when the
644 * relative IO call is requested.
645 * For example: INPUT -> GSocket_Read()
646 * CONNECTION -> GSocket_Accept()
649 /* GSocket_SetCallback:
650 * Enables the callbacks specified by 'flags'. Note that 'flags'
651 * may be a combination of flags OR'ed toghether, so the same
652 * callback function can be made to accept different events.
653 * The callback function must have the following prototype:
655 * void function(GSocket *socket, GSocketEvent event, char *cdata)
657 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
658 GSocketCallback callback
, char *cdata
)
662 assert(socket
!= NULL
);
664 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
666 if ((flags
& (1 << count
)) != 0)
668 socket
->m_cbacks
[count
] = callback
;
669 socket
->m_data
[count
] = cdata
;
674 /* GSocket_UnsetCallback:
675 * Disables all callbacks specified by 'flags', which may be a
676 * combination of flags OR'ed toghether.
678 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
682 assert(socket
!= NULL
);
684 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
686 if ((flags
& (1 << count
)) != 0)
688 socket
->m_cbacks
[count
] = NULL
;
689 socket
->m_data
[count
] = NULL
;
694 #define CALL_CALLBACK(socket, event) { \
695 _GSocket_Disable(socket, event); \
696 if (socket->m_cbacks[event]) \
697 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
701 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
703 socket
->m_detected
&= ~(1 << event
);
704 _GSocket_Install_Callback(socket
, event
);
707 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
709 socket
->m_detected
|= (1 << event
);
710 _GSocket_Uninstall_Callback(socket
, event
);
713 /* _GSocket_Input_Timeout:
714 * For blocking sockets, wait until data is available or
715 * until timeout ellapses.
717 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
722 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
723 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
725 if (!socket
->m_non_blocking
)
728 FD_SET(socket
->m_fd
, &readfds
);
729 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
731 socket
->m_error
= GSOCK_TIMEDOUT
;
732 return GSOCK_TIMEDOUT
;
735 return GSOCK_NOERROR
;
738 /* _GSocket_Output_Timeout:
739 * For blocking sockets, wait until data can be sent without
740 * blocking or until timeout ellapses.
742 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
747 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
748 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
750 if (!socket
->m_non_blocking
)
753 FD_SET(socket
->m_fd
, &writefds
);
754 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
756 socket
->m_error
= GSOCK_TIMEDOUT
;
757 return GSOCK_TIMEDOUT
;
760 return GSOCK_NOERROR
;
763 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
767 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
772 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
774 struct sockaddr from
;
779 fromlen
= sizeof(from
);
781 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
786 /* Translate a system address into a GSocket address */
789 socket
->m_peer
= GAddress_new();
792 socket
->m_error
= GSOCK_MEMERR
;
796 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
797 if (err
!= GSOCK_NOERROR
)
799 GAddress_destroy(socket
->m_peer
);
800 socket
->m_peer
= NULL
;
801 socket
->m_error
= err
;
808 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
813 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
818 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
820 struct sockaddr
*addr
;
824 if (!socket
->m_peer
) {
825 socket
->m_error
= GSOCK_INVADDR
;
829 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
830 if (err
!= GSOCK_NOERROR
) {
831 socket
->m_error
= err
;
835 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
837 /* Frees memory allocated from _GAddress_translate_to */
843 void _GSocket_Detected_Read(GSocket
*socket
)
848 if (socket
->m_stream
)
850 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
852 if (ret
< 0 && socket
->m_server
)
854 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
860 CALL_CALLBACK(socket
, GSOCK_INPUT
);
864 CALL_CALLBACK(socket
, GSOCK_LOST
);
869 void _GSocket_Detected_Write(GSocket
*socket
)
871 if (socket
->m_establishing
&& !socket
->m_server
)
875 socket
->m_establishing
= FALSE
;
878 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
882 CALL_CALLBACK(socket
, GSOCK_LOST
);
886 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
887 /* We have to fire this event by hand because CONNECTION (for clients)
888 * and OUTPUT are internally the same and we just disabled CONNECTION
889 * events with the above macro.
891 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
896 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
901 * -------------------------------------------------------------------------
903 * -------------------------------------------------------------------------
906 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
907 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
908 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
910 #define CHECK_ADDRESS(address, family, retval) \
912 if (address->m_family == GSOCK_NOFAMILY) \
913 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
914 return address->m_error; \
916 if (address->m_family != GSOCK_##family) {\
917 address->m_error = GSOCK_INVADDR; \
922 GAddress
*GAddress_new()
926 address
= (GAddress
*)malloc(sizeof(GAddress
));
931 address
->m_family
= GSOCK_NOFAMILY
;
932 address
->m_addr
= NULL
;
938 GAddress
*GAddress_copy(GAddress
*address
)
942 assert(address
!= NULL
);
944 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
949 memcpy(addr2
, address
, sizeof(GAddress
));
951 if (address
->m_addr
) {
952 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
953 if (addr2
->m_addr
== NULL
) {
957 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
963 void GAddress_destroy(GAddress
*address
)
965 assert(address
!= NULL
);
970 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
972 assert(address
!= NULL
);
974 address
->m_family
= type
;
977 GAddressType
GAddress_GetFamily(GAddress
*address
)
979 assert(address
!= NULL
);
981 return address
->m_family
;
984 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
985 address
->m_realfamily
= addr
->sa_family
;
986 switch (addr
->sa_family
) {
988 address
->m_family
= GSOCK_INET
;
991 address
->m_family
= GSOCK_UNIX
;
995 address
->m_family
= GSOCK_INET6
;
1000 address
->m_error
= GSOCK_INVOP
;
1005 if (address
->m_addr
)
1006 free(address
->m_addr
);
1008 address
->m_len
= len
;
1009 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1010 if (address
->m_addr
== NULL
) {
1011 address
->m_error
= GSOCK_MEMERR
;
1012 return GSOCK_MEMERR
;
1014 memcpy(address
->m_addr
, addr
, len
);
1016 return GSOCK_NOERROR
;
1019 GSocketError
_GAddress_translate_to(GAddress
*address
,
1020 struct sockaddr
**addr
, int *len
)
1022 if (!address
->m_addr
) {
1023 address
->m_error
= GSOCK_INVADDR
;
1024 return GSOCK_INVADDR
;
1027 *len
= address
->m_len
;
1028 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1029 if (*addr
== NULL
) {
1030 address
->m_error
= GSOCK_MEMERR
;
1031 return GSOCK_MEMERR
;
1034 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1035 return GSOCK_NOERROR
;
1039 * -------------------------------------------------------------------------
1040 * Internet address family
1041 * -------------------------------------------------------------------------
1044 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1046 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1047 if (address
->m_addr
== NULL
) {
1048 address
->m_error
= GSOCK_MEMERR
;
1049 return GSOCK_MEMERR
;
1052 address
->m_len
= sizeof(struct sockaddr_in
);
1054 address
->m_family
= GSOCK_INET
;
1055 address
->m_realfamily
= PF_INET
;
1056 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1057 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1059 return GSOCK_NOERROR
;
1062 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1065 struct in_addr
*addr
;
1067 assert(address
!= NULL
);
1069 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1071 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1073 /* If it is a numeric host name, convert it now */
1074 #if defined(HAVE_INET_ATON)
1075 if (inet_aton(hostname
, addr
) == 0) {
1076 #elif defined(HAVE_INET_ADDR)
1077 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1078 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1080 struct in_addr
*array_addr
;
1082 /* It is a real name, we solve it */
1083 he
= gethostbyname((char*)hostname
);
1085 address
->m_error
= GSOCK_NOHOST
;
1086 return GSOCK_NOHOST
;
1088 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1089 addr
->s_addr
= array_addr
[0].s_addr
;
1090 #if defined(HAVE_INET_ATON)
1092 #elif defined(HAVE_INET_ADDR)
1095 return GSOCK_NOERROR
;
1098 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1099 unsigned long hostaddr
)
1101 struct in_addr
*addr
;
1103 assert(address
!= NULL
);
1105 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1107 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1108 addr
->s_addr
= hostaddr
;
1110 return GSOCK_NOERROR
;
1113 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1114 const char *protocol
)
1117 struct sockaddr_in
*addr
;
1119 assert(address
!= NULL
);
1120 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1123 address
->m_error
= GSOCK_INVPORT
;
1124 return GSOCK_INVPORT
;
1127 se
= getservbyname((char*)port
, (char*)protocol
);
1129 if (isdigit(port
[0])) {
1132 port_int
= atoi(port
);
1133 addr
= (struct sockaddr_in
*)address
->m_addr
;
1134 addr
->sin_port
= htons(port_int
);
1135 return GSOCK_NOERROR
;
1138 address
->m_error
= GSOCK_INVPORT
;
1139 return GSOCK_INVPORT
;
1142 addr
= (struct sockaddr_in
*)address
->m_addr
;
1143 addr
->sin_port
= se
->s_port
;
1145 return GSOCK_NOERROR
;
1148 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1150 struct sockaddr_in
*addr
;
1152 assert(address
!= NULL
);
1153 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1155 addr
= (struct sockaddr_in
*)address
->m_addr
;
1156 addr
->sin_port
= htons(port
);
1158 return GSOCK_NOERROR
;
1161 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1165 struct sockaddr_in
*addr
;
1167 assert(address
!= NULL
);
1168 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1170 addr
= (struct sockaddr_in
*)address
->m_addr
;
1171 addr_buf
= (char *)&(addr
->sin_addr
);
1173 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1175 address
->m_error
= GSOCK_NOHOST
;
1176 return GSOCK_NOHOST
;
1179 strncpy(hostname
, he
->h_name
, sbuf
);
1181 return GSOCK_NOERROR
;
1184 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1186 struct sockaddr_in
*addr
;
1188 assert(address
!= NULL
);
1189 CHECK_ADDRESS(address
, INET
, 0);
1191 addr
= (struct sockaddr_in
*)address
->m_addr
;
1193 return addr
->sin_addr
.s_addr
;
1196 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1198 struct sockaddr_in
*addr
;
1200 assert(address
!= NULL
);
1201 CHECK_ADDRESS(address
, INET
, 0);
1203 addr
= (struct sockaddr_in
*)address
->m_addr
;
1204 return ntohs(addr
->sin_port
);