ksocks.cpp Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# ksocks.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2001-2003 George Staikos <staikos@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 #include <config.h>
    00020
    00021 #ifdef HAVE_SYS_TIME_H
    00022 #include <sys/time.h>
    00023 #endif
    00024
    00025 #include <qfile.h>
    00026 #include <qstring.h>
    00027 #include <qmap.h>
    00028
    00029 #include <klocale.h>
    00030 #include <kdebug.h>
    00031 #include "klibloader.h"
    00032 #include <kconfig.h>
    00033 #include <kapplication.h>
    00034
    00035 #include <sys/types.h>
    00036 #include <sys/socket.h>
    00037
    00038 #include <unistd.h>
    00039
    00040 #include "ksocks.h"
    00041
    00042 // DO NOT RE-ORDER THESE.
    00043 enum SymbolKeys {
    00044       S_SOCKSinit    =  0,
    00045       S_connect      =  1,
    00046       S_read         =  2,
    00047       S_write        =  3,
    00048       S_recvfrom     =  4,
    00049       S_sendto       =  5,
    00050       S_recv         =  6,
    00051       S_send         =  7,
    00052       S_getsockname  =  8,
    00053       S_getpeername  =  9,
    00054       S_accept       = 10,
    00055       S_select       = 11,
    00056       S_listen       = 12,
    00057       S_bind         = 13
    00058      };
    00059
    00060
    00061 extern "C" {
    00062 // Function pointer table
    00063 static int     (*F_SOCKSinit)   (char *) = 0L;
    00064 static int     (*F_connect)     (int, const struct sockaddr *, ksocklen_t) = 0L;
    00065 static signed long int (*F_read)        (int, void *, unsigned long int) = 0L;
    00066 static signed long int (*F_write)       (int, const void *, unsigned long int) = 0L;
    00067 static int     (*F_recvfrom)    (int, void *, unsigned long int, int, struct sockaddr *,
    00068                                  ksocklen_t *) = 0L;
    00069 static int     (*F_sendto)      (int, const void *, unsigned long int, int,
    00070                                  const struct sockaddr *, ksocklen_t) = 0L;
    00071 static int     (*F_recv)        (int, void *, unsigned long int, int) = 0L;
    00072 static int     (*F_send)        (int, const void *, unsigned long int, int) = 0L;
    00073 static int     (*F_getsockname) (int, struct sockaddr *, ksocklen_t *) = 0L;
    00074 static int     (*F_getpeername) (int, struct sockaddr *, ksocklen_t *) = 0L;
    00075 static int     (*F_accept)      (int, struct sockaddr *, ksocklen_t *) = 0L;
    00076 static int     (*F_select)      (int, fd_set *, fd_set *, fd_set *,
    00077                                                      struct timeval *) = 0L;
    00078 static int     (*F_listen)      (int, int) = 0L;
    00079 static int     (*F_bind)        (int, struct sockaddr *, ksocklen_t) = 0L;
    00080 }
    00081
    00082
    00083 class KSocksTable {
    00084  public:
    00085    KSocksTable();
    00086    virtual ~KSocksTable();
    00087
    00088    // The name of each symbol and it's SOCKS replacement
    00089    QMap<SymbolKeys,QString>  symbols;
    00090    // The name of this library
    00091    QString                   myname;
    00092    bool                      hasWorkingAsyncConnect;
    00093 };
    00094
    00095
    00096 KSocksTable::KSocksTable() : myname("Unknown"), hasWorkingAsyncConnect(true) {
    00097 }
    00098
    00099 KSocksTable::~KSocksTable() {
    00100 }
    00101
    00102
    00103 /*
    00104  *   How to add support for a new SOCKS package.
    00105  *
    00106  *   1) Subclass KSocksTable as is done below and write out all the symbols
    00107  *   1.b) Give the class a "myname"
    00108  *   2) Make sure that all possible library names are written into the
    00109  *      _libNames string list.  Don't forget that different OSes name shared
    00110  *      libraries differently.  Expect .so, .sl, .a (!) (AIX does this).
    00111  *   3) Find a unique symbol in the library that we can use to identify that
    00112  *      library and write out the test case in the constructor
    00113  *   4) Make necessary changes to the KControl module in kdebase/kcontrol/....
    00114  *   5) TEST!
    00115  *
    00116  */
    00117
    00121
    00122
    00123 //
    00124 //    Support for NEC SOCKS client
    00125 //
    00126
    00127 class KNECSocksTable : public KSocksTable {
    00128   public:
    00129     KNECSocksTable();
    00130     virtual ~KNECSocksTable();
    00131 };
    00132
    00133
    00134 KNECSocksTable::KNECSocksTable() : KSocksTable() {
    00135   myname = i18n("NEC SOCKS client");
    00136   symbols.insert(S_SOCKSinit,   "SOCKSinit");
    00137   symbols.insert(S_connect,     "connect");
    00138   symbols.insert(S_read,        "read");
    00139   symbols.insert(S_write,       "write");
    00140   symbols.insert(S_recvfrom,    "recvfrom");
    00141   symbols.insert(S_sendto,      "sendto");
    00142   symbols.insert(S_recv,        "recv");
    00143   symbols.insert(S_send,        "send");
    00144   symbols.insert(S_getsockname, "getsockname");
    00145   symbols.insert(S_getpeername, "getpeername");
    00146   symbols.insert(S_accept,      "accept");
    00147   symbols.insert(S_select,      "select");
    00148   symbols.insert(S_listen,      "listen");
    00149   symbols.insert(S_bind,        "bind");
    00150 }
    00151
    00152 KNECSocksTable::~KNECSocksTable() {
    00153 }
    00154
    00155
    00156
    00157
    00158 //
    00159 //    Support for Dante SOCKS client
    00160 //
    00161
    00162 class KDanteSocksTable : public KSocksTable {
    00163   public:
    00164     KDanteSocksTable();
    00165     virtual ~KDanteSocksTable();
    00166 };
    00167
    00168 KDanteSocksTable::KDanteSocksTable() : KSocksTable() {
    00169   hasWorkingAsyncConnect = false;
    00170   myname = i18n("Dante SOCKS client");
    00171   symbols.insert(S_SOCKSinit,   "SOCKSinit");
    00172   symbols.insert(S_connect,     "Rconnect");
    00173   symbols.insert(S_read,        "Rread");
    00174   symbols.insert(S_write,       "Rwrite");
    00175   symbols.insert(S_recvfrom,    "Rrecvfrom");
    00176   symbols.insert(S_sendto,      "Rsendto");
    00177   symbols.insert(S_recv,        "Rrecv");
    00178   symbols.insert(S_send,        "Rsend");
    00179   symbols.insert(S_getsockname, "Rgetsockname");
    00180   symbols.insert(S_getpeername, "Rgetpeername");
    00181   symbols.insert(S_accept,      "Raccept");
    00182   symbols.insert(S_select,      "Rselect");
    00183   symbols.insert(S_listen,      "Rlisten");
    00184   symbols.insert(S_bind,        "Rbind");
    00185 }
    00186
    00187
    00188 KDanteSocksTable::~KDanteSocksTable() {
    00189 }
    00190
    00191
    00192
    00196
    00197
    00198 KSocks *KSocks::_me = 0;
    00199 bool KSocks::_disabled = false;
    00200
    00201 void KSocks::disable()
    00202 {
    00203    if (!_me)
    00204       _disabled = true;
    00205 }
    00206
    00207 KSocks *KSocks::self() {
    00208   // Note that we don't use a static deleter here. It makes no sense and tends to cause crashes.
    00209   if (!_me) {
    00210      if (kapp) {
    00211         KConfigGroup cfg(kapp->config(), "Socks");
    00212         _me = new KSocks(&cfg);
    00213      } else {
    00214         _disabled = true;
    00215         _me = new KSocks(0);
    00216      }
    00217   }
    00218   return _me;
    00219 }
    00220
    00221 void KSocks::setConfig(KConfigBase *config)
    00222 {
    00223   // We can change the config from disabled to enabled
    00224   // but not the other way around.
    00225   if (_me && _disabled) {
    00226      delete _me;
    00227      _me = 0;
    00228      _disabled = false;
    00229   }
    00230   if (!_me)
    00231     _me = new KSocks(config);
    00232 }
    00233
    00234 bool KSocks::activated() { return (_me != 0L); }
    00235
    00236
    00237 KSocks::KSocks(KConfigBase *config) : _socksLib(0L), _st(0L) {
    00238    _hasSocks = false;
    00239    _useSocks = false;
    00240
    00241    if (!config)
    00242       return;
    00243
    00244    if (!(config->readBoolEntry("SOCKS_enable", false))) {
    00245       _disabled = true;
    00246    }
    00247
    00248    if (_disabled)
    00249       return;
    00250
    00251    _libPaths << ""
    00252              << "/usr/lib/"
    00253              << "/usr/local/lib/"
    00254              << "/usr/local/socks5/lib/"
    00255              << "/opt/socks5/lib/";
    00256    _libNames << "libsocks.so"                  // Dante
    00257              << "libsocks5.so"                 // ?
    00258              << "libsocks5_sh.so";             // NEC
    00259
    00260    // Add the custom library paths here
    00261    QStringList newlibs = config->readListEntry("SOCKS_lib_path");
    00262
    00263    for (QStringList::Iterator it = newlibs.begin();
    00264                               it != newlibs.end();
    00265                               ++it) {
    00266       QString thisone = *it;
    00267       if (thisone[thisone.length()-1] != '/') thisone += "/";
    00268       _libPaths << thisone;
    00269       kdDebug(171) << "KSocks added a new library path: " << thisone << endl;
    00270    }
    00271
    00272    // Load the proper libsocks and KSocksTable
    00273    KLibLoader *ll = KLibLoader::self();
    00274
    00275
    00276    int _meth = config->readNumEntry("SOCKS_method", 1);
    00277          /****       Current methods
    00278           *   1) Autodetect (read: any)     2) NEC
    00279           *   3) Dante                      4) Custom
    00280           */
    00281
    00282    if (_meth == 4) {         // try to load^H^H^H^Hguess at a custom library
    00283       _socksLib = ll->library(config->readPathEntry("SOCKS_lib", "").latin1());
    00284       if (_socksLib && _socksLib->symbol("Rconnect")) {  // Dante compatible?
    00285          _st = new KDanteSocksTable;
    00286          _useSocks = true;
    00287          _hasSocks = true;
    00288       } else if (_socksLib && _socksLib->symbol("connect")) { // NEC compatible?
    00289          _st = new KNECSocksTable;
    00290          _useSocks = true;
    00291          _hasSocks = true;
    00292       } else if (_socksLib) {
    00293          _socksLib->unload();
    00294          _socksLib = 0L;
    00295       }
    00296    } else              // leave this here   "else for {}"
    00297    for (QStringList::Iterator pit  = _libPaths.begin();
    00298                               !_hasSocks && pit != _libPaths.end();
    00299                               ++pit)
    00300    for (QStringList::Iterator it  = _libNames.begin();
    00301                               it != _libNames.end();
    00302                               ++it) {
    00303       _socksLib = ll->library((*pit + *it).latin1());
    00304       if (_socksLib) {
    00305          if ((_meth == 1 || _meth == 2) &&
    00306              _socksLib->symbol("S5LogShowThreadIDS") != 0L) {  // NEC SOCKS
    00307             kdDebug(171) << "Found NEC SOCKS" << endl;
    00308             _st = new KNECSocksTable;
    00309             _useSocks = true;
    00310             _hasSocks = true;
    00311             break;
    00312          } else if ((_meth == 1 || _meth == 3) &&
    00313                     _socksLib->symbol("sockaddr2ruleaddress") != 0L) { //Dante
    00314             kdDebug(171) << "Found Dante SOCKS" << endl;
    00315             _st = new KDanteSocksTable;
    00316             _useSocks = true;
    00317             _hasSocks = true;
    00318             break;
    00319          } else {
    00320            _socksLib->unload();
    00321            _socksLib = 0L;
    00322          }
    00323       }
    00324    }
    00325
    00326    // Load in all the symbols
    00327    if (_st) {
    00328       for (QMap<SymbolKeys,QString>::Iterator it  = _st->symbols.begin();
    00329                                               it != _st->symbols.end();
    00330                                               ++it) {
    00331          switch(it.key()) {
    00332          case S_SOCKSinit:
    00333            F_SOCKSinit = (int (*)(char *))
    00334                          _socksLib->symbol(it.data().latin1());
    00335           break;
    00336          case S_connect:
    00337            F_connect = (int (*)(int, const struct sockaddr *, ksocklen_t))
    00338                        _socksLib->symbol(it.data().latin1());
    00339           break;
    00340          case S_read:
    00341            F_read = (signed long int (*)(int, void *, unsigned long int))
    00342                     _socksLib->symbol(it.data().latin1());
    00343           break;
    00344          case S_write:
    00345            F_write = (signed long int (*)(int, const void *, unsigned long int))
    00346                      _socksLib->symbol(it.data().latin1());
    00347           break;
    00348          case S_recvfrom:
    00349            F_recvfrom = (int (*)(int, void *, unsigned long int, int,
    00350                                  struct sockaddr *, ksocklen_t *))
    00351                         _socksLib->symbol(it.data().latin1());
    00352           break;
    00353          case S_sendto:
    00354            F_sendto = (int (*)(int, const void *, unsigned long int, int,
    00355                                const struct sockaddr *, ksocklen_t))
    00356                       _socksLib->symbol(it.data().latin1());
    00357           break;
    00358          case S_recv:
    00359            F_recv = (int (*)(int, void *, unsigned long int, int))
    00360                     _socksLib->symbol(it.data().latin1());
    00361           break;
    00362          case S_send:
    00363            F_send = (int (*)(int, const void *, unsigned long int, int))
    00364                     _socksLib->symbol(it.data().latin1());
    00365           break;
    00366          case S_getsockname:
    00367            F_getsockname = (int (*)(int, struct sockaddr *, ksocklen_t *))
    00368                            _socksLib->symbol(it.data().latin1());
    00369           break;
    00370          case S_getpeername:
    00371            F_getpeername = (int (*)(int, struct sockaddr *, ksocklen_t *))
    00372                            _socksLib->symbol(it.data().latin1());
    00373           break;
    00374          case S_accept:
    00375            F_accept = (int (*)(int, struct sockaddr *, ksocklen_t *))
    00376                       _socksLib->symbol(it.data().latin1());
    00377           break;
    00378          case S_select:
    00379            F_select = (int (*)(int, fd_set *, fd_set *, fd_set *, struct timeval *))
    00380                       _socksLib->symbol(it.data().latin1());
    00381           break;
    00382          case S_listen:
    00383            F_listen = (int (*)(int, int))
    00384                       _socksLib->symbol(it.data().latin1());
    00385           break;
    00386          case S_bind:
    00387            F_bind = (int (*)(int, struct sockaddr *, ksocklen_t))
    00388                     _socksLib->symbol(it.data().latin1());
    00389           break;
    00390          default:
    00391           kdDebug(171) << "KSocks got a symbol it doesn't know about!" << endl;
    00392           break;
    00393          }
    00394       }
    00395
    00396       // Now we check for the critical stuff.
    00397       if (F_SOCKSinit) {
    00398          int rc = (*F_SOCKSinit)((char *)"KDE");
    00399          if (rc != 0)
    00400             stopSocks();
    00401          else kdDebug(171) << "SOCKS has been activated!" << endl;
    00402       } else {
    00403          stopSocks();
    00404       }
    00405    }
    00406 }
    00407
    00408
    00409 KSocks::~KSocks() {
    00410    stopSocks();
    00411    _me = 0;
    00412 }
    00413
    00414 void KSocks::die() {
    00415    if (_me == this) {
    00416       _me = 0;
    00417       delete this;
    00418    }
    00419 }
    00420
    00421 void KSocks::stopSocks() {
    00422    if (_hasSocks) {
    00423       // This library doesn't even provide the basics.
    00424       // It's probably broken.  Let's abort.
    00425       _useSocks = false;
    00426       _hasSocks = false;
    00427       if (_socksLib) {
    00428          _socksLib->unload();
    00429          _socksLib = 0L;
    00430       }
    00431       delete _st;
    00432       _st = 0L;
    00433    }
    00434 }
    00435
    00436
    00437 bool KSocks::usingSocks() {
    00438    return _useSocks;
    00439 }
    00440
    00441
    00442 bool KSocks::hasSocks() {
    00443    return _hasSocks;
    00444 }
    00445
    00446
    00447 void KSocks::disableSocks() {
    00448    _useSocks = false;
    00449 }
    00450
    00451
    00452 void KSocks::enableSocks() {
    00453    if (_hasSocks)
    00454       _useSocks = true;
    00455 }
    00456
    00457 bool KSocks::hasWorkingAsyncConnect()
    00458 {
    00459    return (_useSocks && _st) ? _st->hasWorkingAsyncConnect : true;
    00460 }
    00461
    00462
    00463 /*
    00464  *   REIMPLEMENTED FUNCTIONS FROM LIBC
    00465  *
    00466  */
    00467
    00468 int KSocks::connect (int sockfd, const sockaddr *serv_addr,
    00469                                                    ksocklen_t addrlen) {
    00470    if (_useSocks && F_connect)
    00471       return (*F_connect)(sockfd, serv_addr, addrlen);
    00472    else return ::connect(sockfd, (sockaddr*) serv_addr, (socklen_t)addrlen);
    00473 }
    00474
    00475
    00476 signed long int KSocks::read (int fd, void *buf, unsigned long int count) {
    00477    if (_useSocks && F_read)
    00478       return (*F_read)(fd, buf, count);
    00479    else return ::read(fd, buf, count);
    00480 }
    00481
    00482
    00483 signed long int KSocks::write (int fd, const void *buf, unsigned long int count) {
    00484    if (_useSocks && F_write)
    00485       return (*F_write)(fd, buf, count);
    00486    else return ::write(fd, buf, count);
    00487 }
    00488
    00489
    00490 int KSocks::recvfrom (int s, void *buf, unsigned long int len, int flags,
    00491                                 sockaddr *from, ksocklen_t *fromlen) {
    00492    if (_useSocks && F_recvfrom) {
    00493       return (*F_recvfrom)(s, buf, len, flags, from, fromlen);
    00494    } else {
    00495       socklen_t casted_len = (socklen_t) *fromlen;
    00496       int rc = ::recvfrom(s, (char*) buf, len, flags, from, &casted_len);
    00497       *fromlen = casted_len;
    00498       return rc;
    00499    }
    00500 }
    00501
    00502
    00503 int KSocks::sendto (int s, const void *msg, unsigned long int len, int flags,
    00504                              const sockaddr *to, ksocklen_t tolen) {
    00505    if (_useSocks && F_sendto)
    00506       return (*F_sendto)(s, msg, len, flags, to, tolen);
    00507    else return ::sendto(s, (char*) msg, len, flags, to, (socklen_t)tolen);
    00508 }
    00509
    00510
    00511 int KSocks::recv (int s, void *buf, unsigned long int len, int flags) {
    00512    if (_useSocks && F_recv)
    00513       return (*F_recv)(s, buf, len, flags);
    00514    else return ::recv(s, (char*) buf, len, flags);
    00515 }
    00516
    00517
    00518 int KSocks::send (int s, const void *msg, unsigned long int len, int flags) {
    00519    if (_useSocks && F_send)
    00520       return (*F_send)(s, msg, len, flags);
    00521    else return ::send(s, (char*) msg, len, flags);
    00522 }
    00523
    00524
    00525 int KSocks::getsockname (int s, sockaddr *name, ksocklen_t *namelen) {
    00526    if (_useSocks && F_getsockname) {
    00527       return (*F_getsockname)(s, name, namelen);
    00528    } else {
    00529      socklen_t casted_len = *namelen;
    00530      int rc = ::getsockname(s, name, &casted_len);
    00531      *namelen = casted_len;
    00532      return rc;
    00533    }
    00534 }
    00535
    00536
    00537 int KSocks::getpeername (int s, sockaddr *name, ksocklen_t *namelen) {
    00538    if (_useSocks && F_getpeername) {
    00539       return (*F_getpeername)(s, name, namelen);
    00540    } else {
    00541       socklen_t casted_len = *namelen;
    00542       int rc = ::getpeername(s, name, &casted_len);
    00543       *namelen = casted_len;
    00544       return rc;
    00545    }
    00546 }
    00547
    00548
    00549 int KSocks::accept (int s, sockaddr *addr, ksocklen_t *addrlen) {
    00550    if (_useSocks && F_accept) {
    00551      return (*F_accept)(s, addr, addrlen);
    00552    } else {
    00553       socklen_t casted_len = *addrlen;
    00554       int rc = ::accept(s, addr, &casted_len);
    00555       *addrlen = casted_len;
    00556       return rc;
    00557    }
    00558 }
    00559
    00560
    00561 int KSocks::select (int n, fd_set *readfds, fd_set *writefds,
    00562                                 fd_set *exceptfds, struct timeval *timeout) {
    00563    if (_useSocks && F_select)
    00564       return (*F_select)(n, readfds, writefds, exceptfds, timeout);
    00565    else return ::select(n, readfds, writefds, exceptfds, timeout);
    00566 }
    00567
    00568
    00569 int KSocks::listen (int s, int backlog) {
    00570    if (_useSocks && F_listen)
    00571       return (*F_listen)(s, backlog);
    00572    else return ::listen(s, backlog);
    00573 }
    00574
    00575
    00576 int KSocks::bind (int sockfd, sockaddr *my_addr, ksocklen_t addrlen) {
    00577    if (_useSocks && F_bind)
    00578       return (*F_bind)(sockfd, my_addr, addrlen);
    00579    else return ::bind(sockfd, my_addr, (socklen_t)addrlen);
    00580 }
    00581
    00582
    00583

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:55 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001