1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Authors: David Elliott (C++ conversion, maintainer)
6 * Guillermo Rodriguez Garcia <guille@iies.es>
7 * Purpose: GSocket main Unix and OS/2 file
8 * Licence: The wxWindows licence
10 * -------------------------------------------------------------------------
14 * PLEASE don't put C++ comments here - this is a C source file.
17 #ifndef __GSOCKET_STANDALONE__
21 #if defined(__VISAGECPP__)
22 /* Seems to be needed by Visual Age C++, though I don't see how it manages
23 to not break on including a C++ header into a plain C source file */
25 #define BSD_SELECT /* use Berkley Sockets select */
28 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
31 #include <sys/types.h>
36 #include <netinet/in.h>
39 #include <sys/ioctl.h>
45 u_char sun_len
; /* sockaddr len including null */
46 u_char sun_family
; /* AF_UNIX */
47 char sun_path
[108]; /* path name (gag) */
50 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
64 #include <machine/endian.h>
70 #define EBADF SOCEBADF
76 #include <sys/socket.h>
77 #include <sys/ioctl.h>
78 #include <sys/select.h>
80 #define close(a) soclose(a)
81 #define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
82 int _System
bsdselect(int,
87 int _System
soclose(int);
91 #include <sys/select.h>
99 # include <sys/filio.h>
102 # include <bstring.h>
105 # include <strings.h>
112 # define SOCKLEN_T unsigned int
116 # define SOCKLEN_T socklen_t
119 # define SOCKLEN_T int
123 #endif /* SOCKLEN_T */
126 #define SOCKOPTLEN_T SOCKLEN_T
130 * MSW defines this, Unices don't.
132 #ifndef INVALID_SOCKET
133 #define INVALID_SOCKET -1
136 /* UnixWare reportedly needs this for FIONBIO definition */
138 #include <sys/filio.h>
142 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
143 * on all systems. INADDR_BROADCAST should be fine to indicate an error.
146 #define INADDR_NONE INADDR_BROADCAST
149 #define MASK_SIGNAL() \
151 void (*old_handler)(int); \
153 old_handler = signal(SIGPIPE, SIG_IGN);
155 #define UNMASK_SIGNAL() \
156 signal(SIGPIPE, old_handler); \
159 /* If a SIGPIPE is issued by a socket call on a remotely closed socket,
160 the program will "crash" unless it explicitly handles the SIGPIPE.
161 By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
162 use SO_NOSIGPIPE (if available), the BSD equivalent. */
164 # define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
165 #else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
166 # define GSOCKET_MSG_NOSIGNAL 0
167 #endif /* MSG_NOSIGNAL */
169 #ifndef __GSOCKET_STANDALONE__
170 # include "wx/unix/gsockunx.h"
171 # include "wx/gsocket.h"
173 # include "gsockunx.h"
174 # include "gsocket.h"
175 #endif /* __GSOCKET_STANDALONE__ */
177 /* debugging helpers */
178 #ifdef __GSOCKET_DEBUG__
179 # define GSocket_Debug(args) printf args
181 # define GSocket_Debug(args)
182 #endif /* __GSOCKET_DEBUG__ */
184 /* Table of GUI-related functions. We must call them indirectly because
185 * of wxBase and GUI separation: */
187 static GSocketGUIFunctionsTable
*gs_gui_functions
;
189 class GSocketGUIFunctionsTableNull
: public GSocketGUIFunctionsTable
192 virtual bool OnInit();
193 virtual void OnExit();
194 virtual bool CanUseEventLoop();
195 virtual bool Init_Socket(GSocket
*socket
);
196 virtual void Destroy_Socket(GSocket
*socket
);
197 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
198 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
199 virtual void Enable_Events(GSocket
*socket
);
200 virtual void Disable_Events(GSocket
*socket
);
203 bool GSocketGUIFunctionsTableNull::OnInit()
205 void GSocketGUIFunctionsTableNull::OnExit()
207 bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
209 bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket
*socket
)
211 void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket
*socket
)
213 void GSocketGUIFunctionsTableNull::Install_Callback(GSocket
*socket
, GSocketEvent event
)
215 void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
217 void GSocketGUIFunctionsTableNull::Enable_Events(GSocket
*socket
)
219 void GSocketGUIFunctionsTableNull::Disable_Events(GSocket
*socket
)
221 /* Global initialisers */
223 void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable
*guifunc
)
225 gs_gui_functions
= guifunc
;
228 int GSocket_Init(void)
230 if (!gs_gui_functions
)
232 static GSocketGUIFunctionsTableNull table
;
233 gs_gui_functions
= &table
;
235 if ( !gs_gui_functions
->OnInit() )
240 void GSocket_Cleanup(void)
242 if (gs_gui_functions
)
244 gs_gui_functions
->OnExit();
248 /* Constructors / Destructors for GSocket */
254 m_fd
= INVALID_SOCKET
;
255 for (i
=0;i
<GSOCK_MAX_EVENT
;i
++)
262 m_error
= GSOCK_NOERROR
;
265 m_gui_dependent
= NULL
;
266 m_non_blocking
= false;
268 m_timeout
= 10*60*1000;
269 /* 10 minutes * 60 sec * 1000 millisec */
270 m_establishing
= false;
272 assert(gs_gui_functions
);
273 /* Per-socket GUI-specific initialization */
274 m_ok
= gs_gui_functions
->Init_Socket(this);
277 void GSocket::Close()
279 gs_gui_functions
->Disable_Events(this);
280 /* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
281 #if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
284 m_fd
= INVALID_SOCKET
;
291 /* Check that the socket is really shutdowned */
292 if (m_fd
!= INVALID_SOCKET
)
295 /* Per-socket GUI-specific cleanup */
296 gs_gui_functions
->Destroy_Socket(this);
298 /* Destroy private addresses */
300 GAddress_destroy(m_local
);
303 GAddress_destroy(m_peer
);
307 * Disallow further read/write operations on this socket, close
308 * the fd and disable all callbacks.
310 void GSocket::Shutdown()
316 /* If socket has been created, shutdown it */
317 if (m_fd
!= INVALID_SOCKET
)
323 /* Disable GUI callbacks */
324 for (evt
= 0; evt
< GSOCK_MAX_EVENT
; evt
++)
325 m_cbacks
[evt
] = NULL
;
327 m_detected
= GSOCK_LOST_FLAG
;
330 /* Address handling */
336 * Set or get the local or peer address for this socket. The 'set'
337 * functions return GSOCK_NOERROR on success, an error code otherwise.
338 * The 'get' functions return a pointer to a GAddress object on success,
339 * or NULL otherwise, in which case they set the error code of the
340 * corresponding GSocket.
343 * GSOCK_INVSOCK - the socket is not valid.
344 * GSOCK_INVADDR - the address is not valid.
346 GSocketError
GSocket::SetLocal(GAddress
*address
)
350 /* the socket must be initialized, or it must be a server */
351 if ((m_fd
!= INVALID_SOCKET
&& !m_server
))
353 m_error
= GSOCK_INVSOCK
;
354 return GSOCK_INVSOCK
;
358 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
360 m_error
= GSOCK_INVADDR
;
361 return GSOCK_INVADDR
;
365 GAddress_destroy(m_local
);
367 m_local
= GAddress_copy(address
);
369 return GSOCK_NOERROR
;
372 GSocketError
GSocket::SetPeer(GAddress
*address
)
377 if (address
== NULL
|| address
->m_family
== GSOCK_NOFAMILY
)
379 m_error
= GSOCK_INVADDR
;
380 return GSOCK_INVADDR
;
384 GAddress_destroy(m_peer
);
386 m_peer
= GAddress_copy(address
);
388 return GSOCK_NOERROR
;
391 GAddress
*GSocket::GetLocal()
394 struct sockaddr addr
;
395 SOCKLEN_T size
= sizeof(addr
);
400 /* try to get it from the m_local var first */
402 return GAddress_copy(m_local
);
404 /* else, if the socket is initialized, try getsockname */
405 if (m_fd
== INVALID_SOCKET
)
407 m_error
= GSOCK_INVSOCK
;
411 if (getsockname(m_fd
, &addr
, (SOCKLEN_T
*) &size
) < 0)
413 m_error
= GSOCK_IOERR
;
417 /* got a valid address from getsockname, create a GAddress object */
418 address
= GAddress_new();
421 m_error
= GSOCK_MEMERR
;
425 err
= _GAddress_translate_from(address
, &addr
, size
);
426 if (err
!= GSOCK_NOERROR
)
428 GAddress_destroy(address
);
436 GAddress
*GSocket::GetPeer()
440 /* try to get it from the m_peer var */
442 return GAddress_copy(m_peer
);
447 /* Server specific parts */
449 /* GSocket_SetServer:
450 * Sets up this socket as a server. The local address must have been
451 * set with GSocket_SetLocal() before GSocket_SetServer() is called.
452 * Returns GSOCK_NOERROR on success, one of the following otherwise:
455 * GSOCK_INVSOCK - the socket is in use.
456 * GSOCK_INVADDR - the local address has not been set.
457 * GSOCK_IOERR - low-level error.
459 GSocketError
GSocket::SetServer()
465 /* must not be in use */
466 if (m_fd
!= INVALID_SOCKET
)
468 m_error
= GSOCK_INVSOCK
;
469 return GSOCK_INVSOCK
;
472 /* the local addr must have been set */
475 m_error
= GSOCK_INVADDR
;
476 return GSOCK_INVADDR
;
479 /* Initialize all fields */
483 /* Create the socket */
484 m_fd
= socket(m_local
->m_realfamily
, SOCK_STREAM
, 0);
486 if (m_fd
== INVALID_SOCKET
)
488 m_error
= GSOCK_IOERR
;
492 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
494 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
497 ioctl(m_fd
, FIONBIO
, &arg
);
498 gs_gui_functions
->Enable_Events(this);
500 /* allow a socket to re-bind if the socket is in the TIME_WAIT
501 state after being previously closed.
504 setsockopt(m_fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char*)&arg
, sizeof(u_long
));
506 /* Bind to the local address,
507 * retrieve the actual address bound,
508 * and listen up to 5 connections.
510 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
513 (SOCKLEN_T
*) &m_local
->m_len
) != 0) ||
514 (listen(m_fd
, 5) != 0))
517 m_error
= GSOCK_IOERR
;
521 return GSOCK_NOERROR
;
524 /* GSocket_WaitConnection:
525 * Waits for an incoming client connection. Returns a pointer to
526 * a GSocket object, or NULL if there was an error, in which case
527 * the last error field will be updated for the calling GSocket.
529 * Error codes (set in the calling GSocket)
530 * GSOCK_INVSOCK - the socket is not valid or not a server.
531 * GSOCK_TIMEDOUT - timeout, no incoming connections.
532 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
533 * GSOCK_MEMERR - couldn't allocate memory.
534 * GSOCK_IOERR - low-level error.
536 GSocket
*GSocket::WaitConnection()
538 struct sockaddr from
;
539 SOCKLEN_T fromlen
= sizeof(from
);
546 /* If the socket has already been created, we exit immediately */
547 if (m_fd
== INVALID_SOCKET
|| !m_server
)
549 m_error
= GSOCK_INVSOCK
;
553 /* Create a GSocket object for the new connection */
554 connection
= GSocket_new();
558 m_error
= GSOCK_MEMERR
;
562 /* Wait for a connection (with timeout) */
563 if (Input_Timeout() == GSOCK_TIMEDOUT
)
566 /* m_error set by _GSocket_Input_Timeout */
570 connection
->m_fd
= accept(m_fd
, &from
, (SOCKLEN_T
*) &fromlen
);
572 /* Reenable CONNECTION events */
573 Enable(GSOCK_CONNECTION
);
575 if (connection
->m_fd
== INVALID_SOCKET
)
577 if (errno
== EWOULDBLOCK
)
578 m_error
= GSOCK_WOULDBLOCK
;
580 m_error
= GSOCK_IOERR
;
586 /* Initialize all fields */
587 connection
->m_server
= false;
588 connection
->m_stream
= true;
590 /* Setup the peer address field */
591 connection
->m_peer
= GAddress_new();
592 if (!connection
->m_peer
)
595 m_error
= GSOCK_MEMERR
;
598 err
= _GAddress_translate_from(connection
->m_peer
, &from
, fromlen
);
599 if (err
!= GSOCK_NOERROR
)
605 #if defined(__EMX__) || defined(__VISAGECPP__)
606 ioctl(connection
->m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
608 ioctl(connection
->m_fd
, FIONBIO
, &arg
);
610 gs_gui_functions
->Enable_Events(connection
);
615 bool GSocket::SetReusable()
617 /* socket must not be null, and must not be in use/already bound */
618 if (this && m_fd
== INVALID_SOCKET
) {
625 /* Client specific parts */
628 * For stream (connection oriented) sockets, GSocket_Connect() tries
629 * to establish a client connection to a server using the peer address
630 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
631 * connection has been succesfully established, or one of the error
632 * codes listed below. Note that for nonblocking sockets, a return
633 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
634 * request can be completed later; you should use GSocket_Select()
635 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
636 * corresponding asynchronous events.
638 * For datagram (non connection oriented) sockets, GSocket_Connect()
639 * just sets the peer address established with GSocket_SetPeer() as
640 * default destination.
643 * GSOCK_INVSOCK - the socket is in use or not valid.
644 * GSOCK_INVADDR - the peer address has not been established.
645 * GSOCK_TIMEDOUT - timeout, the connection failed.
646 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
647 * GSOCK_MEMERR - couldn't allocate memory.
648 * GSOCK_IOERR - low-level error.
650 GSocketError
GSocket::Connect(GSocketStream stream
)
657 /* Enable CONNECTION events (needed for nonblocking connections) */
658 Enable(GSOCK_CONNECTION
);
660 if (m_fd
!= INVALID_SOCKET
)
662 m_error
= GSOCK_INVSOCK
;
663 return GSOCK_INVSOCK
;
668 m_error
= GSOCK_INVADDR
;
669 return GSOCK_INVADDR
;
672 /* Streamed or dgram socket? */
673 m_stream
= (stream
== GSOCK_STREAMED
);
675 m_establishing
= false;
677 /* Create the socket */
678 m_fd
= socket(m_peer
->m_realfamily
,
679 m_stream
? SOCK_STREAM
: SOCK_DGRAM
, 0);
681 if (m_fd
== INVALID_SOCKET
)
683 m_error
= GSOCK_IOERR
;
687 /* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
689 setsockopt(m_fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&arg
, sizeof(u_long
));
692 #if defined(__EMX__) || defined(__VISAGECPP__)
693 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
695 ioctl(m_fd
, FIONBIO
, &arg
);
698 /* Connect it to the peer address, with a timeout (see below) */
699 ret
= connect(m_fd
, m_peer
->m_addr
, m_peer
->m_len
);
701 /* We only call Enable_Events if we know e aren't shutting down the socket */
705 gs_gui_functions
->Enable_Events(this);
712 /* If connect failed with EINPROGRESS and the GSocket object
713 * is in blocking mode, we select() for the specified timeout
714 * checking for writability to see if the connection request
717 if ((err
== EINPROGRESS
) && (!m_non_blocking
))
719 if (Output_Timeout() == GSOCK_TIMEDOUT
)
722 /* m_error is set in _GSocket_Output_Timeout */
723 return GSOCK_TIMEDOUT
;
728 SOCKOPTLEN_T len
= sizeof(error
);
730 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*) &error
, &len
);
732 gs_gui_functions
->Enable_Events(this);
735 return GSOCK_NOERROR
;
739 /* If connect failed with EINPROGRESS and the GSocket object
740 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
741 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
742 * this way if the connection completes, a GSOCK_CONNECTION
743 * event will be generated, if enabled.
745 if ((err
== EINPROGRESS
) && (m_non_blocking
))
747 m_establishing
= true;
748 m_error
= GSOCK_WOULDBLOCK
;
749 return GSOCK_WOULDBLOCK
;
752 /* If connect failed with an error other than EINPROGRESS,
753 * then the call to GSocket_Connect has failed.
756 m_error
= GSOCK_IOERR
;
760 return GSOCK_NOERROR
;
763 /* Datagram sockets */
765 /* GSocket_SetNonOriented:
766 * Sets up this socket as a non-connection oriented (datagram) socket.
767 * Before using this function, the local address must have been set
768 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
769 * on success, or one of the following otherwise.
772 * GSOCK_INVSOCK - the socket is in use.
773 * GSOCK_INVADDR - the local address has not been set.
774 * GSOCK_IOERR - low-level error.
776 GSocketError
GSocket::SetNonOriented()
782 if (m_fd
!= INVALID_SOCKET
)
784 m_error
= GSOCK_INVSOCK
;
785 return GSOCK_INVSOCK
;
790 m_error
= GSOCK_INVADDR
;
791 return GSOCK_INVADDR
;
794 /* Initialize all fields */
798 /* Create the socket */
799 m_fd
= socket(m_local
->m_realfamily
, SOCK_DGRAM
, 0);
801 if (m_fd
== INVALID_SOCKET
)
803 m_error
= GSOCK_IOERR
;
806 #if defined(__EMX__) || defined(__VISAGECPP__)
807 ioctl(m_fd
, FIONBIO
, (char*)&arg
, sizeof(arg
));
809 ioctl(m_fd
, FIONBIO
, &arg
);
811 gs_gui_functions
->Enable_Events(this);
813 /* Bind to the local address,
814 * and retrieve the actual address bound.
816 if ((bind(m_fd
, m_local
->m_addr
, m_local
->m_len
) != 0) ||
819 (SOCKLEN_T
*) &m_local
->m_len
) != 0))
822 m_error
= GSOCK_IOERR
;
826 return GSOCK_NOERROR
;
831 /* Like recv(), send(), ... */
832 int GSocket::Read(char *buffer
, int size
)
838 if (m_fd
== INVALID_SOCKET
|| m_server
)
840 m_error
= GSOCK_INVSOCK
;
844 /* Disable events during query of socket status */
845 Disable(GSOCK_INPUT
);
847 /* If the socket is blocking, wait for data (with a timeout) */
848 if (Input_Timeout() == GSOCK_TIMEDOUT
)
849 /* We no longer return here immediately, otherwise socket events would not be re-enabled! */
854 ret
= Recv_Stream(buffer
, size
);
856 ret
= Recv_Dgram(buffer
, size
);
861 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
862 m_error
= GSOCK_WOULDBLOCK
;
864 m_error
= GSOCK_IOERR
;
867 /* Enable events again now that we are done processing */
873 int GSocket::Write(const char *buffer
, int size
)
879 GSocket_Debug(( "GSocket_Write #1, size %d\n", size
));
881 if (m_fd
== INVALID_SOCKET
|| m_server
)
883 m_error
= GSOCK_INVSOCK
;
887 GSocket_Debug(( "GSocket_Write #2, size %d\n", size
));
889 /* If the socket is blocking, wait for writability (with a timeout) */
890 if (Output_Timeout() == GSOCK_TIMEDOUT
)
893 GSocket_Debug(( "GSocket_Write #3, size %d\n", size
));
897 ret
= Send_Stream(buffer
, size
);
899 ret
= Send_Dgram(buffer
, size
);
901 GSocket_Debug(( "GSocket_Write #4, size %d\n", size
));
905 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
))
907 m_error
= GSOCK_WOULDBLOCK
;
908 GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
912 m_error
= GSOCK_IOERR
;
913 GSocket_Debug(( "GSocket_Write error IOERR\n" ));
916 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
917 * in MSW). Once the first OUTPUT event is received, users can assume
918 * that the socket is writable until a read operation fails. Only then
919 * will further OUTPUT events be posted.
921 Enable(GSOCK_OUTPUT
);
925 GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size
, ret
));
931 * Polls the socket to determine its status. This function will
932 * check for the events specified in the 'flags' parameter, and
933 * it will return a mask indicating which operations can be
934 * performed. This function won't block, regardless of the
935 * mode (blocking | nonblocking) of the socket.
937 GSocketEventFlags
GSocket::Select(GSocketEventFlags flags
)
939 if (!gs_gui_functions
->CanUseEventLoop())
942 GSocketEventFlags result
= 0;
950 /* Do not use a static struct, Linux can garble it */
951 tv
.tv_sec
= m_timeout
/ 1000;
952 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
957 FD_SET(m_fd
, &readfds
);
958 if (flags
& GSOCK_OUTPUT_FLAG
|| flags
& GSOCK_CONNECTION_FLAG
)
959 FD_SET(m_fd
, &writefds
);
960 FD_SET(m_fd
, &exceptfds
);
962 /* Check 'sticky' CONNECTION flag first */
963 result
|= (GSOCK_CONNECTION_FLAG
& m_detected
);
965 /* If we have already detected a LOST event, then don't try
966 * to do any further processing.
968 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
970 m_establishing
= false;
972 return (GSOCK_LOST_FLAG
& flags
);
976 if (select(m_fd
+ 1, &readfds
, &writefds
, &exceptfds
, &tv
) <= 0)
978 /* What to do here? */
979 return (result
& flags
);
982 /* Check for readability */
983 if (FD_ISSET(m_fd
, &readfds
))
987 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
991 result
|= GSOCK_INPUT_FLAG
;
995 if (m_server
&& m_stream
)
997 result
|= GSOCK_CONNECTION_FLAG
;
998 m_detected
|= GSOCK_CONNECTION_FLAG
;
1000 else if ((errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
) && (errno
!= EINTR
))
1002 m_detected
= GSOCK_LOST_FLAG
;
1003 m_establishing
= false;
1005 /* LOST event: Abort any further processing */
1006 return (GSOCK_LOST_FLAG
& flags
);
1011 /* Check for writability */
1012 if (FD_ISSET(m_fd
, &writefds
))
1014 if (m_establishing
&& !m_server
)
1017 SOCKOPTLEN_T len
= sizeof(error
);
1019 m_establishing
= false;
1021 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1025 m_detected
= GSOCK_LOST_FLAG
;
1027 /* LOST event: Abort any further processing */
1028 return (GSOCK_LOST_FLAG
& flags
);
1032 result
|= GSOCK_CONNECTION_FLAG
;
1033 m_detected
|= GSOCK_CONNECTION_FLAG
;
1038 result
|= GSOCK_OUTPUT_FLAG
;
1042 /* Check for exceptions and errors (is this useful in Unices?) */
1043 if (FD_ISSET(m_fd
, &exceptfds
))
1045 m_establishing
= false;
1046 m_detected
= GSOCK_LOST_FLAG
;
1048 /* LOST event: Abort any further processing */
1049 return (GSOCK_LOST_FLAG
& flags
);
1052 return (result
& flags
);
1059 return flags
& m_detected
;
1066 /* GSocket_SetNonBlocking:
1067 * Sets the socket to non-blocking mode. All IO calls will return
1070 void GSocket::SetNonBlocking(bool non_block
)
1074 GSocket_Debug( ("GSocket_SetNonBlocking: %d\n", (int)non_block
) );
1076 m_non_blocking
= non_block
;
1079 /* GSocket_SetTimeout:
1080 * Sets the timeout for blocking calls. Time is expressed in
1083 void GSocket::SetTimeout(unsigned long millisec
)
1087 m_timeout
= millisec
;
1090 /* GSocket_GetError:
1091 * Returns the last error occured for this socket. Note that successful
1092 * operations do not clear this back to GSOCK_NOERROR, so use it only
1095 GSocketError WXDLLIMPEXP_NET
GSocket::GetError()
1105 * There is data to be read in the input buffer. If, after a read
1106 * operation, there is still data available, the callback function will
1109 * The socket is available for writing. That is, the next write call
1110 * won't block. This event is generated only once, when the connection is
1111 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
1112 * when the output buffer empties again. This means that the app should
1113 * assume that it can write since the first OUTPUT event, and no more
1114 * OUTPUT events will be generated unless an error occurs.
1116 * Connection succesfully established, for client sockets, or incoming
1117 * client connection, for server sockets. Wait for this event (also watch
1118 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1120 * The connection is lost (or a connection request failed); this could
1121 * be due to a failure, or due to the peer closing it gracefully.
1124 /* GSocket_SetCallback:
1125 * Enables the callbacks specified by 'flags'. Note that 'flags'
1126 * may be a combination of flags OR'ed toghether, so the same
1127 * callback function can be made to accept different events.
1128 * The callback function must have the following prototype:
1130 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1132 void GSocket::SetCallback(GSocketEventFlags flags
,
1133 GSocketCallback callback
, char *cdata
)
1139 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1141 if ((flags
& (1 << count
)) != 0)
1143 m_cbacks
[count
] = callback
;
1144 m_data
[count
] = cdata
;
1149 /* GSocket_UnsetCallback:
1150 * Disables all callbacks specified by 'flags', which may be a
1151 * combination of flags OR'ed toghether.
1153 void GSocket::UnsetCallback(GSocketEventFlags flags
)
1159 for (count
= 0; count
< GSOCK_MAX_EVENT
; count
++)
1161 if ((flags
& (1 << count
)) != 0)
1163 m_cbacks
[count
] = NULL
;
1164 m_data
[count
] = NULL
;
1169 GSocketError
GSocket::GetSockOpt(int level
, int optname
,
1170 void *optval
, int *optlen
)
1172 if (getsockopt(m_fd
, level
, optname
, (char*)optval
, (SOCKOPTLEN_T
*)optlen
) == 0)
1174 return GSOCK_NOERROR
;
1176 return GSOCK_OPTERR
;
1179 GSocketError
GSocket::SetSockOpt(int level
, int optname
,
1180 const void *optval
, int optlen
)
1182 if (setsockopt(m_fd
, level
, optname
, (const char*)optval
, optlen
) == 0)
1184 return GSOCK_NOERROR
;
1186 return GSOCK_OPTERR
;
1189 #define CALL_CALLBACK(socket, event) { \
1190 socket->Disable(event); \
1191 if (socket->m_cbacks[event]) \
1192 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1196 void GSocket::Enable(GSocketEvent event
)
1198 m_detected
&= ~(1 << event
);
1199 gs_gui_functions
->Install_Callback(this, event
);
1202 void GSocket::Disable(GSocketEvent event
)
1204 m_detected
|= (1 << event
);
1205 gs_gui_functions
->Uninstall_Callback(this, event
);
1208 /* _GSocket_Input_Timeout:
1209 * For blocking sockets, wait until data is available or
1210 * until timeout ellapses.
1212 GSocketError
GSocket::Input_Timeout()
1218 /* Linux select() will overwrite the struct on return */
1219 tv
.tv_sec
= (m_timeout
/ 1000);
1220 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1222 if (!m_non_blocking
)
1225 FD_SET(m_fd
, &readfds
);
1226 ret
= select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
1229 GSocket_Debug(( "GSocket_Input_Timeout, select returned 0\n" ));
1230 m_error
= GSOCK_TIMEDOUT
;
1231 return GSOCK_TIMEDOUT
;
1235 GSocket_Debug(( "GSocket_Input_Timeout, select returned -1\n" ));
1236 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1237 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1238 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1239 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1240 m_error
= GSOCK_TIMEDOUT
;
1241 return GSOCK_TIMEDOUT
;
1244 return GSOCK_NOERROR
;
1247 /* _GSocket_Output_Timeout:
1248 * For blocking sockets, wait until data can be sent without
1249 * blocking or until timeout ellapses.
1251 GSocketError
GSocket::Output_Timeout()
1257 /* Linux select() will overwrite the struct on return */
1258 tv
.tv_sec
= (m_timeout
/ 1000);
1259 tv
.tv_usec
= (m_timeout
% 1000) * 1000;
1261 GSocket_Debug( ("m_non_blocking has: %d\n", (int)m_non_blocking
) );
1263 if (!m_non_blocking
)
1266 FD_SET(m_fd
, &writefds
);
1267 ret
= select(m_fd
+ 1, NULL
, &writefds
, NULL
, &tv
);
1270 GSocket_Debug(( "GSocket_Output_Timeout, select returned 0\n" ));
1271 m_error
= GSOCK_TIMEDOUT
;
1272 return GSOCK_TIMEDOUT
;
1276 GSocket_Debug(( "GSocket_Output_Timeout, select returned -1\n" ));
1277 if (errno
== EBADF
) { GSocket_Debug(( "Invalid file descriptor\n" )); }
1278 if (errno
== EINTR
) { GSocket_Debug(( "A non blocked signal was caught\n" )); }
1279 if (errno
== EINVAL
) { GSocket_Debug(( "The highest number descriptor is negative\n" )); }
1280 if (errno
== ENOMEM
) { GSocket_Debug(( "Not enough memory\n" )); }
1281 m_error
= GSOCK_TIMEDOUT
;
1282 return GSOCK_TIMEDOUT
;
1284 if ( ! FD_ISSET(m_fd
, &writefds
) ) {
1285 GSocket_Debug(( "GSocket_Output_Timeout is buggy!\n" ));
1288 GSocket_Debug(( "GSocket_Output_Timeout seems correct\n" ));
1293 GSocket_Debug(( "GSocket_Output_Timeout, didn't try select!\n" ));
1296 return GSOCK_NOERROR
;
1299 int GSocket::Recv_Stream(char *buffer
, int size
)
1304 ret
= recv(m_fd
, buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1305 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1309 int GSocket::Recv_Dgram(char *buffer
, int size
)
1311 struct sockaddr from
;
1312 SOCKLEN_T fromlen
= sizeof(from
);
1316 fromlen
= sizeof(from
);
1320 ret
= recvfrom(m_fd
, buffer
, size
, 0, &from
, (SOCKLEN_T
*) &fromlen
);
1321 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1326 /* Translate a system address into a GSocket address */
1329 m_peer
= GAddress_new();
1332 m_error
= GSOCK_MEMERR
;
1336 err
= _GAddress_translate_from(m_peer
, &from
, fromlen
);
1337 if (err
!= GSOCK_NOERROR
)
1339 GAddress_destroy(m_peer
);
1348 int GSocket::Send_Stream(const char *buffer
, int size
)
1352 #ifndef __VISAGECPP__
1357 ret
= send(m_fd
, (char *)buffer
, size
, GSOCKET_MSG_NOSIGNAL
);
1358 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1359 #ifndef __VISAGECPP__
1366 int GSocket::Send_Dgram(const char *buffer
, int size
)
1368 struct sockaddr
*addr
;
1374 m_error
= GSOCK_INVADDR
;
1378 err
= _GAddress_translate_to(m_peer
, &addr
, &len
);
1379 if (err
!= GSOCK_NOERROR
)
1385 #ifndef __VISAGECPP__
1390 ret
= sendto(m_fd
, (char *)buffer
, size
, 0, addr
, len
);
1391 } while (ret
== -1 && errno
== EINTR
); /* Loop until not interrupted */
1392 #ifndef __VISAGECPP__
1396 /* Frees memory allocated from _GAddress_translate_to */
1402 void GSocket::Detected_Read()
1406 /* Safeguard against straggling call to Detected_Read */
1407 if (m_fd
== INVALID_SOCKET
)
1412 /* If we have already detected a LOST event, then don't try
1413 * to do any further processing.
1415 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1417 m_establishing
= false;
1419 CALL_CALLBACK(this, GSOCK_LOST
);
1424 int num
= recv(m_fd
, &c
, 1, MSG_PEEK
| GSOCKET_MSG_NOSIGNAL
);
1428 CALL_CALLBACK(this, GSOCK_INPUT
);
1432 if (m_server
&& m_stream
)
1434 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1438 /* Do not throw a lost event in cases where the socket isn't really lost */
1439 if ((errno
== EWOULDBLOCK
) || (errno
== EAGAIN
) || (errno
== EINTR
))
1441 CALL_CALLBACK(this, GSOCK_INPUT
);
1445 CALL_CALLBACK(this, GSOCK_LOST
);
1452 void GSocket::Detected_Write()
1454 /* If we have already detected a LOST event, then don't try
1455 * to do any further processing.
1457 if ((m_detected
& GSOCK_LOST_FLAG
) != 0)
1459 m_establishing
= false;
1461 CALL_CALLBACK(this, GSOCK_LOST
);
1466 if (m_establishing
&& !m_server
)
1469 SOCKOPTLEN_T len
= sizeof(error
);
1471 m_establishing
= false;
1473 getsockopt(m_fd
, SOL_SOCKET
, SO_ERROR
, (char*)&error
, &len
);
1477 CALL_CALLBACK(this, GSOCK_LOST
);
1482 CALL_CALLBACK(this, GSOCK_CONNECTION
);
1483 /* We have to fire this event by hand because CONNECTION (for clients)
1484 * and OUTPUT are internally the same and we just disabled CONNECTION
1485 * events with the above macro.
1487 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1492 CALL_CALLBACK(this, GSOCK_OUTPUT
);
1496 /* Compatibility functions for GSocket */
1497 GSocket
*GSocket_new(void)
1499 GSocket
*newsocket
= new GSocket();
1500 if(newsocket
->IsOk())
1507 * -------------------------------------------------------------------------
1509 * -------------------------------------------------------------------------
1512 /* CHECK_ADDRESS verifies that the current address family is either
1513 * GSOCK_NOFAMILY or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it
1514 * initalizes it to be a GSOCK_*family*. In other cases, it returns
1515 * an appropiate error code.
1517 * CHECK_ADDRESS_RETVAL does the same but returning 'retval' on error.
1519 #define CHECK_ADDRESS(address, family) \
1521 if (address->m_family == GSOCK_NOFAMILY) \
1522 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1523 return address->m_error; \
1524 if (address->m_family != GSOCK_##family) \
1526 address->m_error = GSOCK_INVADDR; \
1527 return GSOCK_INVADDR; \
1531 #define CHECK_ADDRESS_RETVAL(address, family, retval) \
1533 if (address->m_family == GSOCK_NOFAMILY) \
1534 if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \
1536 if (address->m_family != GSOCK_##family) \
1538 address->m_error = GSOCK_INVADDR; \
1544 GAddress
*GAddress_new(void)
1548 if ((address
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1551 address
->m_family
= GSOCK_NOFAMILY
;
1552 address
->m_addr
= NULL
;
1558 GAddress
*GAddress_copy(GAddress
*address
)
1562 assert(address
!= NULL
);
1564 if ((addr2
= (GAddress
*) malloc(sizeof(GAddress
))) == NULL
)
1567 memcpy(addr2
, address
, sizeof(GAddress
));
1569 if (address
->m_addr
&& address
->m_len
> 0)
1571 addr2
->m_addr
= (struct sockaddr
*)malloc(addr2
->m_len
);
1572 if (addr2
->m_addr
== NULL
)
1577 memcpy(addr2
->m_addr
, address
->m_addr
, addr2
->m_len
);
1583 void GAddress_destroy(GAddress
*address
)
1585 assert(address
!= NULL
);
1587 if (address
->m_addr
)
1588 free(address
->m_addr
);
1593 void GAddress_SetFamily(GAddress
*address
, GAddressType type
)
1595 assert(address
!= NULL
);
1597 address
->m_family
= type
;
1600 GAddressType
GAddress_GetFamily(GAddress
*address
)
1602 assert(address
!= NULL
);
1604 return address
->m_family
;
1607 GSocketError
_GAddress_translate_from(GAddress
*address
,
1608 struct sockaddr
*addr
, int len
)
1610 address
->m_realfamily
= addr
->sa_family
;
1611 switch (addr
->sa_family
)
1614 address
->m_family
= GSOCK_INET
;
1617 address
->m_family
= GSOCK_UNIX
;
1621 address
->m_family
= GSOCK_INET6
;
1626 address
->m_error
= GSOCK_INVOP
;
1631 if (address
->m_addr
)
1632 free(address
->m_addr
);
1634 address
->m_len
= len
;
1635 address
->m_addr
= (struct sockaddr
*)malloc(len
);
1637 if (address
->m_addr
== NULL
)
1639 address
->m_error
= GSOCK_MEMERR
;
1640 return GSOCK_MEMERR
;
1642 memcpy(address
->m_addr
, addr
, len
);
1644 return GSOCK_NOERROR
;
1647 GSocketError
_GAddress_translate_to(GAddress
*address
,
1648 struct sockaddr
**addr
, int *len
)
1650 if (!address
->m_addr
)
1652 address
->m_error
= GSOCK_INVADDR
;
1653 return GSOCK_INVADDR
;
1656 *len
= address
->m_len
;
1657 *addr
= (struct sockaddr
*)malloc(address
->m_len
);
1660 address
->m_error
= GSOCK_MEMERR
;
1661 return GSOCK_MEMERR
;
1664 memcpy(*addr
, address
->m_addr
, address
->m_len
);
1665 return GSOCK_NOERROR
;
1669 * -------------------------------------------------------------------------
1670 * Internet address family
1671 * -------------------------------------------------------------------------
1674 GSocketError
_GAddress_Init_INET(GAddress
*address
)
1676 address
->m_len
= sizeof(struct sockaddr_in
);
1677 address
->m_addr
= (struct sockaddr
*) malloc(address
->m_len
);
1678 if (address
->m_addr
== NULL
)
1680 address
->m_error
= GSOCK_MEMERR
;
1681 return GSOCK_MEMERR
;
1684 address
->m_family
= GSOCK_INET
;
1685 address
->m_realfamily
= PF_INET
;
1686 ((struct sockaddr_in
*)address
->m_addr
)->sin_family
= AF_INET
;
1687 ((struct sockaddr_in
*)address
->m_addr
)->sin_addr
.s_addr
= INADDR_ANY
;
1689 return GSOCK_NOERROR
;
1692 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
)
1695 struct in_addr
*addr
;
1697 assert(address
!= NULL
);
1699 CHECK_ADDRESS(address
, INET
);
1701 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1703 /* If it is a numeric host name, convert it now */
1704 #if defined(HAVE_INET_ATON)
1705 if (inet_aton(hostname
, addr
) == 0)
1707 #elif defined(HAVE_INET_ADDR)
1708 if ( (addr
->s_addr
= inet_addr(hostname
)) == (unsigned)-1 )
1711 /* Use gethostbyname by default */
1713 int val
= 1; /* VA doesn't like constants in conditional expressions */
1718 struct in_addr
*array_addr
;
1720 /* It is a real name, we solve it */
1721 if ((he
= gethostbyname(hostname
)) == NULL
)
1723 /* Reset to invalid address */
1724 addr
->s_addr
= INADDR_NONE
;
1725 address
->m_error
= GSOCK_NOHOST
;
1726 return GSOCK_NOHOST
;
1728 array_addr
= (struct in_addr
*) *(he
->h_addr_list
);
1729 addr
->s_addr
= array_addr
[0].s_addr
;
1731 return GSOCK_NOERROR
;
1734 GSocketError
GAddress_INET_SetAnyAddress(GAddress
*address
)
1736 return GAddress_INET_SetHostAddress(address
, INADDR_ANY
);
1739 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
1740 unsigned long hostaddr
)
1742 struct in_addr
*addr
;
1744 assert(address
!= NULL
);
1746 CHECK_ADDRESS(address
, INET
);
1748 addr
= &(((struct sockaddr_in
*)address
->m_addr
)->sin_addr
);
1749 addr
->s_addr
= htonl(hostaddr
);
1751 return GSOCK_NOERROR
;
1754 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
1755 const char *protocol
)
1758 struct sockaddr_in
*addr
;
1760 assert(address
!= NULL
);
1761 CHECK_ADDRESS(address
, INET
);
1765 address
->m_error
= GSOCK_INVPORT
;
1766 return GSOCK_INVPORT
;
1769 #if defined(__WXPM__) && defined(__EMX__)
1770 se
= getservbyname(port
, (char*)protocol
);
1772 se
= getservbyname(port
, protocol
);
1776 /* the cast to int suppresses compiler warnings about subscript having the
1778 if (isdigit((int)port
[0]))
1782 port_int
= atoi(port
);
1783 addr
= (struct sockaddr_in
*)address
->m_addr
;
1784 addr
->sin_port
= htons(port_int
);
1785 return GSOCK_NOERROR
;
1788 address
->m_error
= GSOCK_INVPORT
;
1789 return GSOCK_INVPORT
;
1792 addr
= (struct sockaddr_in
*)address
->m_addr
;
1793 addr
->sin_port
= se
->s_port
;
1795 return GSOCK_NOERROR
;
1798 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
)
1800 struct sockaddr_in
*addr
;
1802 assert(address
!= NULL
);
1803 CHECK_ADDRESS(address
, INET
);
1805 addr
= (struct sockaddr_in
*)address
->m_addr
;
1806 addr
->sin_port
= htons(port
);
1808 return GSOCK_NOERROR
;
1811 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
, size_t sbuf
)
1815 struct sockaddr_in
*addr
;
1817 assert(address
!= NULL
);
1818 CHECK_ADDRESS(address
, INET
);
1820 addr
= (struct sockaddr_in
*)address
->m_addr
;
1821 addr_buf
= (char *)&(addr
->sin_addr
);
1823 he
= gethostbyaddr(addr_buf
, sizeof(addr
->sin_addr
), AF_INET
);
1826 address
->m_error
= GSOCK_NOHOST
;
1827 return GSOCK_NOHOST
;
1830 strncpy(hostname
, he
->h_name
, sbuf
);
1832 return GSOCK_NOERROR
;
1835 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
)
1837 struct sockaddr_in
*addr
;
1839 assert(address
!= NULL
);
1840 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1842 addr
= (struct sockaddr_in
*)address
->m_addr
;
1844 return ntohl(addr
->sin_addr
.s_addr
);
1847 unsigned short GAddress_INET_GetPort(GAddress
*address
)
1849 struct sockaddr_in
*addr
;
1851 assert(address
!= NULL
);
1852 CHECK_ADDRESS_RETVAL(address
, INET
, 0);
1854 addr
= (struct sockaddr_in
*)address
->m_addr
;
1855 return ntohs(addr
->sin_port
);
1859 * -------------------------------------------------------------------------
1860 * Unix address family
1861 * -------------------------------------------------------------------------
1864 #ifndef __VISAGECPP__
1865 GSocketError
_GAddress_Init_UNIX(GAddress
*address
)
1867 address
->m_len
= sizeof(struct sockaddr_un
);
1868 address
->m_addr
= (struct sockaddr
*)malloc(address
->m_len
);
1869 if (address
->m_addr
== NULL
)
1871 address
->m_error
= GSOCK_MEMERR
;
1872 return GSOCK_MEMERR
;
1875 address
->m_family
= GSOCK_UNIX
;
1876 address
->m_realfamily
= PF_UNIX
;
1877 ((struct sockaddr_un
*)address
->m_addr
)->sun_family
= AF_UNIX
;
1878 ((struct sockaddr_un
*)address
->m_addr
)->sun_path
[0] = 0;
1880 return GSOCK_NOERROR
;
1883 #define UNIX_SOCK_PATHLEN (sizeof(addr->sun_path)/sizeof(addr->sun_path[0]))
1885 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
)
1887 struct sockaddr_un
*addr
;
1889 assert(address
!= NULL
);
1891 CHECK_ADDRESS(address
, UNIX
);
1893 addr
= ((struct sockaddr_un
*)address
->m_addr
);
1894 strncpy(addr
->sun_path
, path
, UNIX_SOCK_PATHLEN
);
1895 addr
->sun_path
[UNIX_SOCK_PATHLEN
- 1] = '\0';
1897 return GSOCK_NOERROR
;
1900 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
)
1902 struct sockaddr_un
*addr
;
1904 assert(address
!= NULL
);
1905 CHECK_ADDRESS(address
, UNIX
);
1907 addr
= (struct sockaddr_un
*)address
->m_addr
;
1909 strncpy(path
, addr
->sun_path
, sbuf
);
1911 return GSOCK_NOERROR
;
1913 #endif /* !defined(__VISAGECPP__) */
1914 #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */