- added syslog suport, changed DPrint to LOG, etc.
authorAndrei Pelinescu-Onciul <andrei@iptel.org>
Fri, 7 Sep 2001 01:24:32 +0000 (01:24 +0000)
committerAndrei Pelinescu-Onciul <andrei@iptel.org>
Fri, 7 Sep 2001 01:24:32 +0000 (01:24 +0000)
cfg_parser.c
dprint.h
forward.c
main.c
msg_parser.c
receive.c
route.c
udp_server.c

index e35e2b5..6d17fff 100644 (file)
@@ -95,9 +95,10 @@ int cfg_parse_stream(FILE* stream)
                        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);
+                                               LOG(L_CRIT, 
+                                                               "ERROR: could not compile rule at line %d\n",
+                                                               line);
+                                               LOG(L_CRIT, " ----: add_rule returned %d\n", ret);
                                                goto error;
                                        }
                                        break;
@@ -105,14 +106,16 @@ int cfg_parse_stream(FILE* stream)
                                case CFG_SKIP:
                                        break;
                                case CFG_ERROR:
-                                       DPrint("ERROR: bad config line (%d):%s\n", line, buf);
+                                       LOG(L_CRIT, "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));
+                               LOG(L_CRIT,
+                                               "ERROR: reading configuration: %s\n",
+                                               strerror(errno));
                                goto error;
                        }
                        break;
index 356a79a..b45eac2 100644 (file)
--- a/dprint.h
+++ b/dprint.h
@@ -6,6 +6,25 @@
 #ifndef dprint_h
 #define dprint_h
 
+#include <syslog.h>
+
+#include "globals.h"
+
+#define L_ALERT -3
+#define L_CRIT  -2
+#define L_ERR   -1
+#define L_WARN   1
+#define L_NOTICE 2
+#define L_INFO   3
+#define L_DBG    4
+
+
+
+#define DPRINT_LEV     1
+/* log facility (see syslog(3)) */
+#define L_FAC  LOG_DAEMON
+/* priority at which we log */
+#define DPRINT_PRIO LOG_DEBUG
 
 
 void dprint (char* format, ...);
@@ -13,8 +32,54 @@ void dprint (char* format, ...);
 #ifdef NO_DEBUG
        #define DPrint(fmt, args...)
 #else
