]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/gsocket.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket main Unix-style file
6 * -------------------------------------------------------------------------
11 /* I don't see, why this include is needed, but it seems to be necessary
12 sometimes. For EMX, including C++ headers into plain C source breaks
13 compilation, so don't do it there. */
19 #define BSD_SELECT /* use Berkley Sockets select */
22 #include <sys\types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
29 #define HAVE_INET_ADDR
37 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
42 #include <sys\socket.h>
43 #include <sys\ioctl.h>
44 #include <sys\select.h>
46 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
47 int _System
bsdselect(int,
52 int _System
soclose(int);
64 #include "wx/gsocket.h"
65 #include "wx/os2/gsockos2.h"
71 # define SOCKLEN_T socklen_t
74 # define SOCKLEN_T int
79 /* Global initialisers */
86 void GSocket_Cleanup()
90 /* Constructors / Destructors */
92 GSocket
*GSocket_new()
97 socket
= (GSocket
*)malloc(sizeof(GSocket
));
103 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
105 socket
->m_cbacks
[i
] = NULL
;
107 socket
->m_detected
= 0;
108 socket
->m_local
= NULL
;
109 socket
->m_peer
= NULL
;
110 socket
->m_error
= GSOCK_NOERROR
;
111 socket
->m_server
= FALSE
;
112 socket
->m_stream
= TRUE
;
113 socket
->m_gui_dependent
= NULL
;
114 socket
->m_non_blocking
= FALSE
;
115 socket
->m_timeout
= 10*60*1000;
116 /* 10 minutes * 60 sec * 1000 millisec */
117 socket
->m_establishing
= FALSE
;
119 /* We initialize the GUI specific entries here */
120 _GSocket_GUI_Init(socket
);
125 void GSocket_destroy(GSocket
*socket
)
127 assert(socket
!= NULL
);
129 /* First, we check that the socket is really shutdowned */
130 if (socket
->m_fd
!= -1)
131 GSocket_Shutdown(socket
);
133 /* We destroy GUI specific variables */
134 _GSocket_GUI_Destroy(socket
);
136 /* We destroy private addresses */
138 GAddress_destroy(socket
->m_local
);
141 GAddress_destroy(socket
->m_peer
);
143 /* We destroy socket itself */
147 void GSocket_Shutdown(GSocket
*socket
)
151 assert(socket
!= NULL
);
153 /* If socket has been created, we shutdown it */
154 if (socket
->m_fd
!= -1)
156 shutdown(socket
->m_fd
, 2);
157 soclose(socket
->m_fd
);
161 /* We also disable GUI callbacks */
162 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
163 socket
->m_cbacks
[evt
] = NULL
;
165 socket
->m_detected
= 0;
166 _GSocket_Disable_Events(socket
);
169 /* Address handling */
171 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
173 assert(socket
!= NULL
);
175 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
176 socket
->m_error
= GSOCK_INVSOCK
;
177 return GSOCK_INVSOCK
;
180 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
181 socket
->m_error
= GSOCK_INVADDR
;
182 return GSOCK_INVADDR
;
186 GAddress_destroy(socket
->m_local
);
188 socket
->m_local
= GAddress_copy(address
);
190 return GSOCK_NOERROR
;
193 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
195 assert(socket
!= NULL
);
197 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
198 socket
->m_error
= GSOCK_INVADDR
;
199 return GSOCK_INVADDR
;
203 GAddress_destroy(socket
->m_peer
);
205 socket
->m_peer
= GAddress_copy(address
);
207 return GSOCK_NOERROR
;
210 GAddress
*GSocket_GetLocal(GSocket
*socket
)
213 struct sockaddr addr
;
217 assert(socket
!= NULL
);
220 return GAddress_copy(socket
->m_local
);
222 if (socket
->m_fd
== -1) {
223 socket
->m_error
= GSOCK_INVSOCK
;
229 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
230 socket
->m_error
= GSOCK_IOERR
;
234 address
= GAddress_new();
235 if (address
== NULL
) {
236 socket
->m_error
= GSOCK_MEMERR
;
239 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
240 if (socket
->m_error
!= GSOCK_NOERROR
) {
241 GAddress_destroy(address
);
248 GAddress
*GSocket_GetPeer(GSocket
*socket
)
250 assert(socket
!= NULL
);
253 return GAddress_copy(socket
->m_peer
);
258 /* Server specific parts */
260 /* GSocket_SetServer:
261 * Sets up the socket as a server. It uses the "Local" field of GSocket.
262 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
263 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
264 * been initialized, GSOCK_INVADDR if the local address has not been
265 * defined and GSOCK_IOERR for other internal errors.
267 GSocketError
GSocket_SetServer(GSocket
*sck
)
274 if (sck
->m_fd
!= -1) {
275 sck
->m_error
= GSOCK_INVSOCK
;
276 return GSOCK_INVSOCK
;
280 sck
->m_error
= GSOCK_INVADDR
;
281 return GSOCK_INVADDR
;
284 /* We always have a stream here */
285 sck
->m_stream
= TRUE
;
286 sck
->m_server
= TRUE
;
288 /* Create the socket */
289 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
291 if (sck
->m_fd
== -1) {
292 sck
->m_error
= GSOCK_IOERR
;
296 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
297 _GSocket_Enable_Events(sck
);
299 /* Bind the socket to the LOCAL address */
300 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
303 sck
->m_error
= GSOCK_IOERR
;
307 /* Enable listening up to 5 connections */
308 if (listen(sck
->m_fd
, 5) < 0) {
311 sck
->m_error
= GSOCK_IOERR
;
315 return GSOCK_NOERROR
;
318 /* GSocket_WaitConnection:
319 * Waits for an incoming client connection.
321 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
326 assert(socket
!= NULL
);
328 /* Reenable CONNECTION events */
329 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
331 /* If the socket has already been created, we exit immediately */
332 if (socket
->m_fd
== -1 || !socket
->m_server
)
334 socket
->m_error
= GSOCK_INVSOCK
;
338 /* Create a GSocket object for the new connection */
339 connection
= GSocket_new();
342 connection
->m_error
= GSOCK_MEMERR
;
346 /* Accept the incoming connection */
347 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
349 GSocket_destroy(connection
);
350 /* socket->m_error set by _GSocket_Input_Timeout */
354 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
356 if (connection
->m_fd
== -1)
358 if (errno
== EWOULDBLOCK
)
359 socket
->m_error
= GSOCK_WOULDBLOCK
;
361 socket
->m_error
= GSOCK_IOERR
;
363 GSocket_destroy(connection
);
367 /* Initialize all fields */
368 connection
->m_server
= FALSE
;
369 connection
->m_stream
= TRUE
;
370 connection
->m_oriented
= TRUE
;
372 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
373 _GSocket_Enable_Events(connection
);
378 /* Non oriented connections */
380 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
386 if (sck
->m_fd
!= -1) {
387 sck
->m_error
= GSOCK_INVSOCK
;
388 return GSOCK_INVSOCK
;
392 sck
->m_error
= GSOCK_INVADDR
;
393 return GSOCK_INVADDR
;
396 sck
->m_stream
= FALSE
;
397 sck
->m_server
= FALSE
;
398 sck
->m_oriented
= FALSE
;
400 /* Create the socket */
401 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
404 sck
->m_error
= GSOCK_IOERR
;
408 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
409 _GSocket_Enable_Events(sck
);
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
);
457 sck
->m_oriented
= TRUE
;
458 sck
->m_server
= FALSE
;
459 sck
->m_establishing
= FALSE
;
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));
475 _GSocket_Enable_Events(sck
);
477 /* Connect it to the PEER address */
478 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
484 /* If connect failed with EINPROGRESS and the GSocket object
485 * is in blocking mode, we select() for the specified timeout
486 * checking for writability to see if the connection request
489 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
491 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
495 /* sck->m_error is set in _GSocket_Output_Timeout */
496 fprintf(stderr
, "Blocking connect timeouts\n");
497 return GSOCK_TIMEDOUT
;
501 fprintf(stderr
, "Blocking connect OK\n");
502 return GSOCK_NOERROR
;
506 /* If connect failed with EINPROGRESS and the GSocket object
507 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
508 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
509 * this way if the connection completes, a GSOCK_CONNECTION
510 * event will be generated, if enabled.
512 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
514 sck
->m_error
= GSOCK_WOULDBLOCK
;
515 sck
->m_establishing
= TRUE
;
516 fprintf(stderr
, "Nonblocking connect in progress\n");
518 return GSOCK_WOULDBLOCK
;
521 /* If connect failed with an error other than EINPROGRESS,
522 * then the call to GSocket_Connect has failed.
526 sck
->m_error
= GSOCK_IOERR
;
528 fprintf(stderr
, "Connect failed (generic err)\n");
532 fprintf(stderr
, "Connect OK\n");
533 return GSOCK_NOERROR
;
538 /* Like recv(), send(), ... */
539 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
543 assert(socket
!= NULL
);
545 /* Reenable INPUT events */
546 _GSocket_Enable(socket
, GSOCK_INPUT
);
548 if (socket
->m_fd
== -1 || socket
->m_server
)
550 socket
->m_error
= GSOCK_INVSOCK
;
554 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
557 if (socket
->m_stream
)
558 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
560 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
564 if (errno
== EWOULDBLOCK
)
565 socket
->m_error
= GSOCK_WOULDBLOCK
;
567 socket
->m_error
= GSOCK_IOERR
;
573 int GSocket_Write(GSocket
*socket
, const char *buffer
,
578 assert(socket
!= NULL
);
580 if (socket
->m_fd
== -1 || socket
->m_server
)
582 socket
->m_error
= GSOCK_INVSOCK
;
586 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
589 if (socket
->m_stream
)
590 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
592 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
596 if (errno
== EWOULDBLOCK
)
597 socket
->m_error
= GSOCK_WOULDBLOCK
;
599 socket
->m_error
= GSOCK_IOERR
;
601 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
602 * in MSW). Once the first OUTPUT event is received, users can assume
603 * that the socket is writable until a read operation fails. Only then
604 * will further OUTPUT events be posted.
606 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
613 * Polls the socket to determine its status. This function will
614 * check for the events specified in the 'flags' parameter, and
615 * it will return a mask indicating which operations can be
616 * performed. This function won't block, regardless of the
617 * mode (blocking|nonblocking) of the socket.
619 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
621 assert(socket
!= NULL
);
623 return (flags
& socket
->m_detected
);
628 /* GSocket_SetNonBlocking:
629 * Sets the socket to non-blocking mode. This is useful if
630 * we don't want to wait.
632 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
634 assert(socket
!= NULL
);
636 socket
->m_non_blocking
= non_block
;
639 /* GSocket_SetTimeout:
640 * Sets the timeout for blocking calls. Time is
641 * expressed in milliseconds.
643 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
645 assert(socket
!= NULL
);
647 socket
->m_timeout
= millisec
;
651 * Returns the last error occured for this socket.
653 GSocketError
GSocket_GetError(GSocket
*socket
)
655 assert(socket
!= NULL
);
657 return socket
->m_error
;
662 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
663 * and LOST). The callbacks are called in the following situations:
665 * INPUT: There is at least one byte in the input buffer
666 * OUTPUT: The system is sure that the next write call will not block
667 * CONNECTION: Two cases are possible:
668 * Client socket -> the connection is established
669 * Server socket -> a client requests a connection
670 * LOST: The connection is lost
672 * An event is generated only once and its state is reseted when the
673 * relative IO call is requested.
674 * For example: INPUT -> GSocket_Read()
675 * CONNECTION -> GSocket_Accept()
678 /* GSocket_SetCallback:
679 * Enables the callbacks specified by 'flags'. Note that 'flags'
680 * may be a combination of flags OR'ed toghether, so the same
681 * callback function can be made to accept different events.
682 * The callback function must have the following prototype:
684 * void function(GSocket *socket, GSocketEvent event, char *cdata)
686 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
687 GSocketCallback callback
, char *cdata
)
691 assert(socket
!= NULL
);
693 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
695 if ((flags
& (1 << count
)) != 0)
697 socket
->m_cbacks
[count
] = callback
;
698 socket
->m_data
[count
] = cdata
;
703 /* GSocket_UnsetCallback:
704 * Disables all callbacks specified by 'flags', which may be a
705 * combination of flags OR'ed toghether.
707 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
711 assert(socket
!= NULL
);
713 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
715 if ((flags
& (1 << count
)) != 0)
717 socket
->m_cbacks
[count
] = NULL
;
718 socket
->m_data
[count
] = NULL
;
723 #define CALL_CALLBACK(socket, event) { \
724 _GSocket_Disable(socket, event); \
725 if (socket->m_cbacks[event]) \
726 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
730 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
732 socket
->m_detected
&= ~(1 << event
);
733 _GSocket_Install_Callback(socket
, event
);
736 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
738 socket
->m_detected
|= (1 << event
);
739 _GSocket_Uninstall_Callback(socket
, event
);
742 /* _GSocket_Input_Timeout:
743 * For blocking sockets, wait until data is available or
744 * until timeout ellapses.
746 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
751 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
752 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
754 if (!socket
->m_non_blocking
)
757 FD_SET(socket
->m_fd
, &readfds
);
758 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
760 socket
->m_error
= GSOCK_TIMEDOUT
;
761 return GSOCK_TIMEDOUT
;
764 return GSOCK_NOERROR
;
767 /* _GSocket_Output_Timeout:
768 * For blocking sockets, wait until data can be sent without
769 * blocking or until timeout ellapses.
771 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
776 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
777 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
779 if (!socket
->m_non_blocking
)
782 FD_SET(socket
->m_fd
, &writefds
);
783 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
785 socket
->m_error
= GSOCK_TIMEDOUT
;
786 return GSOCK_TIMEDOUT
;
789 return GSOCK_NOERROR
;
792 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
796 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
801 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
803 struct sockaddr from
;
808 fromlen
= sizeof(from
);
810 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
815 /* Translate a system address into a GSocket address */
818 socket
->m_peer
= GAddress_new();
821 socket
->m_error
= GSOCK_MEMERR
;
825 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
826 if (err
!= GSOCK_NOERROR
)
828 GAddress_destroy(socket
->m_peer
);
829 socket
->m_peer
= NULL
;
830 socket
->m_error
= err
;
837 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
842 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
847 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
849 struct sockaddr
*addr
;
853 if (!socket
->m_peer
) {
854 socket
->m_error
= GSOCK_INVADDR
;
858 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
859 if (err
!= GSOCK_NOERROR
) {
860 socket
->m_error
= err
;
864 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
866 /* Frees memory allocated from _GAddress_translate_to */
872 void _GSocket_Detected_Read(GSocket
*socket
)
877 if (socket
->m_stream
)
879 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
881 if (ret
< 0 && socket
->m_server
)
883 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
889 CALL_CALLBACK(socket
, GSOCK_INPUT
);
893 CALL_CALLBACK(socket
, GSOCK_LOST
);
898 void _GSocket_Detected_Write(GSocket
*socket
)
900 if (socket
->m_establishing
&& !socket
->m_server
)
904 socket
->m_establishing
= FALSE
;
907 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
911 CALL_CALLBACK(socket
, GSOCK_LOST
);
915 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
916 /* We have to fire this event by hand because CONNECTION (for clients)
917 * and OUTPUT are internally the same and we just disabled CONNECTION
918 * events with the above macro.
920 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
925 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
930 * -------------------------------------------------------------------------
932 * -------------------------------------------------------------------------
935 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
936 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
937 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
939 #define CHECK_ADDRESS(address, family, retval) \
941 if (address->m_family == GSOCK_NOFAMILY) \
942 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
943 return address->m_error; \
945 if (address->m_family != GSOCK_##family) {\
946 address->m_error = GSOCK_INVADDR; \
951 GAddress
*GAddress_new()
955 address
= (GAddress
*)malloc(sizeof(GAddress
));
960 address
->m_family
= GSOCK_NOFAMILY
;
961 address
->m_addr
= NULL
;
967 GAddress
*GAddress_copy(GAddress
*address
)
971 assert(address
!= NULL
);
973 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
978 memcpy(addr2
, address
, sizeof(GAddress
));
980 if (address
->m_addr
) {
981 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
982 if (addr2
->m_addr
== NULL
) {
986 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
992 void GAddress_destroy(GAddress
*address
)
994 assert(address
!= NULL
);
999 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1001 assert(address
!= NULL
);
1003 address
->m_family
= type
;
1006 GAddressType
GAddress_GetFamily(GAddress
*address
)
1008 assert(address
!= NULL
);
1010 return address
->m_family
;
1013 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1014 address
->m_realfamily
= addr
->sa_family
;
1015 switch (addr
->sa_family
) {
1017 address
->m_family
= GSOCK_INET
;
1020 address
->m_family
= GSOCK_UNIX
;
1024 address
->m_family
= GSOCK_INET6
;
1029 address
->m_error
= GSOCK_INVOP
;
1034 if (address
->m_addr
)
1035 free(address
->m_addr
);
1037 address
->m_len
= len
;
1038 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1039 if (address
->m_addr
== NULL
) {
1040 address
->m_error
= GSOCK_MEMERR
;
1041 return GSOCK_MEMERR
;
1043 memcpy(address
->m_addr
, addr
, len
);
1045 return GSOCK_NOERROR
;
1048 GSocketError
_GAddress_translate_to(GAddress
*address
,
1049 struct sockaddr
**addr
, int *len
)
1051 if (!address
->m_addr
) {
1052 address
->m_error
= GSOCK_INVADDR
;
1053 return GSOCK_INVADDR
;
1056 *len
= address
->m_len
;
1057 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1058 if (*addr
== NULL
) {
1059 address
->m_error
= GSOCK_MEMERR
;
1060 return GSOCK_MEMERR
;
1063 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1064 return GSOCK_NOERROR
;
1068 * -------------------------------------------------------------------------
1069 * Internet address family
1070 * -------------------------------------------------------------------------
1073 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1075 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1076 if (address
->m_addr
== NULL
) {
1077 address
->m_error
= GSOCK_MEMERR
;
1078 return GSOCK_MEMERR
;
1081 address
->m_len
= sizeof(struct sockaddr_in
);
1083 address
->m_family
= GSOCK_INET
;
1084 address
->m_realfamily
= PF_INET
;
1085 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1086 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1088 return GSOCK_NOERROR
;
1091 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1094 struct in_addr
*addr
;
1096 assert(address
!= NULL
);
1098 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1100 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1102 /* If it is a numeric host name, convert it now */
1103 #if defined(HAVE_INET_ATON)
1104 if (inet_aton(hostname
, addr
) == 0) {
1105 #elif defined(HAVE_INET_ADDR)
1106 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1107 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1109 struct in_addr
*array_addr
;
1111 /* It is a real name, we solve it */
1112 he
= gethostbyname((char*)hostname
);
1114 address
->m_error
= GSOCK_NOHOST
;
1115 return GSOCK_NOHOST
;
1117 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1118 addr
->s_addr
= array_addr
[0].s_addr
;
1119 #if defined(HAVE_INET_ATON)
1121 #elif defined(HAVE_INET_ADDR)
1124 return GSOCK_NOERROR
;
1127 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1128 unsigned long hostaddr
)
1130 struct in_addr
*addr
;
1132 assert(address
!= NULL
);
1134 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1136 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1137 addr
->s_addr
= hostaddr
;
1139 return GSOCK_NOERROR
;
1142 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1143 const char *protocol
)
1146 struct sockaddr_in
*addr
;
1148 assert(address
!= NULL
);
1149 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1152 address
->m_error
= GSOCK_INVPORT
;
1153 return GSOCK_INVPORT
;
1156 se
= getservbyname((char*)port
, (char*)protocol
);
1158 if (isdigit(port
[0])) {
1161 port_int
= atoi(port
);
1162 addr
= (struct sockaddr_in
*)address
->m_addr
;
1163 addr
->sin_port
= htons(port_int
);
1164 return GSOCK_NOERROR
;
1167 address
->m_error
= GSOCK_INVPORT
;
1168 return GSOCK_INVPORT
;
1171 addr
= (struct sockaddr_in
*)address
->m_addr
;
1172 addr
->sin_port
= se
->s_port
;
1174 return GSOCK_NOERROR
;
1177 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1179 struct sockaddr_in
*addr
;
1181 assert(address
!= NULL
);
1182 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1184 addr
= (struct sockaddr_in
*)address
->m_addr
;
1185 addr
->sin_port
= htons(port
);
1187 return GSOCK_NOERROR
;
1190 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1194 struct sockaddr_in
*addr
;
1196 assert(address
!= NULL
);
1197 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1199 addr
= (struct sockaddr_in
*)address
->m_addr
;
1200 addr_buf
= (char *)&(addr
->sin_addr
);
1202 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1204 address
->m_error
= GSOCK_NOHOST
;
1205 return GSOCK_NOHOST
;
1208 strncpy(hostname
, he
->h_name
, sbuf
);
1210 return GSOCK_NOERROR
;
1213 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1215 struct sockaddr_in
*addr
;
1217 assert(address
!= NULL
);
1218 CHECK_ADDRESS(address
, INET
, 0);
1220 addr
= (struct sockaddr_in
*)address
->m_addr
;
1222 return addr
->sin_addr
.s_addr
;
1225 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1227 struct sockaddr_in
*addr
;
1229 assert(address
!= NULL
);
1230 CHECK_ADDRESS(address
, INET
, 0);
1232 addr
= (struct sockaddr_in
*)address
->m_addr
;
1233 return ntohs(addr
->sin_port
);