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

KIO

  • kio
  • misc
  • kpac
discovery.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2003 Malte Starostik <malte@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 
21 #include <config.h>
22 #include <config-kpac.h>
23 #include <netdb.h>
24 #include <unistd.h>
25 
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32 #include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER8_COMPAT_H
34 #include <arpa/nameser8_compat.h>
35 #else
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 #include <arpa/nameser_compat.h>
38 #endif
39 #endif
40 #ifdef HAVE_SYS_PARAM_H
41 // Basically, the BSDs need this before resolv.h
42 #include <sys/param.h>
43 #endif
44 
45 #include <resolv.h>
46 #include <sys/utsname.h>
47 
48 #include <QtCore/QTimer>
49 #include <QtNetwork/QHostInfo>
50 
51 #include <klocale.h>
52 #include <kurl.h>
53 #include <kstandarddirs.h>
54 #include <kprocess.h>
55 #include "discovery.moc"
56 
57 namespace KPAC
58 {
59  Discovery::Discovery( QObject* parent )
60  : Downloader( parent ),
61  m_helper( new KProcess(this) )
62  {
63  connect( m_helper, SIGNAL(readyReadStandardOutput()), SLOT(helperOutput()) );
64  connect( m_helper, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(failed()) );
65  *m_helper << KStandardDirs::findExe("kpac_dhcp_helper");
66  m_helper->start();
67  if ( !m_helper->waitForStarted() )
68  QTimer::singleShot( 0, this, SLOT(failed()) );
69  }
70 
71  bool Discovery::initDomainName()
72  {
73  m_domainName = QHostInfo::localDomainName();
74  return !m_domainName.isEmpty();
75  }
76 
77  bool Discovery::checkDomain() const
78  {
79  // If a domain has a SOA record, don't traverse any higher.
80  // Returns true if no SOA can be found (domain is "ok" to use)
81  // Stick to old resolver interface for portability reasons.
82  union
83  {
84  HEADER header;
85  unsigned char buf[ PACKETSZ ];
86  } response;
87 
88  int len = res_query( m_domainName.toLocal8Bit(), C_IN, T_SOA,
89  response.buf, sizeof( response.buf ) );
90  if ( len <= int( sizeof( response.header ) ) ||
91  ntohs( response.header.ancount ) != 1 )
92  return true;
93 
94  unsigned char* pos = response.buf + sizeof( response.header );
95  unsigned char* end = response.buf + len;
96 
97  // skip query section
98  pos += dn_skipname( pos, end ) + QFIXEDSZ;
99  if ( pos >= end )
100  return true;
101 
102  // skip answer domain
103  pos += dn_skipname( pos, end );
104  short type;
105  GETSHORT( type, pos );
106  return type != T_SOA;
107  }
108 
109  void Discovery::failed()
110  {
111  setError( i18n( "Could not find a usable proxy configuration script" ) );
112 
113  // If this is the first DNS query, initialize our host name or abort
114  // on failure. Otherwise abort if the current domain (which was already
115  // queried for a host called "wpad" contains a SOA record)
116  const bool firstQuery = m_domainName.isEmpty();
117  if ( ( firstQuery && !initDomainName() ) ||
118  ( !firstQuery && !checkDomain() ) )
119  {
120  emit result( false );
121  return;
122  }
123 
124  const int dot = m_domainName.indexOf( '.' );
125  if ( dot >= 0 )
126  {
127  KUrl url( QLatin1String("http://wpad.") + m_domainName + QLatin1String("/wpad.dat") );
128  m_domainName.remove( 0, dot + 1 ); // remove one domain level
129  download( url );
130  return;
131  }
132 
133  emit result( false );
134  }
135 
136  void Discovery::helperOutput()
137  {
138  m_helper->disconnect( this );
139  const QByteArray line = m_helper->readLine();
140  const KUrl url( QString::fromLocal8Bit(line.constData(), line.length()).trimmed() );
141  download( url );
142  }
143 }
144 
145 // vim: ts=4 sw=4 et
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Feb 9 2013 12:13:31 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

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-4.10.0 API Reference

Skip menu "kdelibs-4.10.0 API Reference"
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal