2 conf.c -- configuration code
3 Copyright (C) 1998 Emphyrio,
4 Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* foute config read code, GPL, emphyrio 1998 */
22 /* Mutilated by me -- Ivo */
36 #include "netutl.h" /* for strtoip */
40 int timeout = 0; /* seconds before timeout */
42 typedef struct internal_config_t {
49 These are all the possible configurable values
51 static internal_config_t hazahaza[] = {
52 { "AllowConnect", allowconnect, TYPE_BOOL },
53 { "ConnectTo", upstreamip, TYPE_IP },
54 { "ConnectPort", upstreamport, TYPE_INT },
55 { "ListenPort", listenport, TYPE_INT },
56 { "MyOwnVPNIP", myvpnip, TYPE_IP },
57 { "MyVirtualIP", myvpnip, TYPE_IP }, /* an alias */
58 { "Passphrases", passphrasesdir, TYPE_NAME },
59 { "PingTimeout", pingtimeout, TYPE_INT },
60 { "TapDevice", tapdevice, TYPE_NAME },
61 { "KeyExpire", keyexpire, TYPE_INT },
62 { "ProxyMode", proxymode, TYPE_BOOL },
67 Add given value to the list of configs cfg
70 add_config_val(config_t **cfg, int argtype, char *val)
75 p = (config_t*)xmalloc(sizeof(*p));
81 p->data.val = strtol(val, &q, 0);
86 p->data.ptr = xmalloc(strlen(val) + 1);
87 strcpy(p->data.ptr, val);
90 p->data.ip = strtoip(val);
93 if(!strcasecmp("yes", val))
94 p->data.val = stupid_true;
95 else if(!strcasecmp("no", val))
96 p->data.val = stupid_false;
113 Get variable from a section in a configfile. returns -1 on failure.
116 readconfig(const char *fname, FILE *fp)
125 if(fgets(line, 80, fp) == NULL)
129 if((p = strtok(line, "\t\n\r =")) == NULL)
130 continue; /* no tokens on this line */
133 continue; /* comment: ignore */
135 for(i = 0; hazahaza[i].name != NULL; i++)
136 if(!strcasecmp(hazahaza[i].name, p))
139 if(!hazahaza[i].name)
141 fprintf(stderr, "%s: %d: Invalid variable name `%s'.\n",
146 if(((q = strtok(NULL, "\t\n\r =")) == NULL) || q[0] == '#')
148 fprintf(stderr, "%s: %d: No value given for `%s'.\n",
149 fname, lineno, hazahaza[i].name);
153 cfg = add_config_val(&config, hazahaza[i].argtype, q);
156 fprintf(stderr, "%s: %d: Invalid value `%s' for variable `%s'.\n",
157 fname, lineno, q, hazahaza[i].name);
161 cfg->which = hazahaza[i].which;
168 wrapper function for readconfig
171 read_config_file(const char *fname)
175 if((fp = fopen (fname, "r")) == NULL)
177 fprintf(stderr, "Could not open %s: %s\n", fname, sys_errlist[errno]);
181 if(readconfig(fname, fp))
190 Look up the value of the config option type
193 get_config_val(which_t type)
197 for(p = config; p != NULL; p = p->next)