kio Library: discovery.cpp Source File (kio Library)

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

# discovery.cpp

    00001 /*
    00002    Copyright (c) 2003 Malte Starostik <malte@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 as published by the Free Software Foundation; either
    00007    version 2 of the License, or (at your option) any later version.
    00008
    00009    This library is distributed in the hope that it will be useful,
    00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012    Library General Public License for more details.
    00013
    00014    You should have received a copy of the GNU Library General Public License
    00015    along with this library; see the file COPYING.LIB.  If not, write to
    00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017    Boston, MA 02111-1307, USA.
    00018 */
    00019
    00020
    00021 #include "config.h"
    00022
    00023 #include <netdb.h>
    00024
    00025 #ifdef HAVE_SYS_TYPES_H
    00026 #include <sys/types.h>
    00027 #endif
    00028 #ifdef HAVE_NETINET_IN_H
    00029 #include <netinet/in.h>
    00030 #endif
    00031 #include <arpa/nameser.h>
    00032 #ifdef HAVE_ARPA_NAMESER8_COMPAT_H
    00033 #include <arpa/nameser8_compat.h>
    00034 #endif
    00035 #ifdef HAVE_SYS_PARAM_H
    00036 // Basically, the BSDs need this before resolv.h
    00037 #include <sys/param.h>
    00038 #endif
    00039 #include <resolv.h>
    00040 #include <sys/utsname.h>
    00041
    00042 #include <qtimer.h>
    00043
    00044 #include <klocale.h>
    00045 #include <kprocio.h>
    00046 #include <kurl.h>
    00047
    00048 #include "discovery.moc"
    00049
    00050 namespace KPAC
    00051 {
    00052     Discovery::Discovery( QObject* parent )
    00053         : Downloader( parent ),
    00054           m_helper( new KProcIO )
    00055     {
    00056         connect( m_helper, SIGNAL( readReady( KProcIO* ) ), SLOT( helperOutput() ) );
    00057         connect( m_helper, SIGNAL( processExited( KProcess* ) ), SLOT( failed() ) );
    00058         *m_helper << "kpac_dhcp_helper";
    00059
    00060         if ( !m_helper->start() )
    00061             QTimer::singleShot( 0, this, SLOT( failed() ) );
    00062     }
    00063
    00064     bool Discovery::initHostName()
    00065     {
    00066         struct utsname uts;
    00067
    00068         if (uname (&uts) > -1)
    00069         {
    00070             struct hostent *hent = gethostbyname (uts.nodename);
    00071             if (hent != 0)
    00072                 m_hostname = QString::fromLocal8Bit( hent->h_name );
    00073         }
    00074
    00075         // If no hostname, try gethostname as a last resort.
    00076         if (m_hostname.isEmpty())
    00077         {
    00078             char buf [256];
    00079             if (gethostname (buf, sizeof(buf)) == 0)
    00080             {
    00081                 buf[255] = '\0';
    00082                 m_hostname = QString::fromLocal8Bit( buf );
    00083             }
    00084         }
    00085         return !m_hostname.isEmpty();
    00086     }
    00087
    00088     bool Discovery::checkDomain() const
    00089     {
    00090         // If a domain has a SOA record, don't traverse any higher.
    00091         // Returns true if no SOA can be found (domain is "ok" to use)
    00092         // Stick to old resolver interface for portability reasons.
    00093         union
    00094         {
    00095             HEADER header;
    00096             unsigned char buf[ PACKETSZ ];
    00097         } response;
    00098         int len = res_query( m_hostname.local8Bit(), C_IN, T_SOA,
    00099                              response.buf, sizeof( response.buf ) );
    00100         if ( len <= int( sizeof( response.header ) ) ||
    00101              ntohs( response.header.ancount ) != 1 ) return true;
    00102         unsigned char* pos = response.buf + sizeof( response.header );
    00103         unsigned char* end = response.buf + len;
    00104         // skip query section
    00105         pos += dn_skipname( pos, end ) + QFIXEDSZ;
    00106         if ( pos >= end ) return true;
    00107         // skip answer domain
    00108         pos += dn_skipname( pos, end );
    00109         short type;
    00110         GETSHORT( type, pos );
    00111         return type != T_SOA;
    00112     }
    00113
    00114     void Discovery::failed()
    00115     {
    00116         setError( i18n( "Could not find a usable proxy configuration script" ) );
    00117
    00118         // If this is the first DNS query, initialize our host name or abort
    00119         // on failure. Otherwise abort if the current domain (which was already
    00120         // queried for a host called "wpad" contains a SOA record)
    00121         bool firstQuery = m_hostname.isEmpty();
    00122         if ( ( firstQuery && !initHostName() ) ||
    00123              ( !firstQuery && !checkDomain() ) )
    00124         {
    00125             emit result( false );
    00126             return;
    00127         }
    00128
    00129         int dot = m_hostname.find( '.' );
    00130         if ( dot >= 0 )
    00131         {
    00132             m_hostname.remove( 0, dot + 1 ); // remove one domain level
    00133             download( KURL( "http://wpad." + m_hostname + "./wpad.dat" ) );
    00134         }
    00135         else emit result( false );
    00136     }
    00137
    00138     void Discovery::helperOutput()
    00139     {
    00140         m_helper->disconnect( this );
    00141         QString line;
    00142         m_helper->readln( line );
    00143         download( KURL( line.stripWhiteSpace() ) );
    00144     }
    00145 }
    00146
    00147 // vim: ts=4 sw=4 et

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:52 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003