From 0170015955d51c7614f38dc11d77ab6fc5b1ac22 Mon Sep 17 00:00:00 2001 From: Thomas Ries Date: Thu, 4 Dec 2003 21:18:19 +0000 Subject: [PATCH] - have registrations persistent across restarts of the daemon ('registration_file' config option) --- ChangeLog | 2 + doc/siproxd.conf.example | 8 ++- src/proxy.c | 4 +- src/readconf.c | 1 + src/register.c | 102 +++++++++++++++++++++++++++++++++++++-- src/siproxd.c | 8 +-- src/siproxd.h | 4 +- 7 files changed, 118 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8a8cd35..6e36963 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 0.5.1 ===== + 04-Dec-2003: - have registrations persistent across restarts of + the daemon ('registration_file' config option) 29-Nov-2003: - some documentation & FAQ updates 0.5.0 diff --git a/doc/siproxd.conf.example b/doc/siproxd.conf.example index 467b7ff..3f975e9 100644 --- a/doc/siproxd.conf.example +++ b/doc/siproxd.conf.example @@ -67,6 +67,12 @@ silence_log = 0 user = nobody #chrootjail = /var/lib/siproxd/ +###################################################################### +# Registration file: +# Where to store the current registrations +# empty means we do not save registrations +registration_file = /var/lib/siproxd/siproxd_registrations + ###################################################################### # global switch to control the RTP proxy behaviour # 0 - RTP proxy disabled @@ -152,7 +158,7 @@ debug_level = 0x00000000 # # Siproxd itself can be told to send all traffic to another # outbound proxy. -# You can use this feature to 'chain' multiple siproxd proxys +# You can use this feature to 'chain' multiple siproxd proxies # if you have several masquerading firewalls to cross. # # outbound_proxy_host = my.outboundproxy.org diff --git a/src/proxy.c b/src/proxy.c index a7e343d..259f15e 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -662,9 +662,9 @@ if (configuration.debuglevel) /* - * PROXY_REWRITE_INVITATION_BODY + * PROXY_REWRITE_REQUEST_URI * - * rewrites the outgoing INVITATION packet + * rewrites the incoming Request URI * * RETURNS * STS_SUCCESS on success diff --git a/src/readconf.c b/src/readconf.c index 6783c22..7a661fd 100644 --- a/src/readconf.c +++ b/src/readconf.c @@ -147,6 +147,7 @@ static int parse_config (FILE *configfile) { { "masked_host", TYP_STRINGA,&configuration.masked_host }, { "outbound_proxy_host", TYP_STRING, &configuration.outbound_proxy_host }, { "outbound_proxy_port", TYP_INT4, &configuration.outbound_proxy_port }, + { "registration_file", TYP_STRING ,&configuration.registrationfile }, {0, 0, 0} }; diff --git a/src/register.c b/src/register.c index bb23192..bb32634 100644 --- a/src/register.c +++ b/src/register.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -45,13 +46,106 @@ static char const ident[]="$Id: " __FILE__ ": " PACKAGE "-" VERSION "-"\ extern struct siproxd_config configuration; struct urlmap_s urlmap[URLMAP_SIZE]; /* URL mapping table */ + extern int sip_socket; /* sending SIP datagrams */ +extern int errno; /* * initialize the URL mapping table */ void register_init(void) { - memset (urlmap, 0, sizeof(urlmap)); + FILE *stream; + int sts, size, i; + char buff[128]; + + memset(urlmap, 0, sizeof(urlmap)); + + if (configuration.registrationfile) { + stream = fopen(configuration.registrationfile, "r"); + + if (!stream) { + /* + * the file does not exist, or the size was incorrect, + * delete it and start from scratch + */ + unlink(configuration.registrationfile); + WARN("registration file not found, starting with empty table"); + } else { + /* read the url table from file */ + for (i=0;i < URLMAP_SIZE; i++) { + fgets(buff, sizeof(buff), stream); + sts=sscanf(buff, "***:%i:%i", &urlmap[i].active, &urlmap[i].expires); + if (urlmap[i].active) { + osip_uri_init(&urlmap[i].true_url); + osip_uri_init(&urlmap[i].masq_url); + osip_uri_init(&urlmap[i].reg_url); + + #define R(X) {\ + fgets(buff, sizeof(buff), stream);\ + buff[sizeof(buff)-1]='\0';\ + if (strchr(buff, 10)) *strchr(buff, 10)='\0';\ + if (strchr(buff, 13)) *strchr(buff, 13)='\0';\ + if (strlen(buff) > 0) {\ + size = strnlen(buff, sizeof(buff));\ + X =(char*)malloc(size);\ + sts=sscanf(buff,"%s",X);\ + } else {\ + X = NULL;\ + }\ + } + + R(urlmap[i].true_url->scheme); + R(urlmap[i].true_url->username); + R(urlmap[i].true_url->host); + R(urlmap[i].true_url->port); + R(urlmap[i].masq_url->scheme); + R(urlmap[i].masq_url->username); + R(urlmap[i].masq_url->host); + R(urlmap[i].masq_url->port); + R(urlmap[i].reg_url->scheme); + R(urlmap[i].reg_url->username); + R(urlmap[i].reg_url->host); + R(urlmap[i].reg_url->port); + } + } + } + } + return; +} + + +/* + * shut down the URL mapping table + */ +void register_shut(void) { + int i; + FILE *stream; + + if (configuration.registrationfile) { + /* write urlmap back to file */ + stream = fopen(configuration.registrationfile, "w+"); + + for (i=0;i < URLMAP_SIZE; i++) { + fprintf(stream, "***:%i:%i\n", urlmap[i].active, urlmap[i].expires); + if (urlmap[i].active) { + #define W(X) fprintf(stream, "%s\n", (X)? X:""); + + W(urlmap[i].true_url->scheme); + W(urlmap[i].true_url->username); + W(urlmap[i].true_url->host); + W(urlmap[i].true_url->port); + W(urlmap[i].masq_url->scheme); + W(urlmap[i].masq_url->username); + W(urlmap[i].masq_url->host); + W(urlmap[i].masq_url->port); + W(urlmap[i].reg_url->scheme); + W(urlmap[i].reg_url->username); + W(urlmap[i].reg_url->host); + W(urlmap[i].reg_url->port); + } + } + fclose(stream); + } return; } @@ -226,8 +320,8 @@ int register_client(osip_message_t *my_msg, int force_lcl_masq) { } /* remember the VIA for later use */ - osip_via_clone( ((osip_via_t*)(my_msg->vias->node->element)), - &urlmap[i].via); +// osip_via_clone( ((osip_via_t*)(my_msg->vias->node->element)), +// &urlmap[i].via); } /* if new entry */ /* give some safety margin for the next update */ @@ -259,7 +353,7 @@ void register_agemap(void) { osip_uri_free(urlmap[i].true_url); osip_uri_free(urlmap[i].masq_url); osip_uri_free(urlmap[i].reg_url); - osip_via_free(urlmap[i].via); +// osip_via_free(urlmap[i].via); } } return; diff --git a/src/siproxd.c b/src/siproxd.c index 0dd1a9d..2d3beea 100644 --- a/src/siproxd.c +++ b/src/siproxd.c @@ -202,9 +202,6 @@ INFO("daemonizing done (pid=%i)", getpid()); /* init the oSIP parser */ parser_init(); - /* initialize the registration facility */ - register_init(); - /* listen for incoming messages */ sts=sipsock_listen(); if (sts == STS_FAILURE) { @@ -213,6 +210,9 @@ INFO("daemonizing done (pid=%i)", getpid()); exit(1); } + /* initialize the registration facility */ + register_init(); + INFO(PACKAGE"-"VERSION"-"BUILDSTR" started"); /* * silence the log - if so required... @@ -382,6 +382,8 @@ INFO("got packet [%i bytes]from %s [%s]", i, } /* while TRUE */ exit_prg: + + register_shut(); INFO("properly terminating siproxd"); return 0; diff --git a/src/siproxd.h b/src/siproxd.h index 76187f3..ad339c7 100644 --- a/src/siproxd.h +++ b/src/siproxd.h @@ -34,6 +34,7 @@ int sockbind(struct in_addr ipaddr, int localport, int errflg); /* register.c */ void register_init(void); +void register_shut(void); int register_client(osip_message_t *request, int force_lcl_masq); /*X*/ void register_agemap(void); int register_response(osip_message_t *request, int flag); /*X*/ @@ -98,7 +99,7 @@ struct urlmap_s { osip_uri_t *true_url; // true URL of UA (inbound URL) osip_uri_t *masq_url; // masqueraded URL (outbound URL) osip_uri_t *reg_url; // registered URL (masq URL as wished by UA) - osip_via_t *via; +// osip_via_t *via; }; /* * the difference between masq_url and reg_url is, @@ -143,6 +144,7 @@ struct siproxd_config { stringa_t masked_host; char *outbound_proxy_host; int outbound_proxy_port; + char *registrationfile; };