20 #define I_KNOW_KSOCKS_ISNT_PUBLIC
22 #undef I_KNOW_KSOCKS_ISNT_PUBLIC
24 #ifdef HAVE_SYS_TIME_H
28 #include <QtCore/QFile>
29 #include <QtCore/QCharRef>
30 #include <QtCore/QMap>
37 #include <sys/types.h>
38 #include <sys/socket.h>
66 static int (*
F_connect) (int,
const struct sockaddr *, kde_socklen_t) = 0L;
67 static signed long int (*
F_read) (int,
void *,
unsigned long int) = 0L;
68 static signed long int (*
F_write) (int,
const void *,
unsigned long int) = 0L;
69 static int (*
F_recvfrom) (int,
void *,
unsigned long int, int,
struct sockaddr *,
70 kde_socklen_t *) = 0L;
71 static int (*
F_sendto) (int,
const void *,
unsigned long int, int,
72 const struct sockaddr *, kde_socklen_t) = 0L;
73 static int (*
F_recv) (int,
void *,
unsigned long int, int) = 0L;
74 static int (*
F_send) (int,
const void *,
unsigned long int, int) = 0L;
75 static int (*
F_getsockname) (int,
struct sockaddr *, kde_socklen_t *) = 0L;
76 static int (*
F_getpeername) (int,
struct sockaddr *, kde_socklen_t *) = 0L;
77 static int (*
F_accept) (int,
struct sockaddr *, kde_socklen_t *) = 0L;
78 static int (*
F_select) (int, fd_set *, fd_set *, fd_set *,
79 struct timeval *) = 0L;
81 static int (*
F_bind) (int,
const struct sockaddr *, kde_socklen_t) = 0L;
88 virtual ~KSocksTable();
94 bool hasWorkingAsyncConnect;
98 KSocksTable::KSocksTable() : myname(
QString::fromLatin1(
"Unknown")), hasWorkingAsyncConnect(true) {
101 KSocksTable::~KSocksTable() {
129 class KNECSocksTable :
public KSocksTable {
132 virtual ~KNECSocksTable();
136 KNECSocksTable::KNECSocksTable() : KSocksTable() {
137 myname =
i18n(
"NEC SOCKS client");
140 symbols.insert(
S_read,
"read");
141 symbols.insert(
S_write,
"write");
144 symbols.insert(
S_recv,
"recv");
145 symbols.insert(
S_send,
"send");
151 symbols.insert(
S_bind,
"bind");
154 KNECSocksTable::~KNECSocksTable() {
164 class KDanteSocksTable :
public KSocksTable {
167 virtual ~KDanteSocksTable();
170 KDanteSocksTable::KDanteSocksTable() : KSocksTable() {
171 hasWorkingAsyncConnect =
false;
172 myname =
i18n(
"Dante SOCKS client");
175 symbols.insert(
S_read,
"Rread");
176 symbols.insert(
S_write,
"Rwrite");
178 symbols.insert(
S_sendto,
"Rsendto");
179 symbols.insert(
S_recv,
"Rrecv");
180 symbols.insert(
S_send,
"Rsend");
183 symbols.insert(
S_accept,
"Raccept");
184 symbols.insert(
S_select,
"Rselect");
185 symbols.insert(
S_listen,
"Rlisten");
186 symbols.insert(
S_bind,
"Rbind");
190 KDanteSocksTable::~KDanteSocksTable() {
199 class KSocks::KSocksPrivate
209 static int debugArea() {
static int s_area =
KDebug::registerArea(
"kdecore (KSocks)");
return s_area; }
219 KSocks *KSocks::_me = 0;
221 bool KSocks::_disabled =
true;
223 bool KSocks::_disabled =
false;
226 void KSocks::disable()
232 KSocks *KSocks::self() {
237 _me =
new KSocks(&cfg);
250 if (_me && _disabled) {
256 _me =
new KSocks(config);
259 bool KSocks::activated() {
return (_me != 0L); }
262 KDECORE_EXPORT
bool kdeHasSocks() {
return KSocks::self()->hasSocks(); }
265 : d(new KSocksPrivate())
270 if (!config->
readEntry(
"SOCKS_enable",
false)) {
278 <<
"/usr/lib" KDELIBSUFF
"/"
280 <<
"/usr/local/lib" KDELIBSUFF
"/"
282 <<
"/usr/local/socks5/lib" KDELIBSUFF
"/"
283 <<
"/usr/local/socks5/lib/"
284 <<
"/opt/socks5/lib" KDELIBSUFF
"/"
285 <<
"/opt/socks5/lib/";
286 d->_libNames <<
"libsocks.so"
290 <<
"libsocks5_sh.so";
295 for (QStringList::Iterator it = newlibs.begin();
299 if (thisone[thisone.length()-1] != QLatin1Char(
'/'))
300 thisone += QLatin1Char(
'/');
301 d->_libPaths << QFile::encodeName(thisone);
302 kDebug(d->debugArea()) <<
"KSocks added a new library path: " << thisone;
309 int _meth = config->
readEntry(
"SOCKS_method", 1);
317 if (d->_socksLib && d->_socksLib->resolveFunction(
"Rconnect")) {
318 d->_st =
new KDanteSocksTable;
321 }
else if (d->_socksLib && d->_socksLib->resolveFunction(
"connect")) {
322 d->_st =
new KNECSocksTable;
325 }
else if (d->_socksLib) {
331 !d->_hasSocks && pit != d->_libPaths.constEnd();
334 it != d->_libNames.constEnd();
336 d->_socksLib = ll->
library(QLatin1String(*pit) + QLatin1String(*it));
338 if ((_meth == 1 || _meth == 2) &&
339 d-> _socksLib->resolveFunction(
"S5LogShowThreadIDS") != 0L) {
340 kDebug(d->debugArea()) <<
"Found NEC SOCKS";
341 d->_st =
new KNECSocksTable;
345 }
else if ((_meth == 1 || _meth == 3) &&
346 d->_socksLib->resolveFunction(
"sockaddr2ruleaddress") != 0L) {
347 kDebug(d->debugArea()) <<
"Found Dante SOCKS";
348 d->_st =
new KDanteSocksTable;
353 d->_socksLib->unload();
362 it != d->_st->symbols.end();
367 d->_socksLib->resolveFunction(it.value());
370 F_connect = (int (*)(int,
const struct sockaddr *, kde_socklen_t))
371 d->_socksLib->resolveFunction(it.value());
374 F_read = (
signed long int (*)(int,
void *,
unsigned long int))
375 d->_socksLib->resolveFunction(it.value());
378 F_write = (
signed long int (*)(int,
const void *,
unsigned long int))
379 d->_socksLib->resolveFunction(it.value());
382 F_recvfrom = (int (*)(int,
void *,
unsigned long int, int,
383 struct sockaddr *, kde_socklen_t *))
384 d->_socksLib->resolveFunction(it.value());
387 F_sendto = (int (*)(int,
const void *,
unsigned long int, int,
388 const struct sockaddr *, kde_socklen_t))
389 d->_socksLib->resolveFunction(it.value());
392 F_recv = (int (*)(int,
void *,
unsigned long int, int))
393 d->_socksLib->resolveFunction(it.value());
396 F_send = (int (*)(int,
const void *,
unsigned long int, int))
397 d->_socksLib->resolveFunction(it.value());
400 F_getsockname = (int (*)(int,
struct sockaddr *, kde_socklen_t *))
401 d->_socksLib->resolveFunction(it.value());
404 F_getpeername = (int (*)(int,
struct sockaddr *, kde_socklen_t *))
405 d->_socksLib->resolveFunction(it.value());
408 F_accept = (int (*)(int,
struct sockaddr *, kde_socklen_t *))
409 d->_socksLib->resolveFunction(it.value());
412 F_select = (int (*)(int, fd_set *, fd_set *, fd_set *,
struct timeval *))
413 d->_socksLib->resolveFunction(it.value());
417 d->_socksLib->resolveFunction(it.value());
420 F_bind = (int (*)(int,
const struct sockaddr *, kde_socklen_t))
421 d->_socksLib->resolveFunction(it.value());
424 kDebug(d->debugArea()) <<
"KSocks got a symbol it doesn't know about!";
431 int rc = (*F_SOCKSinit)((
char *)
"KDE");
434 else kDebug(d->debugArea()) <<
"SOCKS has been activated!";
455 void KSocks::stopSocks() {
459 d->_useSocks =
false;
460 d->_hasSocks =
false;
462 d->_socksLib->unload();
471 bool KSocks::usingSocks() {
476 bool KSocks::hasSocks() {
481 void KSocks::disableSocks() {
482 d->_useSocks =
false;
486 void KSocks::enableSocks() {
491 bool KSocks::hasWorkingAsyncConnect()
493 return (d->_useSocks && d->_st) ? d->_st->hasWorkingAsyncConnect :
true;
502 int KSocks::connect (
int sockfd,
const sockaddr *serv_addr,
503 kde_socklen_t addrlen) {
505 return (*
F_connect)(sockfd, serv_addr, addrlen);
506 else return ::connect(sockfd, (sockaddr*) serv_addr, (socklen_t)addrlen);
510 signed long int KSocks::read (
int fd,
void *buf,
unsigned long int count) {
511 if (d->_useSocks &&
F_read)
512 return (*
F_read)(fd, buf, count);
513 else return ::read(fd, buf, count);
517 signed long int KSocks::write (
int fd,
const void *buf,
unsigned long int count) {
519 return (*
F_write)(fd, buf, count);
520 else return ::write(fd, buf, count);
524 int KSocks::recvfrom (
int s,
void *buf,
unsigned long int len,
int flags,
525 sockaddr *from, kde_socklen_t *fromlen) {
527 return (*
F_recvfrom)(s, buf, len, flags, from, fromlen);
529 socklen_t casted_len = (socklen_t) *fromlen;
530 int rc = ::recvfrom(s, (
char*) buf, len, flags, from, &casted_len);
531 *fromlen = casted_len;
537 int KSocks::sendto (
int s,
const void *msg,
unsigned long int len,
int flags,
538 const sockaddr *to, kde_socklen_t tolen) {
540 return (*
F_sendto)(s, msg, len, flags, to, tolen);
541 else return ::sendto(s, (
char*) msg, len, flags, to, (socklen_t)tolen);
545 int KSocks::recv (
int s,
void *buf,
unsigned long int len,
int flags) {
546 if (d->_useSocks &&
F_recv)
547 return (*
F_recv)(s, buf, len, flags);
548 else return ::recv(s, (
char*) buf, len, flags);
552 int KSocks::send (
int s,
const void *msg,
unsigned long int len,
int flags) {
553 if (d->_useSocks &&
F_send)
554 return (*
F_send)(s, msg, len, flags);
555 else return ::send(s, (
char*) msg, len, flags);
559 int KSocks::getsockname (
int s, sockaddr *name, kde_socklen_t *namelen) {
563 socklen_t casted_len = *namelen;
564 int rc = ::getsockname(s, name, &casted_len);
565 *namelen = casted_len;
571 int KSocks::getpeername (
int s, sockaddr *name, kde_socklen_t *namelen) {
575 socklen_t casted_len = *namelen;
576 int rc = ::getpeername(s, name, &casted_len);
577 *namelen = casted_len;
583 int KSocks::accept (
int s, sockaddr *addr, kde_socklen_t *addrlen) {
585 return (*
F_accept)(s, addr, addrlen);
587 socklen_t casted_len = *addrlen;
588 int rc = ::accept(s, addr, &casted_len);
589 *addrlen = casted_len;
595 int KSocks::select (
int n, fd_set *readfds, fd_set *writefds,
596 fd_set *exceptfds,
struct timeval *
timeout) {
599 else return ::select(n, readfds, writefds, exceptfds, timeout);
610 int KSocks::bind (
int sockfd,
const sockaddr *my_addr, kde_socklen_t addrlen) {
611 if (d->_useSocks &&
F_bind)
612 return (*
F_bind)(sockfd, my_addr, addrlen);
613 else return ::bind(sockfd, my_addr, (socklen_t)addrlen);
616 int KSocks::bind (
int sockfd, sockaddr *my_addr, kde_socklen_t addrlen) {
617 if (d->_useSocks &&
F_bind)
618 return (*
F_bind)(sockfd, my_addr, addrlen);
619 else return ::bind(sockfd, my_addr, (socklen_t)addrlen);
QString i18n(const char *text)
Returns a localized version of a string.
KLibrary * library(const QString &libname, QLibrary::LoadHints loadHint=0)
Loads and initializes a library.
QString readPathEntry(const QString &pKey, const QString &aDefault) const
Reads a path.
static signed long int(* F_write)(int, const void *, unsigned long int)=0L
static int(* F_getsockname)(int, struct sockaddr *, kde_socklen_t *)=0L
static int(* F_accept)(int, struct sockaddr *, kde_socklen_t *)=0L
QTcpServer * listen(const QString &protocol, const QHostAddress &address=QHostAddress::Any, quint16 port=0, QObject *parent=0)
Opens a TCP/IP socket for listening protocol protocol, binding only at address address.
static int(* F_send)(int, const void *, unsigned long int, int)=0L
KSharedConfigPtr config()
Returns the general config object.
static int(* F_recvfrom)(int, void *, unsigned long int, int, struct sockaddr *, kde_socklen_t *)=0L
static int(* F_getpeername)(int, struct sockaddr *, kde_socklen_t *)=0L
static int(* F_SOCKSinit)(char *)=0L
static int(* F_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *)=0L
static int(* F_sendto)(int, const void *, unsigned long int, int, const struct sockaddr *, kde_socklen_t)=0L
static int(* F_listen)(int, int)=0L
static int registerArea(const QByteArray &areaName, bool enabled=true)
Thin wrapper around QLibrary; you should rarely use this directly, see KPluginLoader for higher-level...
static signed long int(* F_read)(int, void *, unsigned long int)=0L
static int(* F_recv)(int, void *, unsigned long int, int)=0L
A class for one specific group in a KConfig object.
static KLibLoader * self()
Returns a pointer to the factory.
The KLibLoader allows you to load libraries dynamically at runtime.
static int(* F_connect)(int, const struct sockaddr *, kde_socklen_t)=0L
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
static int(* F_bind)(int, const struct sockaddr *, kde_socklen_t)=0L