• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KIO

discovery.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 
00021 #include <config.h>
00022 
00023 #include <netdb.h>
00024 #include <unistd.h>
00025 
00026 #ifdef HAVE_SYS_TYPES_H
00027 #include <sys/types.h>
00028 #endif
00029 #ifdef HAVE_NETINET_IN_H
00030 #include <netinet/in.h>
00031 #endif
00032 #include <arpa/nameser.h>
00033 #ifdef HAVE_ARPA_NAMESER8_COMPAT_H
00034 #include <arpa/nameser8_compat.h>
00035 #else
00036 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
00037 #include <arpa/nameser_compat.h>
00038 #endif
00039 #endif
00040 #ifdef HAVE_SYS_PARAM_H
00041 // Basically, the BSDs need this before resolv.h
00042 #include <sys/param.h>
00043 #endif
00044 
00045 #include <resolv.h>
00046 #include <sys/utsname.h>
00047 
00048 #include <QtCore/QTimer>
00049 #include <QtNetwork/QHostInfo>
00050 
00051 #include <klocale.h>
00052 #include <kurl.h>
00053 #include <kstandarddirs.h>
00054 #include <kprocess.h>
00055 #include "discovery.moc"
00056 
00057 namespace KPAC
00058 {
00059     Discovery::Discovery( QObject* parent )
00060         : Downloader( parent ),
00061           m_helper( new KProcess(this) )
00062     {
00063         connect( m_helper, SIGNAL( readyReadStandardOutput() ), SLOT( helperOutput() ) );
00064         connect( m_helper, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( failed() ) );
00065         *m_helper << KStandardDirs::findExe("kpac_dhcp_helper");
00066         m_helper->start();
00067         if ( !m_helper->waitForStarted() )
00068             QTimer::singleShot( 0, this, SLOT( failed() ) );
00069     }
00070 
00071     bool Discovery::initDomainName()
00072     {
00073         m_domainName = QHostInfo::localDomainName();
00074         return !m_domainName.isEmpty();
00075     }
00076 
00077     bool Discovery::checkDomain() const
00078     {
00079         // If a domain has a SOA record, don't traverse any higher.
00080         // Returns true if no SOA can be found (domain is "ok" to use)
00081         // Stick to old resolver interface for portability reasons.
00082         union
00083         {
00084             HEADER header;
00085             unsigned char buf[ PACKETSZ ];
00086         } response;
00087 
00088         int len = res_query( m_domainName.toLocal8Bit(), C_IN, T_SOA,
00089                              response.buf, sizeof( response.buf ) );
00090         if ( len <= int( sizeof( response.header ) ) ||
00091              ntohs( response.header.ancount ) != 1 )
00092             return true;
00093 
00094         unsigned char* pos = response.buf + sizeof( response.header );
00095         unsigned char* end = response.buf + len;
00096 
00097         // skip query section
00098         pos += dn_skipname( pos, end ) + QFIXEDSZ;
00099         if ( pos >= end )
00100             return true;
00101 
00102         // skip answer domain
00103         pos += dn_skipname( pos, end );
00104         short type;
00105         GETSHORT( type, pos );
00106         return type != T_SOA;
00107     }
00108 
00109     void Discovery::failed()
00110     {
00111         setError( i18n( "Could not find a usable proxy configuration script" ) );
00112 
00113         // If this is the first DNS query, initialize our host name or abort
00114         // on failure. Otherwise abort if the current domain (which was already
00115         // queried for a host called "wpad" contains a SOA record)
00116         const bool firstQuery = m_domainName.isEmpty();
00117         if ( ( firstQuery && !initDomainName() ) ||
00118              ( !firstQuery && !checkDomain() ) )
00119         {
00120             emit result( false );
00121             return;
00122         }
00123 
00124         const int dot = m_domainName.indexOf( '.' );
00125         if ( dot >= 0 )
00126         {
00127             KUrl url( QLatin1String("http://wpad.") + m_domainName + QLatin1String("/wpad.dat") );
00128             m_domainName.remove( 0, dot + 1 ); // remove one domain level
00129             download( url );
00130             return;
00131         }
00132 
00133         emit result( false );
00134     }
00135 
00136     void Discovery::helperOutput()
00137     {
00138         m_helper->disconnect( this );
00139         const QByteArray line = m_helper->readLine();
00140         const KUrl url( QString::fromLocal8Bit(line.constData(), line.length()).trimmed() );
00141         download( url );
00142     }
00143 }
00144 
00145 // vim: ts=4 sw=4 et

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal