Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 512dcd9
Showing
24 changed files
with
1,147 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* $Id$ | ||
*/ | ||
|
||
#include <errno.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
#include "cfg_parser.h" | ||
#include "dprint.h" | ||
#include "parser_f.h" | ||
#include "route.h" | ||
|
||
|
||
|
||
|
||
/* params: null terminated text line => fills cl | ||
* returns 0, or on error -1. */ | ||
int cfg_parse_line(char* line, struct cfg_line* cl) | ||
{ | ||
/* format: | ||
line = rule | comment | ||
comment = SP* '#'.* | ||
rule = SP* method_re SP* uri_re SP* ip_address comment? | ||
*/ | ||
|
||
char* tmp; | ||
char* end; | ||
|
||
end=line+strlen(line); | ||
tmp=eat_space(line, end-line); | ||
if ((tmp==end)||(is_empty(tmp, end-tmp))) { | ||
cl->type=CFG_EMPTY; | ||
goto skip; | ||
} | ||
if (*tmp=='#'){ | ||
cl->type=CFG_COMMENT; | ||
goto skip; | ||
} | ||
cl->method=tmp; | ||
tmp=eat_token(cl->method,end-cl->method); | ||
if (tmp==end) goto error; | ||
printf("%d\n", tmp-line); | ||
*tmp=0; | ||
tmp++; | ||
cl->uri=eat_space(tmp,end-tmp); | ||
if (tmp==end) goto error; | ||
tmp=eat_token(cl->uri,end-cl->uri); | ||
if (tmp==end) goto error; | ||
printf("%d\n", tmp-line); | ||
*tmp=0; | ||
tmp++; | ||
cl->address=eat_space(tmp,end-tmp); | ||
if (tmp==end) goto error; | ||
tmp=eat_token(cl->address, end-cl->address); | ||
printf("%d(%02x)\n", tmp-line, *tmp); | ||
if (tmp<end) { | ||
*tmp=0; | ||
if (tmp+1<end){ | ||
if (!is_empty(tmp+1,end-tmp-1)){ | ||
printf("%d(%02x) e: %d\n", tmp-line, *tmp, end-line); | ||
/* check if comment */ | ||
tmp=eat_space(tmp+1, end-tmp-1); | ||
printf("%d(%02x) e: %d\n", tmp-line, *tmp, end-line); | ||
if (*tmp!='#'){ | ||
/* extra chars at the end of line */ | ||
goto error; | ||
} | ||
} | ||
} | ||
} | ||
|
||
cl->type=CFG_RULE; | ||
skip: | ||
return 0; | ||
error: | ||
cl->type=CFG_ERROR; | ||
return -1; | ||
} | ||
|
||
|
||
|
||
/* parses the cfg, returns 0 on success, line no otherwise */ | ||
int cfg_parse_stream(FILE* stream) | ||
{ | ||
int line; | ||
struct cfg_line cl; | ||
char buf[MAX_LINE_SIZE]; | ||
int ret; | ||
|
||
line=1; | ||
while(!feof(stream)){ | ||
if (fgets(buf, MAX_LINE_SIZE, stream)){ | ||
cfg_parse_line(buf, &cl); | ||
switch (cl.type){ | ||
case CFG_RULE: | ||
if ((ret=add_rule(&cl, &rlist))!=0){ | ||
DPrint("ERROR: could not compile rule at line %d\n", | ||
line); | ||
DPrint(" ----: add_rule returned %d\n", ret); | ||
goto error; | ||
} | ||
break; | ||
case CFG_COMMENT: | ||
case CFG_SKIP: | ||
break; | ||
case CFG_ERROR: | ||
DPrint("ERROR: bad config line (%d):%s\n", line, buf); | ||
goto error; | ||
break; | ||
} | ||
line++; | ||
}else{ | ||
if (ferror(stream)){ | ||
DPrint("ERROR: reading configuration: %s\n", strerror(errno)); | ||
goto error; | ||
} | ||
break; | ||
} | ||
} | ||
return 0; | ||
|
||
error: | ||
return line; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* $Id$ | ||
*/ | ||
|
||
#ifndef cfg_parser_h | ||
#define cfg_parser_h | ||
|
||
#include <stdio.h> | ||
|
||
#define CFG_EMPTY 0 | ||
#define CFG_COMMENT 1 | ||
#define CFG_SKIP 2 | ||
#define CFG_RULE 3 | ||
#define CFG_ERROR -1 | ||
|
||
#define MAX_LINE_SIZE 800 | ||
|
||
struct cfg_line{ | ||
int type; | ||
char* method; | ||
char* uri; | ||
char* address; | ||
}; | ||
|
||
|
||
int cfg_parse_line(char* line, struct cfg_line* cl); | ||
int cfg_parse_stream(FILE* stream); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* debug print | ||
* | ||
*/ | ||
|
||
#include "dprint.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdio.h> | ||
|
||
void dprint(char * format, ...) | ||
{ | ||
va_list ap; | ||
|
||
va_start(ap, format); | ||
vfprintf(stderr,format,ap); | ||
fflush(stderr); | ||
va_end(ap); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* $Id$ | ||
*/ | ||
|
||
|
||
#ifndef dprint_h | ||
#define dprint_h | ||
|
||
|
||
|
||
void dprint (char* format, ...); | ||
|
||
#ifdef NO_DEBUG | ||
#define DPrint(fmt, args...) | ||
#else | ||
#define DPrint(fmt,args...) dprint(fmt, ## args); | ||
#endif | ||
|
||
|
||
#endif /* ifndef dprint_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* $Id$ | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
|
||
#include "dprint.h" | ||
#include "route.h" | ||
|
||
#define CFG_FILE "./sip_router.cfg" | ||
|
||
|
||
int main(int argc, char** argv) | ||
{ | ||
|
||
char * cfg_file; | ||
FILE* cfg_stream; | ||
|
||
cfg_file=CFG_FILE; | ||
|
||
/* process command line (get port no, cfg. file path etc) */ | ||
/* ...*/ | ||
|
||
/* load config file or die */ | ||
cfg_stream=fopen (cfg_file, "r"); | ||
if (cfg_stream==0){ | ||
DPrint("ERROR: could not load config file: %s\n", strerror(errno)); | ||
goto error; | ||
} | ||
|
||
if (cfg_parse_stream(cfg_stream)!=0){ | ||
DPrint("ERROR: config parser failure\n"); | ||
goto error; | ||
} | ||
|
||
|
||
print_rl(); | ||
|
||
|
||
|
||
/* start other processes/threads ? */ | ||
|
||
/* receive loop */ | ||
|
||
|
||
error: | ||
return -1; | ||
|
||
} |
Oops, something went wrong.