2 * Code for reading and writing the disabled protocols file.
4 * $Id: disabled_protos.c,v 1.3 2003/11/16 23:17:15 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include <epan/filesystem.h>
41 #include <epan/proto.h>
43 #include "disabled_protos.h"
45 #define PROTOCOLS_FILE_NAME "disabled_protos"
48 * List of disabled protocols
50 static GList *disabled_protos = NULL;
52 #define INIT_BUF_SIZE 128
55 * Read in a list of disabled protocols.
57 * On success, "*pref_path_return" is set to NULL.
58 * On error, "*pref_path_return" is set to point to the pathname of
59 * the file we tried to read - it should be freed by our caller -
60 * and "*open_errno_return" is set to the error if an open failed
61 * or "*read_errno_return" is set to the error if a read failed.
65 read_disabled_protos_list(char **pref_path_return, int *open_errno_return,
66 int *read_errno_return)
68 char *ff_path, *ff_name;
79 *pref_path_return = NULL; /* assume no error */
81 ff_name = PROTOCOLS_FILE_NAME;
82 flp = &disabled_protos;
84 /* To do: generalize this */
85 ff_path = get_persconffile_path(ff_name, FALSE);
86 if ((ff = fopen(ff_path, "r")) == NULL) {
88 * Did that fail because we the file didn't exist?
90 if (errno != ENOENT) {
94 *pref_path_return = ff_path;
95 *open_errno_return = errno;
96 *read_errno_return = 0;
101 * Yes. See if there's a "protocols" file; if so, read it.
104 ff_path = get_persconffile_path(PROTOCOLS_FILE_NAME, FALSE);
105 if ((ff = fopen(ff_path, "r")) == NULL) {
107 * Well, that didn't work, either. Just give up.
108 * Return an error if the file existed but we couldn't open it.
110 if (errno != ENOENT) {
111 *pref_path_return = ff_path;
112 *open_errno_return = errno;
113 *read_errno_return = 0;
119 /* If we already have a list of protocols, discard it. */
121 fl_ent = g_list_first(*flp);
122 while (fl_ent != NULL) {
123 prot = (protocol_def *) fl_ent->data;
126 fl_ent = fl_ent->next;
132 /* Allocate the protocol name buffer. */
133 prot_name_len = INIT_BUF_SIZE;
134 prot_name = g_malloc(prot_name_len + 1);
136 for (line = 1; ; line++) {
137 /* Lines in a disabled protocol file contain the "filter name" of
138 a protocol to be disabled. */
140 /* Skip over leading white space, if any. */
141 while ((c = getc(ff)) != EOF && isspace(c)) {
150 goto error; /* I/O error */
152 break; /* Nothing more to read */
154 ungetc(c, ff); /* Unread the non-white-space character. */
156 /* Get the name of the protocol. */
161 break; /* End of file, or I/O error */
163 break; /* Trailing white space, or end of line. */
165 break; /* Start of comment, running to end of line. */
166 /* Add this character to the protocol name string. */
167 if (prot_name_index >= prot_name_len) {
168 /* protocol name buffer isn't long enough; double its length. */
170 prot_name = g_realloc(prot_name, prot_name_len + 1);
172 prot_name[prot_name_index] = c;
176 if (isspace(c) && c != '\n') {
177 /* Skip over trailing white space. */
178 while ((c = getc(ff)) != EOF && c != '\n' && isspace(c))
180 if (c != EOF && c != '\n' && c != '#') {
181 /* Non-white-space after the protocol name; warn about it,
182 in case we come up with a reason to use it. */
183 g_warning("'%s' line %d has extra stuff after the protocol name.",
187 if (c != EOF && c != '\n') {
188 /* Skip to end of line. */
189 while ((c = getc(ff)) != EOF && c != '\n')
195 goto error; /* I/O error */
197 /* EOF, not error; no newline seen before EOF */
198 g_warning("'%s' line %d doesn't have a newline.", ff_path,
201 break; /* nothing more to read */
204 /* Null-terminate the protocol name. */
205 if (prot_name_index >= prot_name_len) {
206 /* protocol name buffer isn't long enough; double its length. */
208 prot_name = g_realloc(prot_name, prot_name_len + 1);
210 prot_name[prot_name_index] = '\0';
212 /* Add the new protocol to the list of disabled protocols */
213 prot = (protocol_def *) g_malloc(sizeof(protocol_def));
214 prot->name = g_strdup(prot_name);
215 *flp = g_list_append(*flp, prot);
223 *pref_path_return = ff_path;
224 *open_errno_return = 0;
225 *read_errno_return = errno;
230 * Disable protocols as per the stored configuration
233 set_disabled_protos_list(void)
240 * assume all protocols are enabled by default
242 if (disabled_protos == NULL)
243 return; /* nothing to disable */
245 fl_ent = g_list_first(disabled_protos);
247 while (fl_ent != NULL) {
248 prot = (protocol_def *) fl_ent->data;
249 i = proto_get_id_by_filter_name(prot->name);
251 /* XXX - complain here? */
253 if (proto_can_disable_protocol(i))
254 proto_set_decoding(i, FALSE);
257 fl_ent = fl_ent->next;
262 * Write out a list of disabled protocols.
264 * On success, "*pref_path_return" is set to NULL.
265 * On error, "*pref_path_return" is set to point to the pathname of
266 * the file we tried to read - it should be freed by our caller -
267 * and "*errno_return" is set to the error.
270 save_disabled_protos_list(char **pref_path_return, int *errno_return)
272 gchar *ff_path, *ff_path_new, *ff_name;
275 protocol_t *protocol;
278 *pref_path_return = NULL; /* assume no error */
280 ff_name = PROTOCOLS_FILE_NAME;
282 ff_path = get_persconffile_path(ff_name, TRUE);
284 /* Write to "XXX.new", and rename if that succeeds.
285 That means we don't trash the file if we fail to write it out
287 ff_path_new = (gchar *) g_malloc(strlen(ff_path) + 5);
288 sprintf(ff_path_new, "%s.new", ff_path);
290 if ((ff = fopen(ff_path_new, "w")) == NULL) {
291 *pref_path_return = ff_path;
292 *errno_return = errno;
297 /* Iterate over all the protocols */
299 for (i = proto_get_first_protocol(&cookie); i != -1;
300 i = proto_get_next_protocol(&cookie)) {
302 if (!proto_can_disable_protocol(i)) {
306 protocol = find_protocol_by_id(i);
307 if (proto_is_protocol_enabled(protocol)) {
311 /* Write out the protocol name. */
312 fprintf(ff, "%s\n", proto_get_protocol_filter_name(i));
315 if (fclose(ff) == EOF) {
316 *pref_path_return = ff_path;
317 *errno_return = errno;
324 /* ANSI C doesn't say whether "rename()" removes the target if it
325 exists; the Win32 call to rename files doesn't do so, which I
326 infer is the reason why the MSVC++ "rename()" doesn't do so.
327 We must therefore remove the target file first, on Windows. */
328 if (remove(ff_path) < 0 && errno != ENOENT) {
329 /* It failed for some reason other than "it's not there"; if
330 it's not there, we don't need to remove it, so we just
332 *pref_path_return = ff_path;
333 *errno_return = errno;
340 if (rename(ff_path_new, ff_path) < 0) {
341 *pref_path_return = ff_path;
342 *errno_return = errno;