# HG changeset patch # User Mychaela Falconia # Date 1692085104 28800 # Node ID e980fe9c186cda3b6d69ed009b4125055b12616a # Parent 257da5474e770756af1ef68f5170ce72948b9383 sip-in: convert to libnumdb2 diff -r 257da5474e77 -r e980fe9c186c sip-in/Makefile --- a/sip-in/Makefile Mon Aug 14 22:56:20 2023 -0800 +++ b/sip-in/Makefile Mon Aug 14 23:38:24 2023 -0800 @@ -5,7 +5,7 @@ disconnect.o invite_dup.o invite_init.o invite_resp.o main.o mgw_ops.o \ mgw_resp.o mgw_sock.o mncc_handle.o mncc_sig_out.o mncc_sock.o prack.o \ readconf.o retrans.o shutdown.o sip_ack.o sip_log.o sip_uas.o sip_udp.o -LIBS= ../libnumdb/libnumdb.a ../libsip/libsip.a ../libutil/libutil.a +LIBS= ../libnumdb2/libnumdb.a ../libsip/libsip.a ../libutil/libutil.a INSTBIN=/usr/local/bin all: ${PROG} diff -r 257da5474e77 -r e980fe9c186c sip-in/invite_init.c --- a/sip-in/invite_init.c Mon Aug 14 22:56:20 2023 -0800 +++ b/sip-in/invite_init.c Mon Aug 14 23:38:24 2023 -0800 @@ -12,6 +12,8 @@ #include #include #include +#include "../include/number_db_v2.h" +#include "../libnumdb2/lookup_func.h" #include "../libsip/parse.h" #include "../libsip/uas_basic.h" #include "../libsip/grok_from.h" @@ -35,7 +37,9 @@ struct sockaddr_in *sin; { static unsigned cycle_tag_num, cycle_sdp_addend; - char uri_user[13], *called_nanp; + char uri_user[13], *called_nanp, redir_nanp[11]; + struct owned_number_rec *own; + int is_redir; struct sip_msg_out resp; struct grok_from gfrom; struct supported_ext supp_ext; @@ -85,11 +89,24 @@ goto not_nanp; /* it is valid NANP - but is it one of ours? */ refresh_number_db(); - if (!is_nanp_locally_owned(called_nanp)) { + own = numdb_lookup_nanp(called_nanp); + if (!own) { start_response_out_msg(&resp, "404 Called number does not belong here"); goto error_resp; } + switch (own->usage & NUMBER_USAGE_MASK) { + case NUMBER_USAGE_TYPE_GSM_SUB: + is_redir = 0; + break; + case NUMBER_USAGE_TYPE_ALIAS: + is_redir = 1; + sprintf(redir_nanp, "%03u%03u%04u", + own->remap[0], own->remap[1], own->remap[2]); + break; + default: + is_redir = -1; /* log CDR before rejecting */ + } /* parse and validate From header */ rc = grok_from_header(ess->from, &gfrom); if (rc < 0) { @@ -201,6 +218,16 @@ sprintf(cdr_str, "%.*s", cdr_num_len, gfrom.user); hval = get_single_header(req, "P-Asserted-Identity", (char *) 0, (int *) 0); + /* weed out calls to unassigned numbers */ + if (is_redir < 0) { + syslog(LOG_INFO, "Call from %s to %s: unassigned number", + cdr_str, uri_user); + if (hval) + syslog(LOG_INFO, "Down-call PAI: %.100s", hval); + start_response_out_msg(&resp, + "404 Called number is unassigned"); + goto error_resp; + } /* check if GSM service is up */ rc = connect_gsm_mtcall(); if (rc < 0) { @@ -250,7 +277,7 @@ bcopy(ess->via, dp, copylen); call->invite_cseq = ess->cseq_num; bcopy(sin, &call->udp_sin, sizeof(struct sockaddr_in)); - bcopy(called_nanp, call->called_nanp, 11); + bcopy(is_redir ? redir_nanp : called_nanp, call->called_nanp, 11); call->from_uri = call->invite_from + (gfrom.uri - ess->from); call->from_uri_len = gfrom.uri_len; call->from_user = call->invite_from + (gfrom.user - ess->from); @@ -277,6 +304,9 @@ call_list = call; syslog(LOG_INFO, "Call in%06u from %s to %s", call->in_tag_num, cdr_str, uri_user); + if (is_redir) + syslog(LOG_INFO, "Call in%06u redirected to %s", + call->in_tag_num, redir_nanp); if (hval) syslog(LOG_INFO, "Call in%06u PAI: %.100s", call->in_tag_num, hval);