declare Debian source format as 3.0 native
[debian/ipip] / config.c
1 /* config.c    config file reader
2  *
3  * $Id: config.c,v 1.3 1995/03/19 17:21:06 bdale Exp $
4  *
5  * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
6  * This software may be freely used, distributed, or modified, providing
7  * this header is not removed.
8  *
9  */
10
11 #include <stdio.h>
12 #include <memory.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <syslog.h>
16 #include <stdlib.h>
17
18 #include "ipip.h"
19
20 static void cerr();             /* General error printer */
21 static void init_config();
22 static void c_interface();
23 static void print_config();
24 struct interface ifs[MAX_IFACES];
25 int ifs_top;
26
27 static int clineno;
28 static int cerrflag;
29
30 /*
31  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
32  * open and read the interface config file. Also initialize the iface defs
33  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
34  */
35 int
36 read_config(f)
37 char *f;
38 {
39         FILE *cf;
40         char buf[256], *p;
41
42         init_config();
43
44 /* Open the file */
45         if((cf = fopen(f,"r"))==NULL){
46                 syslog(LOG_ERR, "Config file %s not found or could not be opened",f);
47                 return -1;
48         }
49
50         while(fgets(buf, 255, cf)!=NULL){
51                 clineno++;
52                 if((p = strtok(buf, " \t\n\r")) == NULL)continue;
53                 if(*p=='#' || *p==';')continue;
54
55                 if(strcmp(p,"interface")==0)c_interface();
56 /*              else if(strcmp(p,"whatever")==0)c_dowhatever(); */
57                 else cerr("Unrecognized command: %s",p);
58         }
59
60         if(ifs_top==0)cerr("No interfaces defined","");
61
62         if(debugd)print_config();
63
64         return cerrflag;
65 }
66
67 /*
68  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
69  * Initialize defaults and setup the interface structures
70  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
71  */
72 static void
73 init_config()
74 {
75         int i;
76
77         for(i=0;i<MAX_IFACES;i++){
78                 ifs[i].type = IF_TYPE_NONE;
79                 ifs[i].status = 0;
80                 ifs[i].id = NULL;
81                 ifs[i].devname = NULL;
82                 ifs[i].unit = 0;
83                 ifs[i].fd = -1;
84                 ifs[i].private = NULL;
85                 ifs[i].ifopen = NULL;
86                 ifs[i].ifread = NULL;
87                 ifs[i].ifsend = NULL;
88                 ifs[i].in = 0;
89                 ifs[i].out = 0;
90                 ifs[i].out_overruns = 0;
91                 ifs[i].martians_in = 0;
92                 ifs[i].bogus_in = 0;
93                 ifs[i].looped_in = 0;
94         }
95         ifs_top = 0;
96
97         clineno = 0;
98         cerrflag = 0;
99 }
100
101 /*
102  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
103  * Print out the config table (DEBUG)
104  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
105  */
106 static void
107 print_config()
108 {
109         int i;
110         char *id, *dev;
111
112         if(cerrflag==0) syslog(LOG_DEBUG, "%d interfaces:\n",ifs_top);
113         else syslog(LOG_DEBUG, "%d interfaces (there are errors):\n", ifs_top);
114         for(i=0;i<ifs_top;i++){
115
116                 if(ifs[i].id == NULL) id = "(none)";
117                 else id = ifs[i].id;
118
119                 if(ifs[i].devname == NULL) dev = "(none)";
120                 else dev = ifs[i].devname;
121
122                 if(ifs[i].type == IF_TYPE_NONE){
123                         syslog(LOG_DEBUG,"interface %-4s  type NONE  devicename %s  unit %d\n",
124                                 id, dev, ifs[i].unit);
125                 } else if(ifs[i].type == IF_TYPE_SLIP){
126                         syslog(LOG_DEBUG,"interface %-4s  type slip  devicename %s  speed %d\n",
127                                 id, dev, ifs[i].unit);
128                 } else if(ifs[i].type == IF_TYPE_TUN){
129                         syslog(LOG_DEBUG,"interface %-4s  type tun  devicename %s\n",
130                                 id, dev);
131                 } else if(ifs[i].type == IF_TYPE_IPUDP){
132                         syslog(LOG_DEBUG,"interface %-4s  type udp  port %d\n",
133                                 id, ifs[i].unit);
134                 } else if(ifs[i].type == IF_TYPE_IPIP){
135                         syslog(LOG_DEBUG,"interface %-4s  type ip  protocol id %d\n",
136                                 id, ifs[i].unit);
137                 } else {
138                         syslog(LOG_DEBUG,"interface %-4s  type UNKNOWN  devicename %s  unit %d\n",
139                                 id, dev, ifs[i].unit);
140                 }
141         }
142 }
143
144 /*
145  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
146  * Print a general config file error
147  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
148  */
149 static void
150 cerr(s,a)
151 char *s;
152 char *a;
153 {
154         syslog(LOG_ERR,"Config file error at line %d:\n",clineno);
155         (void)fprintf(stderr,s,a);
156         (void)fprintf(stderr,"\n");
157         cerrflag--;
158 }
159
160 /*
161  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
162  * Handle the "define" command
163  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
164  */
165 static void
166 c_interface()
167 {
168         int syntax;
169         char *q;
170
171         if(ifs_top>=MAX_IFACES){
172                 cerr("Too many interfaces defined","");
173                 return;
174         }
175
176         syntax = 0;
177
178         if((q = strtok((char *)NULL, " \t\n\r"))){
179                 ifs[ifs_top].id = strdup(q);
180         } else syntax++;
181
182         if((q = strtok((char *)NULL, " \t\n\r"))){
183                 if(strcmp(q,"tunnel")==0){
184                         ifs[ifs_top].type = IF_TYPE_TUN;
185                         ifs[ifs_top].ifopen = tun_open;
186                         ifs[ifs_top].ifread = tun_read;
187                         ifs[ifs_top].ifsend = tun_send;
188                 } else if(strcmp(q,"slip")==0){
189                         ifs[ifs_top].type = IF_TYPE_SLIP;
190                         ifs[ifs_top].ifopen = slip_open;
191                         ifs[ifs_top].ifread = slip_read;
192                         ifs[ifs_top].ifsend = slip_send;
193                 } else if(strcmp(q,"ip")==0){
194                         ifs[ifs_top].type = IF_TYPE_IPIP;
195                         ifs[ifs_top].ifopen = ip_open;
196                         ifs[ifs_top].ifread = ip_read;
197                         ifs[ifs_top].ifsend = ip_send;
198                 } else if(strcmp(q,"udp")==0){
199                         ifs[ifs_top].type = IF_TYPE_IPUDP;
200                         ifs[ifs_top].ifopen = ip_open;
201                         ifs[ifs_top].ifread = ip_read;
202                         ifs[ifs_top].ifsend = ip_send;
203                 } else cerr("Bad interface type: %s",q);
204         } else syntax++;
205
206         if((q = strtok((char *)NULL, " \t\n\r"))){
207                 ifs[ifs_top].devname = strdup(q);
208         } else syntax++;
209
210         if((q = strtok((char *)NULL, " \t\n\r"))){
211                 ifs[ifs_top].unit = atoi(q);
212         } else syntax++;
213
214         if(syntax)cerr("Syntax error (interface <name> <type> <device> <int>","");
215
216         ifs_top++;
217 }