New revision of the Tunnel6 client with improvements and routed prefix support
[tunnel6.git] / client / src / config.c
blob5328a3145f04693b12fcb523da03ddc08ec6d1a6
1 /*
2 * tunnel6
3 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include "config.h"
24 #include "defaults.h"
26 config_t config; /* structure with config values */
28 int config_parse (char *var, char *val)
30 if (!strcasecmp (var, CONFIG_VAR_NAME)) {
31 config.name = strdup (val);
32 return 0;
35 if (!strcasecmp (var, CONFIG_VAR_PWD)) {
36 config.pwd = strdup (val);
37 return 0;
40 if (!strcasecmp (var, CONFIG_VAR_SERVER)) {
41 config.server = strdup (val);
42 return 0;
45 if (!strcasecmp (var, CONFIG_VAR_PORT)) {
46 config.port = (unsigned short) atoi (strdup (val));
47 return 0;
50 return -1;
53 /* load config file */
54 int config_load (char *filename)
56 unsigned l, line = 0;
57 char *buffer, *c;
58 char var[256];
59 char val[256];
61 /* open specified file */
62 FILE *fp = fopen (filename, "r");
64 if (!fp) {
65 printf ("> WARNING -> config file '%s' not found\n", DEFAULT_CONFIG_PATH);
66 return -1;
69 buffer = (char *) calloc (600, sizeof (char));
71 if (!buffer)
72 return -1;
74 while (!feof (fp)) {
75 /* read line */
76 char *r = fgets (buffer, 600, fp);
78 if (!r)
79 continue;
81 line ++;
83 l = strlen (buffer);
85 if (l < 8)
86 continue;
88 if (buffer[l - 1] == '\n')
89 buffer[l -- - 1] = '\0';
91 /* move to first valid letter */
92 for (c = buffer; *c && *c == ' '; c ++);
94 /* allow comments */
95 if (*c == '#')
96 continue;
98 /* parse line */
99 sscanf (c, "%s %s", var, val);
101 config_parse (var, val);
103 /* clear line */
104 memset (buffer, 0, l);
107 free (buffer);
108 fclose (fp);
110 return 0;
113 int config_init ()
115 memset (&config, 0, sizeof (config_t));
117 int ret = config_load (DEFAULT_CONFIG_PATH);
119 if (!ret && (!config.name || !config.pwd || !config.server)) {
120 printf ("ERROR -> config file is not valid !\nPlease rewrite " DEFAULT_CONFIG_PATH " in this way:\n\n"
121 "\tname YOURNAME\n"
122 "\tpassword YOURPASSWORD\n"
123 "\tserver TUNNELSERVER\n"
124 "\tport 6060\n");
126 return -1;
129 return ret;