From dcb9b8f9a2e39a85392a67342e7906e344767b1e Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Sun, 24 Aug 2003 22:35:06 +0000 Subject: [PATCH] Make SIP registration persistent (bug #159) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1419 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- channels/chan_iax2.c | 8 +++--- channels/chan_sip.c | 63 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index 4ae5de7581..fb0e683a85 100755 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -3259,7 +3259,7 @@ static int expire_registry(void *data) p->expire = -1; /* Reset expirey value */ p->expirey = expirey; - ast_db_del("IAX2/Registry", p->name); + ast_db_del("IAX/Registry", p->name); if (iax2_regfunk) iax2_regfunk(p->name, 0); return 0; @@ -3273,7 +3273,7 @@ static void reg_source_db(struct iax2_peer *p) char data[80]; struct in_addr in; char *c, *d; - if (!ast_db_get("IAX2/Registry", p->name, data, sizeof(data))) { + if (!ast_db_get("IAX/Registry", p->name, data, sizeof(data))) { c = strchr(data, ':'); if (c) { *c = '\0'; @@ -3294,6 +3294,8 @@ static void reg_source_db(struct iax2_peer *p) if (p->expire > -1) ast_sched_del(sched, p->expire); p->expire = ast_sched_add(sched, p->expirey * 1000, expire_registry, (void *)p); + if (iax2_regfunk) + iax2_regfunk(p->name, 1); } } @@ -3315,7 +3317,7 @@ static int update_registry(char *name, struct sockaddr_in *sin, int callno) if (iax2_regfunk) iax2_regfunk(p->name, 1); snprintf(data, sizeof(data), "%s:%d:%d", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), p->expirey); - ast_db_put("IAX2/Registry", p->name, data); + ast_db_put("IAX/Registry", p->name, data); if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Registered '%s' (%s) at %s:%d\n", p->name, iaxs[callno]->state & IAX_STATE_AUTHENTICATED ? "AUTHENTICATED" : "UNAUTHENTICATED", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 54cee4bb9b..30d7f1bc46 100755 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -3024,6 +3025,7 @@ static int expire_register(void *data) { struct sip_peer *p = data; memset(&p->addr, 0, sizeof(p->addr)); + ast_db_del("SIP/Registry", p->name); p->expire = -1; ast_device_state_changed("SIP/%s", p->name); return 0; @@ -3031,9 +3033,44 @@ static int expire_register(void *data) static int sip_poke_peer(struct sip_peer *peer); +static void reg_source_db(struct sip_peer *p) +{ + char data[80]; + struct in_addr in; + char *c, *d; + int expiry; + if (!ast_db_get("SIP/Registry", p->name, data, sizeof(data))) { + c = strchr(data, ':'); + if (c) { + *c = '\0'; + c++; + if (inet_aton(data, &in)) { + d = strchr(c, ':'); + if (d) { + *d = '\0'; + d++; + ast_verbose(VERBOSE_PREFIX_3 "SIP Seeding '%s' at %s:%d for %d\n", p->name, + inet_ntoa(in), atoi(c), atoi(d)); + sip_poke_peer(p); + expiry = atoi(d); + memset(&p->addr, 0, sizeof(p->addr)); + p->addr.sin_family = AF_INET; + p->addr.sin_addr = in; + p->addr.sin_port = htons(atoi(c)); + if (p->expire > -1) + ast_sched_del(sched, p->expire); + p->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, (void *)p); + } + + } + } + } +} + static int parse_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req) { char contact[80]= ""; + char data[256]; char *expires = get_header(req, "Expires"); int expiry = atoi(expires); char *c, *n, *pt; @@ -3068,7 +3105,7 @@ static int parse_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_req ast_sched_del(sched, p->expire); p->expire = -1; if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", p->username); + ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", p->name); return 0; } /* Make sure it's a SIP URL */ @@ -3124,8 +3161,10 @@ static int parse_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_req pvt->expiry = expiry; if (inaddrcmp(&p->addr, &oldsin)) { sip_poke_peer(p); + snprintf(data, sizeof(data), "%s:%d:%d", inet_ntoa(p->addr.sin_addr), ntohs(p->addr.sin_port), expiry); + ast_db_put("SIP/Registry", p->name, data); if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s port %d expires %d\n", p->username, inet_ntoa(p->addr.sin_addr), ntohs(p->addr.sin_port), expiry); + ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s port %d expires %d\n", p->name, inet_ntoa(p->addr.sin_addr), ntohs(p->addr.sin_port), expiry); } return 0; } @@ -5606,6 +5645,8 @@ static struct sip_peer *build_peer(char *name, struct ast_variable *v) } if (!strlen(peer->methods)) strcpy(peer->methods, "md5,plaintext"); + if (!found && peer->dynamic) + reg_source_db(peer); peer->delme = 0; } return peer; @@ -5876,6 +5917,15 @@ int load_module() int res; struct sip_peer *peer; struct sip_registry *reg; + sched = sched_context_create(); + if (!sched) { + ast_log(LOG_WARNING, "Unable to create schedule context\n"); + } + io = io_context_create(); + if (!io) { + ast_log(LOG_WARNING, "Unable to create I/O context\n"); + } + res = reload_config(); if (!res) { /* Make sure we can register our sip channel type */ @@ -5893,15 +5943,6 @@ int load_module() ast_cli_register(&cli_inuse_show); sip_rtp.type = type; ast_rtp_proto_register(&sip_rtp); - sched = sched_context_create(); - if (!sched) { - ast_log(LOG_WARNING, "Unable to create schedule context\n"); - } - io = io_context_create(); - if (!io) { - ast_log(LOG_WARNING, "Unable to create I/O context\n"); - } - ast_mutex_lock(&peerl.lock); for (peer = peerl.peers; peer; peer = peer->next) sip_poke_peer(peer);