]>
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 soclose(a) close(a)
48 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
49 int _System
bsdselect(int,
54 int _System
soclose(int);
66 #include "wx/gsocket.h"
67 #include "wx/os2/gsockos2.h"
73 # define SOCKLEN_T socklen_t
76 # define SOCKLEN_T int
81 /* Global initialisers */
88 void GSocket_Cleanup()
92 /* Constructors / Destructors */
94 GSocket
*GSocket_new()
99 socket
= (GSocket
*)malloc(sizeof(GSocket
));
105 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
107 socket
->m_cbacks
[i
] = NULL
;
109 socket
->m_detected
= 0;
110 socket
->m_local
= NULL
;
111 socket
->m_peer
= NULL
;
112 socket
->m_error
= GSOCK_NOERROR
;
113 socket
->m_server
= FALSE
;
114 socket
->m_stream
= TRUE
;
115 socket
->m_gui_dependent
= NULL
;
116 socket
->m_non_blocking
= FALSE
;
117 socket
->m_timeout
= 10*60*1000;
118 /* 10 minutes * 60 sec * 1000 millisec */
119 socket
->m_establishing
= FALSE
;
121 /* We initialize the GUI specific entries here */
122 _GSocket_GUI_Init(socket
);
127 void GSocket_destroy(GSocket
*socket
)
129 assert(socket
!= NULL
);
131 /* First, we check that the socket is really shutdowned */
132 if (socket
->m_fd
!= -1)
133 GSocket_Shutdown(socket
);
135 /* We destroy GUI specific variables */
136 _GSocket_GUI_Destroy(socket
);
138 /* We destroy private addresses */
140 GAddress_destroy(socket
->m_local
);
143 GAddress_destroy(socket
->m_peer
);
145 /* We destroy socket itself */
149 void GSocket_Shutdown(GSocket
*socket
)
153 assert(socket
!= NULL
);
155 /* If socket has been created, we shutdown it */
156 if (socket
->m_fd
!= -1)
158 shutdown(socket
->m_fd
, 2);
159 soclose(socket
->m_fd
);
163 /* We also disable GUI callbacks */
164 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
165 socket
->m_cbacks
[evt
] = NULL
;
167 socket
->m_detected
= 0;
168 _GSocket_Disable_Events(socket
);
171 /* Address handling */
173 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
175 assert(socket
!= NULL
);
177 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
178 socket
->m_error
= GSOCK_INVSOCK
;
179 return GSOCK_INVSOCK
;
182 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
183 socket
->m_error
= GSOCK_INVADDR
;
184 return GSOCK_INVADDR
;
188 GAddress_destroy(socket
->m_local
);
190 socket
->m_local
= GAddress_copy(address
);
192 return GSOCK_NOERROR
;
195 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
197 assert(socket
!= NULL
);
199 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
200 socket
->m_error
= GSOCK_INVADDR
;
201 return GSOCK_INVADDR
;
205 GAddress_destroy(socket
->m_peer
);
207 socket
->m_peer
= GAddress_copy(address
);
209 return GSOCK_NOERROR
;
212 GAddress
*GSocket_GetLocal(GSocket
*socket
)
215 struct sockaddr addr
;
219 assert(socket
!= NULL
);
222 return GAddress_copy(socket
->m_local
);
224 if (socket
->m_fd
== -1) {
225 socket
->m_error
= GSOCK_INVSOCK
;
231 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
232 socket
->m_error
= GSOCK_IOERR
;
236 address
= GAddress_new();
237 if (address
== NULL
) {
238 socket
->m_error
= GSOCK_MEMERR
;
241 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
242 if (socket
->m_error
!= GSOCK_NOERROR
) {
243 GAddress_destroy(address
);
250 GAddress
*GSocket_GetPeer(GSocket
*socket
)
252 assert(socket
!= NULL
);
255 return GAddress_copy(socket
->m_peer
);
260 /* Server specific parts */
262 /* GSocket_SetServer:
263 * Sets up the socket as a server. It uses the "Local" field of GSocket.
264 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
265 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
266 * been initialized, GSOCK_INVADDR if the local address has not been
267 * defined and GSOCK_IOERR for other internal errors.
269 GSocketError
GSocket_SetServer(GSocket
*sck
)
276 if (sck
->m_fd
!= -1) {
277 sck
->m_error
= GSOCK_INVSOCK
;
278 return GSOCK_INVSOCK
;
282 sck
->m_error
= GSOCK_INVADDR
;
283 return GSOCK_INVADDR
;
286 /* We always have a stream here */
287 sck
->m_stream
= TRUE
;
288 sck
->m_server
= TRUE
;
290 /* Create the socket */
291 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
293 if (sck
->m_fd
== -1) {
294 sck
->m_error
= GSOCK_IOERR
;
298 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
299 _GSocket_Enable_Events(sck
);
301 /* Bind the socket to the LOCAL address */
302 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
305 sck
->m_error
= GSOCK_IOERR
;
309 /* Enable listening up to 5 connections */
310 if (listen(sck
->m_fd
, 5) < 0) {
313 sck
->m_error
= GSOCK_IOERR
;
317 return GSOCK_NOERROR
;
320 /* GSocket_WaitConnection:
321 * Waits for an incoming client connection.
323 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
328 assert(socket
!= NULL
);
330 /* Reenable CONNECTION events */
331 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
333 /* If the socket has already been created, we exit immediately */
334 if (socket
->m_fd
== -1 || !socket
->m_server
)
336 socket
->m_error
= GSOCK_INVSOCK
;
340 /* Create a GSocket object for the new connection */
341 connection
= GSocket_new();
344 connection
->m_error
= GSOCK_MEMERR
;
348 /* Accept the incoming connection */
349 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
351 GSocket_destroy(connection
);
352 /* socket->m_error set by _GSocket_Input_Timeout */
356 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
358 if (connection
->m_fd
== -1)
360 if (errno
== EWOULDBLOCK
)
361 socket
->m_error
= GSOCK_WOULDBLOCK
;
363 socket
->m_error
= GSOCK_IOERR
;
365 GSocket_destroy(connection
);
369 /* Initialize all fields */
370 connection
->m_server
= FALSE
;
371 connection
->m_stream
= TRUE
;
372 connection
->m_oriented
= TRUE
;
374 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
375 _GSocket_Enable_Events(connection
);
380 /* Non oriented connections */
382 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
388 if (sck
->m_fd
!= -1) {
389 sck
->m_error
= GSOCK_INVSOCK
;
390 return GSOCK_INVSOCK
;
394 sck
->m_error
= GSOCK_INVADDR
;
395 return GSOCK_INVADDR
;
398 sck
->m_stream
= FALSE
;
399 sck
->m_server
= FALSE
;
400 sck
->m_oriented
= FALSE
;
402 /* Create the socket */
403 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
406 sck
->m_error
= GSOCK_IOERR
;
410 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
411 _GSocket_Enable_Events(sck
);
413 /* Bind it to the LOCAL address */
414 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
417 sck
->m_error
= GSOCK_IOERR
;
421 return GSOCK_NOERROR
;
424 /* Client specific parts */
427 * Establishes a client connection to a server using the "Peer"
428 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
429 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
430 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
431 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
432 * the connection request can be completed later. Use GSocket_Select()
433 * to check or wait for a GSOCK_CONNECTION event.
435 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
442 /* Enable CONNECTION events (needed for nonblocking connections) */
443 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
447 sck
->m_error
= GSOCK_INVSOCK
;
448 return GSOCK_INVSOCK
;
453 sck
->m_error
= GSOCK_INVADDR
;
454 return GSOCK_INVADDR
;
457 /* Test whether we want the socket to be a stream (e.g. TCP) */
458 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
459 sck
->m_oriented
= TRUE
;
460 sck
->m_server
= FALSE
;
461 sck
->m_establishing
= FALSE
;
468 /* Create the socket */
469 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
471 if (sck
->m_fd
== -1) {
472 sck
->m_error
= GSOCK_IOERR
;
476 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
477 _GSocket_Enable_Events(sck
);
479 /* Connect it to the PEER address */
480 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
486 /* If connect failed with EINPROGRESS and the GSocket object
487 * is in blocking mode, we select() for the specified timeout
488 * checking for writability to see if the connection request
491 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
493 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
497 /* sck->m_error is set in _GSocket_Output_Timeout */
498 fprintf(stderr
, "Blocking connect timeouts\n");
499 return GSOCK_TIMEDOUT
;
503 fprintf(stderr
, "Blocking connect OK\n");
504 return GSOCK_NOERROR
;
508 /* If connect failed with EINPROGRESS and the GSocket object
509 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
510 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
511 * this way if the connection completes, a GSOCK_CONNECTION
512 * event will be generated, if enabled.
514 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
516 sck
->m_error
= GSOCK_WOULDBLOCK
;
517 sck
->m_establishing
= TRUE
;
518 fprintf(stderr
, "Nonblocking connect in progress\n");
520 return GSOCK_WOULDBLOCK
;
523 /* If connect failed with an error other than EINPROGRESS,
524 * then the call to GSocket_Connect has failed.
528 sck
->m_error
= GSOCK_IOERR
;
530 fprintf(stderr
, "Connect failed (generic err)\n");
534 fprintf(stderr
, "Connect OK\n");
535 return GSOCK_NOERROR
;
540 /* Like recv(), send(), ... */
541 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
545 assert(socket
!= NULL
);
547 /* Reenable INPUT events */
548 _GSocket_Enable(socket
, GSOCK_INPUT
);
550 if (socket
->m_fd
== -1 || socket
->m_server
)
552 socket
->m_error
= GSOCK_INVSOCK
;
556 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
559 if (socket
->m_stream
)
560 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
562 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
566 if (errno
== EWOULDBLOCK
)
567 socket
->m_error
= GSOCK_WOULDBLOCK
;
569 socket
->m_error
= GSOCK_IOERR
;
575 int GSocket_Write(GSocket
*socket
, const char *buffer
,
580 assert(socket
!= NULL
);
582 if (socket
->m_fd
== -1 || socket
->m_server
)
584 socket
->m_error
= GSOCK_INVSOCK
;
588 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
591 if (socket
->m_stream
)
592 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
594 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
598 if (errno
== EWOULDBLOCK
)
599 socket
->m_error
= GSOCK_WOULDBLOCK
;
601 socket
->m_error
= GSOCK_IOERR
;
603 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
604 * in MSW). Once the first OUTPUT event is received, users can assume
605 * that the socket is writable until a read operation fails. Only then
606 * will further OUTPUT events be posted.
608 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
615 * Polls the socket to determine its status. This function will
616 * check for the events specified in the 'flags' parameter, and
617 * it will return a mask indicating which operations can be
618 * performed. This function won't block, regardless of the
619 * mode (blocking|nonblocking) of the socket.
621 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
623 assert(socket
!= NULL
);
625 return (flags
& socket
->m_detected
);
630 /* GSocket_SetNonBlocking:
631 * Sets the socket to non-blocking mode. This is useful if
632 * we don't want to wait.
634 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
636 assert(socket
!= NULL
);
638 socket
->m_non_blocking
= non_block
;
641 /* GSocket_SetTimeout:
642 * Sets the timeout for blocking calls. Time is
643 * expressed in milliseconds.
645 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
647 assert(socket
!= NULL
);
649 socket
->m_timeout
= millisec
;
653 * Returns the last error occured for this socket.
655 GSocketError
GSocket_GetError(GSocket
*socket
)
657 assert(socket
!= NULL
);
659 return socket
->m_error
;
664 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
665 * and LOST). The callbacks are called in the following situations:
667 * INPUT: There is at least one byte in the input buffer
668 * OUTPUT: The system is sure that the next write call will not block
669 * CONNECTION: Two cases are possible:
670 * Client socket -> the connection is established
671 * Server socket -> a client requests a connection
672 * LOST: The connection is lost
674 * An event is generated only once and its state is reseted when the
675 * relative IO call is requested.
676 * For example: INPUT -> GSocket_Read()
677 * CONNECTION -> GSocket_Accept()
680 /* GSocket_SetCallback:
681 * Enables the callbacks specified by 'flags'. Note that 'flags'
682 * may be a combination of flags OR'ed toghether, so the same
683 * callback function can be made to accept different events.
684 * The callback function must have the following prototype:
686 * void function(GSocket *socket, GSocketEvent event, char *cdata)
688 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
689 GSocketCallback callback
, char *cdata
)
693 assert(socket
!= NULL
);
695 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
697 if ((flags
& (1 << count
)) != 0)
699 socket
->m_cbacks
[count
] = callback
;
700 socket
->m_data
[count
] = cdata
;
705 /* GSocket_UnsetCallback:
706 * Disables all callbacks specified by 'flags', which may be a
707 * combination of flags OR'ed toghether.
709 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
713 assert(socket
!= NULL
);
715 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
717 if ((flags
& (1 << count
)) != 0)
719 socket
->m_cbacks
[count
] = NULL
;
720 socket
->m_data
[count
] = NULL
;
725 #define CALL_CALLBACK(socket, event) { \
726 _GSocket_Disable(socket, event); \
727 if (socket->m_cbacks[event]) \
728 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
732 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
734 socket
->m_detected
&= ~(1 << event
);
735 _GSocket_Install_Callback(socket
, event
);
738 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
740 socket
->m_detected
|= (1 << event
);
741 _GSocket_Uninstall_Callback(socket
, event
);
744 /* _GSocket_Input_Timeout:
745 * For blocking sockets, wait until data is available or
746 * until timeout ellapses.
748 GSocketError
_GSocket_Input_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
, &readfds
);
760 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
762 socket
->m_error
= GSOCK_TIMEDOUT
;
763 return GSOCK_TIMEDOUT
;
766 return GSOCK_NOERROR
;
769 /* _GSocket_Output_Timeout:
770 * For blocking sockets, wait until data can be sent without
771 * blocking or until timeout ellapses.
773 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
778 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
779 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
781 if (!socket
->m_non_blocking
)
784 FD_SET(socket
->m_fd
, &writefds
);
785 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
787 socket
->m_error
= GSOCK_TIMEDOUT
;
788 return GSOCK_TIMEDOUT
;
791 return GSOCK_NOERROR
;
794 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
798 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
803 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
805 struct sockaddr from
;
810 fromlen
= sizeof(from
);
812 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
817 /* Translate a system address into a GSocket address */
820 socket
->m_peer
= GAddress_new();
823 socket
->m_error
= GSOCK_MEMERR
;
827 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
828 if (err
!= GSOCK_NOERROR
)
830 GAddress_destroy(socket
->m_peer
);
831 socket
->m_peer
= NULL
;
832 socket
->m_error
= err
;
839 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
844 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
849 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
851 struct sockaddr
*addr
;
855 if (!socket
->m_peer
) {
856 socket
->m_error
= GSOCK_INVADDR
;
860 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
861 if (err
!= GSOCK_NOERROR
) {
862 socket
->m_error
= err
;
866 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
868 /* Frees memory allocated from _GAddress_translate_to */
874 void _GSocket_Detected_Read(GSocket
*socket
)
879 if (socket
->m_stream
)
881 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
883 if (ret
< 0 && socket
->m_server
)
885 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
891 CALL_CALLBACK(socket
, GSOCK_INPUT
);
895 CALL_CALLBACK(socket
, GSOCK_LOST
);
900 void _GSocket_Detected_Write(GSocket
*socket
)
902 if (socket
->m_establishing
&& !socket
->m_server
)
906 socket
->m_establishing
= FALSE
;
909 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
913 CALL_CALLBACK(socket
, GSOCK_LOST
);
917 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
918 /* We have to fire this event by hand because CONNECTION (for clients)
919 * and OUTPUT are internally the same and we just disabled CONNECTION
920 * events with the above macro.
922 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
927 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
932 * -------------------------------------------------------------------------
934 * -------------------------------------------------------------------------
937 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
938 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
939 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
941 #define CHECK_ADDRESS(address, family, retval) \
943 if (address->m_family == GSOCK_NOFAMILY) \
944 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
945 return address->m_error; \
947 if (address->m_family != GSOCK_##family) {\
948 address->m_error = GSOCK_INVADDR; \
953 GAddress
*GAddress_new()
957 address
= (GAddress
*)malloc(sizeof(GAddress
));
962 address
->m_family
= GSOCK_NOFAMILY
;
963 address
->m_addr
= NULL
;
969 GAddress
*GAddress_copy(GAddress
*address
)
973 assert(address
!= NULL
);
975 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
980 memcpy(addr2
, address
, sizeof(GAddress
));
982 if (address
->m_addr
) {
983 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
984 if (addr2
->m_addr
== NULL
) {
988 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
994 void GAddress_destroy(GAddress
*address
)
996 assert(address
!= NULL
);
1001 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1003 assert(address
!= NULL
);
1005 address
->m_family
= type
;
1008 GAddressType
GAddress_GetFamily(GAddress
*address
)
1010 assert(address
!= NULL
);
1012 return address
->m_family
;
1015 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1016 address
->m_realfamily
= addr
->sa_family
;
1017 switch (addr
->sa_family
) {
1019 address
->m_family
= GSOCK_INET
;
1022 address
->m_family
= GSOCK_UNIX
;
1026 address
->m_family
= GSOCK_INET6
;
1031 address
->m_error
= GSOCK_INVOP
;
1036 if (address
->m_addr
)
1037 free(address
->m_addr
);
1039 address
->m_len
= len
;
1040 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1041 if (address
->m_addr
== NULL
) {
1042 address
->m_error
= GSOCK_MEMERR
;
1043 return GSOCK_MEMERR
;
1045 memcpy(address
->m_addr
, addr
, len
);
1047 return GSOCK_NOERROR
;
1050 GSocketError
_GAddress_translate_to(GAddress
*address
,
1051 struct sockaddr
**addr
, int *len
)
1053 if (!address
->m_addr
) {
1054 address
->m_error
= GSOCK_INVADDR
;
1055 return GSOCK_INVADDR
;
1058 *len
= address
->m_len
;
1059 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1060 if (*addr
== NULL
) {
1061 address
->m_error
= GSOCK_MEMERR
;
1062 return GSOCK_MEMERR
;
1065 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1066 return GSOCK_NOERROR
;
1070 * -------------------------------------------------------------------------
1071 * Internet address family
1072 * -------------------------------------------------------------------------
1075 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1077 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1078 if (address
->m_addr
== NULL
) {
1079 address
->m_error
= GSOCK_MEMERR
;
1080 return GSOCK_MEMERR
;
1083 address
->m_len
= sizeof(struct sockaddr_in
);
1085 address
->m_family
= GSOCK_INET
;
1086 address
->m_realfamily
= PF_INET
;
1087 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1088 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1090 return GSOCK_NOERROR
;
1093 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1096 struct in_addr
*addr
;
1098 assert(address
!= NULL
);
1100 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1102 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1104 /* If it is a numeric host name, convert it now */
1105 #if defined(HAVE_INET_ATON)
1106 if (inet_aton(hostname
, addr
) == 0) {
1107 #elif defined(HAVE_INET_ADDR)
1108 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1109 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1111 struct in_addr
*array_addr
;
1113 /* It is a real name, we solve it */
1114 he
= gethostbyname((char*)hostname
);
1116 address
->m_error
= GSOCK_NOHOST
;
1117 return GSOCK_NOHOST
;
1119 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1120 addr
->s_addr
= array_addr
[0].s_addr
;
1121 #if defined(HAVE_INET_ATON)
1123 #elif defined(HAVE_INET_ADDR)
1126 return GSOCK_NOERROR
;
1129 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1131 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1134 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1135 unsigned long hostaddr
)
1137 struct in_addr
*addr
;
1139 assert(address
!= NULL
);
1141 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1143 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1144 addr
->s_addr
= hostaddr
;
1146 return GSOCK_NOERROR
;
1149 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1150 const char *protocol
)
1153 struct sockaddr_in
*addr
;
1155 assert(address
!= NULL
);
1156 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1159 address
->m_error
= GSOCK_INVPORT
;
1160 return GSOCK_INVPORT
;
1163 se
= getservbyname((char*)port
, (char*)protocol
);
1165 if (isdigit(port
[0])) {
1168 port_int
= atoi(port
);
1169 addr
= (struct sockaddr_in
*)address
->m_addr
;
1170 addr
->sin_port
= htons(port_int
);
1171 return GSOCK_NOERROR
;
1174 address
->m_error
= GSOCK_INVPORT
;
1175 return GSOCK_INVPORT
;
1178 addr
= (struct sockaddr_in
*)address
->m_addr
;
1179 addr
->sin_port
= se
->s_port
;
1181 return GSOCK_NOERROR
;
1184 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1186 struct sockaddr_in
*addr
;
1188 assert(address
!= NULL
);
1189 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1191 addr
= (struct sockaddr_in
*)address
->m_addr
;
1192 addr
->sin_port
= htons(port
);
1194 return GSOCK_NOERROR
;
1197 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1201 struct sockaddr_in
*addr
;
1203 assert(address
!= NULL
);
1204 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1206 addr
= (struct sockaddr_in
*)address
->m_addr
;
1207 addr_buf
= (char *)&(addr
->sin_addr
);
1209 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1211 address
->m_error
= GSOCK_NOHOST
;
1212 return GSOCK_NOHOST
;
1215 strncpy(hostname
, he
->h_name
, sbuf
);
1217 return GSOCK_NOERROR
;
1220 unsigned long GAddress_INET_GetHostAddress(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
;
1229 return addr
->sin_addr
.s_addr
;
1232 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1234 struct sockaddr_in
*addr
;
1236 assert(address
!= NULL
);
1237 CHECK_ADDRESS(address
, INET
, 0);
1239 addr
= (struct sockaddr_in
*)address
->m_addr
;
1240 return ntohs(addr
->sin_port
);
1244 * -------------------------------------------------------------------------
1245 * Unix address family
1246 * -------------------------------------------------------------------------
1249 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1251 assert (address
!= NULL
);
1252 address
->m_error
= GSOCK_INVADDR
;
1253 return GSOCK_INVADDR
;
1256 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1258 assert (address
!= NULL
);
1259 address
->m_error
= GSOCK_INVADDR
;
1260 return GSOCK_INVADDR
;
1263 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1265 assert (address
!= NULL
);
1266 address
->m_error
= GSOCK_INVADDR
;
1267 return GSOCK_INVADDR
;