]>
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
;
124 void GSocket_destroy(GSocket
*socket
)
126 assert(socket
!= NULL
);
128 /* First, we check that the socket is really shutdowned */
129 if (socket
->m_fd
!= -1)
130 GSocket_Shutdown(socket
);
132 /* We destroy private addresses */
134 GAddress_destroy(socket
->m_local
);
137 GAddress_destroy(socket
->m_peer
);
139 /* We destroy socket itself */
143 void GSocket_Shutdown(GSocket
*socket
)
147 assert(socket
!= NULL
);
149 /* If socket has been created, we shutdown it */
150 if (socket
->m_fd
!= -1)
152 shutdown(socket
->m_fd
, 2);
153 soclose(socket
->m_fd
);
157 /* We also disable GUI callbacks */
158 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
159 socket
->m_cbacks
[evt
] = NULL
;
161 socket
->m_detected
= 0;
165 /* Address handling */
167 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
169 assert(socket
!= NULL
);
171 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
172 socket
->m_error
= GSOCK_INVSOCK
;
173 return GSOCK_INVSOCK
;
176 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
177 socket
->m_error
= GSOCK_INVADDR
;
178 return GSOCK_INVADDR
;
182 GAddress_destroy(socket
->m_local
);
184 socket
->m_local
= GAddress_copy(address
);
186 return GSOCK_NOERROR
;
189 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
191 assert(socket
!= NULL
);
193 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
194 socket
->m_error
= GSOCK_INVADDR
;
195 return GSOCK_INVADDR
;
199 GAddress_destroy(socket
->m_peer
);
201 socket
->m_peer
= GAddress_copy(address
);
203 return GSOCK_NOERROR
;
206 GAddress
*GSocket_GetLocal(GSocket
*socket
)
209 struct sockaddr addr
;
213 assert(socket
!= NULL
);
216 return GAddress_copy(socket
->m_local
);
218 if (socket
->m_fd
== -1) {
219 socket
->m_error
= GSOCK_INVSOCK
;
225 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
226 socket
->m_error
= GSOCK_IOERR
;
230 address
= GAddress_new();
231 if (address
== NULL
) {
232 socket
->m_error
= GSOCK_MEMERR
;
235 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
236 if (socket
->m_error
!= GSOCK_NOERROR
) {
237 GAddress_destroy(address
);
244 GAddress
*GSocket_GetPeer(GSocket
*socket
)
246 assert(socket
!= NULL
);
249 return GAddress_copy(socket
->m_peer
);
254 /* Server specific parts */
256 /* GSocket_SetServer:
257 * Sets up the socket as a server. It uses the "Local" field of GSocket.
258 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
259 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
260 * been initialized, GSOCK_INVADDR if the local address has not been
261 * defined and GSOCK_IOERR for other internal errors.
263 GSocketError
GSocket_SetServer(GSocket
*sck
)
270 if (sck
->m_fd
!= -1) {
271 sck
->m_error
= GSOCK_INVSOCK
;
272 return GSOCK_INVSOCK
;
276 sck
->m_error
= GSOCK_INVADDR
;
277 return GSOCK_INVADDR
;
280 /* We always have a stream here */
281 sck
->m_stream
= TRUE
;
282 sck
->m_server
= TRUE
;
284 /* Create the socket */
285 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
287 if (sck
->m_fd
== -1) {
288 sck
->m_error
= GSOCK_IOERR
;
292 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
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));
371 /* Non oriented connections */
373 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
379 if (sck
->m_fd
!= -1) {
380 sck
->m_error
= GSOCK_INVSOCK
;
381 return GSOCK_INVSOCK
;
385 sck
->m_error
= GSOCK_INVADDR
;
386 return GSOCK_INVADDR
;
389 sck
->m_stream
= FALSE
;
390 sck
->m_server
= FALSE
;
391 sck
->m_oriented
= FALSE
;
393 /* Create the socket */
394 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
397 sck
->m_error
= GSOCK_IOERR
;
401 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
403 /* Bind it to the LOCAL address */
404 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
407 sck
->m_error
= GSOCK_IOERR
;
411 return GSOCK_NOERROR
;
414 /* Client specific parts */
417 * Establishes a client connection to a server using the "Peer"
418 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
419 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
420 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
421 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
422 * the connection request can be completed later. Use GSocket_Select()
423 * to check or wait for a GSOCK_CONNECTION event.
425 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
432 /* Enable CONNECTION events (needed for nonblocking connections) */
433 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
437 sck
->m_error
= GSOCK_INVSOCK
;
438 return GSOCK_INVSOCK
;
443 sck
->m_error
= GSOCK_INVADDR
;
444 return GSOCK_INVADDR
;
447 /* Test whether we want the socket to be a stream (e.g. TCP) */
448 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
449 sck
->m_oriented
= TRUE
;
450 sck
->m_server
= FALSE
;
451 sck
->m_establishing
= FALSE
;
458 /* Create the socket */
459 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
461 if (sck
->m_fd
== -1) {
462 sck
->m_error
= GSOCK_IOERR
;
466 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
468 /* Connect it to the PEER address */
469 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
475 /* If connect failed with EINPROGRESS and the GSocket object
476 * is in blocking mode, we select() for the specified timeout
477 * checking for writability to see if the connection request
480 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
482 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
486 /* sck->m_error is set in _GSocket_Output_Timeout */
487 fprintf(stderr
, "Blocking connect timeouts\n");
488 return GSOCK_TIMEDOUT
;
492 fprintf(stderr
, "Blocking connect OK\n");
493 return GSOCK_NOERROR
;
497 /* If connect failed with EINPROGRESS and the GSocket object
498 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
499 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
500 * this way if the connection completes, a GSOCK_CONNECTION
501 * event will be generated, if enabled.
503 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
505 sck
->m_error
= GSOCK_WOULDBLOCK
;
506 sck
->m_establishing
= TRUE
;
507 fprintf(stderr
, "Nonblocking connect in progress\n");
509 return GSOCK_WOULDBLOCK
;
512 /* If connect failed with an error other than EINPROGRESS,
513 * then the call to GSocket_Connect has failed.
517 sck
->m_error
= GSOCK_IOERR
;
519 fprintf(stderr
, "Connect failed (generic err)\n");
523 fprintf(stderr
, "Connect OK\n");
524 return GSOCK_NOERROR
;
529 /* Like recv(), send(), ... */
530 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
534 assert(socket
!= NULL
);
536 /* Reenable INPUT events */
537 _GSocket_Enable(socket
, GSOCK_INPUT
);
539 if (socket
->m_fd
== -1 || socket
->m_server
)
541 socket
->m_error
= GSOCK_INVSOCK
;
545 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
548 if (socket
->m_stream
)
549 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
551 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
555 if (errno
== EWOULDBLOCK
)
556 socket
->m_error
= GSOCK_WOULDBLOCK
;
558 socket
->m_error
= GSOCK_IOERR
;
564 int GSocket_Write(GSocket
*socket
, const char *buffer
,
569 assert(socket
!= NULL
);
571 if (socket
->m_fd
== -1 || socket
->m_server
)
573 socket
->m_error
= GSOCK_INVSOCK
;
577 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
580 if (socket
->m_stream
)
581 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
583 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
587 if (errno
== EWOULDBLOCK
)
588 socket
->m_error
= GSOCK_WOULDBLOCK
;
590 socket
->m_error
= GSOCK_IOERR
;
592 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
593 * in MSW). Once the first OUTPUT event is received, users can assume
594 * that the socket is writable until a read operation fails. Only then
595 * will further OUTPUT events be posted.
597 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
604 * Polls the socket to determine its status. This function will
605 * check for the events specified in the 'flags' parameter, and
606 * it will return a mask indicating which operations can be
607 * performed. This function won't block, regardless of the
608 * mode (blocking|nonblocking) of the socket.
610 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
612 assert(socket
!= NULL
);
614 return (flags
& socket
->m_detected
);
619 /* GSocket_SetNonBlocking:
620 * Sets the socket to non-blocking mode. This is useful if
621 * we don't want to wait.
623 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
625 assert(socket
!= NULL
);
627 socket
->m_non_blocking
= non_block
;
630 /* GSocket_SetTimeout:
631 * Sets the timeout for blocking calls. Time is
632 * expressed in milliseconds.
634 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
636 assert(socket
!= NULL
);
638 socket
->m_timeout
= millisec
;
642 * Returns the last error occured for this socket.
644 GSocketError
GSocket_GetError(GSocket
*socket
)
646 assert(socket
!= NULL
);
648 return socket
->m_error
;
653 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
654 * and LOST). The callbacks are called in the following situations:
656 * INPUT: There is at least one byte in the input buffer
657 * OUTPUT: The system is sure that the next write call will not block
658 * CONNECTION: Two cases are possible:
659 * Client socket -> the connection is established
660 * Server socket -> a client requests a connection
661 * LOST: The connection is lost
663 * An event is generated only once and its state is reseted when the
664 * relative IO call is requested.
665 * For example: INPUT -> GSocket_Read()
666 * CONNECTION -> GSocket_Accept()
669 /* GSocket_SetCallback:
670 * Enables the callbacks specified by 'flags'. Note that 'flags'
671 * may be a combination of flags OR'ed toghether, so the same
672 * callback function can be made to accept different events.
673 * The callback function must have the following prototype:
675 * void function(GSocket *socket, GSocketEvent event, char *cdata)
677 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
678 GSocketCallback callback
, char *cdata
)
682 assert(socket
!= NULL
);
684 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
686 if ((flags
& (1 << count
)) != 0)
688 socket
->m_cbacks
[count
] = callback
;
689 socket
->m_data
[count
] = cdata
;
694 /* GSocket_UnsetCallback:
695 * Disables all callbacks specified by 'flags', which may be a
696 * combination of flags OR'ed toghether.
698 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
702 assert(socket
!= NULL
);
704 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
706 if ((flags
& (1 << count
)) != 0)
708 socket
->m_cbacks
[count
] = NULL
;
709 socket
->m_data
[count
] = NULL
;
714 #define CALL_CALLBACK(socket, event) { \
715 _GSocket_Disable(socket, event); \
716 if (socket->m_cbacks[event]) \
717 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
721 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
723 socket
->m_detected
&= ~(1 << event
);
727 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
729 socket
->m_detected
|= (1 << event
);
732 /* _GSocket_Input_Timeout:
733 * For blocking sockets, wait until data is available or
734 * until timeout ellapses.
736 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
741 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
742 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
744 if (!socket
->m_non_blocking
)
747 FD_SET(socket
->m_fd
, &readfds
);
748 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
750 socket
->m_error
= GSOCK_TIMEDOUT
;
751 return GSOCK_TIMEDOUT
;
754 return GSOCK_NOERROR
;
757 /* _GSocket_Output_Timeout:
758 * For blocking sockets, wait until data can be sent without
759 * blocking or until timeout ellapses.
761 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
766 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
767 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
769 if (!socket
->m_non_blocking
)
772 FD_SET(socket
->m_fd
, &writefds
);
773 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
775 socket
->m_error
= GSOCK_TIMEDOUT
;
776 return GSOCK_TIMEDOUT
;
779 return GSOCK_NOERROR
;
782 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
786 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
791 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
793 struct sockaddr from
;
798 fromlen
= sizeof(from
);
800 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
805 /* Translate a system address into a GSocket address */
808 socket
->m_peer
= GAddress_new();
811 socket
->m_error
= GSOCK_MEMERR
;
815 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
816 if (err
!= GSOCK_NOERROR
)
818 GAddress_destroy(socket
->m_peer
);
819 socket
->m_peer
= NULL
;
820 socket
->m_error
= err
;
827 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
832 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
837 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
839 struct sockaddr
*addr
;
843 if (!socket
->m_peer
) {
844 socket
->m_error
= GSOCK_INVADDR
;
848 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
849 if (err
!= GSOCK_NOERROR
) {
850 socket
->m_error
= err
;
854 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
856 /* Frees memory allocated from _GAddress_translate_to */
862 void _GSocket_Detected_Read(GSocket
*socket
)
867 if (socket
->m_stream
)
869 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
871 if (ret
< 0 && socket
->m_server
)
873 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
879 CALL_CALLBACK(socket
, GSOCK_INPUT
);
883 CALL_CALLBACK(socket
, GSOCK_LOST
);
888 void _GSocket_Detected_Write(GSocket
*socket
)
890 if (socket
->m_establishing
&& !socket
->m_server
)
894 socket
->m_establishing
= FALSE
;
897 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
901 CALL_CALLBACK(socket
, GSOCK_LOST
);
905 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
906 /* We have to fire this event by hand because CONNECTION (for clients)
907 * and OUTPUT are internally the same and we just disabled CONNECTION
908 * events with the above macro.
910 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
915 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
920 * -------------------------------------------------------------------------
922 * -------------------------------------------------------------------------
925 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
926 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
927 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
929 #define CHECK_ADDRESS(address, family, retval) \
931 if (address->m_family == GSOCK_NOFAMILY) \
932 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
933 return address->m_error; \
935 if (address->m_family != GSOCK_##family) {\
936 address->m_error = GSOCK_INVADDR; \
941 GAddress
*GAddress_new()
945 address
= (GAddress
*)malloc(sizeof(GAddress
));
950 address
->m_family
= GSOCK_NOFAMILY
;
951 address
->m_addr
= NULL
;
957 GAddress
*GAddress_copy(GAddress
*address
)
961 assert(address
!= NULL
);
963 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
968 memcpy(addr2
, address
, sizeof(GAddress
));
970 if (address
->m_addr
) {
971 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
972 if (addr2
->m_addr
== NULL
) {
976 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
982 void GAddress_destroy(GAddress
*address
)
984 assert(address
!= NULL
);
989 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
991 assert(address
!= NULL
);
993 address
->m_family
= type
;
996 GAddressType
GAddress_GetFamily(GAddress
*address
)
998 assert(address
!= NULL
);
1000 return address
->m_family
;
1003 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
1004 address
->m_realfamily
= addr
->sa_family
;
1005 switch (addr
->sa_family
) {
1007 address
->m_family
= GSOCK_INET
;
1010 address
->m_family
= GSOCK_UNIX
;
1014 address
->m_family
= GSOCK_INET6
;
1019 address
->m_error
= GSOCK_INVOP
;
1024 if (address
->m_addr
)
1025 free(address
->m_addr
);
1027 address
->m_len
= len
;
1028 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1029 if (address
->m_addr
== NULL
) {
1030 address
->m_error
= GSOCK_MEMERR
;
1031 return GSOCK_MEMERR
;
1033 memcpy(address
->m_addr
, addr
, len
);
1035 return GSOCK_NOERROR
;
1038 GSocketError
_GAddress_translate_to(GAddress
*address
,
1039 struct sockaddr
**addr
, int *len
)
1041 if (!address
->m_addr
) {
1042 address
->m_error
= GSOCK_INVADDR
;
1043 return GSOCK_INVADDR
;
1046 *len
= address
->m_len
;
1047 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1048 if (*addr
== NULL
) {
1049 address
->m_error
= GSOCK_MEMERR
;
1050 return GSOCK_MEMERR
;
1053 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1054 return GSOCK_NOERROR
;
1058 * -------------------------------------------------------------------------
1059 * Internet address family
1060 * -------------------------------------------------------------------------
1063 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1065 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1066 if (address
->m_addr
== NULL
) {
1067 address
->m_error
= GSOCK_MEMERR
;
1068 return GSOCK_MEMERR
;
1071 address
->m_len
= sizeof(struct sockaddr_in
);
1073 address
->m_family
= GSOCK_INET
;
1074 address
->m_realfamily
= PF_INET
;
1075 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1076 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1078 return GSOCK_NOERROR
;
1081 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1084 struct in_addr
*addr
;
1086 assert(address
!= NULL
);
1088 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1090 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1092 /* If it is a numeric host name, convert it now */
1093 #if defined(HAVE_INET_ATON)
1094 if (inet_aton(hostname
, addr
) == 0) {
1095 #elif defined(HAVE_INET_ADDR)
1096 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1097 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1099 struct in_addr
*array_addr
;
1101 /* It is a real name, we solve it */
1102 he
= gethostbyname((char*)hostname
);
1104 address
->m_error
= GSOCK_NOHOST
;
1105 return GSOCK_NOHOST
;
1107 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1108 addr
->s_addr
= array_addr
[0].s_addr
;
1109 #if defined(HAVE_INET_ATON)
1111 #elif defined(HAVE_INET_ADDR)
1114 return GSOCK_NOERROR
;
1117 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1119 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
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
);
1232 * -------------------------------------------------------------------------
1233 * Unix address family
1234 * -------------------------------------------------------------------------
1237 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1239 assert (address
!= NULL
);
1240 address
->m_error
= GSOCK_INVADDR
;
1241 return GSOCK_INVADDR
;
1244 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1246 assert (address
!= NULL
);
1247 address
->m_error
= GSOCK_INVADDR
;
1248 return GSOCK_INVADDR
;
1251 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1253 assert (address
!= NULL
);
1254 address
->m_error
= GSOCK_INVADDR
;
1255 return GSOCK_INVADDR
;