-       #define DPrint(fmt,args...) dprint(fmt, ## args);
+       //#define DPrint(fmt,args...) dprint(fmt, ## args)
+       #define DPrint(fmt,args...) \
+               do{ \
+                       if (debug>=DPRINT_LEV){ \
+                               if (log_stderr){ \
+                                       dprint (fmt, ## args); \
+                               }else{ \
+                                       syslog(DPRINT_LEV|L_FAC, fmt, ## args); \
+                               }\
+                       } \
+               }while(0)
+
 #endif
 
 
+
+#define LOG(lev, fmt, args...) \
+                       do { \
+                               if (debug>=(lev)){ \
+                                       if (log_stderr) dprint (fmt, ## args); \
+                                       else { \
+                                               switch(lev){ \
+                                                       case L_CRIT: \
+                                                               syslog(LOG_CRIT | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_ALERT: \
+                                                               syslog(LOG_ALERT | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_ERR: \
+                                                               syslog(LOG_ERR | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_WARN: \
+                                                               syslog(LOG_WARNING | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_NOTICE: \
+                                                               syslog(LOG_NOTICE | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_INFO: \
+                                                               syslog(LOG_INFO | L_FAC, fmt, ##args); \
+                                                               break; \
+                                                       case L_DBG: \
+                                                               syslog(LOG_DEBUG | L_FAC, fmt, ##args); \
+                                                               break; \
+                                               } \
+                                       } \
+                               } \
+                       }while(0)
+
+#define DBG(fmt, args...) LOG(L_DBG, fmt, ## args)
+
 #endif /* ifndef dprint_h */
index d353746..59085af 100644 (file)
--- a/forward.c
+++ b/forward.c
@@ -76,7 +76,7 @@ int forward_request(char * orig, char* buf,
        to=0;
        to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
        if (to==0){
-               DPrint("ERROR: forward_reply: out of memory\n");
+               LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
                goto error;
        }
 
@@ -92,7 +92,7 @@ int forward_request(char * orig, char* buf,
        new_len=len+via_len+received_len;
        new_buf=(char*)malloc(new_len+1);
        if (new_buf==0){
-               DPrint("ERROR: forward_request: out of memory\n");
+               LOG(L_ERR, "ERROR: forward_request: out of memory\n");
                goto error;
        }
 /* copy msg till first via */
@@ -126,8 +126,8 @@ int forward_request(char * orig, char* buf,
        new_buf[new_len]=0;
 
         /* send it! */
-       printf("Sending:\n%s.\n", new_buf);
-       printf("orig. len=%d, new_len=%d, via_len=%d, received_len=%d\n",
+       DBG("Sending:\n%s.\n", new_buf);
+       DBG("orig. len=%d, new_len=%d, via_len=%d, received_len=%d\n",
                        len, new_len, via_len, received_len);
 
        to->sin_family = AF_INET;
@@ -178,7 +178,7 @@ int forward_reply(char * orig, char* buf,
        to=0;
        to=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
        if (to==0){
-               DPrint("ERROR: forward_reply: out of memory\n");
+               LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
                goto error;
        }
 
@@ -187,7 +187,7 @@ int forward_reply(char * orig, char* buf,
                for (r=0; r<addresses_no; r++)
                        if(strcmp(msg->via1.host, names[r])==0) break;
                if (r==addresses_no){
-                       DPrint("ERROR: forward_reply: host in first via != me : %s\n",
+                       LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me : %s\n",
                                        msg->via1.host);
                        /* send error msg back? */
                        goto error;
@@ -196,21 +196,21 @@ int forward_reply(char * orig, char* buf,
        /* we must remove the first via */
        via_len=msg->via1.size;
        size=msg->via1.hdr-buf;
-       printf("via len: %d, initial size: %d\n", via_len, size);
+       DBG("via len: %d, initial size: %d\n", via_len, size);
        if (msg->via1.next){
                /* keep hdr =substract hdr size +1 (hdr':') and add
                 */
                via_len-=strlen(msg->via1.hdr)+1;
                size+=strlen(msg->via1.hdr)+1;
-           printf(" adjusted via len: %d, initial size: %d\n",
+           DBG(" adjusted via len: %d, initial size: %d\n",
                                via_len, size);
        }
        new_len=len-via_len;
        
-       printf(" old size: %d, new size: %d\n", len, new_len);
+       DBG(" old size: %d, new size: %d\n", len, new_len);
        new_buf=(char*)malloc(new_len+1);/* +1 is for debugging (\0 to print it )*/
        if (new_buf==0){
-               DPrint("ERROR: forward_reply: out of memory\n");
+               LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
                goto error;
        }
        new_buf[new_len]=0; /* debug: print the message */
@@ -219,17 +219,18 @@ int forward_reply(char * orig, char* buf,
        s_offset=size+via_len;
        memcpy(new_buf+offset,orig+s_offset, len-s_offset);
         /* send it! */
-       printf(" copied size: orig:%d, new: %d, rest: %d\n",
+       DBG(" copied size: orig:%d, new: %d, rest: %d\n",
                        s_offset, offset, 
                        len-s_offset );
-       printf("Sending: to %s:%d, \n%s.\n",
+       DBG("Sending: to %s:%d, \n%s.\n",
                        msg->via2.host, 
                        (unsigned short)msg->via2.port,
                        new_buf);
        /* fork? gethostbyname will probably block... */
        he=gethostbyname(msg->via2.host);
        if (he==0){
-               DPrint("ERROR:forward_reply:gethostbyname failure\n");
+               LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
+                               msg->via2.host);
                goto error;
        }
        to->sin_family = AF_INET;
diff --git a/main.c b/main.c
index 36afcd5..dba74e9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -212,13 +212,13 @@ int main(int argc, char** argv)
        /* load config file or die */
        cfg_stream=fopen (cfg_file, "r");
        if (cfg_stream==0){
-               DPrint("ERROR: loading config file(%s): %s\n", cfg_file,
+               fprintf(stderr, "ERROR: loading config file(%s): %s\n", cfg_file,
                                strerror(errno));
                goto error;
        }
 
        if (cfg_parse_stream(cfg_stream)!=0){
-               DPrint("ERROR: config parser failure\n");
+               fprintf(stderr, "ERROR: config parser failure\n");
                goto error;
        }
        
index 42803b9..31ec86a 100644 (file)
@@ -40,7 +40,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
        /* see if it's a reply (status) */
        tmp=eat_token(buffer, len);
        if ((tmp==buffer)||(tmp>=end)){
-               DPrint("ERROR: empty  or bad first line\n");
+               LOG(L_INFO, "ERROR:parse_first_line: empty  or bad first line\n");
                goto error1;
        }
        if ((strlen(SIP_VERSION)==(tmp-buffer)) &&
@@ -103,11 +103,11 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
        return nl;
 
 error:
-       DPrint("ERROR: bad %s first line\n", 
+       LOG(L_INFO, "ERROR:parse_first_line: bad %s first line\n", 
                (fl->type==SIP_REPLY)?"reply(status)":"request");
 error1:
        fl->type=SIP_INVALID;
-       DPrint("ERROR: at line 0 char %d\n", offset);
+       LOG(L_INFO, "ERROR: at line 0 char %d\n", offset);
        /* skip  line */
        nl=eat_line(buffer,len);
        return nl;
@@ -168,7 +168,7 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
        }while( (*tmp==' ' ||  *tmp=='\t') && (offset<len) );
        if (offset==len){
                hdr_f->type=HDR_ERROR;
-               DPrint("ERROR: field body too  long\n");
+               LOG(L_INFO, "ERROR: het_hdr_field: field body too  long\n");
                goto error;
        }
        *(tmp-1)=0; /* should be an LF */
@@ -193,7 +193,8 @@ char* parse_hostport(char* buf, char** host, short int* port)
                invalid=0;
                *port=strtol(tmp+1, &invalid, 10);
                if ((invalid!=0)&&(*invalid)){
-                       DPrint("ERROR: hostport: trailing chars in port number: %s(%x)\n",
+                       LOG(L_INFO, 
+                                       "ERROR: hostport: trailing chars in port number: %s(%x)\n",
                                        invalid, invalid);
                        /* report error? */
                }
@@ -362,23 +363,23 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
        tmp=rest;
        switch(fl.type){
                case SIP_INVALID:
-                       DPrint("invalid message\n");
+                       DBG("parse_msg: invalid message\n");
                        goto error;
                        break;
                case SIP_REQUEST:
-                       DPrint("SIP Request:\n");
-                       DPrint(" method:  <%s>\n",fl.u.request.method);
-                       DPrint(" uri:     <%s>\n",fl.u.request.uri);
-                       DPrint(" version: <%s>\n",fl.u.request.version);
+                       DBG("SIP Request:\n");
+                       DBG(" method:  <%s>\n",fl.u.request.method);
+                       DBG(" uri:     <%s>\n",fl.u.request.uri);
+                       DBG(" version: <%s>\n",fl.u.request.version);
                        break;
                case SIP_REPLY:
-                       DPrint("SIP Reply  (status):\n");
-                       DPrint(" version: <%s>\n",fl.u.reply.version);
-                       DPrint(" status:  <%s>\n",fl.u.reply.status);
-                       DPrint(" reason:  <%s>\n",fl.u.reply.reason);
+                       DBG("SIP Reply  (status):\n");
+                       DBG(" version: <%s>\n",fl.u.reply.version);
+                       DBG(" status:  <%s>\n",fl.u.reply.status);
+                       DBG(" reason:  <%s>\n",fl.u.reply.reason);
                        break;
                default:
-                       DPrint("unknown type %d\n",fl.type);
+                       DBG("unknown type %d\n",fl.type);
        }
        
        /*find first Via: */
@@ -390,7 +391,7 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
                offset+=rest-tmp;
                switch (hf.type){
                        case HDR_ERROR:
-                               DPrint("ERROR: bad header  field\n");
+                               LOG(L_INFO,"ERROR: bad header  field\n");
                                goto  error;
                        case HDR_EOH: 
                                goto skip;
@@ -402,11 +403,12 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
                                                for (bar=first_via;(first_via) && (*bar);bar++)
                                                        if ((*bar=='\r')||(*bar=='\n')) *bar=' ';
                                #ifdef DEBUG
-                                               printf("first via: <%s>\n", first_via);
+                                               DBG("first via: <%s>\n", first_via);
                                #endif
                                                bar=parse_via_body(first_via, strlen(first_via), &vb1);
                                                if (vb1.error!=VIA_PARSE_OK){
-                                                       DPrint("ERROR: parsing via body: %s\n", first_via);
+                                                       LOG(L_INFO, "ERROR: parsing via body: %s\n",
+                                                                       first_via);
                                                        goto error;
                                                }
                                                
@@ -430,7 +432,7 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
                                break;
                }
        #ifdef DEBUG
-               printf("header field type %d, name=<%s>, body=<%s>\n",
+               DBG("header field type %d, name=<%s>, body=<%s>\n",
                        hf.type, hf.name, hf.body);
        #endif
                tmp=rest;
@@ -444,7 +446,7 @@ skip:
        if (second_via) {
                tmp=parse_via_body(second_via, strlen(second_via), &vb2);
                if (vb2.error!=VIA_PARSE_OK){
-                       DPrint("ERROR: parsing via2 body: %s\n", second_via);
+                       LOG(L_INFO, "ERROR: parsing via2 body: %s\n", second_via);
                        goto error;
                }
                vb2.size=tmp-second_via; 
@@ -455,17 +457,17 @@ skip:
 
 #ifdef DEBUG
        /* dump parsed data */
-       printf(" first  via: <%s/%s/%s> <%s:%d>",
+       DBG(" first  via: <%s/%s/%s> <%s:%d>",
                        vb1.name, vb1.version, vb1.transport, vb1.host, vb1.port);
-       if (vb1.params) printf(";<%s>", vb1.params);
-       if (vb1.comment) printf(" <%s>", vb1.comment);
-       printf ("\n");
+       if (vb1.params)  DBG(";<%s>", vb1.params);
+       if (vb1.comment) DBG(" <%s>", vb1.comment);
+       DBG ("\n");
        if (second_via){
-               printf(" second via: <%s/%s/%s> <%s:%d>",
+               DBG(" second via: <%s/%s/%s> <%s:%d>",
                                vb2.name, vb2.version, vb2.transport, vb2.host, vb2.port);
-               if (vb2.params) printf(";<%s>", vb2.params);
-               if (vb2.comment) printf(" <%s>", vb2.comment);
-               printf ("\n");
+               if (vb2.params)  DBG(";<%s>", vb2.params);
+               if (vb2.comment) DBG(" <%s>", vb2.comment);
+               DBG("\n");
        }
 #endif
        
@@ -475,7 +477,7 @@ skip:
        memcpy(&(msg->via2), &vb2, sizeof(struct via_body));
 
 #ifdef DEBUG
-       printf ("exiting parse_msg\n");
+       DBG("exiting parse_msg\n");
 #endif
 
        return 0;
index ca5d394..3b97d69 100644 (file)
--- a/receive.c
+++ b/receive.c
@@ -20,7 +20,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
        /* make a copy of the message */
        orig=(char*) malloc(len);
        if (orig==0){
-               DPrint("ERROR: memory allocation failure\n");
+               LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
                goto error1;
        }
        memcpy(orig, buf, len);
@@ -44,12 +44,12 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
                                          );
                if (re==0){
                        /* no route found, send back error msg? */
-                       DPrint("WARNING: no route found!\n");
+                       LOG(L_WARN, "WARNING: receive_msg: no route found!\n");
                        goto skip;
                }
                re->tx++;
                /* send msg */
-               DPrint(" found route to: %s\n", re->host.h_name);
+               DBG(" found route to: %s\n", re->host.h_name);
                forward_request(orig, buf, len, &msg, re, src_ip);
        }else if (msg.first_line.type==SIP_REPLY){
                /* sanity checks */
@@ -65,7 +65,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
                
                /* send the msg */
                if (forward_reply(orig, buf, len, &msg)==0){
-                       DPrint(" reply forwarded to %s:%d\n", 
+                       DBG(" reply forwarded to %s:%d\n", 
                                                msg.via2.host,
                                                (unsigned short) msg.via2.port);
                }
diff --git a/route.c b/route.c
index f051557..cf75f49 100644 (file)
--- a/route.c
+++ b/route.c
@@ -95,12 +95,12 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
        if (re==0) return E_OUT_OF_MEM;
 
        if (regcomp(&(re->method), cl->method, REG_EXTENDED|REG_NOSUB|REG_ICASE)){
-               DPrint("ERROR: bad re \"%s\"\n", cl->method);
+               LOG(L_CRIT, "ERROR: add_rule: bad re \"%s\"\n", cl->method);
                ret=E_BAD_RE;
                goto error;
        }
        if (regcomp(&(re->uri), cl->uri, REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
-               DPrint("ERROR: bad re \"%s\"\n", cl->uri);
+               LOG(L_CRIT, "ERROR: add_rule: bad re \"%s\"\n", cl->uri);
                ret=E_BAD_RE;
                goto error;
        }
@@ -108,7 +108,7 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
        
        he=gethostbyname(cl->address);
        if (he==0){
-               DPrint("ERROR: cannot resolve \"%s\"\n", cl->address);
+               LOG(L_CRIT, "ERROR: add_rule: cannot resolve \"%s\"\n", cl->address);
                ret=E_BAD_ADDRESS;
                goto error;
        }
@@ -181,7 +181,7 @@ struct route_elem* route_match(char* method, char* uri, struct route_elem** rl)
 {
        struct route_elem* t;
        if (*rl==0){
-               DPrint("WARNING: empty routing table\n");
+               LOG(L_ERR, "WARNING: route_match: empty routing table\n");
                return 0;
        }
        for (t=*rl; t; t=t->next){
@@ -206,25 +206,25 @@ void print_rl()
        int i,j;
 
        if (rlist==0){
-               DPrint("the routing table is empty\n");
+               LOG(L_INFO, "the routing table is empty\n");
                return;
        }
        
        for (t=rlist,i=0; t; i++, t=t->next){
-               DPrint("%2d.to=%s ; route ok=%d\n", i,
+               LOG(L_INFO, "%2d.to=%s ; route ok=%d\n", i,
                                t->host.h_name, t->ok);
-               DPrint("   ips: ");
+               LOG(L_INFO, "   ips: ");
                for (j=0; t->host.h_addr_list[j]; j++){
-                       DPrint("%d.%d.%d.%d ", 
+                       LOG(L_INFO, "%d.%d.%d.%d ", 
                                (unsigned char) t->host.h_addr_list[j][0],
                                (unsigned char) t->host.h_addr_list[j][1],
                            (unsigned char) t->host.h_addr_list[j][2],
                                (unsigned char) t->host.h_addr_list[j][3]
                                  );
                }
-               DPrint("\n");
-               DPrint("   port:%d\n", (unsigned short)t->port);
-               DPrint("   Statistics: tx=%d, errors=%d, tx_bytes=%d, idx=%d\n",
+               LOG(L_INFO, "\n");
+               LOG(L_INFO, "   port:%d\n", (unsigned short)t->port);
+               LOG(L_INFO, "   Statistics: tx=%d, errors=%d, tx_bytes=%d, idx=%d\n",
                                t->tx, t->errors, t->tx_bytes, t->current_addr_idx);
        }
 
index 6547195..2b162c2 100644 (file)
@@ -24,7 +24,7 @@ int udp_init(unsigned long ip, unsigned short port)
 
        addr=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
        if (addr==0){
-               DPrint("ERROR: udp_init: out of memory\n");
+               LOG(L_ERR, "ERROR: udp_init: out of memory\n");
                goto error;
        }
        addr->sin_family=AF_INET;
@@ -33,7 +33,7 @@ int udp_init(unsigned long ip, unsigned short port)
 
        udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (udp_sock==-1){
-               DPrint("ERROR: udp_init: socket: %s\n", strerror());
+               LOG(L_ERR, "ERROR: udp_init: socket: %s\n", strerror());
                goto error;
        }
        /* set sock opts? */
@@ -41,12 +41,12 @@ int udp_init(unsigned long ip, unsigned short port)
        if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR,
                                        (void*)&optval, sizeof(optval)) ==-1)
        {
-               DPrint("ERROR: udp_init: setsockopt: %s\n", strerror());
+               LOG(L_ERR, "ERROR: udp_init: setsockopt: %s\n", strerror());
                goto error;
        }
 
        if (bind(udp_sock, (struct sockaddr*) addr, sizeof(struct sockaddr))==-1){
-               DPrint("ERROR: udp_init: bind: %s\n", strerror());
+               LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror());
                goto error;
        }
 
@@ -69,7 +69,7 @@ int udp_rcv_loop()
 
        from=(struct sockaddr*) malloc(sizeof(struct sockaddr));
        if (from==0){
-               DPrint("ERROR: udp_rcv_loop: out of memory\n");
+               LOG(L_ERR, "ERROR: udp_rcv_loop: out of memory\n");
                goto error;
        }
 
@@ -77,7 +77,7 @@ int udp_rcv_loop()
                fromlen=sizeof(struct sockaddr);
                len=recvfrom(udp_sock, buf, BUF_SIZE, 0, from, &fromlen);
                if (len==-1){
-                       DPrint("ERROR: udp_rcv_loop:recvfrom: %s\n", strerror());
+                       LOG(L_ERR, "ERROR: udp_rcv_loop:recvfrom: %s\n", strerror());
                        if (errno==EINTR)       goto skip;
                        else goto error;
                }
@@ -108,7 +108,7 @@ int udp_send(char *buf, int len, struct sockaddr*  to, int tolen)
 again:
        n=sendto(udp_sock, buf, len, 0, to, tolen);
        if (n==-1){
-               DPrint("ERROR: udp_send: sendto: %s\n", strerror());
+               LOG(L_ERR, "ERROR: udp_send: sendto: %s\n", strerror());
                if (errno==EINTR) goto again;
        }
        return n;