Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kamailio/kamailio
Browse files Browse the repository at this point in the history
  • Loading branch information
juha-h committed Oct 16, 2017
2 parents 8596e84 + cf0814d commit 02074c8
Show file tree
Hide file tree
Showing 14 changed files with 1,092 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.defs
Expand Up @@ -107,7 +107,7 @@ INSTALL_FLAVOUR=$(FLAVOUR)
VERSION = 5
PATCHLEVEL = 1
SUBLEVEL = 0
EXTRAVERSION = -dev7
EXTRAVERSION = -dev8

# memory manager switcher
# 0 - f_malloc (fast malloc)
Expand Down
11 changes: 9 additions & 2 deletions src/Makefile.groups
Expand Up @@ -15,7 +15,7 @@ mod_list_basic=async auth benchmark blst cfg_rpc cfgutils corex counters \
nat_traversal nathelper path pike pv ratelimit rr rtimer \
rtpproxy sanity sdpops siputils sl statistics textops \
textopsx tm tmx topoh xlog rtpengine stun sipt tcpops \
auth_xkeys smsops tsilo cfgt statsc topos
auth_xkeys smsops tsilo cfgt statsc topos sipdump

# - extra used modules, with no extra dependency
mod_list_extra=avp auth_diameter call_control call_obj dmq domainpolicy msrp \
Expand Down Expand Up @@ -199,6 +199,9 @@ mod_list_nsq=nsq
# - modules depending on librabbitmq library
mod_list_rabbitmq=rabbitmq

# - modules depending on libphonenumber library
mod_list_phonenum=phonenum

# - all modules
mod_list_all=$(sort $(mod_list_basic) $(mod_list_extra) \
$(mod_list_db) $(mod_list_dbuid) \
Expand Down Expand Up @@ -229,7 +232,8 @@ mod_list_all=$(sort $(mod_list_basic) $(mod_list_extra) \
$(mod_list_geoip2) \
$(mod_list_erlang) $(mod_list_systemd) \
$(mod_list_http_async) $(mod_list_nsq) \
$(mod_list_rabbitmq) $(mod_list_jsdt))
$(mod_list_rabbitmq) $(mod_list_jsdt)) \
$(mod_list_phonenum)



Expand Down Expand Up @@ -437,6 +441,9 @@ module_group_knsq=$(mod_list_nsq)
# K rabbitmq modules
module_group_krabbitmq=$(mod_list_rabbitmq)

# K phonenumber modules
module_group_kphonenum=$(mod_list_phonenum)

# list of static modules
#
static_modules:=
Expand Down
25 changes: 25 additions & 0 deletions src/modules/phonenum/Makefile
@@ -0,0 +1,25 @@
#
# WARNING: do not run this directly, it should be run by the master Makefile

include ../../Makefile.defs
auto_gen=
NAME=phonenum.so

CXX=g++
LD=g++
LIB_DIR = /opt/local/lib

LIBS= -L${LIB_DIR} cphonenumber.o -lphonenumber

DEFS+= -I/opt/local/include -DKAMAILIO_MOD_INTERFACE

CXXFLAGS=$(CFLAGS:-Wno-deprecated option=)
CXXFLAGS+=-Wno-write-strings -Wno-deprecated -Wno-unused-function -Wno-sign-compare -Wno-strict-aliasing

include ../../Makefile.modules

cphonenumber.o: cphonenumber.cpp cphonenumber.h
@echo "Compiling $<"
$(CXX) $(CXXFLAGS) $(CFLAGS) $(C_DEFS) $(DEFS) -c $< -o $@

phonenum.so: cphonenumber.o
138 changes: 138 additions & 0 deletions src/modules/phonenum/README
@@ -0,0 +1,138 @@
PHONENUM Module

Daniel-Constantin Mierla

asipto.com

Edited by

Daniel-Constantin Mierla

<miconda@gmail.com>

Copyright � 2017 Daniel-Constantin Mierla (asipto.com)
__________________________________________________________________

Table of Contents

1. Admin Guide

1. Overview
2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

3. Parameters

3.1. smode (int)

4. Functions

4.1. phonenum_match(num, pvc)

5. Pseudo Variables

List of Examples

1.1. Set smode parameter
1.2. phonenum_match usage

Chapter 1. Admin Guide

Table of Contents

1. Overview
2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

3. Parameters

3.1. smode (int)

4. Functions

4.1. phonenum_match(num, pvc)

5. Pseudo Variables

1. Overview

This module allows real-time queries against the libphonenumber to be
performed from the config script. With that it is possible to get
normalize and get details about a phone number.

More details about libphonenumber can be found at
https://github.com/googlei18n/libphonenumber.

This module exports a new class of pseudo-variables - $phn(pvc=>key) -
to enable access to the results of a query to the database.

Many queries can be done and store results in different containers to
be able to use in parallel.

2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

2.1. Kamailio Modules

The following modules must be loaded before this module:
* none.

2.2. External Libraries or Applications

The following libraries or applications must be installed before
running Kamailio with this module loaded:
* libphonenumber - the phone number library.

3. Parameters

3.1. smode (int)

3.1. smode (int)

Phone number search mode.

Default value is "0".

Example 1.1. Set smode parameter
...
modparam("phonenum", "smode", 0)
...

4. Functions

4.1. phonenum_match(num, pvc)

4.1. phonenum_match(num, pvc)

Match num against the libphonenumber and set the attributes inside the
pvc container. The function has to be called before accessing a key
via: $phn(pvc=>key).

The parameters can be static strings or strings with variables.

It can be used from ANY_ROUTE.

Example 1.2. phonenum_match usage
...
if(phonenum_match("1-484-555-8888", "src"))
xlog("number normalized to: $phn(src=>num)\n");
...

