kpac_discovery.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kpac_discovery.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (c) 2001 Malte Starostik <malte@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 // $Id: kpac_discovery.cpp,v 1.4.4.8 2003/02/25 07:39:36 adawit Exp $
    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     // If no hostname, try gethostname as a last resort.
    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         } // Fallthrough
    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); // strip till first dot inclusive
    00093             if (m_data.find('.') == -1) // only TLD left, bail out
    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 // vim: ts=4 sw=4 et

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:53 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001