Skip to content

Commit

Permalink
Initial revision
Browse files Browse the repository at this point in the history
  • Loading branch information
poandrei committed Sep 3, 2001
0 parents commit 512dcd9
Show file tree
Hide file tree
Showing 24 changed files with 1,147 additions and 0 deletions.
Binary file added .cfg_parser.c.swp
Binary file not shown.
Binary file added .cfg_parser.h.swp
Binary file not shown.
Binary file added .main.c.swp
Binary file not shown.
Binary file added .msg_parser.c.swp
Binary file not shown.
Binary file added .msg_parser.h.swp
Binary file not shown.
Binary file added .parser_f.c.swp
Binary file not shown.
Binary file added .parser_f.h.swp
Binary file not shown.
Binary file added .route.c.swp
Binary file not shown.
Binary file added .route.h.swp
Binary file not shown.
Binary file added .sip_router.cfg.swp
Binary file not shown.
Binary file added .test.c.swp
Binary file not shown.
126 changes: 126 additions & 0 deletions cfg_parser.c
@@ -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;
}

29 changes: 29 additions & 0 deletions cfg_parser.h
@@ -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
21 changes: 21 additions & 0 deletions dprint.c
@@ -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);
}
20 changes: 20 additions & 0 deletions dprint.h
@@ -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 */
51 changes: 51 additions & 0 deletions main.c
@@ -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;

}

0 comments on commit 512dcd9

Please sign in to comment.