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