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 <netdb.h>
00022 #include <unistd.h>
00023 #include <sys/utsname.h>
00024
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <kprocess.h>
00028
00029 #include "kpac_discovery.moc"
00030
00031 KPACDiscovery::KPACDiscovery()
00032 : QObject(), m_stage(DHCP)
00033 {
00034 struct utsname uts;
00035
00036 if (uname (&uts) > -1)
00037 {
00038 struct hostent *hent = gethostbyname (uts.nodename);
00039 if (hent != 0)
00040 m_hostname = hent->h_name;
00041 }
00042
00043
00044 if (m_hostname.isEmpty())
00045 {
00046 char buf [256];
00047 if (gethostname (buf, sizeof(buf)) == 0)
00048 {
00049 buf[255] = '\0';
00050 m_hostname = buf;
00051 }
00052 }
00053 }
00054
00055 bool KPACDiscovery::tryDiscovery()
00056 {
00057 m_curl = KURL();
00058 switch (m_stage)
00059 {
00060 case DHCP:
00061 {
00062 m_stage = DNSAlias;
00063 KProcess proc;
00064 proc << "kpac_dhcp_helper";
00065 connect(&proc, SIGNAL(receivedStdout(KProcess *, char *, int)),
00066 SLOT(slotDHCPData(KProcess *, char *, int)));
00067 connect(&proc, SIGNAL(processExited(KProcess *)), SLOT(slotDone()));
00068 m_data = 0;
00069 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DHCP" << endl;
00070 if ((m_working = proc.start(KProcess::NotifyOnExit, KProcess::Stdout)))
00071 {
00072 loop();
00073 if (proc.normalExit() && proc.exitStatus() == 0)
00074 {
00075 m_curl = m_data.data();
00076 m_data = 0;
00077 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): found " << m_curl.prettyURL() << " via DHCP" << endl;
00078 return true;
00079 }
00080 }
00081 }
00082 case DNSAlias:
00083 {
00084 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DNS Alias" << endl;
00085 if (m_hostname.isEmpty())
00086 return false;
00087 else if (m_data.isEmpty())
00088 m_data = m_hostname;
00089 int pos = m_data.find('.');
00090 if (pos == -1)
00091 return false;
00092 m_data.remove(0, pos + 1);
00093 if (m_data.find('.') == -1)
00094 return false;
00095 m_curl.setProtocol("http");
00096 m_curl.setHost("wpad." + m_data + ".");
00097 m_curl.setPath("/wpad.dat");
00098 kdDebug(7025) << "KPACDiscovery::tryDiscovery(): returning " << m_curl.prettyURL() << endl;
00099 return true;
00100 }
00101 default:
00102 return false;
00103 }
00104 }
00105
00106 void KPACDiscovery::slotDHCPData(KProcess *, char *buffer, int len)
00107 {
00108 m_data += QCString(buffer, len);
00109 }
00110
00111 void KPACDiscovery::slotDone()
00112 {
00113 m_working = false;
00114 }
00115
00116 void KPACDiscovery::loop()
00117 {
00118 while (m_working)
00119 kapp->processOneEvent();
00120 }
00121
00122
This file is part of the documentation for kdelibs Version 3.1.5.