~ubuntu-branches/ubuntu/karmic/gtk-gnutella/karmic

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (c) 2002, Vidar Madsen
 *
 *----------------------------------------------------------------------
 * This file is part of gtk-gnutella.
 *
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  gtk-gnutella 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gtk-gnutella; if not, write to the Free Software
 *  Foundation, Inc.:
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *----------------------------------------------------------------------
 */

/**
 * @ingroup core
 * @file
 *
 * Needs brief description here.
 *
 * Functions for keeping a whitelist of nodes we always allow in,
 * and whom we try to keep a connection to.
 *
 * @author Vidar Madsen
 * @date 2002
 */

#include "common.h"

RCSID("$Id: whitelist.c,v 1.6 2005/06/29 14:24:25 daichik Exp $");

#include "whitelist.h"
#include "settings.h"
#include "nodes.h"

#include "lib/file.h"
#include "lib/glib-missing.h"
#include "lib/override.h"		/* Must be the last header included */

static GSList *sl_whitelist = NULL;

static const gchar whitelist_file[] = "whitelist";
static time_t whitelist_mtime, whitelist_checked;
static gchar *whitelist_path = NULL;

/**
 * whitelist_retrieve
 *
 * Loads the whitelist into memory.
 */
static void whitelist_retrieve(void)
{
    gchar line[1024];
    gchar *p, *sport, *snetmask;
    guint32 ip, port, netmask;
    struct whitelist *n;
    FILE *f;
    struct stat st;
    int linenum = 0;
	file_path_t fp[1];

	file_path_set(fp, settings_config_dir(), whitelist_file);
	f = file_config_open_read_norename("Host Whitelist", fp, G_N_ELEMENTS(fp));
	if (!f)
		return;

	if (fstat(fileno(f), &st)) {
		g_warning("whitelist_retrieve: fstat() failed: %s", g_strerror(errno));
		fclose(f);
		return;
	}
    whitelist_checked = time(NULL);
    whitelist_mtime = st.st_mtime;

    while (fgets(line, sizeof(line), f)) {
        linenum++;
        if (*line == '#') continue;

		/* Remove trailing spaces so that lines that contain spaces only
		 * are ignored and cause no warnings. */
		p = strchr(line, '\0');
        while (--p >= line) {
			if (!is_ascii_space((guchar) *p))
				break;
           	*p = '\0';
		}

        if ('\0' == *line)
            continue;

        sport = snetmask = NULL;

        if ((p = strchr(line, '/')) != NULL) {
            *p = '\0';
            snetmask = ++p;
        }
        if ((p = strchr(line, ':')) != NULL) {
            *p = '\0';
            sport = ++p;
        }

        ip = host_to_ip(line);
        if (!ip) {
            g_warning("whitelist_retrieve(): "
				"Line %d: Invalid IP \"%s\"", linenum, line);
            continue;
        }

        if (sport)
            port = atol(sport);
        else
            port = 0;

        netmask = 0xffffffffU; /* Default mask */
        if (snetmask) {
            if (strchr(snetmask, '.')) {
                netmask = gchar_to_ip(snetmask);
            	if (!netmask) {
                	netmask = 0xffffffff;
                	g_warning("whitelist_retrieve(): "
						"Line %d: Invalid netmask \"%s\", "
						"using 255.255.255.255 instead.", linenum, snetmask);
            	}
            } else {
				gint error;
				gulong v;

				v = gm_atoul(snetmask, NULL, &error);
                if (!error && v > 0 && v <= 32) {
                	netmask = ~(0xffffffffU >> v);
				} else {
					g_warning("whitelist_retrieve(): "
						"Line %d: Invalid netmask \"%s\", "
						"using /32 instead.", linenum, snetmask);
				}
            }
		}

        n = g_malloc0(sizeof(*n));
        n->ip = ip;
        n->port = port;
        n->netmask = netmask;

        sl_whitelist = g_slist_prepend(sl_whitelist, n);
    }

    sl_whitelist = g_slist_reverse(sl_whitelist);
	fclose(f);
}

/**
 * whitelist_connect
 *
 * Attempts to connect to the nodes we have whitelisted.
 * Only entries with a specified port will be tried.
 *
 * @returns the number of new nodes that are connected to.
 */
guint whitelist_connect(void)
{
    GSList *sl;
    struct whitelist *n;
    time_t now = time(NULL);
    guint num = 0;

    for (sl = sl_whitelist; sl; sl = g_slist_next(sl)) {
        n = sl->data;
        if (!n->port)
            continue;
        if (node_is_connected(n->ip, n->port, TRUE))
            continue;

        if (delta_time(now, n->last_try) > WHITELIST_RETRY_DELAY) {
            n->last_try = now;
            node_add(n->ip, n->port);
            num++;
        }
    }
    return num;
}

/**
 * whitelist_init
 *
 * Called on startup. Loads the whitelist into memory.
 */
void whitelist_init(void)
{
	whitelist_path = make_pathname(settings_config_dir(), whitelist_file);
    whitelist_retrieve();
}

/**
 * whitelist_close
 *
 * Frees all entries in the whitelist.
 */
void whitelist_close(void)
{
    GSList *sl;

    for (sl = sl_whitelist; sl; sl = g_slist_next(sl))
        g_free(sl->data);

    g_slist_free(sl_whitelist);
    sl_whitelist = NULL;
	G_FREE_NULL(whitelist_path);
}

/**
 * whitelist_reload
 *
 * Reloads the whitelist.
 */
void whitelist_reload(void)
{
    whitelist_close();
    whitelist_retrieve();
}

/**
 * whitelist_check
 *
 * Check the given IP agains the entries in the whitelist.
 * @returns TRUE if found, and FALSE if not.
 *
 * Also, it will peridically check the whitelist file for
 * updates, and reload it if it has changed.
 */
gboolean whitelist_check(guint32 ip)
{
    struct whitelist *n;
    time_t now = time(NULL);
    GSList *sl;

    /* Check if the file has changed on disk, and reload it if necessary. */
    if (delta_time(now, whitelist_checked) > WHITELIST_CHECK_INTERVAL) {
        struct stat st;

        whitelist_checked = now;

        if (NULL != whitelist_path && 0 == stat(whitelist_path, &st)) {
            if (st.st_mtime != whitelist_mtime) {
                g_warning("whitelist_check(): "
					"Whitelist changed on disk. Reloading.");
                whitelist_reload();
            }
        }
    }

    for (sl = sl_whitelist; sl; sl = g_slist_next(sl)) {
        n = sl->data;
        if ((ip & n->netmask) == (n->ip & n->netmask))
            return TRUE;
    }

    return FALSE;
}

/* vi: set ts=4: */