]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/gsocket.c
0ea14036b81d920ba78b201778abd4165fd95857
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>
23 #if defined(__VISAGECPP__) && __IBMCPP__ < 400
28 #include <sys\socket.h>
29 #include <sys\ioctl.h>
30 #include <sys\select.h>
31 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
32 int _System
bsdselect(int,
37 int _System
soclose(int);
48 #include "wx/gsocket.h"
49 #include "wx/os2/gsockos2.h"
55 # define SOCKLEN_T socklen_t
58 # define SOCKLEN_T int
63 /* Global initialisers */
70 void GSocket_Cleanup()
74 /* Constructors / Destructors */
76 GSocket
*GSocket_new()
81 socket
= (GSocket
*)malloc(sizeof(GSocket
));
87 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
89 socket
->m_cbacks
[i
] = NULL
;
91 socket
->m_detected
= 0;
92 socket
->m_local
= NULL
;
93 socket
->m_peer
= NULL
;
94 socket
->m_error
= GSOCK_NOERROR
;
95 socket
->m_server
= FALSE
;
96 socket
->m_stream
= TRUE
;
97 socket
->m_gui_dependent
= NULL
;
98 socket
->m_non_blocking
= FALSE
;
99 socket
->m_timeout
= 10*60*1000;
100 /* 10 minutes * 60 sec * 1000 millisec */
101 socket
->m_establishing
= FALSE
;
103 /* We initialize the GUI specific entries here */
104 _GSocket_GUI_Init(socket
);
109 void GSocket_destroy(GSocket
*socket
)
111 assert(socket
!= NULL
);
113 /* First, we check that the socket is really shutdowned */
114 if (socket
->m_fd
!= -1)
115 GSocket_Shutdown(socket
);
117 /* We destroy GUI specific variables */
118 _GSocket_GUI_Destroy(socket
);
120 /* We destroy private addresses */
122 GAddress_destroy(socket
->m_local
);
125 GAddress_destroy(socket
->m_peer
);
127 /* We destroy socket itself */
131 void GSocket_Shutdown(GSocket
*socket
)
135 assert(socket
!= NULL
);
137 /* If socket has been created, we shutdown it */
138 if (socket
->m_fd
!= -1)
140 shutdown(socket
->m_fd
, 2);
141 soclose(socket
->m_fd
);
145 /* We also disable GUI callbacks */
146 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
147 socket
->m_cbacks
[evt
] = NULL
;
149 socket
->m_detected
= 0;
150 _GSocket_Disable_Events(socket
);
153 /* Address handling */
155 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
)
157 assert(socket
!= NULL
);
159 if ((socket
->m_fd
!= -1 && !socket
->m_server
)) {
160 socket
->m_error
= GSOCK_INVSOCK
;
161 return GSOCK_INVSOCK
;
164 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
165 socket
->m_error
= GSOCK_INVADDR
;
166 return GSOCK_INVADDR
;
170 GAddress_destroy(socket
->m_local
);
172 socket
->m_local
= GAddress_copy(address
);
174 return GSOCK_NOERROR
;
177 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
)
179 assert(socket
!= NULL
);
181 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
) {
182 socket
->m_error
= GSOCK_INVADDR
;
183 return GSOCK_INVADDR
;
187 GAddress_destroy(socket
->m_peer
);
189 socket
->m_peer
= GAddress_copy(address
);
191 return GSOCK_NOERROR
;
194 GAddress
*GSocket_GetLocal(GSocket
*socket
)
197 struct sockaddr addr
;
201 assert(socket
!= NULL
);
204 return GAddress_copy(socket
->m_local
);
206 if (socket
->m_fd
== -1) {
207 socket
->m_error
= GSOCK_INVSOCK
;
213 if (getsockname(socket
->m_fd
, &addr
, &size
) < 0) {
214 socket
->m_error
= GSOCK_IOERR
;
218 address
= GAddress_new();
219 if (address
== NULL
) {
220 socket
->m_error
= GSOCK_MEMERR
;
223 socket
->m_error
= _GAddress_translate_from(address
, &addr
, size
);
224 if (socket
->m_error
!= GSOCK_NOERROR
) {
225 GAddress_destroy(address
);
232 GAddress
*GSocket_GetPeer(GSocket
*socket
)
234 assert(socket
!= NULL
);
237 return GAddress_copy(socket
->m_peer
);
242 /* Server specific parts */
244 /* GSocket_SetServer:
245 * Sets up the socket as a server. It uses the "Local" field of GSocket.
246 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
247 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
248 * been initialized, GSOCK_INVADDR if the local address has not been
249 * defined and GSOCK_IOERR for other internal errors.
251 GSocketError
GSocket_SetServer(GSocket
*sck
)
258 if (sck
->m_fd
!= -1) {
259 sck
->m_error
= GSOCK_INVSOCK
;
260 return GSOCK_INVSOCK
;
264 sck
->m_error
= GSOCK_INVADDR
;
265 return GSOCK_INVADDR
;
268 /* We always have a stream here */
269 sck
->m_stream
= TRUE
;
270 sck
->m_server
= TRUE
;
272 /* Create the socket */
273 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_STREAM
, 0);
275 if (sck
->m_fd
== -1) {
276 sck
->m_error
= GSOCK_IOERR
;
280 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
281 _GSocket_Enable_Events(sck
);
283 /* Bind the socket to the LOCAL address */
284 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
287 sck
->m_error
= GSOCK_IOERR
;
291 /* Enable listening up to 5 connections */
292 if (listen(sck
->m_fd
, 5) < 0) {
295 sck
->m_error
= GSOCK_IOERR
;
299 return GSOCK_NOERROR
;
302 /* GSocket_WaitConnection:
303 * Waits for an incoming client connection.
305 GSocket
*GSocket_WaitConnection(GSocket
*socket
)
310 assert(socket
!= NULL
);
312 /* Reenable CONNECTION events */
313 _GSocket_Enable(socket
, GSOCK_CONNECTION
);
315 /* If the socket has already been created, we exit immediately */
316 if (socket
->m_fd
== -1 || !socket
->m_server
)
318 socket
->m_error
= GSOCK_INVSOCK
;
322 /* Create a GSocket object for the new connection */
323 connection
= GSocket_new();
326 connection
->m_error
= GSOCK_MEMERR
;
330 /* Accept the incoming connection */
331 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
333 GSocket_destroy(connection
);
334 /* socket->m_error set by _GSocket_Input_Timeout */
338 connection
->m_fd
= accept(socket
->m_fd
, NULL
, NULL
);
340 if (connection
->m_fd
== -1)
342 if (errno
== EWOULDBLOCK
)
343 socket
->m_error
= GSOCK_WOULDBLOCK
;
345 socket
->m_error
= GSOCK_IOERR
;
347 GSocket_destroy(connection
);
351 /* Initialize all fields */
352 connection
->m_server
= FALSE
;
353 connection
->m_stream
= TRUE
;
354 connection
->m_oriented
= TRUE
;
356 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
357 _GSocket_Enable_Events(connection
);
362 /* Non oriented connections */
364 GSocketError
GSocket_SetNonOriented(GSocket
*sck
)
370 if (sck
->m_fd
!= -1) {
371 sck
->m_error
= GSOCK_INVSOCK
;
372 return GSOCK_INVSOCK
;
376 sck
->m_error
= GSOCK_INVADDR
;
377 return GSOCK_INVADDR
;
380 sck
->m_stream
= FALSE
;
381 sck
->m_server
= FALSE
;
382 sck
->m_oriented
= FALSE
;
384 /* Create the socket */
385 sck
->m_fd
= socket(sck
->m_local
->m_realfamily
, SOCK_DGRAM
, 0);
388 sck
->m_error
= GSOCK_IOERR
;
392 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
393 _GSocket_Enable_Events(sck
);
395 /* Bind it to the LOCAL address */
396 if (bind(sck
->m_fd
, sck
->m_local
->m_addr
, sck
->m_local
->m_len
) < 0) {
399 sck
->m_error
= GSOCK_IOERR
;
403 return GSOCK_NOERROR
;
406 /* Client specific parts */
409 * Establishes a client connection to a server using the "Peer"
410 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
411 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
412 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
413 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
414 * the connection request can be completed later. Use GSocket_Select()
415 * to check or wait for a GSOCK_CONNECTION event.
417 GSocketError
GSocket_Connect(GSocket
*sck
, GSocketStream stream
)
424 /* Enable CONNECTION events (needed for nonblocking connections) */
425 _GSocket_Enable(sck
, GSOCK_CONNECTION
);
429 sck
->m_error
= GSOCK_INVSOCK
;
430 return GSOCK_INVSOCK
;
435 sck
->m_error
= GSOCK_INVADDR
;
436 return GSOCK_INVADDR
;
439 /* Test whether we want the socket to be a stream (e.g. TCP) */
440 sck
->m_stream
= (stream
== GSOCK_STREAMED
);
441 sck
->m_oriented
= TRUE
;
442 sck
->m_server
= FALSE
;
443 sck
->m_establishing
= FALSE
;
450 /* Create the socket */
451 sck
->m_fd
= socket(sck
->m_peer
->m_realfamily
, type
, 0);
453 if (sck
->m_fd
== -1) {
454 sck
->m_error
= GSOCK_IOERR
;
458 ioctl(sck
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(int));
459 _GSocket_Enable_Events(sck
);
461 /* Connect it to the PEER address */
462 ret
= connect(sck
->m_fd
, sck
->m_peer
->m_addr
, sck
->m_peer
->m_len
);
468 /* If connect failed with EINPROGRESS and the GSocket object
469 * is in blocking mode, we select() for the specified timeout
470 * checking for writability to see if the connection request
473 if ((err
== EINPROGRESS
) && (!sck
->m_non_blocking
))
475 if (_GSocket_Output_Timeout(sck
) == GSOCK_TIMEDOUT
)
479 /* sck->m_error is set in _GSocket_Output_Timeout */
480 fprintf(stderr
, "Blocking connect timeouts\n");
481 return GSOCK_TIMEDOUT
;
485 fprintf(stderr
, "Blocking connect OK\n");
486 return GSOCK_NOERROR
;
490 /* If connect failed with EINPROGRESS and the GSocket object
491 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
492 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
493 * this way if the connection completes, a GSOCK_CONNECTION
494 * event will be generated, if enabled.
496 if ((err
== EINPROGRESS
) && (sck
->m_non_blocking
))
498 sck
->m_error
= GSOCK_WOULDBLOCK
;
499 sck
->m_establishing
= TRUE
;
500 fprintf(stderr
, "Nonblocking connect in progress\n");
502 return GSOCK_WOULDBLOCK
;
505 /* If connect failed with an error other than EINPROGRESS,
506 * then the call to GSocket_Connect has failed.
510 sck
->m_error
= GSOCK_IOERR
;
512 fprintf(stderr
, "Connect failed (generic err)\n");
516 fprintf(stderr
, "Connect OK\n");
517 return GSOCK_NOERROR
;
522 /* Like recv(), send(), ... */
523 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
)
527 assert(socket
!= NULL
);
529 /* Reenable INPUT events */
530 _GSocket_Enable(socket
, GSOCK_INPUT
);
532 if (socket
->m_fd
== -1 || socket
->m_server
)
534 socket
->m_error
= GSOCK_INVSOCK
;
538 if (_GSocket_Input_Timeout(socket
) == GSOCK_TIMEDOUT
)
541 if (socket
->m_stream
)
542 ret
= _GSocket_Recv_Stream(socket
, buffer
, size
);
544 ret
= _GSocket_Recv_Dgram(socket
, buffer
, size
);
548 if (errno
== EWOULDBLOCK
)
549 socket
->m_error
= GSOCK_WOULDBLOCK
;
551 socket
->m_error
= GSOCK_IOERR
;
557 int GSocket_Write(GSocket
*socket
, const char *buffer
,
562 assert(socket
!= NULL
);
564 if (socket
->m_fd
== -1 || socket
->m_server
)
566 socket
->m_error
= GSOCK_INVSOCK
;
570 if (_GSocket_Output_Timeout(socket
) == GSOCK_TIMEDOUT
)
573 if (socket
->m_stream
)
574 ret
= _GSocket_Send_Stream(socket
, buffer
, size
);
576 ret
= _GSocket_Send_Dgram(socket
, buffer
, size
);
580 if (errno
== EWOULDBLOCK
)
581 socket
->m_error
= GSOCK_WOULDBLOCK
;
583 socket
->m_error
= GSOCK_IOERR
;
585 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
586 * in MSW). Once the first OUTPUT event is received, users can assume
587 * that the socket is writable until a read operation fails. Only then
588 * will further OUTPUT events be posted.
590 _GSocket_Enable(socket
, GSOCK_OUTPUT
);
597 * Polls the socket to determine its status. This function will
598 * check for the events specified in the 'flags' parameter, and
599 * it will return a mask indicating which operations can be
600 * performed. This function won't block, regardless of the
601 * mode (blocking|nonblocking) of the socket.
603 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
)
605 assert(socket
!= NULL
);
607 return (flags
& socket
->m_detected
);
612 /* GSocket_SetNonBlocking:
613 * Sets the socket to non-blocking mode. This is useful if
614 * we don't want to wait.
616 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
)
618 assert(socket
!= NULL
);
620 socket
->m_non_blocking
= non_block
;
623 /* GSocket_SetTimeout:
624 * Sets the timeout for blocking calls. Time is
625 * expressed in milliseconds.
627 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
)
629 assert(socket
!= NULL
);
631 socket
->m_timeout
= millisec
;
635 * Returns the last error occured for this socket.
637 GSocketError
GSocket_GetError(GSocket
*socket
)
639 assert(socket
!= NULL
);
641 return socket
->m_error
;
646 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
647 * and LOST). The callbacks are called in the following situations:
649 * INPUT: There is at least one byte in the input buffer
650 * OUTPUT: The system is sure that the next write call will not block
651 * CONNECTION: Two cases are possible:
652 * Client socket -> the connection is established
653 * Server socket -> a client requests a connection
654 * LOST: The connection is lost
656 * An event is generated only once and its state is reseted when the
657 * relative IO call is requested.
658 * For example: INPUT -> GSocket_Read()
659 * CONNECTION -> GSocket_Accept()
662 /* GSocket_SetCallback:
663 * Enables the callbacks specified by 'flags'. Note that 'flags'
664 * may be a combination of flags OR'ed toghether, so the same
665 * callback function can be made to accept different events.
666 * The callback function must have the following prototype:
668 * void function(GSocket *socket, GSocketEvent event, char *cdata)
670 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
671 GSocketCallback callback
, char *cdata
)
675 assert(socket
!= NULL
);
677 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
679 if ((flags
& (1 << count
)) != 0)
681 socket
->m_cbacks
[count
] = callback
;
682 socket
->m_data
[count
] = cdata
;
687 /* GSocket_UnsetCallback:
688 * Disables all callbacks specified by 'flags', which may be a
689 * combination of flags OR'ed toghether.
691 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
)
695 assert(socket
!= NULL
);
697 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
699 if ((flags
& (1 << count
)) != 0)
701 socket
->m_cbacks
[count
] = NULL
;
702 socket
->m_data
[count
] = NULL
;
707 #define CALL_CALLBACK(socket, event) { \
708 _GSocket_Disable(socket, event); \
709 if (socket->m_cbacks[event]) \
710 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
714 void _GSocket_Enable(GSocket
*socket
, GSocketEvent event
)
716 socket
->m_detected
&= ~(1 << event
);
717 _GSocket_Install_Callback(socket
, event
);
720 void _GSocket_Disable(GSocket
*socket
, GSocketEvent event
)
722 socket
->m_detected
|= (1 << event
);
723 _GSocket_Uninstall_Callback(socket
, event
);
726 /* _GSocket_Input_Timeout:
727 * For blocking sockets, wait until data is available or
728 * until timeout ellapses.
730 GSocketError
_GSocket_Input_Timeout(GSocket
*socket
)
735 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
736 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
738 if (!socket
->m_non_blocking
)
741 FD_SET(socket
->m_fd
, &readfds
);
742 if (select(socket
->m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) == 0)
744 socket
->m_error
= GSOCK_TIMEDOUT
;
745 return GSOCK_TIMEDOUT
;
748 return GSOCK_NOERROR
;
751 /* _GSocket_Output_Timeout:
752 * For blocking sockets, wait until data can be sent without
753 * blocking or until timeout ellapses.
755 GSocketError
_GSocket_Output_Timeout(GSocket
*socket
)
760 tv
.tv_sec
= (socket
->m_timeout
/ 1000);
761 tv
.tv_usec
= (socket
->m_timeout
% 1000) * 1000;
763 if (!socket
->m_non_blocking
)
766 FD_SET(socket
->m_fd
, &writefds
);
767 if (select(socket
->m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
) == 0)
769 socket
->m_error
= GSOCK_TIMEDOUT
;
770 return GSOCK_TIMEDOUT
;
773 return GSOCK_NOERROR
;
776 int _GSocket_Recv_Stream(GSocket
*socket
, char *buffer
, int size
)
780 ret
= recv(socket
->m_fd
, buffer
, size
, 0);
785 int _GSocket_Recv_Dgram(GSocket
*socket
, char *buffer
, int size
)
787 struct sockaddr from
;
792 fromlen
= sizeof(from
);
794 ret
= recvfrom(socket
->m_fd
, buffer
, size
, 0, &from
, &fromlen
);
799 /* Translate a system address into a GSocket address */
802 socket
->m_peer
= GAddress_new();
805 socket
->m_error
= GSOCK_MEMERR
;
809 err
= _GAddress_translate_from(socket
->m_peer
, &from
, fromlen
);
810 if (err
!= GSOCK_NOERROR
)
812 GAddress_destroy(socket
->m_peer
);
813 socket
->m_peer
= NULL
;
814 socket
->m_error
= err
;
821 int _GSocket_Send_Stream(GSocket
*socket
, const char *buffer
, int size
)
826 ret
= send(socket
->m_fd
, (char*)buffer
, size
, 0);
831 int _GSocket_Send_Dgram(GSocket
*socket
, const char *buffer
, int size
)
833 struct sockaddr
*addr
;
837 if (!socket
->m_peer
) {
838 socket
->m_error
= GSOCK_INVADDR
;
842 err
= _GAddress_translate_to(socket
->m_peer
, &addr
, &len
);
843 if (err
!= GSOCK_NOERROR
) {
844 socket
->m_error
= err
;
848 ret
= sendto(socket
->m_fd
, (char*)buffer
, size
, 0, addr
, len
);
850 /* Frees memory allocated from _GAddress_translate_to */
856 void _GSocket_Detected_Read(GSocket
*socket
)
861 if (socket
->m_stream
)
863 ret
= recv(socket
->m_fd
, &c
, 1, MSG_PEEK
);
865 if (ret
< 0 && socket
->m_server
)
867 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
873 CALL_CALLBACK(socket
, GSOCK_INPUT
);
877 CALL_CALLBACK(socket
, GSOCK_LOST
);
882 void _GSocket_Detected_Write(GSocket
*socket
)
884 if (socket
->m_establishing
&& !socket
->m_server
)
888 socket
->m_establishing
= FALSE
;
891 getsockopt(socket
->m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
895 CALL_CALLBACK(socket
, GSOCK_LOST
);
899 CALL_CALLBACK(socket
, GSOCK_CONNECTION
);
900 /* We have to fire this event by hand because CONNECTION (for clients)
901 * and OUTPUT are internally the same and we just disabled CONNECTION
902 * events with the above macro.
904 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
909 CALL_CALLBACK(socket
, GSOCK_OUTPUT
);
914 * -------------------------------------------------------------------------
916 * -------------------------------------------------------------------------
919 /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY or
920 * GSOCK_*family*. In case it is GSOCK_NOFAMILY, it initializes address to be
921 * a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR.
923 #define CHECK_ADDRESS(address, family, retval) \
925 if (address->m_family == GSOCK_NOFAMILY) \
926 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) {\
927 return address->m_error; \
929 if (address->m_family != GSOCK_##family) {\
930 address->m_error = GSOCK_INVADDR; \
935 GAddress
*GAddress_new()
939 address
= (GAddress
*)malloc(sizeof(GAddress
));
944 address
->m_family
= GSOCK_NOFAMILY
;
945 address
->m_addr
= NULL
;
951 GAddress
*GAddress_copy(GAddress
*address
)
955 assert(address
!= NULL
);
957 addr2
= (GAddress
*)malloc(sizeof(GAddress
));
962 memcpy(addr2
, address
, sizeof(GAddress
));
964 if (address
->m_addr
) {
965 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
966 if (addr2
->m_addr
== NULL
) {
970 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
976 void GAddress_destroy(GAddress
*address
)
978 assert(address
!= NULL
);
983 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
985 assert(address
!= NULL
);
987 address
->m_family
= type
;
990 GAddressType
GAddress_GetFamily(GAddress
*address
)
992 assert(address
!= NULL
);
994 return address
->m_family
;
997 GSocketError
_GAddress_translate_from(GAddress
*address
, struct sockaddr
*addr
, int len
){
998 address
->m_realfamily
= addr
->sa_family
;
999 switch (addr
->sa_family
) {
1001 address
->m_family
= GSOCK_INET
;
1004 address
->m_family
= GSOCK_UNIX
;
1008 address
->m_family
= GSOCK_INET6
;
1013 address
->m_error
= GSOCK_INVOP
;
1018 if (address
->m_addr
)
1019 free(address
->m_addr
);
1021 address
->m_len
= len
;
1022 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1023 if (address
->m_addr
== NULL
) {
1024 address
->m_error
= GSOCK_MEMERR
;
1025 return GSOCK_MEMERR
;
1027 memcpy(address
->m_addr
, addr
, len
);
1029 return GSOCK_NOERROR
;
1032 GSocketError
_GAddress_translate_to(GAddress
*address
,
1033 struct sockaddr
**addr
, int *len
)
1035 if (!address
->m_addr
) {
1036 address
->m_error
= GSOCK_INVADDR
;
1037 return GSOCK_INVADDR
;
1040 *len
= address
->m_len
;
1041 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1042 if (*addr
== NULL
) {
1043 address
->m_error
= GSOCK_MEMERR
;
1044 return GSOCK_MEMERR
;
1047 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1048 return GSOCK_NOERROR
;
1052 * -------------------------------------------------------------------------
1053 * Internet address family
1054 * -------------------------------------------------------------------------
1057 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1059 address
->m_addr
= (struct sockaddr
*)malloc(sizeof(struct sockaddr_in
));
1060 if (address
->m_addr
== NULL
) {
1061 address
->m_error
= GSOCK_MEMERR
;
1062 return GSOCK_MEMERR
;
1065 address
->m_len
= sizeof(struct sockaddr_in
);
1067 address
->m_family
= GSOCK_INET
;
1068 address
->m_realfamily
= PF_INET
;
1069 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1070 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1072 return GSOCK_NOERROR
;
1075 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1078 struct in_addr
*addr
;
1080 assert(address
!= NULL
);
1082 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1084 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1086 /* If it is a numeric host name, convert it now */
1087 #if defined(HAVE_INET_ATON)
1088 if (inet_aton(hostname
, addr
) == 0) {
1089 #elif defined(HAVE_INET_ADDR)
1090 /* Fix from Guillermo Rodriguez Garcia <guille@iies.es> */
1091 if ( (addr
->s_addr
= inet_addr(hostname
)) == -1 ) {
1093 struct in_addr
*array_addr
;
1095 /* It is a real name, we solve it */
1096 he
= gethostbyname((char*)hostname
);
1098 address
->m_error
= GSOCK_NOHOST
;
1099 return GSOCK_NOHOST
;
1101 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1102 addr
->s_addr
= array_addr
[0].s_addr
;
1103 #if defined(HAVE_INET_ATON)
1105 #elif defined(HAVE_INET_ADDR)
1108 return GSOCK_NOERROR
;
1111 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1112 unsigned long hostaddr
)
1114 struct in_addr
*addr
;
1116 assert(address
!= NULL
);
1118 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1120 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1121 addr
->s_addr
= hostaddr
;
1123 return GSOCK_NOERROR
;
1126 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1127 const char *protocol
)
1130 struct sockaddr_in
*addr
;
1132 assert(address
!= NULL
);
1133 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1136 address
->m_error
= GSOCK_INVPORT
;
1137 return GSOCK_INVPORT
;
1140 se
= getservbyname((char*)port
, (char*)protocol
);
1142 if (isdigit(port
[0])) {
1145 port_int
= atoi(port
);
1146 addr
= (struct sockaddr_in
*)address
->m_addr
;
1147 addr
->sin_port
= htons(port_int
);
1148 return GSOCK_NOERROR
;
1151 address
->m_error
= GSOCK_INVPORT
;
1152 return GSOCK_INVPORT
;
1155 addr
= (struct sockaddr_in
*)address
->m_addr
;
1156 addr
->sin_port
= se
->s_port
;
1158 return GSOCK_NOERROR
;
1161 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1163 struct sockaddr_in
*addr
;
1165 assert(address
!= NULL
);
1166 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1168 addr
= (struct sockaddr_in
*)address
->m_addr
;
1169 addr
->sin_port
= htons(port
);
1171 return GSOCK_NOERROR
;
1174 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1178 struct sockaddr_in
*addr
;
1180 assert(address
!= NULL
);
1181 CHECK_ADDRESS(address
, INET
, GSOCK_INVADDR
);
1183 addr
= (struct sockaddr_in
*)address
->m_addr
;
1184 addr_buf
= (char *)&(addr
->sin_addr
);
1186 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1188 address
->m_error
= GSOCK_NOHOST
;
1189 return GSOCK_NOHOST
;
1192 strncpy(hostname
, he
->h_name
, sbuf
);
1194 return GSOCK_NOERROR
;
1197 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1199 struct sockaddr_in
*addr
;
1201 assert(address
!= NULL
);
1202 CHECK_ADDRESS(address
, INET
, 0);
1204 addr
= (struct sockaddr_in
*)address
->m_addr
;
1206 return addr
->sin_addr
.s_addr
;
1209 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1211 struct sockaddr_in
*addr
;
1213 assert(address
!= NULL
);
1214 CHECK_ADDRESS(address
, INET
, 0);
1216 addr
= (struct sockaddr_in
*)address
->m_addr
;
1217 return ntohs(addr
->sin_port
);