5. Pseudo Variables

* $phn(pvc=>key) - pvc is an identifier for this query result; it is
designated by the second parameter of phonenum_match(). The key can
be one of the following:
+ number - normalized phone number
+ country - country for phone number
+ region - region for phone number
+ operator - operator for phone number

Exported pseudo-variables are documented at
https://www.kamailio.org/wiki/.
179 changes: 179 additions & 0 deletions src/modules/phonenum/cphonenumber.cpp
@@ -0,0 +1,179 @@
/**
*
* Copyright (C) 2017 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include <phonenumbers/geocoding/phonenumber_offline_geocoder.h>
#include <phonenumbers/phonenumberutil.h>
#include <unicode/locid.h>
#include <string.h>
#include <cstring>
#include <string>
#include "cphonenumber.h"

using namespace i18n::phonenumbers;
using icu::Locale;
using std::string;

const PhoneNumberUtil& _phoneUtil(*PhoneNumberUtil::GetInstance());
static PhoneNumberOfflineGeocoder *_phoneGeoCoder = new PhoneNumberOfflineGeocoder();

const char* telnum_linetype(PhoneNumberUtil::PhoneNumberType ltype)
{
switch(ltype) {
case PhoneNumberUtil::FIXED_LINE:
return "fixed-line";
case PhoneNumberUtil::MOBILE:
return "mobile";
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself.
case PhoneNumberUtil::FIXED_LINE_OR_MOBILE:
return "fixed-line-or-mobile";
// Freephone lines
case PhoneNumberUtil::TOLL_FREE:
return "toll-free";
case PhoneNumberUtil::PREMIUM_RATE:
return "premium-rate";
// The cost of this call is shared between the caller and the recipient, and
// is hence typically less than PREMIUM_RATE calls. See
// http://en.wikipedia.org/wiki/Shared_Cost_Service for more information.
case PhoneNumberUtil::SHARED_COST:
return "shared-cost";
// Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
case PhoneNumberUtil::VOIP:
return "voip";
// A personal number is associated with a particular person, and may be
// routed to either a MOBILE or FIXED_LINE number. Some more information can
// be found here: http://en.wikipedia.org/wiki/Personal_Numbers
case PhoneNumberUtil::PERSONAL_NUMBER:
return "personal-number";
case PhoneNumberUtil::PAGER:
return "pager";
// Used for "Universal Access Numbers" or "Company Numbers". They may be
// further routed to specific offices, but allow one number to be used for a
// company.
case PhoneNumberUtil::UAN:
return "uan";
// Used for "Voice Mail Access Numbers".
case PhoneNumberUtil::VOICEMAIL:
return "voicemail";
// A phone number is of type UNKNOWN when it does not fit any of the known
// patterns for a specific region.
case PhoneNumberUtil::UNKNOWN:
return "unknown";
}
return "unknown";
}
int telnum_possible(char* number, char* region)
{
string numStr(number);
string regionStr(region);

bool isPossible = _phoneUtil.IsPossibleNumberForString(numStr, regionStr);
return (isPossible ? 1 : 0);
}

char* telnum_cc(char* number)
{
string numStr(number);
string defaultRegion("ZZ");
PhoneNumber parsedNumber;

PhoneNumberUtil::ErrorType error = _phoneUtil.Parse(numStr, defaultRegion, &parsedNumber);
if (error != PhoneNumberUtil::NO_PARSING_ERROR) {
return NULL;
}
string regionCode;
_phoneUtil.GetRegionCodeForNumber(parsedNumber, &regionCode);
return strdup(regionCode.c_str());
}

telnum_t* telnum_parse(char* number, char* region)
{
string numStr(number);
string regionStr(region);

PhoneNumber parsedNumber;
PhoneNumberUtil::ErrorType error = _phoneUtil.Parse(numStr, regionStr, &parsedNumber);
telnum_t* res = telnum_new(number);
if(res==NULL) {
return NULL;
}
if (error != PhoneNumberUtil::NO_PARSING_ERROR) {
string error = "Parsing number failed";
res->error = strdup(error.c_str());
return res;
}
if (!_phoneUtil.IsValidNumber(parsedNumber)) {
string error = "Invalid number";
res->error = strdup(error.c_str());
return res;
}
res->valid = 1;
string formattedNumber;
_phoneUtil.Format(parsedNumber, PhoneNumberUtil::E164, &formattedNumber);
res->normalized = strdup(formattedNumber.c_str());
string descNumber = _phoneGeoCoder->GetDescriptionForNumber(parsedNumber, Locale("en"));
res->ndesc = strdup(descNumber.c_str());
res->ltype = strdup(telnum_linetype(_phoneUtil.GetNumberType(parsedNumber)));
res->cctel = _phoneUtil.GetCountryCodeForRegion(regionStr);

return res;
}

telnum_t* telnum_new(char* number)
{
telnum_t* tn = (telnum_t*)malloc(sizeof(telnum_t));
if(tn==NULL) {
return NULL;
}
tn->valid = 0;
tn->cctel = 0;
tn->number = strdup(number);
tn->normalized = NULL;
tn->ltype = NULL;
tn->ndesc = NULL;
tn->error = NULL;
return tn;
}

void telnum_free(telnum_t* tn)
{
if(tn==NULL) {
return;
}
if (tn->number) {
free(tn->number);
}
if (tn->normalized) {
free(tn->normalized);
}
if (tn->error) {
free(tn->error);
}
if (tn->ltype) {
free(tn->ltype);
}
if (tn->ndesc) {
free(tn->ndesc);
}
free(tn);
}

0 comments on commit 02074c8

Please sign in to comment.