// Author: Robert Rusecki // Company: Kolmisoft // Year: 2020 // About: MNP module for m2. #define M2_MNP_VERSION "0.1" #define TEST 0 #define RHOST "127.0.0.1" #define REDPORT 6379 #define RPASS "mypass" #define LOAD_DATA_ON_START 1 #define CACHE_EMPTY 2 #define CACHE_NO_LNP 1 #define CACHE_VALUE_FOUND 3 #define CACHE_REDIS_NO_LNP "-1" #define SIP_HEADER "x-M2-lnp" #define AVP_CACHE_EMPTY "1" #define AVP_CACHE_FOUND "0" #define AVP_CACHE_NO_LNP "-1" #define ANELEMS(x) (sizeof(x) / sizeof((x)[0])) typedef struct calldata_struct { int o_id; char o_ip[64]; char src[256]; char dst[256]; char *redis_value; int cache_code; struct sip_msg *msg; char *key; } calldata_t; typedef struct lnp_contact_struct { str uri; str name; struct lnp_contact_struct *next; } lnp_contact_t; typedef struct contacts_header { str header; } contact_h_t; static int mod_init(void); static int child_init(int rank); static void mod_destroy(void); int open_mysql_connection(); int validate_params(); int open_redis_connection(); int find_cip_in_redis (calldata_t *cd); void find_cip_in_mysql (calldata_t *cd); void cache_cip_to_redis (calldata_t *cd); void add_cip_to_invite(calldata_t *cd); int add_lnp_header(struct sip_msg *msg, char *value); // proceed contacts; int mnp_get_contacts(struct sip_msg *msg); hdr_field_t *get_contact_header(struct sip_msg *msg, struct sip_msg *rpl_msg); lnp_contact_t *get_contacts_list(hdr_field_t *contact_hdr); // get contact from redis static int m2_mnp(struct sip_msg *msg); int process_lnp(calldata_t *cd); int find_contact_transfer_in_redis(calldata_t *cd); char *redis_get_key(struct sip_msg *msg); hdr_field_t *get_to_header(struct sip_msg *msg); hdr_field_t *get_from_header(struct sip_msg *msg); int cache_contact_transfer_to_redis(lnp_contact_t *contacts, struct sip_msg *msg); int cache_empty_contact_transfer_to_redis(struct sip_msg *msg); int check_if_response_with_no_lnp(char *response); /* the parameters are not used, they are only meant as an example*/ char* string_param = 0; int int_param = 0; str str_param = STR_STATIC_INIT(""); ndb_redis_api_t _m3_redis_api = {0}; redisc_server_t *rsrv = NULL; int routing_table_count; str _m3_redis_serverid = STR_NULL; // MYSQL variables str dbhost = STR_NULL; str dbuser = STR_NULL; str dbpassword = STR_NULL; str dbname = STR_NULL; int dbport = 0; // static cmd_export_t cmds[]={ {"m2_mnp", (cmd_function)m2_mnp, 0, 0, 0, REQUEST_ROUTE}, {"m2_mnp_contacts", (cmd_function)mnp_get_contacts, 0, 0, 0, FAILURE_ROUTE}, {0, 0, 0, 0, 0, 0} }; static param_export_t params[]={ {"dbhost", PARAM_STR, &dbhost}, {"dbuser", PARAM_STR, &dbuser}, {"dbpassword", PARAM_STR, &dbpassword}, {"dbname", PARAM_STR, &dbname}, {"dbport", INT_PARAM, &dbport}, {0,0,0} }; static rpc_export_t m3_routing_rpc[]={ // {"m3_routing.update", update_rpc, update_rpc_doc, 0}, example {0,0,0,0} }; struct module_exports exports = { "m2_mnp", /* module name */ DEFAULT_DLFLAGS, /* dlopen flags */ cmds, /* exported functions */ params, /* exported parameters */ m3_routing_rpc, /* RPC method exports */ 0, /* exported pseudo-variables */ 0, /* response handling function */ mod_init, /* module initialization function */ child_init, /* per-child init function */ mod_destroy /* module destroy function */ };