]>
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>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #define HAVE_INET_ADDR
32 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
37 #include <sys\socket.h>
38 #include <sys\ioctl.h>
39 #include <sys\select.h>
41 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
42 int _System
bsdselect(int,
47 int _System
soclose(int);
59 #include "wx/gsocket.h"
60 #include "wx/os2/gsockos2.h"
66 # define SOCKLEN_T socklen_t
69 # define SOCKLEN_T int
74 /* Global initialisers */
81 void GSocket_Cleanup()
85 /* Constructors / Destructors */
87 GSocket
*GSocket_new()
92 socket
= (GSocket
*)malloc(sizeof(GSocket
));
98 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
100 socket
->m_cbacks
[i
] = NULL
;
102 socket
->m_detected
= 0;
103 socket
->m_local
= NULL
;
104 socket
->m_peer
= NULL
;
105 socket
->m_error
= GSOCK_NOERROR
;
106 socket
->m_server
= FALSE
;
107 socket
->m_stream
= TRUE
;
108 socket
->m_gui_dependent
= NULL
;
109 socket
->m_non_blocking
= FALSE
;
110 socket
->m_timeout
= 10*60*1000;
111 /* 10 minutes * 60 sec * 1000 millisec */
112 socket
->m_establishing
= FALSE
;
114 /* We initialize the GUI specific entries here */
115 _GSocket_GUI_Init(socket
);
120 void GSocket_destroy(GSocket
*socket
)
122 assert(socket
!= NULL
);
124 /* First, we check that the socket is really shutdowned */
125 if (socket
->m_fd
!= -1)
126 GSocket_Shutdown(socket
);
128 /* We destroy GUI specific variables */
129 _GSocket_GUI_Destroy(socket
);
131 /* We destroy private addresses */
133 GAddress_destroy(socket
->m_local
);
136 GAddress_destroy(socket
->m_peer
);
138 /* We destroy socket itself */
142 void GSocket_Shutdown(GSocket
*socket
)
146 assert(socket
!= NULL
);
148 /* If socket has been created, we shutdown it */
149 if (socket
->m_fd
!= -1)
151 shutdown(socket
->m_fd
, 2);
152 soclose(socket
->m_fd
);
156 /* We also disable GUI callbacks */
157 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
158 socket
->m_cbacks
[evt
] = NULL
;
160 socket
->m_detected
= 0;
161 _GSocket_Disable_Events(socket
);
164 /* Address handling */
166 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
168 assert(socket
!= NULL
);
170 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
171 socket
->m_error
= GSOCK_INVSOCK
;
172 return GSOCK_INVSOCK
;
175 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
176 socket
->m_error
= GSOCK_INVADDR
;
177 return GSOCK_INVADDR
;
181 GAddress_destroy(socket
->m_local
);
183 socket
->m_local
= GAddress_copy(address
);
185 return GSOCK_NOERROR
;
188 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
190 assert(socket
!= NULL
);
192 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
193 socket
->m_error
= GSOCK_INVADDR
;
194 return GSOCK_INVADDR
;
198 GAddress_destroy(socket
->m_peer
);
200 socket
->m_peer
= GAddress_copy(address
);
202 return GSOCK_NOERROR
;
205 GAddress
*GSocket_GetLocal(GSocket
*socket
)
208 struct sockaddr addr
;
212 assert(socket
!= NULL
);
215 return GAddress_copy(socket
->m_local
);
217 if (socket
->m_fd
== -1) {
218 socket
->m_error
= GSOCK_INVSOCK
;
224 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
225 socket
->m_error
= GSOCK_IOERR
;
229 address
= GAddress_new();
230 if (address
== NULL
) {
231 socket
->m_error
= GSOCK_MEMERR
;
234 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
235 if (socket
->m_error
!= GSOCK_NOERROR
) {
236 GAddress_destroy(address
);
243 GAddress
*GSocket_GetPeer(GSocket
*socket
)
245 assert(socket
!= NULL
);
248 return GAddress_copy(socket
->m_peer
);
253 /* Server specific parts */
255 /* GSocket_SetServer:
256 * Sets up the socket as a server. It uses the "Local" field of GSocket.
257 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
258 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
259 * been initialized, GSOCK_INVADDR if the local address has not been
260 * defined and GSOCK_IOERR for other internal errors.
262 GSocketError
GSocket_SetServer(GSocket
*sck
)
269 if (sck
->m_fd
!= -1) {
270 sck
->m_error
= GSOCK_INVSOCK
;
271 return GSOCK_INVSOCK
;
275 sck
->m_error
= GSOCK_INVADDR
;
276 return GSOCK_INVADDR
;
279 /* We always have a stream here */
280 sck
->m_stream
= TRUE
;
281 sck
->m_server
= TRUE
;
283 /* Create the socket */
284 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
286 if (sck
->m_fd
== -1) {
287 sck
->m_error
= GSOCK_IOERR
;
291 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
292 _GSocket_Enable_Events(sck
);
294 /* Bind the socket to the LOCAL address */
295 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
298 sck
->m_error
= GSOCK_IOERR
;
302 /* Enable listening up to 5 connections */
303 if (listen(sck
->m_fd
, 5) < 0) {
306 sck
->m_error
= GSOCK_IOERR
;
310 return GSOCK_NOERROR
;
313 /* GSocket_WaitConnection:
314 * Waits for an incoming client connection.
316 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
321 assert(socket
!= NULL
);
323 /* Reenable CONNECTION events */
324 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
326 /* If the socket has already been created, we exit immediately */
327 if (socket
->m_fd
== -1 || !socket
->m_server
)
329 socket
->m_error
= GSOCK_INVSOCK
;
333 /* Create a GSocket object for the new connection */
334 connection
= GSocket_new();
337 connection
->m_error
= GSOCK_MEMERR
;
341 /* Accept the incoming connection */
342 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
344 GSocket_destroy(connection
);
345 /* socket->m_error set by _GSocket_Input_Timeout */
349 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
351 if (connection
->m_fd
== -1)
353 if (errno
== EWOULDBLOCK
)
354 socket
->m_error
= GSOCK_WOULDBLOCK
;
356 socket
->m_error
= GSOCK_IOERR
;
358 GSocket_destroy(connection
);
362 /* Initialize all fields */
363 connection
->m_server
= FALSE
;
364 connection
->m_stream
= TRUE
;
365 connection
->m_oriented
= TRUE
;
367 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
368 _GSocket_Enable_Events(connection
);
373 /* Non oriented connections */
375 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
381 if (sck
->m_fd
!= -1) {
382 sck
->m_error
= GSOCK_INVSOCK
;
383 return GSOCK_INVSOCK
;
387 sck
->m_error
= GSOCK_INVADDR
;
388 return GSOCK_INVADDR
;
391 sck
->m_stream
= FALSE
;
392 sck
->m_server
= FALSE
;
393 sck
->m_oriented
= FALSE
;
395 /* Create the socket */
396 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
399 sck
->m_error
= GSOCK_IOERR
;
403 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
404 _GSocket_Enable_Events(sck
);
406 /* Bind it to the LOCAL address */
407 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
410 sck
->m_error
= GSOCK_IOERR
;
414 return GSOCK_NOERROR
;
417 /* Client specific parts */
420 * Establishes a client connection to a server using the "Peer"
421 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
422 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
423 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
424 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
425 * the connection request can be completed later. Use GSocket_Select()
426 * to check or wait for a GSOCK_CONNECTION event.
428 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
435 /* Enable CONNECTION events (needed for nonblocking connections) */
436 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
440 sck
->m_error
= GSOCK_INVSOCK
;
441 return GSOCK_INVSOCK
;
446 sck
->m_error
= GSOCK_INVADDR
;
447 return GSOCK_INVADDR
;
450 /* Test whether we want the socket to be a stream (e.g. TCP) */
451 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
452 sck
->m_oriented
= TRUE
;
453 sck
->m_server
= FALSE
;
454 sck
->m_establishing
= FALSE
;
461 /* Create the socket */
462 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
464 if (sck
->m_fd
== -1) {
465 sck
->m_error
= GSOCK_IOERR
;
469 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
470 _GSocket_Enable_Events(sck
);
472 /* Connect it to the PEER address */
473 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
479 /* If connect failed with EINPROGRESS and the GSocket object
480 * is in blocking mode, we select() for the specified timeout
481 * checking for writability to see if the connection request
484 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
486 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
490 /* sck->m_error is set in _GSocket_Output_Timeout */
491 fprintf(stderr
, "Blocking connect timeouts\n");
492 return GSOCK_TIMEDOUT
;
496 fprintf(stderr
, "Blocking connect OK\n");
497 return GSOCK_NOERROR
;
501 /* If connect failed with EINPROGRESS and the GSocket object
502 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
503 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
504 * this way if the connection completes, a GSOCK_CONNECTION
505 * event will be generated, if enabled.
507 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
509 sck
->m_error
= GSOCK_WOULDBLOCK
;
510 sck
->m_establishing
= TRUE
;
511 fprintf(stderr
, "Nonblocking connect in progress\n");
513 return GSOCK_WOULDBLOCK
;
516 /* If connect failed with an error other than EINPROGRESS,
517 * then the call to GSocket_Connect has failed.
521 sck
->m_error
= GSOCK_IOERR
;
523 fprintf(stderr
, "Connect failed (generic err)\n");
527 fprintf(stderr
, "Connect OK\n");
528 return GSOCK_NOERROR
;
533 /* Like recv(), send(), ... */
534 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
538 assert(socket
!= NULL
);
540 /* Reenable INPUT events */
541 _GSocket_Enable(socket
, GSOCK_INPUT
);
543 if (socket
->m_fd
== -1 || socket
->m_server
)
545 socket
->m_error
= GSOCK_INVSOCK
;
549 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
552 if (socket
->m_stream
)
553 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
555 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
559 if (errno
== EWOULDBLOCK
)
560 socket
->m_error
= GSOCK_WOULDBLOCK
;
562 socket
->m_error
= GSOCK_IOERR
;
568 int GSocket_Write(GSocket
*socket
, const char *buffer
,
573 assert(socket
!= NULL
);
575 if (socket
->m_fd
== -1 || socket
->m_server
)
577 socket
->m_error
= GSOCK_INVSOCK
;
581 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
584 if (socket
->m_stream
)
585 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
587 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
591 if (errno
== EWOULDBLOCK
)
592 socket
->m_error
= GSOCK_WOULDBLOCK
;
594 socket
->m_error
= GSOCK_IOERR
;
596 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
597 * in MSW). Once the first OUTPUT event is received, users can assume
598 * that the socket is writable until a read operation fails. Only then
599 * will further OUTPUT events be posted.
601 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
608 * Polls the socket to determine its status. This function will
609 * check for the events specified in the 'flags' parameter, and
610 * it will return a mask indicating which operations can be
611 * performed. This function won't block, regardless of the
612 * mode (blocking|nonblocking) of the socket.
614 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
616 assert(socket
!= NULL
);
618 return (flags
& socket
->m_detected
);
623 /* GSocket_SetNonBlocking:
624 * Sets the socket to non-blocking mode. This is useful if
625 * we don't want to wait.
627 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
629 assert(socket
!= NULL
);
631 socket
->m_non_blocking
= non_block
;
634 /* GSocket_SetTimeout:
635 * Sets the timeout for blocking calls. Time is
636 * expressed in milliseconds.
638 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
640 assert(socket
!= NULL
);
642 socket
->m_timeout
= millisec
;
646 * Returns the last error occured for this socket.
648 GSocketError
GSocket_GetError(GSocket
*socket
)
650 assert(socket
!= NULL
);
652 return socket
->m_error
;
657 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
658 * and LOST). The callbacks are called in the following situations:
660 * INPUT: There is at least one byte in the input buffer
661 * OUTPUT: The system is sure that the next write call will not block
662 * CONNECTION: Two cases are possible:
663 * Client socket -> the connection is established
664 * Server socket -> a client requests a connection
665 * LOST: The connection is lost
667 * An event is generated only once and its state is reseted when the
668 * relative IO call is requested.
669 * For example: INPUT -> GSocket_Read()
670 * CONNECTION -> GSocket_Accept()
673 /* GSocket_SetCallback:
674 * Enables the callbacks specified by 'flags'. Note that 'flags'
675 * may be a combination of flags OR'ed toghether, so the same
676 * callback function can be made to accept different events.
677 * The callback function must have the following prototype:
679 * void function(GSocket *socket, GSocketEvent event, char *cdata)
681 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
682 GSocketCallback callback
, char *cdata
)
686 assert(socket
!= NULL
);
688 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
690 if ((flags
& (1 << count
)) != 0)
692 socket
->m_cbacks
[count
] = callback
;
693 socket
->m_data
[count
] = cdata
;
698 /* GSocket_UnsetCallback:
699 * Disables all callbacks specified by 'flags', which may be a
700 * combination of flags OR'ed toghether.
702 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
706 assert(socket
!= NULL
);
708 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
710 if ((flags
& (1 << count
)) != 0)
712 socket
->m_cbacks
[count
] = NULL
;
713 socket
->m_data
[count
] = NULL
;
718 #define CALL_CALLBACK(socket, event) { \
719 _GSocket_Disable(socket, event); \
720 if (socket->m_cbacks[event]) \
721 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
725 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
727 socket
->m_detected
&= ~(1 << event
);
728 _GSocket_Install_Callback(socket
, event
);
731 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
733 socket
->m_detected
|= (1 << event
);
734 _GSocket_Uninstall_Callback(socket
, 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
= FALSE
;
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_SetHostAddress(GAddress
*address
,
1123 unsigned long hostaddr
)
1125 struct in_addr
*addr
;
1127 assert(address
!= NULL
);
1129 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1131 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1132 addr
->s_addr
= hostaddr
;
1134 return GSOCK_NOERROR
;
1137 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1138 const char *protocol
)
1141 struct sockaddr_in
*addr
;
1143 assert(address
!= NULL
);
1144 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1147 address
->m_error
= GSOCK_INVPORT
;
1148 return GSOCK_INVPORT
;
1151 se
= getservbyname((char*)port
, (char*)protocol
);
1153 if (isdigit(port
[0])) {
1156 port_int
= atoi(port
);
1157 addr
= (struct sockaddr_in
*)address
->m_addr
;
1158 addr
->sin_port
= htons(port_int
);
1159 return GSOCK_NOERROR
;
1162 address
->m_error
= GSOCK_INVPORT
;
1163 return GSOCK_INVPORT
;
1166 addr
= (struct sockaddr_in
*)address
->m_addr
;
1167 addr
->sin_port
= se
->s_port
;
1169 return GSOCK_NOERROR
;
1172 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1174 struct sockaddr_in
*addr
;
1176 assert(address
!= NULL
);
1177 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1179 addr
= (struct sockaddr_in
*)address
->m_addr
;
1180 addr
->sin_port
= htons(port
);
1182 return GSOCK_NOERROR
;
1185 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1189 struct sockaddr_in
*addr
;
1191 assert(address
!= NULL
);
1192 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1194 addr
= (struct sockaddr_in
*)address
->m_addr
;
1195 addr_buf
= (char *)&(addr
->sin_addr
);
1197 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1199 address
->m_error
= GSOCK_NOHOST
;
1200 return GSOCK_NOHOST
;
1203 strncpy(hostname
, he
->h_name
, sbuf
);
1205 return GSOCK_NOERROR
;
1208 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1210 struct sockaddr_in
*addr
;
1212 assert(address
!= NULL
);
1213 CHECK_ADDRESS(address
, INET
, 0);
1215 addr
= (struct sockaddr_in
*)address
->m_addr
;
1217 return addr
->sin_addr
.s_addr
;
1220 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1222 struct sockaddr_in
*addr
;
1224 assert(address
!= NULL
);
1225 CHECK_ADDRESS(address
, INET
, 0);
1227 addr
= (struct sockaddr_in
*)address
->m_addr
;
1228 return ntohs(addr
->sin_port
);