summaryrefslogtreecommitdiff
path: root/libproxy/modules/wpad_dnsdevolution.cpp
blob: cc5aa951c8321023c7430a034239ae8f9a96581f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * libproxy - A library for proxy configuration
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#include <cstring>

#include "../module_types.hpp"
using namespace com::googlecode::libproxy;

/* The domain blacklist */
/* TODO: Make this not suck */
/* Is there a list of 'public' domains somewhere? */
static const char *blacklist[] = {
	"co.uk", "com.au",

	/* Terminator */
	NULL
};

static string
_get_fqdn()
{
#define BUFLEN 512
	char hostname[BUFLEN];

	// Zero out the buffer
	memset(hostname, 0, BUFLEN);

	// Get the hostname
	if (gethostname(hostname, BUFLEN)) return "";

	/* Lookup the hostname */
	/* TODO: Make this whole process not suck */
	struct hostent *host_info = gethostbyname(hostname);
	if (host_info)
		strncpy(hostname, host_info->h_name, BUFLEN-1);

	try { return string(hostname).substr(string(hostname).find(".")).substr(1); }
	catch (out_of_range& e) { return ""; }
}

class dnsdevolution_wpad_module : public wpad_module {
public:
	PX_MODULE_ID(NULL);

	dnsdevolution_wpad_module() {
		this->rewind();
	}

	bool found() {
		return this->lpac != NULL;
	}

	pac* next() {
		// If we have rewound start the new count
		if (this->last == "")
			this->last = _get_fqdn();

		// Get the 'next' segment
		if (this->last.find(".") == string::npos) return NULL;
		this->last = this->last.substr(this->last.find(".")+1);

		// Don't do TLD's
		if (this->last.find(".") == string::npos) return NULL;

		// Process blacklist
		for (int i=0 ; blacklist[i] ; i++)
			if (this->last == blacklist[i])
				return NULL;

		// Try to load
		try { this->lpac = new pac(url(string("http://wpad.") + this->last + "/wpad.dat")); }
		catch (parse_error& e) { }
		catch (io_error& e) { }

		return this->lpac;
	}

	void rewind() {
		this->last = "";
	}


private:
	string last;
	pac*   lpac;
};

PX_MODULE_LOAD(wpad, dnsdevolution, true);