You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.4 KiB
148 lines
4.4 KiB
15 years ago
|
/*
|
||
|
Copyright (c) 2003 Malte Starostik <malte@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
|
||
|
#include "config.h"
|
||
|
|
||
|
#include <netdb.h>
|
||
|
|
||
|
#ifdef HAVE_SYS_TYPES_H
|
||
|
#include <sys/types.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_NETINET_IN_H
|
||
|
#include <netinet/in.h>
|
||
|
#endif
|
||
|
#include <arpa/nameser.h>
|
||
|
#ifdef HAVE_ARPA_NAMESER8_COMPAT_H
|
||
|
#include <arpa/nameser8_compat.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_PARAM_H
|
||
|
// Basically, the BSDs need this before resolv.h
|
||
|
#include <sys/param.h>
|
||
|
#endif
|
||
|
#include <resolv.h>
|
||
|
#include <sys/utsname.h>
|
||
|
|
||
15 years ago
|
#include <tqtimer.h>
|
||
15 years ago
|
|
||
|
#include <klocale.h>
|
||
|
#include <kprocio.h>
|
||
|
#include <kurl.h>
|
||
|
|
||
|
#include "discovery.moc"
|
||
|
|
||
|
namespace KPAC
|
||
|
{
|
||
15 years ago
|
Discovery::Discovery( TQObject* parent )
|
||
15 years ago
|
: Downloader( parent ),
|
||
|
m_helper( new KProcIO )
|
||
|
{
|
||
15 years ago
|
connect( m_helper, TQT_SIGNAL( readReady( KProcIO* ) ), TQT_SLOT( helperOutput() ) );
|
||
12 years ago
|
connect( m_helper, TQT_SIGNAL( processExited( TDEProcess* ) ), TQT_SLOT( failed() ) );
|
||
15 years ago
|
*m_helper << "kpac_dhcp_helper";
|
||
|
|
||
|
if ( !m_helper->start() )
|
||
15 years ago
|
TQTimer::singleShot( 0, this, TQT_SLOT( failed() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
bool Discovery::initHostName()
|
||
|
{
|
||
|
struct utsname uts;
|
||
|
|
||
|
if (uname (&uts) > -1)
|
||
|
{
|
||
|
struct hostent *hent = gethostbyname (uts.nodename);
|
||
|
if (hent != 0)
|
||
15 years ago
|
m_hostname = TQString::fromLocal8Bit( hent->h_name );
|
||
15 years ago
|
}
|
||
|
|
||
|
// If no hostname, try gethostname as a last resort.
|
||
|
if (m_hostname.isEmpty())
|
||
|
{
|
||
|
char buf [256];
|
||
|
if (gethostname (buf, sizeof(buf)) == 0)
|
||
|
{
|
||
|
buf[255] = '\0';
|
||
15 years ago
|
m_hostname = TQString::fromLocal8Bit( buf );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
return !m_hostname.isEmpty();
|
||
|
}
|
||
|
|
||
|
bool Discovery::checkDomain() const
|
||
|
{
|
||
|
// If a domain has a SOA record, don't traverse any higher.
|
||
|
// Returns true if no SOA can be found (domain is "ok" to use)
|
||
|
// Stick to old resolver interface for portability reasons.
|
||
|
union
|
||
|
{
|
||
|
HEADER header;
|
||
|
unsigned char buf[ PACKETSZ ];
|
||
|
} response;
|
||
|
int len = res_query( m_hostname.local8Bit(), C_IN, T_SOA,
|
||
|
response.buf, sizeof( response.buf ) );
|
||
|
if ( len <= int( sizeof( response.header ) ) ||
|
||
|
ntohs( response.header.ancount ) != 1 ) return true;
|
||
|
unsigned char* pos = response.buf + sizeof( response.header );
|
||
|
unsigned char* end = response.buf + len;
|
||
|
// skip query section
|
||
|
pos += dn_skipname( pos, end ) + QFIXEDSZ;
|
||
|
if ( pos >= end ) return true;
|
||
|
// skip answer domain
|
||
|
pos += dn_skipname( pos, end );
|
||
|
short type;
|
||
|
GETSHORT( type, pos );
|
||
|
return type != T_SOA;
|
||
|
}
|
||
|
|
||
|
void Discovery::failed()
|
||
|
{
|
||
14 years ago
|
setError( i18n( "Could not find a usable proxy configuration script" ) );
|
||
15 years ago
|
|
||
|
// If this is the first DNS query, initialize our host name or abort
|
||
|
// on failure. Otherwise abort if the current domain (which was already
|
||
14 years ago
|
// queried for a host called "wpad" contains a SOA record)
|
||
15 years ago
|
bool firstQuery = m_hostname.isEmpty();
|
||
|
if ( ( firstQuery && !initHostName() ) ||
|
||
|
( !firstQuery && !checkDomain() ) )
|
||
|
{
|
||
|
emit result( false );
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
int dot = m_hostname.find( '.' );
|
||
15 years ago
|
if ( dot >= 0 )
|
||
|
{
|
||
|
m_hostname.remove( 0, dot + 1 ); // remove one domain level
|
||
|
download( KURL( "http://wpad." + m_hostname + "./wpad.dat" ) );
|
||
|
}
|
||
|
else emit result( false );
|
||
|
}
|
||
|
|
||
|
void Discovery::helperOutput()
|
||
|
{
|
||
|
m_helper->disconnect( this );
|
||
15 years ago
|
TQString line;
|
||
15 years ago
|
m_helper->readln( line );
|
||
|
download( KURL( line.stripWhiteSpace() ) );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// vim: ts=4 sw=4 et
|