00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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
00091
00092
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
00105 pos += dn_skipname( pos, end ) + QFIXEDSZ;
00106
if ( pos >=
end )
return true;
00107
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
00119
00120
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 );
00133 download(
"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( line.
stripWhiteSpace() );
00144 }
00145 }
00146
00147