-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
375 lines (345 loc) · 11.2 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* config.c */
#include "packman.h"
char *unpackname= "UNPACK=";
char *packname= "PACK=";
char *filesname= "FILES=";
char *outboundname= "OUTBOUND=";
char *inboundname= "INBOUND=";
char *verbosename= "VERBOSE=";
char *loglevelname= "LOGLEVEL=";
char *logfilename= "LOGFILE=";
char *backupname= "BACKUP=";
char *tearlinename= "TEARLINE=";
char *originname= "ORIGIN=";
/* Reads the configuration and puts it in the config structure */
int readconfig(char *name) {
FILE *cofg;
char line[80];
char file[80];
char *pos1,*pos2;
int len;
/* Initialize config */
cfg.PACK=NULL;
cfg.UNPACK=NULL;
cfg.FILES=NULL;
cfg.INBOUND=NULL;
cfg.OUTBOUND=NULL;
cfg.VERBOSE=0;
cfg.LOGLEVEL=0;
cfg.LOGFILE=NULL;
cfg.BACKUP=NULL;
cfg.TEARLINE=NULL;
cfg.ORIGIN=NULL;
/* Open config file */
strcpy(file,name);
strcat(file,".cfg");
if ((cofg=fopen(file,"r"))==NULL) {
fprintf(stderr,"%s : Can't open config file <%s>\n",progname,file);
return(-1);
} else {
/* Get config lines */
while (fgets(line,80,cofg)!=NULL) {
/* Lines preceded with ; and empty lines are skipped */
if (line[0]!=';' && strcmp(line,"\n")!=0) {
if (strncmp(line,unpackname,strlen(unpackname))==0) {
/* UNPACK keyword (archiver to used for unpacking) */
pos1=pos2=line+strlen(unpackname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.UNPACK=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.UNPACK,pos1,len);
cfg.UNPACK[len]='\0';
} else if (strncmp(line,packname,strlen(packname))==0) {
/* PACK keyword (archiver to use for packing) */
pos1=pos2=line+strlen(packname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.PACK=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.PACK,pos1,len);
cfg.PACK[len]='\0';
} else if (strncmp(line,filesname,strlen(filesname))==0) {
/* FILES keyword (Location to where .log .tab .krl and .ksl files are) */
pos1=pos2=line+strlen(filesname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.FILES=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.FILES,pos1,len);
cfg.FILES[len]='\0';
} else if (strncmp(line,outboundname,strlen(outboundname))==0) {
/* OUTBOUND keyword (location of .pkt files while packing) */
pos1=pos2=line+strlen(outboundname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.OUTBOUND=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.OUTBOUND,pos1,len);
cfg.OUTBOUND[len]='\0';
} else if (strncmp(line,inboundname,strlen(inboundname))==0) {
/* INBOUND keyword (location of .pkt files while extracting) */
pos1=pos2=line+strlen(inboundname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.INBOUND=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.INBOUND,pos1,len);
cfg.INBOUND[len]='\0';
} else if (strncmp(line,verbosename,strlen(verbosename))==0) {
/* VERBOSE keyword (Level at which messages are printed) */
pos1=line+strlen(verbosename);
if (getnum(&len,&pos1)==-1) {
fprintf(stderr,"%s : VERBOSE level is not a number\n",progname);
return(-1);
} else {
cfg.VERBOSE=len;
}
} else if (strncmp(line,loglevelname,strlen(loglevelname))==0) {
/* LOGLEVEL keyword (Level at which message are written to the logfile) NOTUSED */
pos1=line+strlen(loglevelname);
if (getnum(&len,&pos1)==-1) {
fprintf(stderr,"%s : LOGLEVEL level is not a number\n",progname);
return(-1);
} else {
cfg.LOGLEVEL=len;
}
} else if (strncmp(line,logfilename,strlen(logfilename))==0) {
/* LOGFILE keyword (Name of logfile EMPTY is no logging) */
pos1=pos2=line+strlen(logfilename);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
if (len!=0) {
cfg.LOGFILE=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.LOGFILE,pos1,len);
cfg.LOGFILE[len]='\0';
}
} else if (strncmp(line,backupname,strlen(backupname))==0) {
/* BACKUP keyword (Location to where .pkt files will be written */
pos1=pos2=line+strlen(backupname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.BACKUP=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.BACKUP,pos1,len);
cfg.BACKUP[len]='\0';
} else if (strncmp(line,tearlinename,strlen(tearlinename))==0) {
/* TEARLINE keyword (Tearline to be used in messages) */
pos1=pos2=line+strlen(tearlinename);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.TEARLINE=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.TEARLINE,pos1,len);
cfg.TEARLINE[len]='\0';
} else if (strncmp(line,originname,strlen(originname))==0) {
/* ORIGIN keyword (Originline to be used or origin file) */
pos1=pos2=line+strlen(originname);
while(strncmp(pos2,"\n",strlen("\n"))!=0) pos2++;
len=pos2-pos1;
cfg.ORIGIN=(char *)malloc(sizeof(char)*(len+1));
strncpy(cfg.ORIGIN,pos1,len);
cfg.ORIGIN[len]='\0';
}
}
}
}
fclose(cofg);
return(0);
}
char *kludgespeclist=".ksl";
char *kludgerepllist=".krl";
/* Read .ksl and .krl files */
/* Kludge Specification List and Kludge Replacement List */
int readlist(char *name,struct kludges *kl) {
char file[80],line[128],*tline;
struct kludgelist *kll;
int len;
FILE *lst;
printmsg(3,"ReadList\n");
/* Open ksl file */
strcpy(file,name);
strcat(file,kludgespeclist);
if ((lst=fopen(file,"r"))==NULL) {
printmsg(0,"Couldn't open specification list file\n");
return(-1);
}
while(fgets(line,80,lst)!=NULL) {
if (line[0]==';' || !strcmp(line,"\n") ) {
; /* newlines and lines begining with ';' are comment lines */
} else {
if ((kll=(struct kludgelist *)malloc(sizeof(struct kludgelist)))==NULL) {
fclose(lst);
printmsg(0,"Can't allocate memory\n");
return(-1);
}
kll->klg.kludge=NULL;
kll->klg.repl=NULL;
kll->klg.spec=NULL;
len=0;
while(line[len]!=' ' && line[len]!=':' && line[len]!=CARRIAGERETURN && line[len]!=LINEFEED && line[len]!='\0') len++;
if (line[len]==':') {
len++;
}
if ((kll->klg.kludge=(char *)malloc((len+1)*sizeof(char)))==NULL) {
fclose(lst);
printmsg(0,"Can't allocate memory\n");
return(-1);
}
strncpy(kll->klg.kludge,line,len);
kll->klg.kludge[len]='\0';
if (line[len]==CARRIAGERETURN || line[len]==LINEFEED || line[len]=='\0') {
kll->klg.spec=NULL;
} else {
tline=line+len;
len=0;
while(tline[len]!=CARRIAGERETURN && tline[len]!=LINEFEED && tline[len]!='\0') len++;
if ((kll->klg.spec=(char *)malloc((len+1)*sizeof(char)))==NULL) {
fclose(lst);
printmsg(0,"Can't allocate memory\n");
return(-1);
}
strncpy(kll->klg.spec,tline,len);
kll->klg.spec[len]='\0';
}
kll->next=kl->root;
kl->root=kll;
}
}
fclose(lst);
strcpy(file,name);
strcat(file,kludgerepllist);
if ((lst=fopen(file,"r"))==NULL) {
printmsg(0,"Couldn't open replacement list file\n");
return(-1);
}
while(fgets(line,80,lst)!=NULL) {
if (line[0]==';' || !strcmp(line,"\n") ) {
; /* newlines and lines begining with ';' are comment lines */
} else {
kll=kl->root;
while(strncmp(kll->klg.kludge,line,strlen(kll->klg.kludge))!=0) {
kll=kll->next;
if (kll==NULL) {
printmsg(0,"Kludge replacement without kludge specification\n");
break;
}
}
if (kll!=NULL) {
tline=line+strlen(kll->klg.kludge);
len=0;
while(tline[len]!=CARRIAGERETURN && tline[len]!=LINEFEED && tline[len]!='\0') len++;
if (len==0) kll->klg.repl=NIL;
else {
if ((kll->klg.repl=(char *)malloc((len+1)*sizeof(char)))==NULL) {
fclose(lst);
printmsg(0,"Can't allocate memory\n");
return(-1);
}
strncpy(kll->klg.repl,tline,len);
kll->klg.repl[len]='\0';
}
}
}
}
fclose(lst);
kll=kl->root;
printmsg(2,"Kludge list\n");
while(kll!=NULL) {
sprintf(line,"KLUDGE : <%s>\nSPEC : <%s>\nREPL : <%s>\n",kll->klg.kludge,kll->klg.spec,kll->klg.repl);
printmsg(2,line);
kll=kll->next;
}
printmsg(2,"\n");
return(0);
}
char *table=".tab";
int readtab(char *filename,struct reroutetab *rrt) {
FILE *tab;
char line[80];
char file[128];
struct rrlist *wlk;
printmsg(3,"ReadTab\n");
strcpy(file,filename);
strcat(file,table);
if ((tab=fopen(file,"r"))==NULL) {
sprintf(file,"Can't open : %s\n",filename);
printmsg(0,file);
exit(1);
}
while (fgets(line,80,tab)!=NULL) {
if (line[0]==';' || !strcmp(line,"\n") ) {
; /* newlines and lines begining with ';' are comment lines */
} else {
parseline(line,rrt);
}
}
fclose(tab);
/* Show all rerouting that will be done */
printmsg(2,"Orginating Address Rerouting :\n");
wlk=rrt->orgroot;
while(wlk!=NULL) {
sprintf(file,"%d:%d/%d.%d -> ",wlk->in.Zone,wlk->in.Net,wlk->in.Node,wlk->in.Point);
printmsg(2,file);
sprintf(file,"%d:%d/%d.%d\n",wlk->out.Zone,wlk->out.Net,wlk->out.Node,wlk->out.Point);
printmsg(2,file);
wlk=wlk->next;
}
printmsg(2,"Destination Address Rerouting :\n");
wlk=rrt->dstroot;
while(wlk!=NULL) {
sprintf(file,"%d:%d/%d.%d -> ",wlk->in.Zone,wlk->in.Net,wlk->in.Node,wlk->in.Point);
printmsg(2,file);
sprintf(file,"%d:%d/%d.%d\n",wlk->out.Zone,wlk->out.Net,wlk->out.Node,wlk->out.Point);
printmsg(2,file);
wlk=wlk->next;
}
printmsg(2,"\n");
return(0);
}
/* parse a line to see if it is a origin address or destination address that
must be changed */
int parseline(char *line,struct reroutetab *rrt) {
int i;
struct rrlist *pl;
char txt[128];
printmsg(3,"ParseLine\n");
if ((pl=(struct rrlist *)malloc(sizeof(struct rrlist)))==NULL) {
printmsg(0,"Can't allocate memory\n");
return(-1);
}
for (i=0;i<3;i++) {
line[i]=toupper(line[i]);
}
if (strncmp(line,"ORG ",4)==0) { /* It's a origin reroute */
if (parsedoubleaddress(&line[4],pl)==-1) return(-1);
if (rrt->orgroot==NULL) { /* first message to be added */
rrt->orgroot=pl;
rrt->orgroot->next=NULL;
rrt->orgthread=rrt->orgroot;
} else { /* other messages (I use thread to add it to the tail) */
rrt->orgthread->next=pl;
rrt->orgthread=rrt->orgthread->next;
rrt->orgthread->next=NULL;
}
} else {
if (strncmp(line,"DST ",4)==0) { /* It's a destination reroute */
if (parsedoubleaddress(&line[4],pl)==-1) return(-1);
if (rrt->dstroot==NULL) { /* first message to be added */
rrt->dstroot=pl;
rrt->dstroot->next=NULL;
rrt->dstthread=rrt->dstroot;
} else { /* other messages (I use thread to add it to the tail) */
rrt->dstthread->next=pl;
rrt->dstthread=rrt->dstthread->next;
rrt->dstthread->next=NULL;
}
} else { /* It's garbage */
sprintf(txt,"Can't parse line : <%60s>\n",line);
printmsg(0,txt);
return(-1);
}
}
return(0);
}
/* Get the two address involved in the rerouting */
int parsedoubleaddress(char *line,struct rrlist *pl) {
if (parseaddress(line,&(pl->in),WILD)==-1) return(-1);
while(*line!=' ' && *line!='\t') line++;
while(*line==' ' || *line=='\t') line++;
if (parseaddress(line,&(pl->out),WILD)==-1) return(-1);
return(0);
}