• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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
57namespace KPAC
58{
59 Discovery::Discovery( QObject* parent )
60 : Downloader( parent ),
61 m_helper( new KProcess(this) )
62 {
63 m_helper->setOutputChannelMode(KProcess::SeparateChannels);
64 connect( m_helper, SIGNAL(readyReadStandardOutput()), SLOT(helperOutput()) );
65 connect( m_helper, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(failed()) );
66 *m_helper << KStandardDirs::findExe("kpac_dhcp_helper");
67 m_helper->start();
68 if ( !m_helper->waitForStarted() )
69 QTimer::singleShot( 0, this, SLOT(failed()) );
70 }
71
72 bool Discovery::initDomainName()
73 {
74 m_domainName = QHostInfo::localDomainName();
75 return !m_domainName.isEmpty();
76 }
77
78 bool Discovery::checkDomain() const
79 {
80 // If a domain has a SOA record, don't traverse any higher.
81 // Returns true if no SOA can be found (domain is "ok" to use)
82 // Stick to old resolver interface for portability reasons.
83 union
84 {
85 HEADER header;
86 unsigned char buf[ PACKETSZ ];
87 } response;
88
89 int len = res_query( m_domainName.toLocal8Bit(), C_IN, T_SOA,
90 response.buf, sizeof( response.buf ) );
91 if ( len <= int( sizeof( response.header ) ) ||
92 ntohs( response.header.ancount ) != 1 )
93 return true;
94
95 unsigned char* pos = response.buf + sizeof( response.header );
96 unsigned char* end = response.buf + len;
97
98 // skip query section
99 pos += dn_skipname( pos, end ) + QFIXEDSZ;
100 if ( pos >= end )
101 return true;
102
103 // skip answer domain
104 pos += dn_skipname( pos, end );
105 short type;
106 GETSHORT( type, pos );
107 return type != T_SOA;
108 }
109
110 void Discovery::failed()
111 {
112 setError( i18n( "Could not find a usable proxy configuration script" ) );
113
114 // If this is the first DNS query, initialize our host name or abort
115 // on failure. Otherwise abort if the current domain (which was already
116 // queried for a host called "wpad" contains a SOA record)
117 const bool firstQuery = m_domainName.isEmpty();
118 if ( ( firstQuery && !initDomainName() ) ||
119 ( !firstQuery && !checkDomain() ) )
120 {
121 emit result( false );
122 return;
123 }
124
125 const int dot = m_domainName.indexOf( '.' );
126 if ( dot > -1 || firstQuery )
127 {
128 QString address (QLatin1String("http://wpad."));
129 address += m_domainName;
130 address += QLatin1String ("/wpad.dat");
131 if ( dot > -1 )
132 m_domainName.remove (0, dot + 1); // remove one domain level
133 download( KUrl(address) );
134 return;
135 }
136
137 emit result( false );
138 }
139
140 void Discovery::helperOutput()
141 {
142 m_helper->disconnect( this );
143 const QByteArray line = m_helper->readLine();
144 const KUrl url( QString::fromLocal8Bit(line.constData(), line.length()).trimmed() );
145 download( url );
146 }
147}
148
149// vim: ts=4 sw=4 et
KPAC::Discovery::Discovery
Discovery(QObject *)
Definition: discovery.cpp:59
KPAC::Discovery::failed
virtual void failed()
Definition: discovery.cpp:110
KPAC::Downloader
Definition: downloader.h:34
KPAC::Downloader::result
void result(bool)
KPAC::Downloader::download
void download(const KUrl &)
Definition: downloader.cpp:39
KPAC::Downloader::setError
void setError(const QString &)
Definition: downloader.cpp:58
KProcess
KProcess::start
void start()
KProcess::SeparateChannels
SeparateChannels
KProcess::setOutputChannelMode
void setOutputChannelMode(OutputChannelMode mode)
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
KUrl
QObject
header
const char header[]
klocale.h
i18n
QString i18n(const char *text)
kprocess.h
kstandarddirs.h
kurl.h
KPAC
Definition: discovery.cpp:58
end
const KShortcut & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

Skip menu "kdelibs-4.14.38 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