kpac_discovery.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <unistd.h>
00022
00023 #include <kapplication.h>
00024 #include <kdebug.h>
00025 #include <kprocess.h>
00026
00027 #include "kpac_discovery.moc"
00028
00029 KPACDiscovery::KPACDiscovery()
00030 : QObject(),
00031 m_stage(DHCP)
00032 {
00033 char hostname[256];
00034 if (gethostname(hostname, 255) == 0)
00035 m_hostname = hostname;
00036 }
00037
00038 bool KPACDiscovery::tryDiscovery()
00039 {
00040 m_curl = KURL();
00041 switch (m_stage)
00042 {
00043 case DHCP:
00044 {
00045 m_stage = DNSAlias;
00046 KProcess proc;
00047 proc << "kpac_dhcp_helper";
00048 connect(&proc, SIGNAL(receivedStdout(KProcess *, char *, int)),
00049 SLOT(slotDHCPData(KProcess *, char *, int)));
00050 connect(&proc, SIGNAL(processExited(KProcess *)), SLOT(slotDone()));
00051 m_data = 0;
00052 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DHCP" << endl;
00053 if ((m_working = proc.start(KProcess::NotifyOnExit, KProcess::Stdout)))
00054 {
00055 loop();
00056 if (proc.normalExit() && proc.exitStatus() == 0)
00057 {
00058 m_curl = m_data.data();
00059 m_data = 0;
00060 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): found " << m_curl.prettyURL() << " via DHCP" << endl;
00061 return true;
00062 }
00063 }
00064 }
00065 case DNSAlias:
00066 {
00067 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DNS Alias" << endl;
00068 if (m_hostname.isEmpty())
00069 return false;
00070 else if (m_data.isEmpty())
00071 m_data = m_hostname;
00072 int pos = m_data.find('.');
00073 if (pos == -1)
00074 return false;
00075 m_data.remove(0, pos + 1);
00076 if (m_data.find('.') == -1)
00077 return false;
00078 m_curl.setProtocol("http");
00079 m_curl.setHost("wpad." + m_data + ".");
00080 m_curl.setPath("/wpad.dat");
00081 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): returning " << m_curl.prettyURL() << endl;
00082 return true;
00083 }
00084 default:
00085 return false;
00086 }
00087 }
00088
00089 void KPACDiscovery::slotDHCPData(KProcess *, char *buffer, int len)
00090 {
00091 m_data += QCString(buffer, len);
00092 }
00093
00094 void KPACDiscovery::slotDone()
00095 {
00096 m_working = false;
00097 }
00098
00099 void KPACDiscovery::loop()
00100 {
00101 while (m_working)
00102 kapp->processOneEvent();
00103 }
00104
00105
This file is part of the documentation for kdelibs Version 3.1.0.