23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #if defined(HAVE_UNISTD_H)
32 #define kde_socklen_t ksocklen_t
35 #include "tdeapplication.h"
38 #include "tdesocketaddress.h"
39 #include "kresolver.h"
40 #include "ksockssocketdevice.h"
42 using namespace KNetwork;
83 if (errno == EADDRINUSE)
84 setError(IO_BindError, AddressInUse);
85 else if (errno == EINVAL)
86 setError(IO_BindError, AlreadyBound);
89 setError(IO_BindError, NotSupported);
103 setError(IO_ListenError, NotSupported);
108 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
115 setError(IO_ListenError, NotCreated);
144 if (errno == EISCONN)
146 else if (errno == EALREADY || errno == EINPROGRESS)
148 setError(IO_ConnectError, InProgress);
151 else if (errno == ECONNREFUSED)
152 setError(IO_ConnectError, ConnectionRefused);
153 else if (errno == ENETDOWN || errno == ENETUNREACH ||
154 errno == ENETRESET || errno == ECONNABORTED ||
155 errno == ECONNRESET || errno == EHOSTDOWN ||
156 errno == EHOSTUNREACH)
157 setError(IO_ConnectError, NetFailure);
159 setError(IO_ConnectError, NotSupported);
164 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
174 setError(IO_AcceptError, NotCreated);
179 kde_socklen_t len =
sizeof(sa);
183 if (errno == EAGAIN || errno == EWOULDBLOCK)
184 setError(IO_AcceptError, WouldBlock);
186 setError(IO_AcceptError, UnknownError);
193 static int socks_read_common(
int sockfd,
char *data, TQ_ULONG maxlen,
TDESocketAddress* from, ssize_t &retval,
bool peek =
false)
206 if (errno == EAGAIN || errno == EWOULDBLOCK)
207 return TDESocketDevice::WouldBlock;
209 return TDESocketDevice::UnknownError;
223 if (maxlen == 0 || data == 0L)
227 int err = socks_read_common(
m_sockfd, data, maxlen, 0L, retval);
231 setError(IO_ReadError, static_cast<SocketError>(err));
244 if (data == 0L || maxlen == 0)
248 int err = socks_read_common(
m_sockfd, data, maxlen, &from, retval);
252 setError(IO_ReadError, static_cast<SocketError>(err));
265 if (maxlen == 0 || data == 0L)
269 int err = socks_read_common(
m_sockfd, data, maxlen, 0L, retval,
true);
273 setError(IO_ReadError, static_cast<SocketError>(err));
286 if (data == 0L || maxlen == 0)
290 int err = socks_read_common(
m_sockfd, data, maxlen, &from, retval,
true);
294 setError(IO_ReadError, static_cast<SocketError>(err));
312 if (data == 0L || len == 0)
318 if (errno == EAGAIN || errno == EWOULDBLOCK)
319 setError(IO_WriteError, WouldBlock);
321 setError(IO_WriteError, UnknownError);
340 if (len <= localAddress.
length())
369 if (len <= peerAddress.
length())
393 int timeout,
bool *timedout)
397 setError(IO_UnspecifiedError, NotCreated);
402 fd_set readfds, writefds, exceptfds;
403 fd_set *preadfds = 0L, *pwritefds = 0L, *pexceptfds = 0L;
414 pwritefds = &writefds;
421 pexceptfds = &exceptfds;
434 tv.tv_sec = timeout / 1000;
435 tv.tv_usec = timeout % 1000 * 1000;
437 retval = select(
m_sockfd + 1, preadfds, pwritefds, pexceptfds, &tv);
442 setError(IO_UnspecifiedError, UnknownError);
453 if (input && FD_ISSET(
m_sockfd, preadfds))
455 if (output && FD_ISSET(
m_sockfd, pwritefds))
457 if (exception && FD_ISSET(
m_sockfd, pexceptfds))
463 void KSocksSocketDevice::initSocks()
465 static bool init =
false;
481 static bool register()
486 static bool register = registered();