From eb8c5c4283753793576a52831b2da50b1cab242f Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 17 May 2023 16:37:04 +0200 Subject: [PATCH] osp: clang-format for coherent indentation and coding style --- src/modules/osp/destination.c | 463 ++++++++------- src/modules/osp/destination.h | 72 +-- src/modules/osp/globals.c | 26 +- src/modules/osp/orig_transaction.c | 854 +++++++++++++--------------- src/modules/osp/orig_transaction.h | 11 +- src/modules/osp/osp_mod.c | 458 +++++++-------- src/modules/osp/osp_mod.h | 48 +- src/modules/osp/osptoolkit.c | 187 +++--- src/modules/osp/osptoolkit.h | 21 +- src/modules/osp/provider.c | 119 ++-- src/modules/osp/provider.h | 1 - src/modules/osp/sipheader.c | 802 +++++++++++++------------- src/modules/osp/sipheader.h | 34 +- src/modules/osp/term_transaction.c | 226 ++++---- src/modules/osp/term_transaction.h | 5 +- src/modules/osp/tm.c | 130 ++--- src/modules/osp/tm.h | 1 - src/modules/osp/usage.c | 873 +++++++++++++---------------- src/modules/osp/usage.h | 10 +- 19 files changed, 2055 insertions(+), 2286 deletions(-) diff --git a/src/modules/osp/destination.c b/src/modules/osp/destination.c index 2aeb127921d..5a577fbeb1f 100644 --- a/src/modules/osp/destination.c +++ b/src/modules/osp/destination.c @@ -43,8 +43,8 @@ const str OSP_ORIGDEST_NAME = {"_osp_orig_dests_", 16}; const str OSP_TERMDEST_NAME = {"_osp_term_dests_", 16}; -static int ospSaveDestination(osp_dest* dest, const str* name); -static void ospRecordCode(int code, osp_dest* dest); +static int ospSaveDestination(osp_dest *dest, const str *name); +static void ospRecordCode(int code, osp_dest *dest); static int ospIsToReportUsage(int code); /* @@ -52,17 +52,17 @@ static int ospIsToReportUsage(int code); * param dest Destination data structure * return initialized destination sturcture */ -osp_dest* ospInitDestination( - osp_dest* dest) +osp_dest *ospInitDestination(osp_dest *dest) { - memset(dest, 0, sizeof(osp_dest)); + memset(dest, 0, sizeof(osp_dest)); - dest->callidsize = sizeof(dest->callid); - dest->tokensize = sizeof(dest->token); + dest->callidsize = sizeof(dest->callid); + dest->tokensize = sizeof(dest->token); - LM_DBG("callidsize '%d' tokensize '%d'\n", dest->callidsize, dest->tokensize); + LM_DBG("callidsize '%d' tokensize '%d'\n", dest->callidsize, + dest->tokensize); - return dest; + return dest; } /* @@ -73,28 +73,27 @@ osp_dest* ospInitDestination( * param name Name of AVP * return 0 success, -1 failure */ -static int ospSaveDestination( - osp_dest* dest, - const str* name) +static int ospSaveDestination(osp_dest *dest, const str *name) { - str wrapper; - int result = -1; + str wrapper; + int result = -1; - wrapper.s = (char*)dest; - wrapper.len = sizeof(osp_dest); + wrapper.s = (char *)dest; + wrapper.len = sizeof(osp_dest); - /* + /* * add_avp will make a private copy of both the name and value in shared memory * which will be released by TM at the end of the transaction */ - if (add_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)*name, (int_str)wrapper) == 0) { - LM_DBG("destination saved\n"); - result = 0; - } else { - LM_ERR("failed to save destination\n"); - } - - return result; + if(add_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)*name, (int_str)wrapper) + == 0) { + LM_DBG("destination saved\n"); + result = 0; + } else { + LM_ERR("failed to save destination\n"); + } + + return result; } /* @@ -102,10 +101,9 @@ static int ospSaveDestination( * param dest Originate destination structure * return 0 success, -1 failure */ -int ospSaveOrigDestination( - osp_dest* dest) +int ospSaveOrigDestination(osp_dest *dest) { - return ospSaveDestination(dest, &OSP_ORIGDEST_NAME); + return ospSaveDestination(dest, &OSP_ORIGDEST_NAME); } /* @@ -113,10 +111,9 @@ int ospSaveOrigDestination( * param dest Terminate destination structure * return 0 success, -1 failure */ -int ospSaveTermDestination( - osp_dest* dest) +int ospSaveTermDestination(osp_dest *dest) { - return ospSaveDestination(dest, &OSP_TERMDEST_NAME); + return ospSaveDestination(dest, &OSP_TERMDEST_NAME); } /* @@ -128,39 +125,38 @@ int ospSaveTermDestination( */ int ospCheckOrigDestination(void) { - struct usr_avp* destavp = NULL; - int_str destval; - osp_dest* dest = NULL; - int result = -1; + struct usr_avp *destavp = NULL; + int_str destval; + osp_dest *dest = NULL; + int result = -1; struct search_state st; - for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, NULL, &st); - destavp != NULL; - destavp = search_next_avp(&st, NULL)) - { - get_avp_val(destavp, &destval); - - /* OSP destintaion is wrapped in a string */ - dest = (osp_dest*)destval.s.s; - - if (dest->used == 0) { - if (dest->supported == 1) { - LM_DBG("orig dest exist\n"); - result = 0; - break; - } else { - LM_DBG("destination does not been supported\n"); - } - } else { - LM_DBG("destination has already been used\n"); - } - } - - if (result == -1) { - LM_DBG("there is not unused destination\n"); - } - - return result; + for(destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, + (int_str)OSP_ORIGDEST_NAME, NULL, &st); + destavp != NULL; destavp = search_next_avp(&st, NULL)) { + get_avp_val(destavp, &destval); + + /* OSP destintaion is wrapped in a string */ + dest = (osp_dest *)destval.s.s; + + if(dest->used == 0) { + if(dest->supported == 1) { + LM_DBG("orig dest exist\n"); + result = 0; + break; + } else { + LM_DBG("destination does not been supported\n"); + } + } else { + LM_DBG("destination has already been used\n"); + } + } + + if(result == -1) { + LM_DBG("there is not unused destination\n"); + } + + return result; } /* @@ -172,46 +168,45 @@ int ospCheckOrigDestination(void) * return it, and mark it as used (used==1). * return NULL on failure */ -osp_dest* ospGetNextOrigDestination(void) +osp_dest *ospGetNextOrigDestination(void) { - struct usr_avp* destavp = NULL; - int_str destval; - osp_dest* dest = NULL; - osp_dest* result = NULL; + struct usr_avp *destavp = NULL; + int_str destval; + osp_dest *dest = NULL; + osp_dest *result = NULL; struct search_state st; - for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, NULL, &st); - destavp != NULL; - destavp = search_next_avp(&st, NULL)) - { - get_avp_val(destavp, &destval); - - /* OSP destintaion is wrapped in a string */ - dest = (osp_dest*)destval.s.s; - - if (dest->used == 0) { - if (dest->supported == 1) { - LM_DBG("orig dest found\n"); - dest->used = 1; - result = dest; - break; - } else { - /* Make it looks like used */ - dest->used = 1; - /* 111 means wrong protocol */ - dest->lastcode = 111; - LM_DBG("destination does not been supported\n"); - } - } else { - LM_DBG("destination has already been used\n"); - } - } - - if (result == NULL) { - LM_DBG("there is not unused destination\n"); - } - - return result; + for(destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, + (int_str)OSP_ORIGDEST_NAME, NULL, &st); + destavp != NULL; destavp = search_next_avp(&st, NULL)) { + get_avp_val(destavp, &destval); + + /* OSP destintaion is wrapped in a string */ + dest = (osp_dest *)destval.s.s; + + if(dest->used == 0) { + if(dest->supported == 1) { + LM_DBG("orig dest found\n"); + dest->used = 1; + result = dest; + break; + } else { + /* Make it looks like used */ + dest->used = 1; + /* 111 means wrong protocol */ + dest->lastcode = 111; + LM_DBG("destination does not been supported\n"); + } + } else { + LM_DBG("destination has already been used\n"); + } + } + + if(result == NULL) { + LM_DBG("there is not unused destination\n"); + } + + return result; } /* @@ -225,34 +220,33 @@ osp_dest* ospGetNextOrigDestination(void) * be wrong for loop condition. * return NULL on failure */ -osp_dest* ospGetLastOrigDestination(void) +osp_dest *ospGetLastOrigDestination(void) { - struct usr_avp* destavp = NULL; - int_str destval; - osp_dest* dest = NULL; - osp_dest* lastdest = NULL; + struct usr_avp *destavp = NULL; + int_str destval; + osp_dest *dest = NULL; + osp_dest *lastdest = NULL; struct search_state st; - for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, NULL, &st); - destavp != NULL; - destavp = search_next_avp(&st, NULL)) - { - get_avp_val(destavp, &destval); - - /* OSP destination is wrapped in a string */ - dest = (osp_dest*)destval.s.s; - - if (dest->used == 1) { - if (dest->supported == 1) { - lastdest = dest; - LM_DBG("curent destination '%s'\n", lastdest->host); - } - } else { - break; - } - } - - return lastdest; + for(destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, + (int_str)OSP_ORIGDEST_NAME, NULL, &st); + destavp != NULL; destavp = search_next_avp(&st, NULL)) { + get_avp_val(destavp, &destval); + + /* OSP destination is wrapped in a string */ + dest = (osp_dest *)destval.s.s; + + if(dest->used == 1) { + if(dest->supported == 1) { + lastdest = dest; + LM_DBG("curent destination '%s'\n", lastdest->host); + } + } else { + break; + } + } + + return lastdest; } /* @@ -262,19 +256,21 @@ osp_dest* ospGetLastOrigDestination(void) * There can be 0 or 1 term destinations. Find and return it. * return NULL on failure (no terminate destination) */ -osp_dest* ospGetTermDestination(void) +osp_dest *ospGetTermDestination(void) { - int_str destval; - osp_dest* dest = NULL; + int_str destval; + osp_dest *dest = NULL; - if (search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_TERMDEST_NAME, &destval, 0) != NULL) { - /* OSP destination is wrapped in a string */ - dest = (osp_dest*)destval.s.s; + if(search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_TERMDEST_NAME, + &destval, 0) + != NULL) { + /* OSP destination is wrapped in a string */ + dest = (osp_dest *)destval.s.s; - LM_DBG("term dest found\n"); - } + LM_DBG("term dest found\n"); + } - return dest; + return dest; } /* @@ -282,42 +278,40 @@ osp_dest* ospGetTermDestination(void) * param code Destination status * param dest Destination */ -static void ospRecordCode( - int code, - osp_dest* dest) +static void ospRecordCode(int code, osp_dest *dest) { - LM_DBG("code '%d'\n", code); - dest->lastcode = code; - - switch (code) { - case 100: - if (!dest->time100) { - dest->time100 = time(NULL); - } else { - LM_DBG("100 already recorded\n"); - } - break; - case 180: - case 181: - case 182: - case 183: - if (!dest->time180) { - dest->time180 = time(NULL); - } else { - LM_DBG("180, 181, 182 or 183 allready recorded\n"); - } - break; - case 200: - case 202: - if (!dest->time200) { - dest->time200 = time(NULL); - } else { - LM_DBG("200 or 202 allready recorded\n"); - } - break; - default: - LM_DBG("will not record time for '%d'\n", code); - } + LM_DBG("code '%d'\n", code); + dest->lastcode = code; + + switch(code) { + case 100: + if(!dest->time100) { + dest->time100 = time(NULL); + } else { + LM_DBG("100 already recorded\n"); + } + break; + case 180: + case 181: + case 182: + case 183: + if(!dest->time180) { + dest->time180 = time(NULL); + } else { + LM_DBG("180, 181, 182 or 183 allready recorded\n"); + } + break; + case 200: + case 202: + if(!dest->time200) { + dest->time200 = time(NULL); + } else { + LM_DBG("200 or 202 allready recorded\n"); + } + break; + default: + LM_DBG("will not record time for '%d'\n", code); + } } /* @@ -325,17 +319,16 @@ static void ospRecordCode( * param code Destination status * return 1 should report, 0 should not report */ -static int ospIsToReportUsage( - int code) +static int ospIsToReportUsage(int code) { - int istime = 0; + int istime = 0; - LM_DBG("code '%d'\n", code); - if (code >= 200) { - istime = 1; - } + LM_DBG("code '%d'\n", code); + if(code >= 200) { + istime = 1; + } - return istime; + return istime; } /* @@ -343,44 +336,41 @@ static int ospIsToReportUsage( * param clientcode Client status * param servercode Server status */ -void ospRecordEvent( - int clientcode, - int servercode) +void ospRecordEvent(int clientcode, int servercode) { - osp_dest* dest; + osp_dest *dest; - LM_DBG("client status '%d'\n", clientcode); - if ((clientcode != 0) && (dest = ospGetLastOrigDestination())) { - ospRecordCode(clientcode, dest); + LM_DBG("client status '%d'\n", clientcode); + if((clientcode != 0) && (dest = ospGetLastOrigDestination())) { + ospRecordCode(clientcode, dest); - if (ospIsToReportUsage(servercode) == 1) { - ospReportOrigSetupUsage(); - } - } + if(ospIsToReportUsage(servercode) == 1) { + ospReportOrigSetupUsage(); + } + } - LM_DBG("server status '%d'\n", servercode); - if ((servercode != 0) && (dest = ospGetTermDestination())) { - ospRecordCode(servercode, dest); + LM_DBG("server status '%d'\n", servercode); + if((servercode != 0) && (dest = ospGetTermDestination())) { + ospRecordCode(servercode, dest); - if (ospIsToReportUsage(servercode) == 1) { - ospReportTermSetupUsage(); - } - } + if(ospIsToReportUsage(servercode) == 1) { + ospReportTermSetupUsage(); + } + } } /* * Dump destination information * param dest Destination */ -void ospDumpDestination( - osp_dest* dest) +void ospDumpDestination(osp_dest *dest) { - LM_DBG("dest->host..........'%s'\n", dest->host); - LM_DBG("dest->used..........'%d'\n", dest->used); - LM_DBG("dest->lastcode......'%d'\n", dest->lastcode); - LM_DBG("dest->time100.......'%d'\n", (unsigned int)dest->time100); - LM_DBG("dest->time180.......'%d'\n", (unsigned int)dest->time180); - LM_DBG("dest->time200.......'%d'\n", (unsigned int)dest->time200); + LM_DBG("dest->host..........'%s'\n", dest->host); + LM_DBG("dest->used..........'%d'\n", dest->used); + LM_DBG("dest->lastcode......'%d'\n", dest->lastcode); + LM_DBG("dest->time100.......'%d'\n", (unsigned int)dest->time100); + LM_DBG("dest->time180.......'%d'\n", (unsigned int)dest->time180); + LM_DBG("dest->time200.......'%d'\n", (unsigned int)dest->time200); } /* @@ -388,39 +378,40 @@ void ospDumpDestination( */ void ospDumpAllDestination(void) { - struct usr_avp* destavp = NULL; - int_str destval; - osp_dest* dest = NULL; - int count = 0; + struct usr_avp *destavp = NULL; + int_str destval; + osp_dest *dest = NULL; + int count = 0; struct search_state st; - for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, NULL, &st); - destavp != NULL; - destavp = search_next_avp(&st, NULL)) - { - get_avp_val(destavp, &destval); + for(destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, + (int_str)OSP_ORIGDEST_NAME, NULL, &st); + destavp != NULL; destavp = search_next_avp(&st, NULL)) { + get_avp_val(destavp, &destval); - /* OSP destination is wrapped in a string */ - dest = (osp_dest*)destval.s.s; + /* OSP destination is wrapped in a string */ + dest = (osp_dest *)destval.s.s; - LM_DBG("....originate '%d'....\n", count++); + LM_DBG("....originate '%d'....\n", count++); - ospDumpDestination(dest); - } - if (count == 0) { - LM_DBG("there is not originate destination AVP\n"); - } + ospDumpDestination(dest); + } + if(count == 0) { + LM_DBG("there is not originate destination AVP\n"); + } - if (search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_TERMDEST_NAME, &destval, 0) != NULL) { - /* OSP destination is wrapped in a string */ - dest = (osp_dest*)destval.s.s; + if(search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_TERMDEST_NAME, + &destval, 0) + != NULL) { + /* OSP destination is wrapped in a string */ + dest = (osp_dest *)destval.s.s; - LM_DBG("....terminate....\n"); + LM_DBG("....terminate....\n"); - ospDumpDestination(dest); - } else { - LM_DBG("there is not terminate destination AVP\n"); - } + ospDumpDestination(dest); + } else { + LM_DBG("there is not terminate destination AVP\n"); + } } /* @@ -429,17 +420,13 @@ void ospDumpAllDestination(void) * param dst Destination address * param buffersize Size of dst buffer */ -void ospConvertAddress( - char* src, - char* dst, - int buffersize) +void ospConvertAddress(char *src, char *dst, int buffersize) { - struct in_addr inp; + struct in_addr inp; - if (inet_aton(src, &inp) != 0) { - snprintf(dst, buffersize, "[%s]", src); - } else { - snprintf(dst, buffersize, "%s", src); - } + if(inet_aton(src, &inp) != 0) { + snprintf(dst, buffersize, "[%s]", src); + } else { + snprintf(dst, buffersize, "%s", src); + } } - diff --git a/src/modules/osp/destination.h b/src/modules/osp/destination.h index 27675fc3be6..4f5fdcb8dcf 100644 --- a/src/modules/osp/destination.h +++ b/src/modules/osp/destination.h @@ -34,46 +34,46 @@ #include #include "osp_mod.h" -typedef struct _osp_dest { - char validafter[OSP_STRBUF_SIZE]; - char validuntil[OSP_STRBUF_SIZE]; - char callid[OSP_STRBUF_SIZE]; - char called[OSP_STRBUF_SIZE]; - char calling[OSP_STRBUF_SIZE]; - char source[OSP_STRBUF_SIZE]; - char srcdev[OSP_STRBUF_SIZE]; - char host[OSP_STRBUF_SIZE]; - char destdev[OSP_STRBUF_SIZE]; - char networkid[OSP_STRBUF_SIZE]; - unsigned char token[OSP_TOKENBUF_SIZE]; - unsigned int callidsize; - unsigned int tokensize; - unsigned int timelimit; - int lastcode; - time_t authtime; - time_t time100; - time_t time180; - time_t time200; - int type; - unsigned long long transid; - int supported; - int used; - int reported; - unsigned int destinationCount; - char origcalled[OSP_STRBUF_SIZE]; +typedef struct _osp_dest +{ + char validafter[OSP_STRBUF_SIZE]; + char validuntil[OSP_STRBUF_SIZE]; + char callid[OSP_STRBUF_SIZE]; + char called[OSP_STRBUF_SIZE]; + char calling[OSP_STRBUF_SIZE]; + char source[OSP_STRBUF_SIZE]; + char srcdev[OSP_STRBUF_SIZE]; + char host[OSP_STRBUF_SIZE]; + char destdev[OSP_STRBUF_SIZE]; + char networkid[OSP_STRBUF_SIZE]; + unsigned char token[OSP_TOKENBUF_SIZE]; + unsigned int callidsize; + unsigned int tokensize; + unsigned int timelimit; + int lastcode; + time_t authtime; + time_t time100; + time_t time180; + time_t time200; + int type; + unsigned long long transid; + int supported; + int used; + int reported; + unsigned int destinationCount; + char origcalled[OSP_STRBUF_SIZE]; } osp_dest; -osp_dest* ospInitDestination(osp_dest* dest); -int ospSaveOrigDestination(osp_dest* dest); -int ospSaveTermDestination(osp_dest* dest); +osp_dest *ospInitDestination(osp_dest *dest); +int ospSaveOrigDestination(osp_dest *dest); +int ospSaveTermDestination(osp_dest *dest); int ospCheckOrigDestination(void); -osp_dest* ospGetNextOrigDestination(void); -osp_dest* ospGetLastOrigDestination(void); -osp_dest* ospGetTermDestination(void); +osp_dest *ospGetNextOrigDestination(void); +osp_dest *ospGetLastOrigDestination(void); +osp_dest *ospGetTermDestination(void); void ospRecordEvent(int clientcode, int servercode); -void ospDumpDestination(osp_dest* dest); +void ospDumpDestination(osp_dest *dest); void ospDumpAllDestination(void); -void ospConvertAddress(char* src, char* dst, int buffersize); +void ospConvertAddress(char *src, char *dst, int buffersize); #endif /* _OSP_MOD_DESTINATION_H_ */ - diff --git a/src/modules/osp/globals.c b/src/modules/osp/globals.c index d293bfa850d..b759e391540 100644 --- a/src/modules/osp/globals.c +++ b/src/modules/osp/globals.c @@ -34,18 +34,17 @@ #include "osp_mod.h" unsigned int _osp_sp_number; -char* _osp_sp_uris[OSP_DEF_SPS]; -unsigned long _osp_sp_weights[OSP_DEF_SPS] = { - OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, - OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, - OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, - OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT -}; -char* _osp_device_ip = NULL; -char* _osp_device_port = NULL; -unsigned char* _osp_private_key = NULL; -unsigned char* _osp_local_certificate = NULL; -unsigned char* _osp_ca_certificate = NULL; +char *_osp_sp_uris[OSP_DEF_SPS]; +unsigned long _osp_sp_weights[OSP_DEF_SPS] = {OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, + OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, + OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, + OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, OSP_DEF_WEIGHT, + OSP_DEF_WEIGHT, OSP_DEF_WEIGHT}; +char *_osp_device_ip = NULL; +char *_osp_device_port = NULL; +unsigned char *_osp_private_key = NULL; +unsigned char *_osp_local_certificate = NULL; +unsigned char *_osp_ca_certificate = NULL; int _osp_crypto_hw = OSP_DEF_HW; int _osp_validate_callid = OSP_DEF_CALLID; int _osp_token_format = OSP_DEF_TOKEN; @@ -60,9 +59,8 @@ int _osp_redir_uri = OSP_DEF_REDIRURI; char _osp_PRIVATE_KEY[OSP_KEYBUF_SIZE]; char _osp_LOCAL_CERTIFICATE[OSP_KEYBUF_SIZE]; char _osp_CA_CERTIFICATE[OSP_KEYBUF_SIZE]; -char* _osp_snid_avp = OSP_DEF_SNIDAVP; +char *_osp_snid_avp = OSP_DEF_SNIDAVP; int_str _osp_snid_avpname; unsigned short _osp_snid_avptype; OSPTPROVHANDLE _osp_provider = -1; - diff --git a/src/modules/osp/orig_transaction.c b/src/modules/osp/orig_transaction.c index cb9f2855f80..14bdaa89a2a 100644 --- a/src/modules/osp/orig_transaction.c +++ b/src/modules/osp/orig_transaction.c @@ -40,8 +40,8 @@ #include "sipheader.h" #include "usage.h" -extern char* _osp_device_ip; -extern char* _osp_device_port; +extern char *_osp_device_ip; +extern char *_osp_device_port; extern int _osp_max_dests; extern int _osp_redir_uri; extern int_str _osp_snid_avpname; @@ -55,9 +55,11 @@ const int OSP_MAIN_ROUTE = 1; const int OSP_BRANCH_ROUTE = 0; const str OSP_CALLING_NAME = {"_osp_calling_translated_", 24}; -static int ospLoadRoutes(OSPTTRANHANDLE transaction, int destcount, char* source, char* sourcedev, char* origcalled, time_t authtime); -static int ospPrepareDestination(struct sip_msg* msg, int isfirst, int type, int format); -static int ospSetRpid(struct sip_msg* msg, osp_dest* dest); +static int ospLoadRoutes(OSPTTRANHANDLE transaction, int destcount, + char *source, char *sourcedev, char *origcalled, time_t authtime); +static int ospPrepareDestination( + struct sip_msg *msg, int isfirst, int type, int format); +static int ospSetRpid(struct sip_msg *msg, osp_dest *dest); /* * Get routes from AuthRsp @@ -69,170 +71,132 @@ static int ospSetRpid(struct sip_msg* msg, osp_dest* dest); * param authtime Request authorization time * return 0 success, -1 failure */ -static int ospLoadRoutes( - OSPTTRANHANDLE transaction, - int destcount, - char* source, - char* sourcedev, - char* origcalled, - time_t authtime) +static int ospLoadRoutes(OSPTTRANHANDLE transaction, int destcount, + char *source, char *sourcedev, char *origcalled, time_t authtime) { - int count; - int errorcode; - osp_dest* dest; - osp_dest dests[OSP_DEF_DESTS]; - OSPE_DEST_PROT protocol; - OSPE_DEST_OSP_ENABLED enabled; - int result = 0; - - for (count = 0; count < destcount; count++) { - /* This is necessary because we will save destinations in reverse order */ - dest = ospInitDestination(&dests[count]); - - if (dest == NULL) { - result = -1; - break; - } - - dest->destinationCount = count + 1; - strncpy(dest->origcalled, origcalled, sizeof(dest->origcalled) - 1); - - if (count == 0) { - errorcode = OSPPTransactionGetFirstDestination( - transaction, - sizeof(dest->validafter), - dest->validafter, - dest->validuntil, - &dest->timelimit, - &dest->callidsize, - (void*)dest->callid, - sizeof(dest->called), - dest->called, - sizeof(dest->calling), - dest->calling, - sizeof(dest->host), - dest->host, - sizeof(dest->destdev), - dest->destdev, - &dest->tokensize, - dest->token); - } else { - errorcode = OSPPTransactionGetNextDestination( - transaction, - 0, - sizeof(dest->validafter), - dest->validafter, - dest->validuntil, - &dest->timelimit, - &dest->callidsize, - (void*)dest->callid, - sizeof(dest->called), - dest->called, - sizeof(dest->calling), - dest->calling, - sizeof(dest->host), - dest->host, - sizeof(dest->destdev), - dest->destdev, - &dest->tokensize, - dest->token); - } - - if (errorcode != OSPC_ERR_NO_ERROR) { - LM_ERR("failed to load routes (%d) expected '%d' current '%d'\n", - errorcode, - destcount, - count); - result = -1; - break; - } - - errorcode = OSPPTransactionGetDestProtocol(transaction, &protocol); - if (errorcode != OSPC_ERR_NO_ERROR) { - /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ - LM_DBG("cannot get dest protocol (%d)\n", errorcode); - protocol = OSPE_DEST_PROT_SIP; - } - switch (protocol) { - case OSPE_DEST_PROT_H323_LRQ: - case OSPE_DEST_PROT_H323_SETUP: - case OSPE_DEST_PROT_IAX: - dest->supported = 0; - break; - case OSPE_DEST_PROT_SIP: - case OSPE_DEST_PROT_UNDEFINED: - case OSPE_DEST_PROT_UNKNOWN: - default: - dest->supported = 1; - break; - } - - errorcode = OSPPTransactionIsDestOSPEnabled(transaction, &enabled); - if (errorcode != OSPC_ERR_NO_ERROR) { - /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ - LM_DBG("cannot get dest OSP version (%d)\n", errorcode); - } else if (enabled == OSPE_OSP_FALSE) { - /* Destination device does not support OSP. Do not send token to it */ - dest->token[0] = '\0'; - dest->tokensize = 0; - } - - errorcode = OSPPTransactionGetDestNetworkId(transaction, dest->networkid); - if (errorcode != OSPC_ERR_NO_ERROR) { - /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ - LM_DBG("cannot get dest network ID (%d)\n", errorcode); - dest->networkid[0] = '\0'; - } - - strncpy(dest->source, source, sizeof(dest->source) - 1); - strncpy(dest->srcdev, sourcedev, sizeof(dest->srcdev) - 1); - dest->type = OSPC_SOURCE; - dest->transid = ospGetTransactionId(transaction); - dest->authtime = authtime; - - LM_INFO("get destination '%d': " - "valid after '%s' " - "valid until '%s' " - "time limit '%d' seconds " - "call id '%.*s' " - "calling '%s' " - "called '%s' " - "host '%s' " - "supported '%d' " - "network id '%s' " - "token size '%d'\n", - count, - dest->validafter, - dest->validuntil, - dest->timelimit, - dest->callidsize, - dest->callid, - dest->calling, - dest->called, - dest->host, - dest->supported, - dest->networkid, - dest->tokensize); - } - - /* + int count; + int errorcode; + osp_dest *dest; + osp_dest dests[OSP_DEF_DESTS]; + OSPE_DEST_PROT protocol; + OSPE_DEST_OSP_ENABLED enabled; + int result = 0; + + for(count = 0; count < destcount; count++) { + /* This is necessary because we will save destinations in reverse order */ + dest = ospInitDestination(&dests[count]); + + if(dest == NULL) { + result = -1; + break; + } + + dest->destinationCount = count + 1; + strncpy(dest->origcalled, origcalled, sizeof(dest->origcalled) - 1); + + if(count == 0) { + errorcode = OSPPTransactionGetFirstDestination(transaction, + sizeof(dest->validafter), dest->validafter, + dest->validuntil, &dest->timelimit, &dest->callidsize, + (void *)dest->callid, sizeof(dest->called), dest->called, + sizeof(dest->calling), dest->calling, sizeof(dest->host), + dest->host, sizeof(dest->destdev), dest->destdev, + &dest->tokensize, dest->token); + } else { + errorcode = OSPPTransactionGetNextDestination(transaction, 0, + sizeof(dest->validafter), dest->validafter, + dest->validuntil, &dest->timelimit, &dest->callidsize, + (void *)dest->callid, sizeof(dest->called), dest->called, + sizeof(dest->calling), dest->calling, sizeof(dest->host), + dest->host, sizeof(dest->destdev), dest->destdev, + &dest->tokensize, dest->token); + } + + if(errorcode != OSPC_ERR_NO_ERROR) { + LM_ERR("failed to load routes (%d) expected '%d' current '%d'\n", + errorcode, destcount, count); + result = -1; + break; + } + + errorcode = OSPPTransactionGetDestProtocol(transaction, &protocol); + if(errorcode != OSPC_ERR_NO_ERROR) { + /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ + LM_DBG("cannot get dest protocol (%d)\n", errorcode); + protocol = OSPE_DEST_PROT_SIP; + } + switch(protocol) { + case OSPE_DEST_PROT_H323_LRQ: + case OSPE_DEST_PROT_H323_SETUP: + case OSPE_DEST_PROT_IAX: + dest->supported = 0; + break; + case OSPE_DEST_PROT_SIP: + case OSPE_DEST_PROT_UNDEFINED: + case OSPE_DEST_PROT_UNKNOWN: + default: + dest->supported = 1; + break; + } + + errorcode = OSPPTransactionIsDestOSPEnabled(transaction, &enabled); + if(errorcode != OSPC_ERR_NO_ERROR) { + /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ + LM_DBG("cannot get dest OSP version (%d)\n", errorcode); + } else if(enabled == OSPE_OSP_FALSE) { + /* Destination device does not support OSP. Do not send token to it */ + dest->token[0] = '\0'; + dest->tokensize = 0; + } + + errorcode = + OSPPTransactionGetDestNetworkId(transaction, dest->networkid); + if(errorcode != OSPC_ERR_NO_ERROR) { + /* This does not mean an ERROR. The OSP server may not support OSP 2.1.1 */ + LM_DBG("cannot get dest network ID (%d)\n", errorcode); + dest->networkid[0] = '\0'; + } + + strncpy(dest->source, source, sizeof(dest->source) - 1); + strncpy(dest->srcdev, sourcedev, sizeof(dest->srcdev) - 1); + dest->type = OSPC_SOURCE; + dest->transid = ospGetTransactionId(transaction); + dest->authtime = authtime; + + LM_INFO("get destination '%d': " + "valid after '%s' " + "valid until '%s' " + "time limit '%d' seconds " + "call id '%.*s' " + "calling '%s' " + "called '%s' " + "host '%s' " + "supported '%d' " + "network id '%s' " + "token size '%d'\n", + count, dest->validafter, dest->validuntil, dest->timelimit, + dest->callidsize, dest->callid, dest->calling, dest->called, + dest->host, dest->supported, dest->networkid, dest->tokensize); + } + + /* * Save destination in reverse order, * when we start searching avps the destinations * will be in order */ - if (result == 0) { - for(count = destcount -1; count >= 0; count--) { - if (ospSaveOrigDestination(&dests[count]) == -1) { - LM_ERR("failed to save originate destination\n"); - /* Report terminate CDR */ - ospRecordEvent(0, 500); - result = -1; - break; - } - } - } - - return result; + if(result == 0) { + for(count = destcount - 1; count >= 0; count--) { + if(ospSaveOrigDestination(&dests[count]) == -1) { + LM_ERR("failed to save originate destination\n"); + /* Report terminate CDR */ + ospRecordEvent(0, 500); + result = -1; + break; + } + } + } + + return result; } /* @@ -242,137 +206,127 @@ static int ospLoadRoutes( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure, MODULE_RETURNCODE_ERROR error */ -int ospRequestRouting( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospRequestRouting(struct sip_msg *msg, char *ignore1, char *ignore2) { - int errorcode; - time_t authtime; - char called[OSP_E164BUF_SIZE]; - char calling[OSP_E164BUF_SIZE]; - char sourcedev[OSP_STRBUF_SIZE]; - char deviceinfo[OSP_STRBUF_SIZE]; - struct usr_avp* snidavp = NULL; - int_str snidval; - char snid[OSP_STRBUF_SIZE]; - unsigned int callidnumber = 1; - OSPTCALLID* callids[callidnumber]; - unsigned int logsize = 0; - char* detaillog = NULL; - const char** preferred = NULL; - unsigned int destcount; - OSPTTRANHANDLE transaction = -1; - int result = MODULE_RETURNCODE_FALSE; - - authtime = time(NULL); - - destcount = _osp_max_dests; - - if ((errorcode = OSPPTransactionNew(_osp_provider, &transaction)) != OSPC_ERR_NO_ERROR) { - LM_ERR("failed to create new OSP transaction (%d)\n", errorcode); - } else if ((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) && - (ospGetFromUserpart(msg, calling, sizeof(calling)) != 0)) - { - LM_ERR("failed to extract calling number\n"); - } else if ((ospGetUriUserpart(msg, called, sizeof(called)) != 0) && - (ospGetToUserpart(msg, called, sizeof(called)) != 0)) - { - LM_ERR("failed to extract called number\n"); - } else if (ospGetCallId(msg, &(callids[0])) != 0) { - LM_ERR("failed to extract call id\n"); - } else if (ospGetSourceAddress(msg, sourcedev, sizeof(sourcedev)) != 0) { - LM_ERR("failed to extract source deivce address\n"); - } else { - ospConvertAddress(sourcedev, deviceinfo, sizeof(deviceinfo)); - - if ((_osp_snid_avpname.n != 0) && - ((snidavp = search_first_avp(_osp_snid_avptype, _osp_snid_avpname, &snidval, 0)) != NULL) && - (snidavp->flags & AVP_VAL_STR) && (snidval.s.s && snidval.s.len)) - { - snprintf(snid, sizeof(snid), "%.*s", snidval.s.len, snidval.s.s); - OSPPTransactionSetNetworkIds(transaction, snid, ""); - } else { - snid[0] = '\0'; - } - - LM_INFO("request auth and routing for: " - "source_ip '%s' " - "source_port '%s' " - "source_dev '%s' " - "source_networkid '%s' " - "calling '%s' " - "called '%s' " - "call_id '%.*s' " - "dest_count '%d'\n", - _osp_device_ip, - _osp_device_port, - deviceinfo, /* in "[x.x.x.x]" or host.domain format */ - snid, - calling, - called, - callids[0]->ospmCallIdLen, - callids[0]->ospmCallIdVal, - destcount - ); - - /* try to request authorization */ - errorcode = OSPPTransactionRequestAuthorisation( - transaction, /* transaction handle */ - _osp_device_ip, /* from the configuration file */ - deviceinfo, /* source device of call, protocol specific, in OSP format */ - calling, /* calling number in nodotted e164 notation */ - OSPC_E164, /* calling number format */ - called, /* called number */ - OSPC_E164, /* called number format */ - "", /* optional username string, used if no number */ - callidnumber, /* number of call ids, here always 1 */ - callids, /* sized-1 array of call ids */ - preferred, /* preferred destinations, here always NULL */ - &destcount, /* max destinations, after call dest_count */ - &logsize, /* size allocated for detaillog (next param) 0=no log */ - detaillog); /* memory location for detaillog to be stored */ - - if ((errorcode == OSPC_ERR_NO_ERROR) && - (ospLoadRoutes(transaction, destcount, _osp_device_ip, sourcedev, called, authtime) == 0)) - { - LM_INFO("there are '%d' OSP routes, call_id '%.*s'\n", - destcount, - callids[0]->ospmCallIdLen, - callids[0]->ospmCallIdVal); - result = MODULE_RETURNCODE_TRUE; - } else { - LM_ERR("failed to request auth and routing (%d), call_id '%.*s'\n", - errorcode, - callids[0]->ospmCallIdLen, - callids[0]->ospmCallIdVal); - switch (errorcode) { - case OSPC_ERR_TRAN_ROUTE_BLOCKED: - result = -403; - break; - case OSPC_ERR_TRAN_ROUTE_NOT_FOUND: - result = -404; - break; - case OSPC_ERR_NO_ERROR: - /* AuthRsp ok but ospLoadRoutes fails */ - result = MODULE_RETURNCODE_ERROR; - break; - default: - result = MODULE_RETURNCODE_FALSE; - break; - } - } - } - - if (callids[0] != NULL) { - OSPPCallIdDelete(&(callids[0])); - } - - if (transaction != -1) { - OSPPTransactionDelete(transaction); - } - - return result; + int errorcode; + time_t authtime; + char called[OSP_E164BUF_SIZE]; + char calling[OSP_E164BUF_SIZE]; + char sourcedev[OSP_STRBUF_SIZE]; + char deviceinfo[OSP_STRBUF_SIZE]; + struct usr_avp *snidavp = NULL; + int_str snidval; + char snid[OSP_STRBUF_SIZE]; + unsigned int callidnumber = 1; + OSPTCALLID *callids[callidnumber]; + unsigned int logsize = 0; + char *detaillog = NULL; + const char **preferred = NULL; + unsigned int destcount; + OSPTTRANHANDLE transaction = -1; + int result = MODULE_RETURNCODE_FALSE; + + authtime = time(NULL); + + destcount = _osp_max_dests; + + if((errorcode = OSPPTransactionNew(_osp_provider, &transaction)) + != OSPC_ERR_NO_ERROR) { + LM_ERR("failed to create new OSP transaction (%d)\n", errorcode); + } else if((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) + && (ospGetFromUserpart(msg, calling, sizeof(calling)) != 0)) { + LM_ERR("failed to extract calling number\n"); + } else if((ospGetUriUserpart(msg, called, sizeof(called)) != 0) + && (ospGetToUserpart(msg, called, sizeof(called)) != 0)) { + LM_ERR("failed to extract called number\n"); + } else if(ospGetCallId(msg, &(callids[0])) != 0) { + LM_ERR("failed to extract call id\n"); + } else if(ospGetSourceAddress(msg, sourcedev, sizeof(sourcedev)) != 0) { + LM_ERR("failed to extract source deivce address\n"); + } else { + ospConvertAddress(sourcedev, deviceinfo, sizeof(deviceinfo)); + + if((_osp_snid_avpname.n != 0) + && ((snidavp = search_first_avp( + _osp_snid_avptype, _osp_snid_avpname, &snidval, 0)) + != NULL) + && (snidavp->flags & AVP_VAL_STR) + && (snidval.s.s && snidval.s.len)) { + snprintf(snid, sizeof(snid), "%.*s", snidval.s.len, snidval.s.s); + OSPPTransactionSetNetworkIds(transaction, snid, ""); + } else { + snid[0] = '\0'; + } + + LM_INFO("request auth and routing for: " + "source_ip '%s' " + "source_port '%s' " + "source_dev '%s' " + "source_networkid '%s' " + "calling '%s' " + "called '%s' " + "call_id '%.*s' " + "dest_count '%d'\n", + _osp_device_ip, _osp_device_port, + deviceinfo, /* in "[x.x.x.x]" or host.domain format */ + snid, calling, called, callids[0]->ospmCallIdLen, + callids[0]->ospmCallIdVal, destcount); + + /* try to request authorization */ + errorcode = OSPPTransactionRequestAuthorisation( + transaction, /* transaction handle */ + _osp_device_ip, /* from the configuration file */ + deviceinfo, /* source device of call, protocol specific, in OSP format */ + calling, /* calling number in nodotted e164 notation */ + OSPC_E164, /* calling number format */ + called, /* called number */ + OSPC_E164, /* called number format */ + "", /* optional username string, used if no number */ + callidnumber, /* number of call ids, here always 1 */ + callids, /* sized-1 array of call ids */ + preferred, /* preferred destinations, here always NULL */ + &destcount, /* max destinations, after call dest_count */ + &logsize, /* size allocated for detaillog (next param) 0=no log */ + detaillog); /* memory location for detaillog to be stored */ + + if((errorcode == OSPC_ERR_NO_ERROR) + && (ospLoadRoutes(transaction, destcount, _osp_device_ip, + sourcedev, called, authtime) + == 0)) { + LM_INFO("there are '%d' OSP routes, call_id '%.*s'\n", destcount, + callids[0]->ospmCallIdLen, callids[0]->ospmCallIdVal); + result = MODULE_RETURNCODE_TRUE; + } else { + LM_ERR("failed to request auth and routing (%d), call_id '%.*s'\n", + errorcode, callids[0]->ospmCallIdLen, + callids[0]->ospmCallIdVal); + switch(errorcode) { + case OSPC_ERR_TRAN_ROUTE_BLOCKED: + result = -403; + break; + case OSPC_ERR_TRAN_ROUTE_NOT_FOUND: + result = -404; + break; + case OSPC_ERR_NO_ERROR: + /* AuthRsp ok but ospLoadRoutes fails */ + result = MODULE_RETURNCODE_ERROR; + break; + default: + result = MODULE_RETURNCODE_FALSE; + break; + } + } + } + + if(callids[0] != NULL) { + OSPPCallIdDelete(&(callids[0])); + } + + if(transaction != -1) { + OSPPTransactionDelete(transaction); + } + + return result; } /* @@ -382,16 +336,13 @@ int ospRequestRouting( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure */ -int ospCheckRoute( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospCheckRoute(struct sip_msg *msg, char *ignore1, char *ignore2) { - if (ospCheckOrigDestination() == 0) { - return MODULE_RETURNCODE_TRUE; - } else { - return MODULE_RETURNCODE_FALSE; - } + if(ospCheckOrigDestination() == 0) { + return MODULE_RETURNCODE_TRUE; + } else { + return MODULE_RETURNCODE_FALSE; + } } /* @@ -400,55 +351,47 @@ int ospCheckRoute( * param dest Destination structure * return 0 success, 1 calling number same, -1 failure */ -static int ospSetRpid( - struct sip_msg* msg, - osp_dest* dest) +static int ospSetRpid(struct sip_msg *msg, osp_dest *dest) { - str rpid; - char calling[OSP_STRBUF_SIZE]; - char source[OSP_STRBUF_SIZE]; - char buffer[OSP_STRBUF_SIZE]; - int result = -1; - - if ((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) && - (ospGetFromUserpart(msg, calling, sizeof(calling)) !=0)) - { - LM_ERR("failed to extract calling number\n"); - return result; - } - - if (strcmp(calling, dest->calling) == 0) { - /* Do nothing for this case */ - result = 1; - } else if ((osp_siputils.rpid_avp.s.s == NULL) - || (osp_siputils.rpid_avp.s.len == 0)) { - LM_WARN("rpid_avp is not foune, cannot set rpid avp\n"); - result = -1; - } else { - if (dest->source[0] == '[') { - /* Strip "[]" */ - memset(source, 0, sizeof(source)); - strncpy(source, &dest->source[1], sizeof(source) - 1); - source[strlen(source) - 1] = '\0'; - } - - snprintf( - buffer, - sizeof(buffer), - "\"%s\" ", - dest->calling, - dest->calling, - source); - - rpid.s = buffer; - rpid.len = strlen(buffer); - add_avp(osp_siputils.rpid_avp_type | AVP_VAL_STR, + str rpid; + char calling[OSP_STRBUF_SIZE]; + char source[OSP_STRBUF_SIZE]; + char buffer[OSP_STRBUF_SIZE]; + int result = -1; + + if((ospGetRpidUserpart(msg, calling, sizeof(calling)) != 0) + && (ospGetFromUserpart(msg, calling, sizeof(calling)) != 0)) { + LM_ERR("failed to extract calling number\n"); + return result; + } + + if(strcmp(calling, dest->calling) == 0) { + /* Do nothing for this case */ + result = 1; + } else if((osp_siputils.rpid_avp.s.s == NULL) + || (osp_siputils.rpid_avp.s.len == 0)) { + LM_WARN("rpid_avp is not foune, cannot set rpid avp\n"); + result = -1; + } else { + if(dest->source[0] == '[') { + /* Strip "[]" */ + memset(source, 0, sizeof(source)); + strncpy(source, &dest->source[1], sizeof(source) - 1); + source[strlen(source) - 1] = '\0'; + } + + snprintf(buffer, sizeof(buffer), "\"%s\" ", dest->calling, + dest->calling, source); + + rpid.s = buffer; + rpid.len = strlen(buffer); + add_avp(osp_siputils.rpid_avp_type | AVP_VAL_STR, (int_str)osp_siputils.rpid_avp, (int_str)rpid); - result = 0; - } + result = 0; + } - return result; + return result; } /* @@ -459,26 +402,24 @@ static int ospSetRpid( * param ignore2 * return MODULE_RETURNCODE_TRUE calling number translated MODULE_RETURNCODE_FALSE without transaltion */ -int ospCheckTranslation( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospCheckTranslation(struct sip_msg *msg, char *ignore1, char *ignore2) { - int_str callingval; - int result = MODULE_RETURNCODE_FALSE; - - if (search_first_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, &callingval, 0) != NULL) { - if (callingval.n == 0) { - LM_DBG("the calling number does not been translated\n"); - } else { - LM_DBG("the calling number is translated\n"); - result = MODULE_RETURNCODE_TRUE; - } - } else { - LM_ERR("there is not calling translation avp\n"); - } - - return result; + int_str callingval; + int result = MODULE_RETURNCODE_FALSE; + + if(search_first_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, &callingval, 0) + != NULL) { + if(callingval.n == 0) { + LM_DBG("the calling number does not been translated\n"); + } else { + LM_DBG("the calling number is translated\n"); + result = MODULE_RETURNCODE_TRUE; + } + } else { + LM_ERR("there is not calling translation avp\n"); + } + + return result; } /* @@ -490,80 +431,79 @@ int ospCheckTranslation( * return MODULE_RETURNCODE_TRUE success MODULE_RETURNCODE_FALSE failure */ static int ospPrepareDestination( - struct sip_msg* msg, - int isfirst, - int type, - int format) + struct sip_msg *msg, int isfirst, int type, int format) { - int_str val; - int res; - str newuri = {NULL, 0}; - int result = MODULE_RETURNCODE_FALSE; - - osp_dest* dest = ospGetNextOrigDestination(); - - if (dest != NULL) { - ospRebuildDestionationUri(&newuri, dest->called, dest->host, "", format); - - LM_INFO("prepare route to URI '%.*s' for call_id '%.*s' transaction_id '%llu'\n", - newuri.len, - newuri.s, - dest->callidsize, - dest->callid, - dest->transid); - - if (type == OSP_MAIN_ROUTE) { - if (isfirst == OSP_FIRST_ROUTE) { - rewrite_uri(msg, &newuri); - } else { - km_append_branch(msg, &newuri, NULL, NULL, Q_UNSPECIFIED, 0, NULL); - } - /* Do not add route specific OSP information */ - result = MODULE_RETURNCODE_TRUE; - } else if (type == OSP_BRANCH_ROUTE) { - /* For branch route, add route specific OSP information */ - - /* Update the Request-Line */ - rewrite_uri(msg, &newuri); - - /* Add OSP token header */ - ospAddOspHeader(msg, dest->token, dest->tokensize); - - /* Add branch-specific OSP Cookie */ - ospRecordOrigTransaction(msg, dest->transid, dest->srcdev, dest->calling, dest->called, dest->authtime, dest->destinationCount); - - /* Add rpid avp for calling number translation */ - res = ospSetRpid(msg, dest); - switch (res) { - case 0: - /* Calling number is translated */ - val.n = 1; - add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val); - break; - default: - LM_DBG("cannot set rpid avp\n"); - /* Just like without calling translation */ - case 1: - /* Calling number does not been translated */ - val.n = 0; - add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val); - break; - } - - result = MODULE_RETURNCODE_TRUE; - } else { - LM_ERR("unsupported route block type\n"); - } - } else { - LM_DBG("there is no more routes\n"); - ospReportOrigSetupUsage(); - } - - if (newuri.len > 0) { - pkg_free(newuri.s); - } - - return result; + int_str val; + int res; + str newuri = {NULL, 0}; + int result = MODULE_RETURNCODE_FALSE; + + osp_dest *dest = ospGetNextOrigDestination(); + + if(dest != NULL) { + ospRebuildDestionationUri( + &newuri, dest->called, dest->host, "", format); + + LM_INFO("prepare route to URI '%.*s' for call_id '%.*s' transaction_id " + "'%llu'\n", + newuri.len, newuri.s, dest->callidsize, dest->callid, + dest->transid); + + if(type == OSP_MAIN_ROUTE) { + if(isfirst == OSP_FIRST_ROUTE) { + rewrite_uri(msg, &newuri); + } else { + km_append_branch( + msg, &newuri, NULL, NULL, Q_UNSPECIFIED, 0, NULL); + } + /* Do not add route specific OSP information */ + result = MODULE_RETURNCODE_TRUE; + } else if(type == OSP_BRANCH_ROUTE) { + /* For branch route, add route specific OSP information */ + + /* Update the Request-Line */ + rewrite_uri(msg, &newuri); + + /* Add OSP token header */ + ospAddOspHeader(msg, dest->token, dest->tokensize); + + /* Add branch-specific OSP Cookie */ + ospRecordOrigTransaction(msg, dest->transid, dest->srcdev, + dest->calling, dest->called, dest->authtime, + dest->destinationCount); + + /* Add rpid avp for calling number translation */ + res = ospSetRpid(msg, dest); + switch(res) { + case 0: + /* Calling number is translated */ + val.n = 1; + add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val); + break; + default: + LM_DBG("cannot set rpid avp\n"); + /* Just like without calling translation */ + case 1: + /* Calling number does not been translated */ + val.n = 0; + add_avp(AVP_NAME_STR, (int_str)OSP_CALLING_NAME, val); + break; + } + + result = MODULE_RETURNCODE_TRUE; + } else { + LM_ERR("unsupported route block type\n"); + } + } else { + LM_DBG("there is no more routes\n"); + ospReportOrigSetupUsage(); + } + + if(newuri.len > 0) { + pkg_free(newuri.s); + } + + return result; } /* @@ -575,17 +515,14 @@ static int ospPrepareDestination( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure */ -int ospPrepareRoute( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospPrepareRoute(struct sip_msg *msg, char *ignore1, char *ignore2) { - int result = MODULE_RETURNCODE_TRUE; + int result = MODULE_RETURNCODE_TRUE; - /* The first parameter will be ignored */ - result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_BRANCH_ROUTE, 0); + /* The first parameter will be ignored */ + result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_BRANCH_ROUTE, 0); - return result; + return result; } /* @@ -596,19 +533,16 @@ int ospPrepareRoute( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure */ -int ospPrepareAllRoutes( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospPrepareAllRoutes(struct sip_msg *msg, char *ignore1, char *ignore2) { - int result = MODULE_RETURNCODE_TRUE; + int result = MODULE_RETURNCODE_TRUE; - for(result = ospPrepareDestination(msg, OSP_FIRST_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri); - result == MODULE_RETURNCODE_TRUE; - result = ospPrepareDestination(msg, OSP_NEXT_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri)) - { - } + for(result = ospPrepareDestination( + msg, OSP_FIRST_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri); + result == MODULE_RETURNCODE_TRUE; + result = ospPrepareDestination( + msg, OSP_NEXT_ROUTE, OSP_MAIN_ROUTE, _osp_redir_uri)) { + } - return MODULE_RETURNCODE_TRUE; + return MODULE_RETURNCODE_TRUE; } - diff --git a/src/modules/osp/orig_transaction.h b/src/modules/osp/orig_transaction.h index 3d517328220..02e3e9b78a4 100644 --- a/src/modules/osp/orig_transaction.h +++ b/src/modules/osp/orig_transaction.h @@ -33,11 +33,10 @@ #include "../../core/parser/msg_parser.h" -int ospRequestRouting(struct sip_msg*, char*, char*); -int ospCheckRoute(struct sip_msg*, char*, char*); -int ospPrepareRoute(struct sip_msg*, char*, char*); -int ospPrepareAllRoutes(struct sip_msg*, char*, char*); -int ospCheckTranslation(struct sip_msg*, char*, char*); +int ospRequestRouting(struct sip_msg *, char *, char *); +int ospCheckRoute(struct sip_msg *, char *, char *); +int ospPrepareRoute(struct sip_msg *, char *, char *); +int ospPrepareAllRoutes(struct sip_msg *, char *, char *); +int ospCheckTranslation(struct sip_msg *, char *, char *); #endif /* _OSP_MOD_ORIG_TRANSACTION_H_ */ - diff --git a/src/modules/osp/osp_mod.c b/src/modules/osp/osp_mod.c index 49f3d7d45f5..f7c0c83281c 100644 --- a/src/modules/osp/osp_mod.c +++ b/src/modules/osp/osp_mod.c @@ -45,13 +45,13 @@ MODULE_VERSION extern unsigned int _osp_sp_number; -extern char* _osp_sp_uris[]; +extern char *_osp_sp_uris[]; extern unsigned long _osp_sp_weights[]; -extern char* _osp_device_ip; -extern char* _osp_device_port; -extern unsigned char* _osp_private_key; -extern unsigned char* _osp_local_certificate; -extern unsigned char* _osp_ca_certificate; +extern char *_osp_device_ip; +extern char *_osp_device_port; +extern unsigned char *_osp_private_key; +extern unsigned char *_osp_local_certificate; +extern unsigned char *_osp_ca_certificate; extern int _osp_crypto_hw; extern int _osp_validate_callid; extern int _osp_token_format; @@ -66,7 +66,7 @@ extern int _osp_redir_uri; extern char _osp_PRIVATE_KEY[]; extern char _osp_LOCAL_CERTIFICATE[]; extern char _osp_CA_CERTIFICATE[]; -extern char* _osp_snid_avp; +extern char *_osp_snid_avp; extern int_str _osp_snid_avpname; extern unsigned short _osp_snid_avptype; extern OSPTPROVHANDLE _osp_provider; @@ -78,85 +78,88 @@ int osp_index[OSP_DEF_SPS]; static int ospInitMod(void); static void ospDestMod(void); static int ospInitChild(int); -static int ospVerifyParameters(void); +static int ospVerifyParameters(void); static void ospDumpParameters(void); -static cmd_export_t cmds[]={ - {"checkospheader", (cmd_function)ospCheckHeader, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, - {"validateospheader", (cmd_function)ospValidateHeader, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, - {"requestosprouting", (cmd_function)ospRequestRouting, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, - {"checkosproute", (cmd_function)ospCheckRoute, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, - {"prepareosproute", (cmd_function)ospPrepareRoute, 0, 0, 0, BRANCH_ROUTE}, - {"prepareallosproutes", (cmd_function)ospPrepareAllRoutes, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, - {"checkcallingtranslation", (cmd_function)ospCheckTranslation, 0, 0, 0, BRANCH_ROUTE}, - {"reportospusage", (cmd_function)ospReportUsage, 1, 0, 0, REQUEST_ROUTE}, - {0, 0, 0, 0, 0, 0} -}; - -static param_export_t params[]={ - {"sp1_uri", PARAM_STRING, &_osp_sp_uris[0]}, - {"sp2_uri", PARAM_STRING, &_osp_sp_uris[1]}, - {"sp3_uri", PARAM_STRING, &_osp_sp_uris[2]}, - {"sp4_uri", PARAM_STRING, &_osp_sp_uris[3]}, - {"sp5_uri", PARAM_STRING, &_osp_sp_uris[4]}, - {"sp6_uri", PARAM_STRING, &_osp_sp_uris[5]}, - {"sp7_uri", PARAM_STRING, &_osp_sp_uris[6]}, - {"sp8_uri", PARAM_STRING, &_osp_sp_uris[7]}, - {"sp9_uri", PARAM_STRING, &_osp_sp_uris[8]}, - {"sp10_uri", PARAM_STRING, &_osp_sp_uris[9]}, - {"sp11_uri", PARAM_STRING, &_osp_sp_uris[10]}, - {"sp12_uri", PARAM_STRING, &_osp_sp_uris[11]}, - {"sp13_uri", PARAM_STRING, &_osp_sp_uris[12]}, - {"sp14_uri", PARAM_STRING, &_osp_sp_uris[13]}, - {"sp15_uri", PARAM_STRING, &_osp_sp_uris[14]}, - {"sp16_uri", PARAM_STRING, &_osp_sp_uris[15]}, - {"sp1_weight", INT_PARAM, &(_osp_sp_weights[0])}, - {"sp2_weight", INT_PARAM, &(_osp_sp_weights[1])}, - {"sp3_weight", INT_PARAM, &(_osp_sp_weights[2])}, - {"sp4_weight", INT_PARAM, &(_osp_sp_weights[3])}, - {"sp5_weight", INT_PARAM, &(_osp_sp_weights[4])}, - {"sp6_weight", INT_PARAM, &(_osp_sp_weights[5])}, - {"sp7_weight", INT_PARAM, &(_osp_sp_weights[6])}, - {"sp8_weight", INT_PARAM, &(_osp_sp_weights[7])}, - {"sp9_weight", INT_PARAM, &(_osp_sp_weights[8])}, - {"sp10_weight", INT_PARAM, &(_osp_sp_weights[9])}, - {"sp11_weight", INT_PARAM, &(_osp_sp_weights[10])}, - {"sp12_weight", INT_PARAM, &(_osp_sp_weights[11])}, - {"sp13_weight", INT_PARAM, &(_osp_sp_weights[12])}, - {"sp14_weight", INT_PARAM, &(_osp_sp_weights[13])}, - {"sp15_weight", INT_PARAM, &(_osp_sp_weights[14])}, - {"sp16_weight", INT_PARAM, &(_osp_sp_weights[15])}, - {"device_ip", PARAM_STRING, &_osp_device_ip}, - {"device_port", PARAM_STRING, &_osp_device_port}, - {"private_key", PARAM_STRING, &_osp_private_key}, - {"local_certificate", PARAM_STRING, &_osp_local_certificate}, - {"ca_certificates", PARAM_STRING, &_osp_ca_certificate}, - {"enable_crypto_hardware_support", INT_PARAM, &_osp_crypto_hw}, - {"validate_callid", INT_PARAM, &(_osp_validate_callid)}, - {"token_format", INT_PARAM, &_osp_token_format}, - {"ssl_lifetime", INT_PARAM, &_osp_ssl_lifetime}, - {"persistence", INT_PARAM, &_osp_persistence}, - {"retry_delay", INT_PARAM, &_osp_retry_delay}, - {"retry_limit", INT_PARAM, &_osp_retry_limit}, - {"timeout", INT_PARAM, &_osp_timeout}, - {"max_destinations", INT_PARAM, &_osp_max_dests}, - {"use_rpid_for_calling_number", INT_PARAM, &_osp_use_rpid}, - {"redirection_uri_format", INT_PARAM, &_osp_redir_uri}, - {"source_networkid_avp", PARAM_STRING, &_osp_snid_avp}, - {0,0,0} -}; +static cmd_export_t cmds[] = {{"checkospheader", (cmd_function)ospCheckHeader, + 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE}, + {"validateospheader", (cmd_function)ospValidateHeader, 0, 0, 0, + REQUEST_ROUTE | FAILURE_ROUTE}, + {"requestosprouting", (cmd_function)ospRequestRouting, 0, 0, 0, + REQUEST_ROUTE | FAILURE_ROUTE}, + {"checkosproute", (cmd_function)ospCheckRoute, 0, 0, 0, + REQUEST_ROUTE | FAILURE_ROUTE}, + {"prepareosproute", (cmd_function)ospPrepareRoute, 0, 0, 0, + BRANCH_ROUTE}, + {"prepareallosproutes", (cmd_function)ospPrepareAllRoutes, 0, 0, 0, + REQUEST_ROUTE | FAILURE_ROUTE}, + {"checkcallingtranslation", (cmd_function)ospCheckTranslation, 0, 0, 0, + BRANCH_ROUTE}, + {"reportospusage", (cmd_function)ospReportUsage, 1, 0, 0, + REQUEST_ROUTE}, + {0, 0, 0, 0, 0, 0}}; + +static param_export_t params[] = {{"sp1_uri", PARAM_STRING, &_osp_sp_uris[0]}, + {"sp2_uri", PARAM_STRING, &_osp_sp_uris[1]}, + {"sp3_uri", PARAM_STRING, &_osp_sp_uris[2]}, + {"sp4_uri", PARAM_STRING, &_osp_sp_uris[3]}, + {"sp5_uri", PARAM_STRING, &_osp_sp_uris[4]}, + {"sp6_uri", PARAM_STRING, &_osp_sp_uris[5]}, + {"sp7_uri", PARAM_STRING, &_osp_sp_uris[6]}, + {"sp8_uri", PARAM_STRING, &_osp_sp_uris[7]}, + {"sp9_uri", PARAM_STRING, &_osp_sp_uris[8]}, + {"sp10_uri", PARAM_STRING, &_osp_sp_uris[9]}, + {"sp11_uri", PARAM_STRING, &_osp_sp_uris[10]}, + {"sp12_uri", PARAM_STRING, &_osp_sp_uris[11]}, + {"sp13_uri", PARAM_STRING, &_osp_sp_uris[12]}, + {"sp14_uri", PARAM_STRING, &_osp_sp_uris[13]}, + {"sp15_uri", PARAM_STRING, &_osp_sp_uris[14]}, + {"sp16_uri", PARAM_STRING, &_osp_sp_uris[15]}, + {"sp1_weight", INT_PARAM, &(_osp_sp_weights[0])}, + {"sp2_weight", INT_PARAM, &(_osp_sp_weights[1])}, + {"sp3_weight", INT_PARAM, &(_osp_sp_weights[2])}, + {"sp4_weight", INT_PARAM, &(_osp_sp_weights[3])}, + {"sp5_weight", INT_PARAM, &(_osp_sp_weights[4])}, + {"sp6_weight", INT_PARAM, &(_osp_sp_weights[5])}, + {"sp7_weight", INT_PARAM, &(_osp_sp_weights[6])}, + {"sp8_weight", INT_PARAM, &(_osp_sp_weights[7])}, + {"sp9_weight", INT_PARAM, &(_osp_sp_weights[8])}, + {"sp10_weight", INT_PARAM, &(_osp_sp_weights[9])}, + {"sp11_weight", INT_PARAM, &(_osp_sp_weights[10])}, + {"sp12_weight", INT_PARAM, &(_osp_sp_weights[11])}, + {"sp13_weight", INT_PARAM, &(_osp_sp_weights[12])}, + {"sp14_weight", INT_PARAM, &(_osp_sp_weights[13])}, + {"sp15_weight", INT_PARAM, &(_osp_sp_weights[14])}, + {"sp16_weight", INT_PARAM, &(_osp_sp_weights[15])}, + {"device_ip", PARAM_STRING, &_osp_device_ip}, + {"device_port", PARAM_STRING, &_osp_device_port}, + {"private_key", PARAM_STRING, &_osp_private_key}, + {"local_certificate", PARAM_STRING, &_osp_local_certificate}, + {"ca_certificates", PARAM_STRING, &_osp_ca_certificate}, + {"enable_crypto_hardware_support", INT_PARAM, &_osp_crypto_hw}, + {"validate_callid", INT_PARAM, &(_osp_validate_callid)}, + {"token_format", INT_PARAM, &_osp_token_format}, + {"ssl_lifetime", INT_PARAM, &_osp_ssl_lifetime}, + {"persistence", INT_PARAM, &_osp_persistence}, + {"retry_delay", INT_PARAM, &_osp_retry_delay}, + {"retry_limit", INT_PARAM, &_osp_retry_limit}, + {"timeout", INT_PARAM, &_osp_timeout}, + {"max_destinations", INT_PARAM, &_osp_max_dests}, + {"use_rpid_for_calling_number", INT_PARAM, &_osp_use_rpid}, + {"redirection_uri_format", INT_PARAM, &_osp_redir_uri}, + {"source_networkid_avp", PARAM_STRING, &_osp_snid_avp}, {0, 0, 0}}; struct module_exports exports = { - "osp", /* module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* cmd (cfg function) exports */ - params, /* param exports */ - 0, /* RPC method exports */ - 0, /* pseudo-variables exports */ - 0, /* response handling function */ - ospInitMod, /* module init function */ - ospInitChild, /* per-child init function */ - ospDestMod /* module destroy function */ + "osp", /* module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* cmd (cfg function) exports */ + params, /* param exports */ + 0, /* RPC method exports */ + 0, /* pseudo-variables exports */ + 0, /* response handling function */ + ospInitMod, /* module init function */ + ospInitChild, /* per-child init function */ + ospDestMod /* module destroy function */ }; /* @@ -165,34 +168,37 @@ struct module_exports exports = { */ static int ospInitMod(void) { - bind_siputils_t bind_su; - - if (ospVerifyParameters() != 0) { - /* At least one parameter incorrect -> error */ - return -1; - } - - /* Load the RR API */ - if (load_rr_api(&osp_rr) != 0) { - LM_WARN("failed to load the RR API. Check if you load the rr module\n"); - LM_WARN("add_rr_param is required for reporting duration for OSP transactions\n"); - memset(&osp_rr, 0, sizeof(osp_rr)); - } - - /* Load the AUTH API */ - bind_su = (bind_siputils_t)find_export("bind_siputils", 1, 0); - if ((bind_su == NULL) || (bind_su(&osp_siputils) != 0)) { - LM_WARN("failed to load the SIPUTILS API. Check if you load the auth module.\n"); - LM_WARN("rpid_avp & rpid_avp_type is required for calling number translation\n"); - memset(&osp_siputils, 0, sizeof(osp_siputils)); - } - - if (ospInitTm() < 0) { - return -1; - } - - /* everything is fine, initialization done */ - return 0; + bind_siputils_t bind_su; + + if(ospVerifyParameters() != 0) { + /* At least one parameter incorrect -> error */ + return -1; + } + + /* Load the RR API */ + if(load_rr_api(&osp_rr) != 0) { + LM_WARN("failed to load the RR API. Check if you load the rr module\n"); + LM_WARN("add_rr_param is required for reporting duration for OSP " + "transactions\n"); + memset(&osp_rr, 0, sizeof(osp_rr)); + } + + /* Load the AUTH API */ + bind_su = (bind_siputils_t)find_export("bind_siputils", 1, 0); + if((bind_su == NULL) || (bind_su(&osp_siputils) != 0)) { + LM_WARN("failed to load the SIPUTILS API. Check if you load the auth " + "module.\n"); + LM_WARN("rpid_avp & rpid_avp_type is required for calling number " + "translation\n"); + memset(&osp_siputils, 0, sizeof(osp_siputils)); + } + + if(ospInitTm() < 0) { + return -1; + } + + /* everything is fine, initialization done */ + return 0; } /* @@ -207,16 +213,15 @@ static void ospDestMod(void) * param rank * return 0 success, -1 failure */ -static int ospInitChild( - int rank) +static int ospInitChild(int rank) { - int code = -1; + int code = -1; - code = ospSetupProvider(); + code = ospSetupProvider(); - LM_DBG("provider '%d' (%d)\n", _osp_provider, code); + LM_DBG("provider '%d' (%d)\n", _osp_provider, code); - return 0; + return 0; } /* @@ -225,119 +230,122 @@ static int ospInitChild( */ static int ospVerifyParameters(void) { - int i; - pv_spec_t avp_spec; - str avp_str; - int result = 0; - - /* Default location for the cert files is in the compile time variable CFG_DIR */ - if (_osp_private_key == NULL) { - sprintf(_osp_PRIVATE_KEY, "%spkey.pem", CFG_DIR); - _osp_private_key = (unsigned char*)_osp_PRIVATE_KEY; - } - - if (_osp_local_certificate == NULL) { - sprintf(_osp_LOCAL_CERTIFICATE, "%slocalcert.pem", CFG_DIR); - _osp_local_certificate = (unsigned char*)_osp_LOCAL_CERTIFICATE; - } - - if (_osp_ca_certificate == NULL) { - sprintf(_osp_CA_CERTIFICATE, "%scacert_0.pem", CFG_DIR); - _osp_ca_certificate = (unsigned char*)_osp_CA_CERTIFICATE; - } - - if (_osp_device_ip == NULL) { - _osp_device_ip = ""; - } - - if (_osp_device_port == NULL) { - _osp_device_port = ""; - } - - if (_osp_max_dests > OSP_DEF_DESTS || _osp_max_dests < 1) { - _osp_max_dests = OSP_DEF_DESTS; - LM_WARN("max_destinations is out of range, reset to %d\n", OSP_DEF_DESTS); - } - - if (_osp_token_format < 0 || _osp_token_format > 2) { - _osp_token_format = OSP_DEF_TOKEN; - LM_WARN("token_format is out of range, reset to %d\n", OSP_DEF_TOKEN); - } - - _osp_sp_number = 0; - for (i = 0; i < OSP_DEF_SPS; i++) { - if (_osp_sp_uris[i] != NULL) { - if (_osp_sp_number != i) { - _osp_sp_uris[_osp_sp_number] = _osp_sp_uris[i]; - _osp_sp_weights[_osp_sp_number] = _osp_sp_weights[i]; - _osp_sp_uris[i] = NULL; - _osp_sp_weights[i] = OSP_DEF_WEIGHT; - } - osp_index[_osp_sp_number] = i + 1; - _osp_sp_number++; - } - } - - if (_osp_sp_number == 0) { - LM_ERR("at least one service point uri must be configured\n"); - result = -1; - } - - if (_osp_snid_avp && *_osp_snid_avp) { - avp_str.s = _osp_snid_avp; - avp_str.len = strlen(_osp_snid_avp); - if (pv_parse_spec(&avp_str, &avp_spec) == NULL || - avp_spec.type != PVT_AVP || - pv_get_avp_name(0, &(avp_spec.pvp), &_osp_snid_avpname, &_osp_snid_avptype) != 0) - { - LM_WARN("'%s' invalid AVP definition\n", _osp_snid_avp); - _osp_snid_avpname.n = 0; - _osp_snid_avptype = 0; - } - } else { - _osp_snid_avpname.n = 0; - _osp_snid_avptype = 0; - } - - ospDumpParameters(); - - return result; + int i; + pv_spec_t avp_spec; + str avp_str; + int result = 0; + + /* Default location for the cert files is in the compile time variable CFG_DIR */ + if(_osp_private_key == NULL) { + sprintf(_osp_PRIVATE_KEY, "%spkey.pem", CFG_DIR); + _osp_private_key = (unsigned char *)_osp_PRIVATE_KEY; + } + + if(_osp_local_certificate == NULL) { + sprintf(_osp_LOCAL_CERTIFICATE, "%slocalcert.pem", CFG_DIR); + _osp_local_certificate = (unsigned char *)_osp_LOCAL_CERTIFICATE; + } + + if(_osp_ca_certificate == NULL) { + sprintf(_osp_CA_CERTIFICATE, "%scacert_0.pem", CFG_DIR); + _osp_ca_certificate = (unsigned char *)_osp_CA_CERTIFICATE; + } + + if(_osp_device_ip == NULL) { + _osp_device_ip = ""; + } + + if(_osp_device_port == NULL) { + _osp_device_port = ""; + } + + if(_osp_max_dests > OSP_DEF_DESTS || _osp_max_dests < 1) { + _osp_max_dests = OSP_DEF_DESTS; + LM_WARN("max_destinations is out of range, reset to %d\n", + OSP_DEF_DESTS); + } + + if(_osp_token_format < 0 || _osp_token_format > 2) { + _osp_token_format = OSP_DEF_TOKEN; + LM_WARN("token_format is out of range, reset to %d\n", OSP_DEF_TOKEN); + } + + _osp_sp_number = 0; + for(i = 0; i < OSP_DEF_SPS; i++) { + if(_osp_sp_uris[i] != NULL) { + if(_osp_sp_number != i) { + _osp_sp_uris[_osp_sp_number] = _osp_sp_uris[i]; + _osp_sp_weights[_osp_sp_number] = _osp_sp_weights[i]; + _osp_sp_uris[i] = NULL; + _osp_sp_weights[i] = OSP_DEF_WEIGHT; + } + osp_index[_osp_sp_number] = i + 1; + _osp_sp_number++; + } + } + + if(_osp_sp_number == 0) { + LM_ERR("at least one service point uri must be configured\n"); + result = -1; + } + + if(_osp_snid_avp && *_osp_snid_avp) { + avp_str.s = _osp_snid_avp; + avp_str.len = strlen(_osp_snid_avp); + if(pv_parse_spec(&avp_str, &avp_spec) == NULL + || avp_spec.type != PVT_AVP + || pv_get_avp_name(0, &(avp_spec.pvp), &_osp_snid_avpname, + &_osp_snid_avptype) + != 0) { + LM_WARN("'%s' invalid AVP definition\n", _osp_snid_avp); + _osp_snid_avpname.n = 0; + _osp_snid_avptype = 0; + } + } else { + _osp_snid_avpname.n = 0; + _osp_snid_avptype = 0; + } + + ospDumpParameters(); + + return result; } /* * Dump OSP module configuration */ -static void ospDumpParameters(void) +static void ospDumpParameters(void) { - int i; - - LM_INFO("module configuration: "); - LM_INFO(" number of service points '%d'", _osp_sp_number); - for (i = 0; i < _osp_sp_number; i++) { - LM_INFO(" sp%d_uri '%s' sp%d_weight '%ld' ", - osp_index[i], _osp_sp_uris[i], osp_index[i], _osp_sp_weights[i]); - } - LM_INFO(" device_ip '%s' device_port '%s' ", _osp_device_ip, _osp_device_port); - LM_INFO(" private_key '%s' ", _osp_private_key); - LM_INFO(" local_certificate '%s' ", _osp_local_certificate); - LM_INFO(" ca_certificates '%s' ", _osp_ca_certificate); - LM_INFO(" enable_crypto_hardware_support '%d' ", _osp_crypto_hw); - LM_INFO(" token_format '%d' ", _osp_token_format); - LM_INFO(" ssl_lifetime '%d' ", _osp_ssl_lifetime); - LM_INFO(" persistence '%d' ", _osp_persistence); - LM_INFO(" retry_delay '%d' ", _osp_retry_delay); - LM_INFO(" retry_limit '%d' ", _osp_retry_limit); - LM_INFO(" timeout '%d' ", _osp_timeout); - LM_INFO(" validate_call_id '%d' ", _osp_validate_callid); - LM_INFO(" use_rpid_for_calling_number '%d' ", _osp_use_rpid); - LM_INFO(" redirection_uri_format '%d' ", _osp_redir_uri); - LM_INFO(" max_destinations '%d'\n", _osp_max_dests); - if (_osp_snid_avpname.n == 0) { - LM_INFO(" source network ID disabled\n"); - } else if (_osp_snid_avptype & AVP_NAME_STR) { - LM_INFO(" source network ID AVP name '%.*s'\n", _osp_snid_avpname.s.len, _osp_snid_avpname.s.s); - } else { - LM_INFO(" source network ID AVP ID '%d'\n", _osp_snid_avpname.n); - } + int i; + + LM_INFO("module configuration: "); + LM_INFO(" number of service points '%d'", _osp_sp_number); + for(i = 0; i < _osp_sp_number; i++) { + LM_INFO(" sp%d_uri '%s' sp%d_weight '%ld' ", osp_index[i], + _osp_sp_uris[i], osp_index[i], _osp_sp_weights[i]); + } + LM_INFO(" device_ip '%s' device_port '%s' ", _osp_device_ip, + _osp_device_port); + LM_INFO(" private_key '%s' ", _osp_private_key); + LM_INFO(" local_certificate '%s' ", _osp_local_certificate); + LM_INFO(" ca_certificates '%s' ", _osp_ca_certificate); + LM_INFO(" enable_crypto_hardware_support '%d' ", _osp_crypto_hw); + LM_INFO(" token_format '%d' ", _osp_token_format); + LM_INFO(" ssl_lifetime '%d' ", _osp_ssl_lifetime); + LM_INFO(" persistence '%d' ", _osp_persistence); + LM_INFO(" retry_delay '%d' ", _osp_retry_delay); + LM_INFO(" retry_limit '%d' ", _osp_retry_limit); + LM_INFO(" timeout '%d' ", _osp_timeout); + LM_INFO(" validate_call_id '%d' ", _osp_validate_callid); + LM_INFO(" use_rpid_for_calling_number '%d' ", _osp_use_rpid); + LM_INFO(" redirection_uri_format '%d' ", _osp_redir_uri); + LM_INFO(" max_destinations '%d'\n", _osp_max_dests); + if(_osp_snid_avpname.n == 0) { + LM_INFO(" source network ID disabled\n"); + } else if(_osp_snid_avptype & AVP_NAME_STR) { + LM_INFO(" source network ID AVP name '%.*s'\n", + _osp_snid_avpname.s.len, _osp_snid_avpname.s.s); + } else { + LM_INFO(" source network ID AVP ID '%d'\n", _osp_snid_avpname.n); + } } - diff --git a/src/modules/osp/osp_mod.h b/src/modules/osp/osp_mod.h index a628f25d735..9ecf6cd3886 100644 --- a/src/modules/osp/osp_mod.h +++ b/src/modules/osp/osp_mod.h @@ -31,31 +31,31 @@ #ifndef _OSP_MOD_H_ #define _OSP_MOD_H_ -#define MODULE_RETURNCODE_TRUE 1 -#define MODULE_RETURNCODE_STOPROUTE 0 -#define MODULE_RETURNCODE_FALSE -1 -#define MODULE_RETURNCODE_ERROR -2 +#define MODULE_RETURNCODE_TRUE 1 +#define MODULE_RETURNCODE_STOPROUTE 0 +#define MODULE_RETURNCODE_FALSE -1 +#define MODULE_RETURNCODE_ERROR -2 -#define OSP_DEF_SPS 16 -#define OSP_DEF_WEIGHT 1000 -#define OSP_DEF_HW 0 -#define OSP_DEF_CALLID 1 /* Validate call ids, set to 0 to disable */ -#define OSP_DEF_TOKEN 2 -#define OSP_DEF_SSLLIFE 300 -#define OSP_DEF_PERSISTENCE (60 * 1000) -#define OSP_DEF_DELAY 0 -#define OSP_DEF_RETRY 2 -#define OSP_DEF_TIMEOUT (60 * 1000) -#define OSP_DEF_DESTS 5 -#define OSP_DEF_USERPID 1 -#define OSP_DEF_REDIRURI 0 /* 0 for "xxxxxxxxxx@xxx.xxx.xxx.xxx", 1 for "" format */ -#define OSP_DEF_SNIDAVP "$avp(s:_osp_source_networkid_)" +#define OSP_DEF_SPS 16 +#define OSP_DEF_WEIGHT 1000 +#define OSP_DEF_HW 0 +#define OSP_DEF_CALLID 1 /* Validate call ids, set to 0 to disable */ +#define OSP_DEF_TOKEN 2 +#define OSP_DEF_SSLLIFE 300 +#define OSP_DEF_PERSISTENCE (60 * 1000) +#define OSP_DEF_DELAY 0 +#define OSP_DEF_RETRY 2 +#define OSP_DEF_TIMEOUT (60 * 1000) +#define OSP_DEF_DESTS 5 +#define OSP_DEF_USERPID 1 +#define OSP_DEF_REDIRURI \ + 0 /* 0 for "xxxxxxxxxx@xxx.xxx.xxx.xxx", 1 for "" format */ +#define OSP_DEF_SNIDAVP "$avp(s:_osp_source_networkid_)" -#define OSP_KEYBUF_SIZE 256 -#define OSP_STRBUF_SIZE 256 -#define OSP_E164BUF_SIZE 1024 -#define OSP_TOKENBUF_SIZE 2048 -#define OSP_HEADERBUF_SIZE 3072 +#define OSP_KEYBUF_SIZE 256 +#define OSP_STRBUF_SIZE 256 +#define OSP_E164BUF_SIZE 1024 +#define OSP_TOKENBUF_SIZE 2048 +#define OSP_HEADERBUF_SIZE 3072 #endif /* _OSP_MOD_H_ */ - diff --git a/src/modules/osp/osptoolkit.c b/src/modules/osp/osptoolkit.c index 6b0e6ec2025..07e2bde320d 100644 --- a/src/modules/osp/osptoolkit.c +++ b/src/modules/osp/osptoolkit.c @@ -32,20 +32,20 @@ #include "../../core/dprint.h" #include "osptoolkit.h" -static OSPTTHREADRETURN ospReportUsageWork(void* usagearg); +static OSPTTHREADRETURN ospReportUsageWork(void *usagearg); typedef struct _osp_usage { - OSPTTRANHANDLE ospvTransaction; /* Transaction handle */ - unsigned ospvReleaseCode; /* Release code */ - unsigned ospvDuration; /* Length of call */ - time_t ospvStartTime; /* Call start time */ - time_t ospvEndTime; /* Call end time */ - time_t ospvAlertTime; /* Call alert time */ - time_t ospvConnectTime; /* Call connect time */ - unsigned ospvIsPDDInfoPresent; /* Is PDD Info present */ - unsigned ospvPostDialDelay; /* Post Dial Delay */ - unsigned ospvReleaseSource; /* EP that released the call */ + OSPTTRANHANDLE ospvTransaction; /* Transaction handle */ + unsigned ospvReleaseCode; /* Release code */ + unsigned ospvDuration; /* Length of call */ + time_t ospvStartTime; /* Call start time */ + time_t ospvEndTime; /* Call end time */ + time_t ospvAlertTime; /* Call alert time */ + time_t ospvConnectTime; /* Call connect time */ + unsigned ospvIsPDDInfoPresent; /* Is PDD Info present */ + unsigned ospvPostDialDelay; /* Post Dial Delay */ + unsigned ospvReleaseSource; /* EP that released the call */ } osp_usage; /* @@ -53,24 +53,23 @@ typedef struct _osp_usage * param transaction OSP transaction headle * return OSP transaction ID */ -unsigned long long ospGetTransactionId( - OSPTTRANHANDLE transaction) +unsigned long long ospGetTransactionId(OSPTTRANHANDLE transaction) { - OSPTTRANS* context = NULL; - unsigned long long id = 0; - int errorcode = OSPC_ERR_NO_ERROR; + OSPTTRANS *context = NULL; + unsigned long long id = 0; + int errorcode = OSPC_ERR_NO_ERROR; - context = OSPPTransactionGetContext(transaction, &errorcode); + context = OSPPTransactionGetContext(transaction, &errorcode); - if (errorcode == OSPC_ERR_NO_ERROR) { - id = (unsigned long long)context->TransactionID; - } else { - LM_ERR("failed to extract transaction_id from transaction handle %d (%d)\n", - transaction, - errorcode); - } + if(errorcode == OSPC_ERR_NO_ERROR) { + id = (unsigned long long)context->TransactionID; + } else { + LM_ERR("failed to extract transaction_id from transaction handle %d " + "(%d)\n", + transaction, errorcode); + } - return id; + return id; } /* @@ -86,45 +85,41 @@ unsigned long long ospGetTransactionId( * param ospvPostDialDelay Post dial delay information * param ospvReleaseSource Which side release the call */ -void ospReportUsageWrapper( - OSPTTRANHANDLE ospvTransaction, - unsigned ospvReleaseCode, - unsigned ospvDuration, - time_t ospvStartTime, - time_t ospvEndTime, - time_t ospvAlertTime, - time_t ospvConnectTime, - unsigned ospvIsPDDInfoPresent, - unsigned ospvPostDialDelay, - unsigned ospvReleaseSource) +void ospReportUsageWrapper(OSPTTRANHANDLE ospvTransaction, + unsigned ospvReleaseCode, unsigned ospvDuration, time_t ospvStartTime, + time_t ospvEndTime, time_t ospvAlertTime, time_t ospvConnectTime, + unsigned ospvIsPDDInfoPresent, unsigned ospvPostDialDelay, + unsigned ospvReleaseSource) { - osp_usage* usage; - OSPTTHREADID threadid; - OSPTTHRATTR threadattr; - int errorcode; + osp_usage *usage; + OSPTTHREADID threadid; + OSPTTHRATTR threadattr; + int errorcode; - LM_DBG("schedule usage report for '%llu'\n", ospGetTransactionId(ospvTransaction)); + LM_DBG("schedule usage report for '%llu'\n", + ospGetTransactionId(ospvTransaction)); - usage = (osp_usage*)malloc(sizeof(osp_usage)); + usage = (osp_usage *)malloc(sizeof(osp_usage)); - usage->ospvTransaction = ospvTransaction; - usage->ospvReleaseCode = ospvReleaseCode; - usage->ospvDuration = ospvDuration; - usage->ospvStartTime = ospvStartTime; - usage->ospvEndTime = ospvEndTime; - usage->ospvAlertTime = ospvAlertTime; - usage->ospvConnectTime = ospvConnectTime; - usage->ospvIsPDDInfoPresent = ospvIsPDDInfoPresent; - usage->ospvPostDialDelay = ospvPostDialDelay; - usage->ospvReleaseSource = ospvReleaseSource; + usage->ospvTransaction = ospvTransaction; + usage->ospvReleaseCode = ospvReleaseCode; + usage->ospvDuration = ospvDuration; + usage->ospvStartTime = ospvStartTime; + usage->ospvEndTime = ospvEndTime; + usage->ospvAlertTime = ospvAlertTime; + usage->ospvConnectTime = ospvConnectTime; + usage->ospvIsPDDInfoPresent = ospvIsPDDInfoPresent; + usage->ospvPostDialDelay = ospvPostDialDelay; + usage->ospvReleaseSource = ospvReleaseSource; - OSPM_THRATTR_INIT(threadattr, errorcode); + OSPM_THRATTR_INIT(threadattr, errorcode); - OSPM_SETDETACHED_STATE(threadattr, errorcode); + OSPM_SETDETACHED_STATE(threadattr, errorcode); - OSPM_CREATE_THREAD(threadid, &threadattr, ospReportUsageWork, usage, errorcode); + OSPM_CREATE_THREAD( + threadid, &threadattr, ospReportUsageWork, usage, errorcode); - OSPM_THRATTR_DESTROY(threadattr); + OSPM_THRATTR_DESTROY(threadattr); } /* @@ -132,49 +127,41 @@ void ospReportUsageWrapper( * param usagearg OSP usage information * return */ -static OSPTTHREADRETURN ospReportUsageWork( - void* usagearg) +static OSPTTHREADRETURN ospReportUsageWork(void *usagearg) { - int i; - const int MAX_RETRIES = 5; - osp_usage* usage; - int errorcode; - - usage = (osp_usage*)usagearg; - - OSPPTransactionRecordFailure( - usage->ospvTransaction, - (enum OSPEFAILREASON)usage->ospvReleaseCode); - - for (i = 1; i <= MAX_RETRIES; i++) { - errorcode = OSPPTransactionReportUsage( - usage->ospvTransaction, - usage->ospvDuration, - usage->ospvStartTime, - usage->ospvEndTime, - usage->ospvAlertTime, - usage->ospvConnectTime, - usage->ospvIsPDDInfoPresent, - usage->ospvPostDialDelay, - usage->ospvReleaseSource, - (unsigned char*)"", 0, 0, 0, 0, NULL, NULL); - - if (errorcode == OSPC_ERR_NO_ERROR) { - LM_DBG("reporte usage for '%llu'\n", - ospGetTransactionId(usage->ospvTransaction)); - break; - } else { - LM_ERR("failed to report usage for '%llu' (%d) attempt '%d' of '%d'\n", - ospGetTransactionId(usage->ospvTransaction), - errorcode, - i, - MAX_RETRIES); - } - } - - OSPPTransactionDelete(usage->ospvTransaction); - - free(usage); - - OSPTTHREADRETURN_NULL(); + int i; + const int MAX_RETRIES = 5; + osp_usage *usage; + int errorcode; + + usage = (osp_usage *)usagearg; + + OSPPTransactionRecordFailure(usage->ospvTransaction, + (enum OSPEFAILREASON)usage->ospvReleaseCode); + + for(i = 1; i <= MAX_RETRIES; i++) { + errorcode = OSPPTransactionReportUsage(usage->ospvTransaction, + usage->ospvDuration, usage->ospvStartTime, usage->ospvEndTime, + usage->ospvAlertTime, usage->ospvConnectTime, + usage->ospvIsPDDInfoPresent, usage->ospvPostDialDelay, + usage->ospvReleaseSource, (unsigned char *)"", 0, 0, 0, 0, NULL, + NULL); + + if(errorcode == OSPC_ERR_NO_ERROR) { + LM_DBG("reporte usage for '%llu'\n", + ospGetTransactionId(usage->ospvTransaction)); + break; + } else { + LM_ERR("failed to report usage for '%llu' (%d) attempt '%d' of " + "'%d'\n", + ospGetTransactionId(usage->ospvTransaction), errorcode, i, + MAX_RETRIES); + } + } + + OSPPTransactionDelete(usage->ospvTransaction); + + free(usage); + + OSPTTHREADRETURN_NULL(); } diff --git a/src/modules/osp/osptoolkit.h b/src/modules/osp/osptoolkit.h index 4d2f821f8af..fd6c1041cdf 100644 --- a/src/modules/osp/osptoolkit.h +++ b/src/modules/osp/osptoolkit.h @@ -44,17 +44,16 @@ */ unsigned long long ospGetTransactionId(OSPTTRANHANDLE transaction); void ospReportUsageWrapper( - OSPTTRANHANDLE ospvTransaction, /* In - Transaction handle */ - unsigned ospvReleaseCode, /* In - Release code */ - unsigned ospvDuration, /* In - Length of call */ - time_t ospvStartTime, /* In - Call start time */ - time_t ospvEndTime, /* In - Call end time */ - time_t ospvAlertTime, /* In - Call alert time */ - time_t ospvConnectTime, /* In - Call connect time */ - unsigned ospvIsPDDInfoPresent, /* In - Is PDD Info present */ - unsigned ospvPostDialDelay, /* In - Post Dial Delay */ - unsigned ospvReleaseSource /* In - EP that released the call */ + OSPTTRANHANDLE ospvTransaction, /* In - Transaction handle */ + unsigned ospvReleaseCode, /* In - Release code */ + unsigned ospvDuration, /* In - Length of call */ + time_t ospvStartTime, /* In - Call start time */ + time_t ospvEndTime, /* In - Call end time */ + time_t ospvAlertTime, /* In - Call alert time */ + time_t ospvConnectTime, /* In - Call connect time */ + unsigned ospvIsPDDInfoPresent, /* In - Is PDD Info present */ + unsigned ospvPostDialDelay, /* In - Post Dial Delay */ + unsigned ospvReleaseSource /* In - EP that released the call */ ); #endif /* _OSP_MOD_OSPTOOLKIT_H_ */ - diff --git a/src/modules/osp/provider.c b/src/modules/osp/provider.c index c3b2cd5ba4d..4b81b7d2e65 100644 --- a/src/modules/osp/provider.c +++ b/src/modules/osp/provider.c @@ -34,11 +34,11 @@ #include "provider.h" extern unsigned int _osp_sp_number; -extern char* _osp_sp_uris[]; +extern char *_osp_sp_uris[]; extern unsigned long _osp_sp_weights[]; -extern unsigned char* _osp_private_key; -extern unsigned char* _osp_local_certificate; -extern unsigned char* _osp_ca_certificate; +extern unsigned char *_osp_private_key; +extern unsigned char *_osp_local_certificate; +extern unsigned char *_osp_ca_certificate; extern int _osp_ssl_lifetime; extern int _osp_persistence; extern int _osp_retry_delay; @@ -51,83 +51,70 @@ extern OSPTPROVHANDLE _osp_provider; * Create a new OSP provider object per process * return 0 success, others failure */ -int ospSetupProvider(void) +int ospSetupProvider(void) { - OSPTPRIVATEKEY privatekey; - OSPTCERT localcert; - OSPTCERT cacert; - OSPTCERT* cacerts[1]; - int result; + OSPTPRIVATEKEY privatekey; + OSPTCERT localcert; + OSPTCERT cacert; + OSPTCERT *cacerts[1]; + int result; - cacerts[0] = &cacert; + cacerts[0] = &cacert; - if ((result = OSPPInit(_osp_crypto_hw)) != 0) { - LM_ERR("failed to initialize OSP (%d)\n", result); - } else if (OSPPUtilLoadPEMPrivateKey(_osp_private_key, &privatekey) != 0) { - LM_ERR("failed to load private key from '%s'\n", _osp_private_key); - } else if (OSPPUtilLoadPEMCert(_osp_local_certificate, &localcert) != 0) { - LM_ERR("failed to load local certificate from '%s'\n",_osp_local_certificate); - } else if (OSPPUtilLoadPEMCert(_osp_ca_certificate, &cacert) != 0) { - LM_ERR("failed to load CA certificate from '%s'\n", _osp_ca_certificate); - } else { - result = OSPPProviderNew( - _osp_sp_number, - (const char**)_osp_sp_uris, - _osp_sp_weights, - "http://localhost:1234", - &privatekey, - &localcert, - 1, - (const OSPTCERT**)cacerts, - 1, - _osp_ssl_lifetime, - _osp_sp_number, - _osp_persistence, - _osp_retry_delay, - _osp_retry_limit, - _osp_timeout, - "", - "", - &_osp_provider); - if (result != 0) { - LM_ERR("failed to create provider (%d)\n", result); - } else { - LM_DBG("created new (per process) provider '%d'\n", _osp_provider); - result = 0; - } - } + if((result = OSPPInit(_osp_crypto_hw)) != 0) { + LM_ERR("failed to initialize OSP (%d)\n", result); + } else if(OSPPUtilLoadPEMPrivateKey(_osp_private_key, &privatekey) != 0) { + LM_ERR("failed to load private key from '%s'\n", _osp_private_key); + } else if(OSPPUtilLoadPEMCert(_osp_local_certificate, &localcert) != 0) { + LM_ERR("failed to load local certificate from '%s'\n", + _osp_local_certificate); + } else if(OSPPUtilLoadPEMCert(_osp_ca_certificate, &cacert) != 0) { + LM_ERR("failed to load CA certificate from '%s'\n", + _osp_ca_certificate); + } else { + result = OSPPProviderNew(_osp_sp_number, (const char **)_osp_sp_uris, + _osp_sp_weights, "http://localhost:1234", &privatekey, + &localcert, 1, (const OSPTCERT **)cacerts, 1, _osp_ssl_lifetime, + _osp_sp_number, _osp_persistence, _osp_retry_delay, + _osp_retry_limit, _osp_timeout, "", "", &_osp_provider); + if(result != 0) { + LM_ERR("failed to create provider (%d)\n", result); + } else { + LM_DBG("created new (per process) provider '%d'\n", _osp_provider); + result = 0; + } + } - /* + /* * Free space allocated while loading crypto information from PEM-encoded files. * There are some problems to free the memory, do not free them */ - if (privatekey.PrivateKeyData != NULL) { - //free(privatekey.PrivateKeyData); - } + if(privatekey.PrivateKeyData != NULL) { + //free(privatekey.PrivateKeyData); + } - if (localcert.CertData != NULL) { - //free(localcert.CertData); - } - - if (cacert.CertData != NULL) { - //free(localcert.CertData); - } + if(localcert.CertData != NULL) { + //free(localcert.CertData); + } - return result; + if(cacert.CertData != NULL) { + //free(localcert.CertData); + } + + return result; } /* * Erase OSP provider object * return 0 success, others failure */ -int ospDeleteProvider(void) +int ospDeleteProvider(void) { - int result; + int result; - if ((result = OSPPProviderDelete(_osp_provider, 0)) != 0) { - LM_ERR("failed to erase provider '%d' (%d)\n", _osp_provider, result); - } - - return result; -} + if((result = OSPPProviderDelete(_osp_provider, 0)) != 0) { + LM_ERR("failed to erase provider '%d' (%d)\n", _osp_provider, result); + } + return result; +} diff --git a/src/modules/osp/provider.h b/src/modules/osp/provider.h index 24ff4b07fae..044790c1adc 100644 --- a/src/modules/osp/provider.h +++ b/src/modules/osp/provider.h @@ -35,4 +35,3 @@ int ospSetupProvider(void); int ospDeleteProvider(void); #endif /* _OSP_MOD_PROVIDER_H_ */ - diff --git a/src/modules/osp/sipheader.c b/src/modules/osp/sipheader.c index 1489c3d529e..724d5e5fdc8 100644 --- a/src/modules/osp/sipheader.c +++ b/src/modules/osp/sipheader.c @@ -42,8 +42,8 @@ extern int _osp_use_rpid; -static void ospSkipPlus(char* e164); -static int ospAppendHeader(struct sip_msg* msg, str* header); +static void ospSkipPlus(char *e164); +static int ospAppendHeader(struct sip_msg *msg, str *header); /* * Copy str to buffer and check overflow @@ -51,38 +51,33 @@ static int ospAppendHeader(struct sip_msg* msg, str* header); * param buffer Buffer * param buffersize Size of buffer */ -void ospCopyStrToBuffer( - str* source, - char* buffer, - int buffersize) +void ospCopyStrToBuffer(str *source, char *buffer, int buffersize) { - int copybytes; - - if (source->len > buffersize - 1) { - LM_ERR("buffer for copying '%.*s' is too small, will copy the first '%d' bytes\n", - source->len, - source->s, - buffersize); - copybytes = buffersize - 1; - } else { - copybytes = source->len; - } - - strncpy(buffer, source->s, copybytes); - buffer[copybytes] = '\0'; + int copybytes; + + if(source->len > buffersize - 1) { + LM_ERR("buffer for copying '%.*s' is too small, will copy the first " + "'%d' bytes\n", + source->len, source->s, buffersize); + copybytes = buffersize - 1; + } else { + copybytes = source->len; + } + + strncpy(buffer, source->s, copybytes); + buffer[copybytes] = '\0'; } /* * Remove '+' in E164 string * param e164 E164 string */ -static void ospSkipPlus( - char* e164) +static void ospSkipPlus(char *e164) { - if (*e164 == '+') { - strncpy(e164, e164 + 1, strlen(e164) - 1); - e164[strlen(e164) - 1] = '\0'; - } + if(*e164 == '+') { + strncpy(e164, e164 + 1, strlen(e164) - 1); + e164[strlen(e164) - 1] = '\0'; + } } /* @@ -92,35 +87,32 @@ static void ospSkipPlus( * param buffersize Size of fromuser buffer * return 0 success, -1 failure */ -int ospGetFromUserpart( - struct sip_msg* msg, - char* fromuser, - int buffersize) +int ospGetFromUserpart(struct sip_msg *msg, char *fromuser, int buffersize) { - struct to_body* from; - struct sip_uri uri; - int result = -1; - - fromuser[0] = '\0'; - - if (msg->from != NULL) { - if (parse_from_header(msg) == 0) { - from = get_from(msg); - if (parse_uri(from->uri.s, from->uri.len, &uri) == 0) { - ospCopyStrToBuffer(&uri.user, fromuser, buffersize); - ospSkipPlus(fromuser); - result = 0; - } else { - LM_ERR("failed to parse From uri\n"); - } - } else { - LM_ERR("failed to parse From header\n"); - } - } else { - LM_ERR("failed to find From header\n"); - } - - return result; + struct to_body *from; + struct sip_uri uri; + int result = -1; + + fromuser[0] = '\0'; + + if(msg->from != NULL) { + if(parse_from_header(msg) == 0) { + from = get_from(msg); + if(parse_uri(from->uri.s, from->uri.len, &uri) == 0) { + ospCopyStrToBuffer(&uri.user, fromuser, buffersize); + ospSkipPlus(fromuser); + result = 0; + } else { + LM_ERR("failed to parse From uri\n"); + } + } else { + LM_ERR("failed to parse From header\n"); + } + } else { + LM_ERR("failed to find From header\n"); + } + + return result; } /* @@ -130,39 +122,36 @@ int ospGetFromUserpart( * param buffersize Size of fromuser buffer * return 0 success, -1 failure */ -int ospGetRpidUserpart( - struct sip_msg* msg, - char* rpiduser, - int buffersize) +int ospGetRpidUserpart(struct sip_msg *msg, char *rpiduser, int buffersize) { - struct to_body* rpid; - struct sip_uri uri; - int result = -1; - - rpiduser[0] = '\0'; - - if (_osp_use_rpid != 0) { - if (msg->rpid != NULL) { - if (parse_rpid_header(msg) == 0) { - rpid = get_rpid(msg); - if (parse_uri(rpid->uri.s, rpid->uri.len, &uri) == 0) { - ospCopyStrToBuffer(&uri.user, rpiduser, buffersize); - ospSkipPlus(rpiduser); - result = 0; - } else { - LM_ERR("failed to parse RPID uri\n"); - } - } else { - LM_ERR("failed to parse RPID header\n"); - } - } else { - LM_DBG("without RPID header\n"); - } - } else { - LM_DBG("do not use RPID header\n"); - } - - return result; + struct to_body *rpid; + struct sip_uri uri; + int result = -1; + + rpiduser[0] = '\0'; + + if(_osp_use_rpid != 0) { + if(msg->rpid != NULL) { + if(parse_rpid_header(msg) == 0) { + rpid = get_rpid(msg); + if(parse_uri(rpid->uri.s, rpid->uri.len, &uri) == 0) { + ospCopyStrToBuffer(&uri.user, rpiduser, buffersize); + ospSkipPlus(rpiduser); + result = 0; + } else { + LM_ERR("failed to parse RPID uri\n"); + } + } else { + LM_ERR("failed to parse RPID header\n"); + } + } else { + LM_DBG("without RPID header\n"); + } + } else { + LM_DBG("do not use RPID header\n"); + } + + return result; } /* @@ -172,35 +161,32 @@ int ospGetRpidUserpart( * param buffersize Size of touser buffer * return 0 success, -1 failure */ -int ospGetToUserpart( - struct sip_msg* msg, - char* touser, - int buffersize) +int ospGetToUserpart(struct sip_msg *msg, char *touser, int buffersize) { - struct to_body* to; - struct sip_uri uri; - int result = -1; - - touser[0] = '\0'; - - if (msg->to != NULL) { - if (parse_headers(msg, HDR_TO_F, 0) == 0) { - to = get_to(msg); - if (parse_uri(to->uri.s, to->uri.len, &uri) == 0) { - ospCopyStrToBuffer(&uri.user, touser, buffersize); - ospSkipPlus(touser); - result = 0; - } else { - LM_ERR("failed to parse To uri\n"); - } - } else { - LM_ERR("failed to parse To header\n"); - } - } else { - LM_ERR("failed to find To header\n"); - } - - return result; + struct to_body *to; + struct sip_uri uri; + int result = -1; + + touser[0] = '\0'; + + if(msg->to != NULL) { + if(parse_headers(msg, HDR_TO_F, 0) == 0) { + to = get_to(msg); + if(parse_uri(to->uri.s, to->uri.len, &uri) == 0) { + ospCopyStrToBuffer(&uri.user, touser, buffersize); + ospSkipPlus(touser); + result = 0; + } else { + LM_ERR("failed to parse To uri\n"); + } + } else { + LM_ERR("failed to parse To header\n"); + } + } else { + LM_ERR("failed to find To header\n"); + } + + return result; } /* @@ -210,24 +196,21 @@ int ospGetToUserpart( * param buffersize Size of touser buffer * return 0 success, -1 failure */ -int ospGetUriUserpart( - struct sip_msg* msg, - char* uriuser, - int buffersize) +int ospGetUriUserpart(struct sip_msg *msg, char *uriuser, int buffersize) { - int result = -1; + int result = -1; - uriuser[0] = '\0'; + uriuser[0] = '\0'; - if (parse_sip_msg_uri(msg) >= 0) { - ospCopyStrToBuffer(&msg->parsed_uri.user, uriuser, buffersize); - ospSkipPlus(uriuser); - result = 0; - } else { - LM_ERR("failed to parse Request-Line URI\n"); - } + if(parse_sip_msg_uri(msg) >= 0) { + ospCopyStrToBuffer(&msg->parsed_uri.user, uriuser, buffersize); + ospSkipPlus(uriuser); + result = 0; + } else { + LM_ERR("failed to parse Request-Line URI\n"); + } - return result; + return result; } /* @@ -236,44 +219,42 @@ int ospGetUriUserpart( * param header Header to be appended * return 0 success, -1 failure */ -static int ospAppendHeader( - struct sip_msg* msg, - str* header) +static int ospAppendHeader(struct sip_msg *msg, str *header) { - char* s; - struct lump* anchor; - - if((msg == 0) || (header == 0) || (header->s == 0) || (header->len <= 0)) { - LM_ERR("bad parameters for appending header\n"); - return -1; - } - - if (parse_headers(msg, HDR_EOH_F, 0) == -1) { - LM_ERR("failed to parse message\n"); - return -1; - } - - anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0); - if (anchor == 0) { - LM_ERR("failed to get anchor\n"); - return -1; - } - - s = (char*)pkg_malloc(header->len); - if (s == 0) { - LM_ERR("no pkg memory\n"); - return -1; - } - - memcpy(s, header->s, header->len); - - if (insert_new_lump_before(anchor, s, header->len, 0) == 0) { - LM_ERR("failed to insert lump\n"); - pkg_free(s); - return -1; - } - - return 0; + char *s; + struct lump *anchor; + + if((msg == 0) || (header == 0) || (header->s == 0) || (header->len <= 0)) { + LM_ERR("bad parameters for appending header\n"); + return -1; + } + + if(parse_headers(msg, HDR_EOH_F, 0) == -1) { + LM_ERR("failed to parse message\n"); + return -1; + } + + anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0); + if(anchor == 0) { + LM_ERR("failed to get anchor\n"); + return -1; + } + + s = (char *)pkg_malloc(header->len); + if(s == 0) { + LM_ERR("no pkg memory\n"); + return -1; + } + + memcpy(s, header->s, header->len); + + if(insert_new_lump_before(anchor, s, header->len, 0) == 0) { + LM_ERR("failed to insert lump\n"); + pkg_free(s); + return -1; + } + + return 0; } /* @@ -284,44 +265,39 @@ static int ospAppendHeader( * return 0 success, -1 failure */ int ospAddOspHeader( - struct sip_msg* msg, - unsigned char* token, - unsigned int tokensize) + struct sip_msg *msg, unsigned char *token, unsigned int tokensize) { - str headerval; - char buffer[OSP_HEADERBUF_SIZE]; - unsigned char encodedtoken[OSP_TOKENBUF_SIZE]; - unsigned int encodedtokensize = sizeof(encodedtoken); - int result = -1; - - if (tokensize == 0) { - LM_DBG("destination is not OSP device\n"); - result = 0; - } else { - if (OSPPBase64Encode(token, tokensize, encodedtoken, &encodedtokensize) == 0) { - snprintf(buffer, - sizeof(buffer), - "%s%.*s\r\n", - OSP_TOKEN_HEADER, - encodedtokensize, - encodedtoken); - - headerval.s = buffer; - headerval.len = strlen(buffer); - - LM_DBG("setting osp token header field '%s'\n", buffer); - - if (ospAppendHeader(msg, &headerval) == 0) { - result = 0; - } else { - LM_ERR("failed to append osp header\n"); - } - } else { - LM_ERR("failed to base64 encode token\n"); - } - } - - return result; + str headerval; + char buffer[OSP_HEADERBUF_SIZE]; + unsigned char encodedtoken[OSP_TOKENBUF_SIZE]; + unsigned int encodedtokensize = sizeof(encodedtoken); + int result = -1; + + if(tokensize == 0) { + LM_DBG("destination is not OSP device\n"); + result = 0; + } else { + if(OSPPBase64Encode(token, tokensize, encodedtoken, &encodedtokensize) + == 0) { + snprintf(buffer, sizeof(buffer), "%s%.*s\r\n", OSP_TOKEN_HEADER, + encodedtokensize, encodedtoken); + + headerval.s = buffer; + headerval.len = strlen(buffer); + + LM_DBG("setting osp token header field '%s'\n", buffer); + + if(ospAppendHeader(msg, &headerval) == 0) { + result = 0; + } else { + LM_ERR("failed to append osp header\n"); + } + } else { + LM_ERR("failed to base64 encode token\n"); + } + } + + return result; } /* @@ -332,32 +308,34 @@ int ospAddOspHeader( * return 0 success, -1 failure */ int ospGetOspHeader( - struct sip_msg* msg, - unsigned char* token, - unsigned int* tokensize) + struct sip_msg *msg, unsigned char *token, unsigned int *tokensize) { - struct hdr_field* hf; - int errorcode; - int result = -1; - - parse_headers(msg, HDR_EOH_F, 0); - - for (hf = msg->headers; hf; hf = hf->next) { - if ((hf->type == HDR_OTHER_T) && (hf->name.len == OSP_HEADER_SIZE - 2)) { - // possible hit - if (strncasecmp(hf->name.s, OSP_TOKEN_HEADER, OSP_HEADER_SIZE) == 0) { - if ((errorcode = OSPPBase64Decode(hf->body.s, hf->body.len, token, tokensize)) == OSPC_ERR_NO_ERROR) { - result = 0; - } else { - LM_ERR("failed to base64 decode token (%d)\n", errorcode); - LM_ERR("header '%.*s' length %d\n", hf->body.len, hf->body.s, hf->body.len); - } - break; - } - } - } - - return result; + struct hdr_field *hf; + int errorcode; + int result = -1; + + parse_headers(msg, HDR_EOH_F, 0); + + for(hf = msg->headers; hf; hf = hf->next) { + if((hf->type == HDR_OTHER_T) && (hf->name.len == OSP_HEADER_SIZE - 2)) { + // possible hit + if(strncasecmp(hf->name.s, OSP_TOKEN_HEADER, OSP_HEADER_SIZE) + == 0) { + if((errorcode = OSPPBase64Decode( + hf->body.s, hf->body.len, token, tokensize)) + == OSPC_ERR_NO_ERROR) { + result = 0; + } else { + LM_ERR("failed to base64 decode token (%d)\n", errorcode); + LM_ERR("header '%.*s' length %d\n", hf->body.len, + hf->body.s, hf->body.len); + } + break; + } + } + } + + return result; } /* @@ -368,32 +346,30 @@ int ospGetOspHeader( * return 0 success, -1 failure */ int ospGetSourceAddress( - struct sip_msg* msg, - char* sourceaddress, - int buffersize) + struct sip_msg *msg, char *sourceaddress, int buffersize) { - struct hdr_field* hf; - struct via_body* via; - int result = -1; + struct hdr_field *hf; + struct via_body *via; + int result = -1; - /* + /* * No need to call parse_headers, called already and VIA is parsed * anyway by default */ - for (hf = msg->headers; hf; hf = hf->next) { - if (hf->type == HDR_VIA_T) { - // found first VIA - via = (struct via_body*)hf->parsed; - ospCopyStrToBuffer(&via->host, sourceaddress, buffersize); + for(hf = msg->headers; hf; hf = hf->next) { + if(hf->type == HDR_VIA_T) { + // found first VIA + via = (struct via_body *)hf->parsed; + ospCopyStrToBuffer(&via->host, sourceaddress, buffersize); - LM_DBG("source address '%s'\n", sourceaddress); + LM_DBG("source address '%s'\n", sourceaddress); - result = 0; - break; - } - } + result = 0; + break; + } + } - return result; + return result; } /* @@ -402,26 +378,25 @@ int ospGetSourceAddress( * param callid Call ID * return 0 success, -1 failure */ -int ospGetCallId( - struct sip_msg* msg, - OSPTCALLID** callid) +int ospGetCallId(struct sip_msg *msg, OSPTCALLID **callid) { - struct hdr_field* hf; - int result = -1; - - hf = (struct hdr_field*)msg->callid; - if (hf != NULL) { - *callid = OSPPCallIdNew(hf->body.len, (unsigned char*)hf->body.s); - if (*callid) { - result = 0; - } else { - LM_ERR("failed to allocate OSPCALLID object for '%.*s'\n", hf->body.len, hf->body.s); - } - } else { - LM_ERR("failed to find Call-ID header\n"); - } - - return result; + struct hdr_field *hf; + int result = -1; + + hf = (struct hdr_field *)msg->callid; + if(hf != NULL) { + *callid = OSPPCallIdNew(hf->body.len, (unsigned char *)hf->body.s); + if(*callid) { + result = 0; + } else { + LM_ERR("failed to allocate OSPCALLID object for '%.*s'\n", + hf->body.len, hf->body.s); + } + } else { + LM_ERR("failed to find Call-ID header\n"); + } + + return result; } /* @@ -432,47 +407,50 @@ int ospGetCallId( * return 0 success, -1 failure */ int ospGetRouteParameters( - struct sip_msg* msg, - char* routeparameters, - int buffersize) + struct sip_msg *msg, char *routeparameters, int buffersize) { - struct hdr_field* hf; - rr_t* rt; - struct sip_uri uri; - int result = -1; - - LM_DBG("parsed uri host '%.*s' port '%d' vars '%.*s'\n", - msg->parsed_uri.host.len, - msg->parsed_uri.host.s, - msg->parsed_uri.port_no, - msg->parsed_uri.params.len, - msg->parsed_uri.params.s); - - if (!(hf = msg->route)) { - LM_DBG("there is no Route headers\n"); - } else if (!(rt = (rr_t*)hf->parsed)) { - LM_ERR("route headers are not parsed\n"); - } else if (parse_uri(rt->nameaddr.uri.s, rt->nameaddr.uri.len, &uri) != 0) { - LM_ERR("failed to parse the Route uri '%.*s'\n", rt->nameaddr.uri.len, rt->nameaddr.uri.s); - } else if (check_self(&uri.host, uri.port_no ? uri.port_no : SIP_PORT, PROTO_NONE) != 1) { - LM_DBG("the Route uri is NOT mine\n"); - LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, uri.port_no); - LM_DBG("params '%.*s'\n", uri.params.len, uri.params.s); - } else { - LM_DBG("the Route uri IS mine - '%.*s'\n", uri.params.len, uri.params.s); - LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, uri.port_no); - ospCopyStrToBuffer(&uri.params, routeparameters, buffersize); - result = 0; - } - - if ((result == -1) && (msg->parsed_uri.params.len > 0)) { - LM_DBG("using route parameters from Request-Line uri\n"); - ospCopyStrToBuffer(&msg->parsed_uri.params, routeparameters, buffersize); - routeparameters[msg->parsed_uri.params.len] = '\0'; - result = 0; - } - - return result; + struct hdr_field *hf; + rr_t *rt; + struct sip_uri uri; + int result = -1; + + LM_DBG("parsed uri host '%.*s' port '%d' vars '%.*s'\n", + msg->parsed_uri.host.len, msg->parsed_uri.host.s, + msg->parsed_uri.port_no, msg->parsed_uri.params.len, + msg->parsed_uri.params.s); + + if(!(hf = msg->route)) { + LM_DBG("there is no Route headers\n"); + } else if(!(rt = (rr_t *)hf->parsed)) { + LM_ERR("route headers are not parsed\n"); + } else if(parse_uri(rt->nameaddr.uri.s, rt->nameaddr.uri.len, &uri) != 0) { + LM_ERR("failed to parse the Route uri '%.*s'\n", rt->nameaddr.uri.len, + rt->nameaddr.uri.s); + } else if(check_self(&uri.host, uri.port_no ? uri.port_no : SIP_PORT, + PROTO_NONE) + != 1) { + LM_DBG("the Route uri is NOT mine\n"); + LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, + uri.port_no); + LM_DBG("params '%.*s'\n", uri.params.len, uri.params.s); + } else { + LM_DBG("the Route uri IS mine - '%.*s'\n", uri.params.len, + uri.params.s); + LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, + uri.port_no); + ospCopyStrToBuffer(&uri.params, routeparameters, buffersize); + result = 0; + } + + if((result == -1) && (msg->parsed_uri.params.len > 0)) { + LM_DBG("using route parameters from Request-Line uri\n"); + ospCopyStrToBuffer( + &msg->parsed_uri.params, routeparameters, buffersize); + routeparameters[msg->parsed_uri.params.len] = '\0'; + result = 0; + } + + return result; } /* @@ -485,72 +463,63 @@ int ospGetRouteParameters( * return 0 success, -1 failure */ int ospRebuildDestionationUri( - str* newuri, - char* called, - char* dest, - char* port, - int format) + str *newuri, char *called, char *dest, char *port, int format) { - static const str TRANS = {";transport=tcp", 14}; - char* buffer; - int calledsize; - int destsize; - int portsize; - - calledsize = strlen(called); - destsize = strlen(dest); - portsize = strlen(port); - - LM_DBG("'%s'(%d) '%s'(%d) '%s'(%d) '%d'\n", - called, - calledsize, - dest, - destsize, - port, - portsize, - format); - - /* "sip:" + called + "@" + dest + : + port + " SIP/2.0" for URI format 0 */ - /* " + " SIP/2.0" for URI format 1 */ - newuri->s = (char*)pkg_malloc(1 + 4 + calledsize + 1 + destsize + 1 + portsize + 1 + 1 + 16 + TRANS.len); - if (newuri == NULL) { - LM_ERR("no pkg memory\n"); - return -1; - } - buffer = newuri->s; - - if (format == 1) { - *buffer++ = '<'; - } - *buffer++ = 's'; - *buffer++ = 'i'; - *buffer++ = 'p'; - *buffer++ = ':'; - - memcpy(buffer, called, calledsize); - buffer += calledsize; - *buffer++ = '@'; - - if (*dest == '[') { - /* leave out annoying [] */ - memcpy(buffer, dest + 1, destsize - 2); - buffer += destsize - 2; - } else { - memcpy(buffer, dest, destsize); - buffer += destsize; - } - - if (portsize > 0) { - *buffer++ = ':'; - memcpy(buffer, port, portsize); - buffer += portsize; - } - - if (format == 1) { - *buffer++ = '>'; - } - -/* + static const str TRANS = {";transport=tcp", 14}; + char *buffer; + int calledsize; + int destsize; + int portsize; + + calledsize = strlen(called); + destsize = strlen(dest); + portsize = strlen(port); + + LM_DBG("'%s'(%d) '%s'(%d) '%s'(%d) '%d'\n", called, calledsize, dest, + destsize, port, portsize, format); + + /* "sip:" + called + "@" + dest + : + port + " SIP/2.0" for URI format 0 */ + /* " + " SIP/2.0" for URI format 1 */ + newuri->s = (char *)pkg_malloc(1 + 4 + calledsize + 1 + destsize + 1 + + portsize + 1 + 1 + 16 + TRANS.len); + if(newuri == NULL) { + LM_ERR("no pkg memory\n"); + return -1; + } + buffer = newuri->s; + + if(format == 1) { + *buffer++ = '<'; + } + *buffer++ = 's'; + *buffer++ = 'i'; + *buffer++ = 'p'; + *buffer++ = ':'; + + memcpy(buffer, called, calledsize); + buffer += calledsize; + *buffer++ = '@'; + + if(*dest == '[') { + /* leave out annoying [] */ + memcpy(buffer, dest + 1, destsize - 2); + buffer += destsize - 2; + } else { + memcpy(buffer, dest, destsize); + buffer += destsize; + } + + if(portsize > 0) { + *buffer++ = ':'; + memcpy(buffer, port, portsize); + buffer += portsize; + } + + if(format == 1) { + *buffer++ = '>'; + } + + /* *buffer++ = ' '; *buffer++ = 'S'; *buffer++ = 'I'; @@ -565,11 +534,11 @@ int ospRebuildDestionationUri( *buffer = '\0'; */ - newuri->len = buffer - newuri->s; + newuri->len = buffer - newuri->s; - LM_DBG("new uri '%.*s'\n", newuri->len, newuri->s); + LM_DBG("new uri '%.*s'\n", newuri->len, newuri->s); - return 0; + return 0; } /* @@ -578,50 +547,49 @@ int ospRebuildDestionationUri( * param nexthop Next hop IP * param buffersize Size of nexthop */ -void ospGetNextHop( - struct sip_msg* msg, - char* nexthop, - int buffersize) +void ospGetNextHop(struct sip_msg *msg, char *nexthop, int buffersize) { - struct hdr_field* hf; - struct sip_uri uri; - rr_t* rt; - int found = 0; - - for (hf = msg->headers; hf; hf = hf->next) { - if (hf->type == HDR_ROUTE_T) { - for (rt = (rr_t*)hf->parsed; rt; rt = rt->next) { - if (parse_uri(rt->nameaddr.uri.s, rt->nameaddr.uri.len, &uri) == 0) { - LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, uri.port_no); - - if (check_self(&uri.host, uri.port_no ? uri.port_no : SIP_PORT, PROTO_NONE) != 1) { - LM_DBG("it is NOT me, FOUND!\n"); - - ospCopyStrToBuffer(&uri.host, nexthop, buffersize); - found = 1; - break; - } else { - LM_DBG("it IS me, keep looking\n"); - } - } else { - LM_ERR("failed to parse route uri '%.*s'\n", - rt->nameaddr.uri.len, - rt->nameaddr.uri.s); - } - } - if (found == 1) { - break; - } - } - } - - if (!found) { - LM_DBG("using the Request-Line instead host '%.*s' port '%d'\n", - msg->parsed_uri.host.len, - msg->parsed_uri.host.s, - msg->parsed_uri.port_no); - - ospCopyStrToBuffer(&msg->parsed_uri.host, nexthop, buffersize); - found = 1; - } + struct hdr_field *hf; + struct sip_uri uri; + rr_t *rt; + int found = 0; + + for(hf = msg->headers; hf; hf = hf->next) { + if(hf->type == HDR_ROUTE_T) { + for(rt = (rr_t *)hf->parsed; rt; rt = rt->next) { + if(parse_uri(rt->nameaddr.uri.s, rt->nameaddr.uri.len, &uri) + == 0) { + LM_DBG("host '%.*s' port '%d'\n", uri.host.len, uri.host.s, + uri.port_no); + + if(check_self(&uri.host, + uri.port_no ? uri.port_no : SIP_PORT, PROTO_NONE) + != 1) { + LM_DBG("it is NOT me, FOUND!\n"); + + ospCopyStrToBuffer(&uri.host, nexthop, buffersize); + found = 1; + break; + } else { + LM_DBG("it IS me, keep looking\n"); + } + } else { + LM_ERR("failed to parse route uri '%.*s'\n", + rt->nameaddr.uri.len, rt->nameaddr.uri.s); + } + } + if(found == 1) { + break; + } + } + } + + if(!found) { + LM_DBG("using the Request-Line instead host '%.*s' port '%d'\n", + msg->parsed_uri.host.len, msg->parsed_uri.host.s, + msg->parsed_uri.port_no); + + ospCopyStrToBuffer(&msg->parsed_uri.host, nexthop, buffersize); + found = 1; + } } diff --git a/src/modules/osp/sipheader.h b/src/modules/osp/sipheader.h index 9ff1d6ce869..13f7138a673 100644 --- a/src/modules/osp/sipheader.h +++ b/src/modules/osp/sipheader.h @@ -34,21 +34,25 @@ #include #include "../../core/parser/msg_parser.h" -#define OSP_TOKEN_HEADER "P-OSP-Auth-Token: " -#define OSP_HEADER_SIZE strlen(OSP_TOKEN_HEADER) +#define OSP_TOKEN_HEADER "P-OSP-Auth-Token: " +#define OSP_HEADER_SIZE strlen(OSP_TOKEN_HEADER) -void ospCopyStrToBuffer(str* source, char* buffer, int buffersize); -int ospGetFromUserpart(struct sip_msg* msg, char* fromuser, int buffersize); -int ospGetRpidUserpart(struct sip_msg* msg, char* fromuser, int buffersize); -int ospGetToUserpart(struct sip_msg* msg, char* touser, int buffersize); -int ospGetUriUserpart(struct sip_msg* msg, char* touser, int buffersize); -int ospAddOspHeader(struct sip_msg* msg, unsigned char* token, unsigned int tokensize); -int ospGetOspHeader(struct sip_msg* msg, unsigned char* token, unsigned int* tokensize); -int ospGetSourceAddress(struct sip_msg* msg, char* sourceaddress, int buffersize); -int ospGetCallId(struct sip_msg* msg, OSPTCALLID** callid); -int ospGetRouteParameters(struct sip_msg* msg, char* routeparams, int buffersize); -int ospRebuildDestionationUri(str* newuri, char* called, char* dest, char* port, int format); -void ospGetNextHop(struct sip_msg* msg, char* nexthop, int buffersize); +void ospCopyStrToBuffer(str *source, char *buffer, int buffersize); +int ospGetFromUserpart(struct sip_msg *msg, char *fromuser, int buffersize); +int ospGetRpidUserpart(struct sip_msg *msg, char *fromuser, int buffersize); +int ospGetToUserpart(struct sip_msg *msg, char *touser, int buffersize); +int ospGetUriUserpart(struct sip_msg *msg, char *touser, int buffersize); +int ospAddOspHeader( + struct sip_msg *msg, unsigned char *token, unsigned int tokensize); +int ospGetOspHeader( + struct sip_msg *msg, unsigned char *token, unsigned int *tokensize); +int ospGetSourceAddress( + struct sip_msg *msg, char *sourceaddress, int buffersize); +int ospGetCallId(struct sip_msg *msg, OSPTCALLID **callid); +int ospGetRouteParameters( + struct sip_msg *msg, char *routeparams, int buffersize); +int ospRebuildDestionationUri( + str *newuri, char *called, char *dest, char *port, int format); +void ospGetNextHop(struct sip_msg *msg, char *nexthop, int buffersize); #endif /* _OSP_MOD_SIPHEADER_H_ */ - diff --git a/src/modules/osp/term_transaction.c b/src/modules/osp/term_transaction.c index 07df126167c..cf9f18e9a37 100644 --- a/src/modules/osp/term_transaction.c +++ b/src/modules/osp/term_transaction.c @@ -35,7 +35,7 @@ #include "osptoolkit.h" #include "usage.h" -extern char* _osp_device_ip; +extern char *_osp_device_ip; extern int _osp_token_format; extern int _osp_validate_callid; extern OSPTPROVHANDLE _osp_provider; @@ -47,19 +47,16 @@ extern OSPTPROVHANDLE _osp_provider; * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure */ -int ospCheckHeader( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospCheckHeader(struct sip_msg *msg, char *ignore1, char *ignore2) { - unsigned char buffer[OSP_TOKENBUF_SIZE]; - unsigned int buffersize = sizeof(buffer); + unsigned char buffer[OSP_TOKENBUF_SIZE]; + unsigned int buffersize = sizeof(buffer); - if (ospGetOspHeader(msg, buffer, &buffersize) != 0) { - return MODULE_RETURNCODE_FALSE; - } else { - return MODULE_RETURNCODE_TRUE; - } + if(ospGetOspHeader(msg, buffer, &buffersize) != 0) { + return MODULE_RETURNCODE_FALSE; + } else { + return MODULE_RETURNCODE_TRUE; + } } /* @@ -68,131 +65,110 @@ int ospCheckHeader( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure MODULE_RETURNCODE_ERROR error */ -int ospValidateHeader ( - struct sip_msg* msg, - char* ignore1, - char* ignore2) +int ospValidateHeader(struct sip_msg *msg, char *ignore1, char *ignore2) { - int errorcode; - OSPTTRANHANDLE transaction = -1; - unsigned int authorized = 0; - unsigned int timelimit = 0; - void* detaillog = NULL; - unsigned int logsize = 0; - unsigned char* callidval = (unsigned char*)""; - OSPTCALLID* callid = NULL; - unsigned callidsize = 0; - unsigned char token[OSP_TOKENBUF_SIZE]; - unsigned int tokensize = sizeof(token); - osp_dest dest; - int result = MODULE_RETURNCODE_FALSE; + int errorcode; + OSPTTRANHANDLE transaction = -1; + unsigned int authorized = 0; + unsigned int timelimit = 0; + void *detaillog = NULL; + unsigned int logsize = 0; + unsigned char *callidval = (unsigned char *)""; + OSPTCALLID *callid = NULL; + unsigned callidsize = 0; + unsigned char token[OSP_TOKENBUF_SIZE]; + unsigned int tokensize = sizeof(token); + osp_dest dest; + int result = MODULE_RETURNCODE_FALSE; - ospInitDestination(&dest); + ospInitDestination(&dest); - if ((errorcode = OSPPTransactionNew(_osp_provider, &transaction) != OSPC_ERR_NO_ERROR)) { - LM_ERR("failed to create a new OSP transaction handle (%d)\n", errorcode); - } else if ((ospGetRpidUserpart(msg, dest.calling, sizeof(dest.calling)) != 0) && - (ospGetFromUserpart(msg, dest.calling, sizeof(dest.calling)) != 0)) - { - LM_ERR("failed to extract calling number\n"); - } else if ((ospGetUriUserpart(msg, dest.called, sizeof(dest.called)) != 0) && - (ospGetToUserpart(msg, dest.called, sizeof(dest.called)) != 0)) - { - LM_ERR("failed to extract called number\n"); - } else if (ospGetCallId(msg, &callid) != 0) { - LM_ERR("failed to extract call id\n"); - } else if (ospGetSourceAddress(msg, dest.source, sizeof(dest.source)) != 0) { - LM_ERR("failed to extract source address\n"); - } else if (ospGetOspHeader(msg, token, &tokensize) != 0) { - LM_ERR("failed to extract OSP authorization token\n"); - } else { - LM_INFO( "validate token for: " - "transaction_handle '%d' " - "e164_source '%s' " - "e164_dest '%s' " - "validate_call_id '%s' " - "call_id '%.*s'\n", - transaction, - dest.calling, - dest.called, - _osp_validate_callid == 0 ? "No" : "Yes", - callid->ospmCallIdLen, - callid->ospmCallIdVal); + if((errorcode = OSPPTransactionNew(_osp_provider, &transaction) + != OSPC_ERR_NO_ERROR)) { + LM_ERR("failed to create a new OSP transaction handle (%d)\n", + errorcode); + } else if((ospGetRpidUserpart(msg, dest.calling, sizeof(dest.calling)) != 0) + && (ospGetFromUserpart(msg, dest.calling, sizeof(dest.calling)) + != 0)) { + LM_ERR("failed to extract calling number\n"); + } else if((ospGetUriUserpart(msg, dest.called, sizeof(dest.called)) != 0) + && (ospGetToUserpart(msg, dest.called, sizeof(dest.called)) + != 0)) { + LM_ERR("failed to extract called number\n"); + } else if(ospGetCallId(msg, &callid) != 0) { + LM_ERR("failed to extract call id\n"); + } else if(ospGetSourceAddress(msg, dest.source, sizeof(dest.source)) != 0) { + LM_ERR("failed to extract source address\n"); + } else if(ospGetOspHeader(msg, token, &tokensize) != 0) { + LM_ERR("failed to extract OSP authorization token\n"); + } else { + LM_INFO("validate token for: " + "transaction_handle '%d' " + "e164_source '%s' " + "e164_dest '%s' " + "validate_call_id '%s' " + "call_id '%.*s'\n", + transaction, dest.calling, dest.called, + _osp_validate_callid == 0 ? "No" : "Yes", callid->ospmCallIdLen, + callid->ospmCallIdVal); - if (_osp_validate_callid != 0) { - callidsize = callid->ospmCallIdLen; - callidval = callid->ospmCallIdVal; - } + if(_osp_validate_callid != 0) { + callidsize = callid->ospmCallIdLen; + callidval = callid->ospmCallIdVal; + } - errorcode = OSPPTransactionValidateAuthorisation( - transaction, - "", - "", - "", - "", - dest.calling, - OSPC_E164, - dest.called, - OSPC_E164, - callidsize, - callidval, - tokensize, - token, - &authorized, - &timelimit, - &logsize, - detaillog, - _osp_token_format); - - if ((errorcode == OSPC_ERR_NO_ERROR) && (authorized == 1)) { - if (callid->ospmCallIdLen > sizeof(dest.callid) - 1) { - dest.callidsize = sizeof(dest.callid) - 1; - } else { - dest.callidsize = callid->ospmCallIdLen; - } - memcpy(dest.callid, callid->ospmCallIdVal, dest.callidsize); - dest.callid[dest.callidsize] = 0; - dest.transid = ospGetTransactionId(transaction); - dest.type = OSPC_DESTINATION; - dest.authtime = time(NULL); - strncpy(dest.host, _osp_device_ip, sizeof(dest.host) - 1); - strncpy(dest.origcalled, dest.called, sizeof(dest.origcalled) - 1); + errorcode = OSPPTransactionValidateAuthorisation(transaction, "", "", + "", "", dest.calling, OSPC_E164, dest.called, OSPC_E164, + callidsize, callidval, tokensize, token, &authorized, + &timelimit, &logsize, detaillog, _osp_token_format); - if (ospSaveTermDestination(&dest) == -1) { - LM_ERR("failed to save terminate destination\n"); - ospRecordEvent(0, 500); - result = MODULE_RETURNCODE_ERROR; - } else { - LM_DBG("call is authorized for %d seconds, call_id '%.*s' transaction_id '%llu'", - timelimit, - dest.callidsize, - dest.callid, - dest.transid); - ospRecordTermTransaction(msg, dest.transid, dest.source, dest.calling, dest.called, dest.authtime); - result = MODULE_RETURNCODE_TRUE; - } - } else { - LM_ERR("token is invalid (%d)\n", errorcode); + if((errorcode == OSPC_ERR_NO_ERROR) && (authorized == 1)) { + if(callid->ospmCallIdLen > sizeof(dest.callid) - 1) { + dest.callidsize = sizeof(dest.callid) - 1; + } else { + dest.callidsize = callid->ospmCallIdLen; + } + memcpy(dest.callid, callid->ospmCallIdVal, dest.callidsize); + dest.callid[dest.callidsize] = 0; + dest.transid = ospGetTransactionId(transaction); + dest.type = OSPC_DESTINATION; + dest.authtime = time(NULL); + strncpy(dest.host, _osp_device_ip, sizeof(dest.host) - 1); + strncpy(dest.origcalled, dest.called, sizeof(dest.origcalled) - 1); - /* + if(ospSaveTermDestination(&dest) == -1) { + LM_ERR("failed to save terminate destination\n"); + ospRecordEvent(0, 500); + result = MODULE_RETURNCODE_ERROR; + } else { + LM_DBG("call is authorized for %d seconds, call_id '%.*s' " + "transaction_id '%llu'", + timelimit, dest.callidsize, dest.callid, dest.transid); + ospRecordTermTransaction(msg, dest.transid, dest.source, + dest.calling, dest.called, dest.authtime); + result = MODULE_RETURNCODE_TRUE; + } + } else { + LM_ERR("token is invalid (%d)\n", errorcode); + + /* * Update terminating status code to 401 and report terminating setup usage. * We may need to make 401 configurable, just in case a user decides to reply with * a different code. Other options - trigger call setup usage reporting from the cpl * (after replying with an error code), or maybe use a different tm callback. */ - ospRecordEvent(0, 401); - result = MODULE_RETURNCODE_FALSE; - } - } + ospRecordEvent(0, 401); + result = MODULE_RETURNCODE_FALSE; + } + } - if (transaction != -1) { - OSPPTransactionDelete(transaction); - } + if(transaction != -1) { + OSPPTransactionDelete(transaction); + } - if (callid != NULL) { - OSPPCallIdDelete(&callid); - } - - return result; -} + if(callid != NULL) { + OSPPCallIdDelete(&callid); + } + return result; +} diff --git a/src/modules/osp/term_transaction.h b/src/modules/osp/term_transaction.h index 9b2c89041cb..bc515212014 100644 --- a/src/modules/osp/term_transaction.h +++ b/src/modules/osp/term_transaction.h @@ -33,8 +33,7 @@ #include "../../core/parser/msg_parser.h" -int ospCheckHeader(struct sip_msg*, char*, char*); -int ospValidateHeader(struct sip_msg*, char*, char*); +int ospCheckHeader(struct sip_msg *, char *, char *); +int ospValidateHeader(struct sip_msg *, char *, char *); #endif /* _OSP_MOD_TERM_TRANSACTION_H_ */ - diff --git a/src/modules/osp/tm.c b/src/modules/osp/tm.c index 58b738efbb4..07af981b1e1 100644 --- a/src/modules/osp/tm.c +++ b/src/modules/osp/tm.c @@ -38,8 +38,8 @@ struct tm_binds osp_tmb; -static void ospOnReq(struct cell* t, int type, struct tmcb_params* ps); -static void ospTmcbFunc(struct cell* t, int type, struct tmcb_params* ps); +static void ospOnReq(struct cell *t, int type, struct tmcb_params *ps); +static void ospTmcbFunc(struct cell *t, int type, struct tmcb_params *ps); /* * Load TM API @@ -47,20 +47,20 @@ static void ospTmcbFunc(struct cell* t, int type, struct tmcb_params* ps); */ int ospInitTm(void) { - if (load_tm_api(&osp_tmb) != 0) { - LM_ERR("failed to load TM API\n"); - LM_ERR("TM is required for reporting call setup usage\n"); - return -1; - } + if(load_tm_api(&osp_tmb) != 0) { + LM_ERR("failed to load TM API\n"); + LM_ERR("TM is required for reporting call setup usage\n"); + return -1; + } - /* Register callbacks, listen for all incoming requests */ - if (osp_tmb.register_tmcb(0, 0, TMCB_REQUEST_IN, ospOnReq, 0, 0) <= 0) { - LM_ERR("failed to register TMCB_REQUEST_IN callback\n"); - LM_ERR("TM callbacks are required for reporting call set up usage\n"); - return -1; - } + /* Register callbacks, listen for all incoming requests */ + if(osp_tmb.register_tmcb(0, 0, TMCB_REQUEST_IN, ospOnReq, 0, 0) <= 0) { + LM_ERR("failed to register TMCB_REQUEST_IN callback\n"); + LM_ERR("TM callbacks are required for reporting call set up usage\n"); + return -1; + } - return 0; + return 0; } /* @@ -69,34 +69,31 @@ int ospInitTm(void) * param type * param ps */ -static void ospOnReq( - struct cell* t, - int type, - struct tmcb_params* ps) +static void ospOnReq(struct cell *t, int type, struct tmcb_params *ps) { - int tmcb_types; + int tmcb_types; - /* install addaitional handlers */ - tmcb_types = -// TMCB_REQUEST_FWDED | -// TMCB_RESPONSE_FWDED | - TMCB_ON_FAILURE | -// TMCB_LOCAL_COMPLETED | - /* report on completed transactions */ - TMCB_RESPONSE_OUT | - /* account e2e acks if configured to do so */ - TMCB_E2EACK_IN | - /* report on missed calls */ - TMCB_ON_FAILURE_RO | - /* get incoming replies ready for processing */ -// TMCB_RESPONSE_IN | - 0; + /* install addaitional handlers */ + tmcb_types = + // TMCB_REQUEST_FWDED | + // TMCB_RESPONSE_FWDED | + TMCB_ON_FAILURE | + // TMCB_LOCAL_COMPLETED | + /* report on completed transactions */ + TMCB_RESPONSE_OUT | + /* account e2e acks if configured to do so */ + TMCB_E2EACK_IN | + /* report on missed calls */ + TMCB_ON_FAILURE_RO | + /* get incoming replies ready for processing */ + // TMCB_RESPONSE_IN | + 0; - if (osp_tmb.register_tmcb(0, t, tmcb_types, ospTmcbFunc, 0, 0) <= 0) { - LM_ERR("failed to register TM callbacks\n"); - LM_ERR("TM callbacks are required for reporting call setup usage\n"); - return; - } + if(osp_tmb.register_tmcb(0, t, tmcb_types, ospTmcbFunc, 0, 0) <= 0) { + LM_ERR("failed to register TM callbacks\n"); + LM_ERR("TM callbacks are required for reporting call setup usage\n"); + return; + } } /* @@ -105,35 +102,32 @@ static void ospOnReq( * param type * param ps */ -static void ospTmcbFunc( - struct cell* t, - int type, - struct tmcb_params* ps) +static void ospTmcbFunc(struct cell *t, int type, struct tmcb_params *ps) { - if (type & TMCB_RESPONSE_OUT) { - LM_DBG("RESPONSE_OUT\n"); - } else if (type & TMCB_E2EACK_IN) { - LM_DBG("E2EACK_IN\n"); - } else if (type & TMCB_ON_FAILURE_RO) { - LM_DBG("FAILURE_RO\n"); - } else if (type & TMCB_RESPONSE_IN) { - LM_DBG("RESPONSE_IN\n"); - } else if (type & TMCB_REQUEST_FWDED) { - LM_DBG("REQUEST_FWDED\n"); - } else if (type & TMCB_RESPONSE_FWDED) { - LM_DBG("RESPONSE_FWDED\n"); - } else if (type & TMCB_ON_FAILURE) { - LM_DBG("FAILURE\n"); - } else if (type & TMCB_LOCAL_COMPLETED) { - LM_DBG("COMPLETED\n"); - } else { - LM_DBG("something else '%d'\n", type); - } + if(type & TMCB_RESPONSE_OUT) { + LM_DBG("RESPONSE_OUT\n"); + } else if(type & TMCB_E2EACK_IN) { + LM_DBG("E2EACK_IN\n"); + } else if(type & TMCB_ON_FAILURE_RO) { + LM_DBG("FAILURE_RO\n"); + } else if(type & TMCB_RESPONSE_IN) { + LM_DBG("RESPONSE_IN\n"); + } else if(type & TMCB_REQUEST_FWDED) { + LM_DBG("REQUEST_FWDED\n"); + } else if(type & TMCB_RESPONSE_FWDED) { + LM_DBG("RESPONSE_FWDED\n"); + } else if(type & TMCB_ON_FAILURE) { + LM_DBG("FAILURE\n"); + } else if(type & TMCB_LOCAL_COMPLETED) { + LM_DBG("COMPLETED\n"); + } else { + LM_DBG("something else '%d'\n", type); + } - if (t) { - ospRecordEvent(t->uac[t->nr_of_outgoings - 1].last_received, - t->uas.status); - } else { - LM_DBG("cell is empty\n"); - } + if(t) { + ospRecordEvent( + t->uac[t->nr_of_outgoings - 1].last_received, t->uas.status); + } else { + LM_DBG("cell is empty\n"); + } } diff --git a/src/modules/osp/tm.h b/src/modules/osp/tm.h index 285acf535eb..419e062f4e4 100644 --- a/src/modules/osp/tm.h +++ b/src/modules/osp/tm.h @@ -39,4 +39,3 @@ int ospInitTm(void); #endif /* _OSP_MOD_TM_H_ */ - diff --git a/src/modules/osp/usage.c b/src/modules/osp/usage.c index 8747e01a8b7..e78803ad389 100644 --- a/src/modules/osp/usage.c +++ b/src/modules/osp/usage.c @@ -40,39 +40,47 @@ #include "osptoolkit.h" #include "sipheader.h" -#define OSP_ORIG_COOKIE "osp-o" -#define OSP_TERM_COOKIE "osp-t" +#define OSP_ORIG_COOKIE "osp-o" +#define OSP_TERM_COOKIE "osp-t" -#define OSP_RELEASE_ORIG 0 -#define OSP_RELEASE_TERM 1 +#define OSP_RELEASE_ORIG 0 +#define OSP_RELEASE_TERM 1 /* The up case tags for the destinations may corrupt OSP cookies */ -#define OSP_COOKIE_TRANSID 't' -#define OSP_COOKIE_TRANSIDUP 'T' -#define OSP_COOKIE_SRCIP 's' -#define OSP_COOKIE_SRCIPUP 'S' -#define OSP_COOKIE_AUTHTIME 'a' -#define OSP_COOKIE_AUTHTIMEUP 'A' -#define OSP_COOKIE_DSTCOUNT 'c' -#define OSP_COOKIE_DSTCOUNTUP 'C' +#define OSP_COOKIE_TRANSID 't' +#define OSP_COOKIE_TRANSIDUP 'T' +#define OSP_COOKIE_SRCIP 's' +#define OSP_COOKIE_SRCIPUP 'S' +#define OSP_COOKIE_AUTHTIME 'a' +#define OSP_COOKIE_AUTHTIMEUP 'A' +#define OSP_COOKIE_DSTCOUNT 'c' +#define OSP_COOKIE_DSTCOUNTUP 'C' /* Flags for OSP cookies */ -#define OSP_COOKIEHAS_TRANSID (1 << 0) -#define OSP_COOKIEHAS_SRCIP (1 << 1) -#define OSP_COOKIEHAS_AUTHTIME (1 << 2) -#define OSP_COOKIEHAS_DSTCOUNT (1 << 3) -#define OSP_COOKIEHAS_ORIGALL (OSP_COOKIEHAS_TRANSID | OSP_COOKIEHAS_SRCIP | OSP_COOKIEHAS_AUTHTIME | OSP_COOKIEHAS_DSTCOUNT) -#define OSP_COOKIEHAS_TERMALL (OSP_COOKIEHAS_TRANSID | OSP_COOKIEHAS_SRCIP | OSP_COOKIEHAS_AUTHTIME) - -extern char* _osp_device_ip; +#define OSP_COOKIEHAS_TRANSID (1 << 0) +#define OSP_COOKIEHAS_SRCIP (1 << 1) +#define OSP_COOKIEHAS_AUTHTIME (1 << 2) +#define OSP_COOKIEHAS_DSTCOUNT (1 << 3) +#define OSP_COOKIEHAS_ORIGALL \ + (OSP_COOKIEHAS_TRANSID | OSP_COOKIEHAS_SRCIP | OSP_COOKIEHAS_AUTHTIME \ + | OSP_COOKIEHAS_DSTCOUNT) +#define OSP_COOKIEHAS_TERMALL \ + (OSP_COOKIEHAS_TRANSID | OSP_COOKIEHAS_SRCIP | OSP_COOKIEHAS_AUTHTIME) + +extern char *_osp_device_ip; extern OSPTPROVHANDLE _osp_provider; extern str OSP_ORIGDEST_NAME; extern struct rr_binds osp_rr; -static void ospRecordTransaction(struct sip_msg* msg, unsigned long long transid, char* uac, char* from, char* to, time_t authtime, int isorig, unsigned destinationCount); -static int ospBuildUsageFromDestination(OSPTTRANHANDLE transaction, osp_dest* dest, int lastcode); -static int ospReportUsageFromDestination(OSPTTRANHANDLE transaction, osp_dest* dest); -static int ospReportUsageFromCookie(struct sip_msg* msg, char* cooky, OSPTCALLID* callid, int release, OSPE_MSG_ROLETYPES type); +static void ospRecordTransaction(struct sip_msg *msg, + unsigned long long transid, char *uac, char *from, char *to, + time_t authtime, int isorig, unsigned destinationCount); +static int ospBuildUsageFromDestination( + OSPTTRANHANDLE transaction, osp_dest *dest, int lastcode); +static int ospReportUsageFromDestination( + OSPTTRANHANDLE transaction, osp_dest *dest); +static int ospReportUsageFromCookie(struct sip_msg *msg, char *cooky, + OSPTCALLID *callid, int release, OSPE_MSG_ROLETYPES type); /* * Create OSP cookie and insert it into Record-Route header @@ -85,61 +93,40 @@ static int ospReportUsageFromCookie(struct sip_msg* msg, char* cooky, OSPTCALLID * param isorig Originate / Terminate * param destinationCount Destination count */ -static void ospRecordTransaction( - struct sip_msg* msg, - unsigned long long transid, - char* uac, - char* from, - char* to, - time_t authtime, - int isorig, - unsigned destinationCount) +static void ospRecordTransaction(struct sip_msg *msg, + unsigned long long transid, char *uac, char *from, char *to, + time_t authtime, int isorig, unsigned destinationCount) { - str cookie; - char buffer[OSP_STRBUF_SIZE]; - - if (osp_rr.add_rr_param == 0) { - LM_WARN("add_rr_param function is not found, cannot record information about the OSP transaction\n"); - return; - } - - cookie.s = buffer; - - if (isorig == 1) { - cookie.len = snprintf( - buffer, - sizeof(buffer), - ";%s=%c%llu_%c%s_%c%d_%c%d", - OSP_ORIG_COOKIE, - OSP_COOKIE_TRANSID, - transid, - OSP_COOKIE_SRCIP, - uac, - OSP_COOKIE_AUTHTIME, - (unsigned int)authtime, - OSP_COOKIE_DSTCOUNT, - destinationCount); - } else { - cookie.len = snprintf( - buffer, - sizeof(buffer), - ";%s=%c%llu_%c%s_%c%d", - OSP_TERM_COOKIE, - OSP_COOKIE_TRANSID, - transid, - OSP_COOKIE_SRCIP, - uac, - OSP_COOKIE_AUTHTIME, - (unsigned int)authtime); - } - - if (cookie.len < 0) { - LM_ERR("failed to create OSP cookie\n"); - return; - } - - LM_DBG("adding RR parameter '%s'\n", buffer); - osp_rr.add_rr_param(msg, &cookie); + str cookie; + char buffer[OSP_STRBUF_SIZE]; + + if(osp_rr.add_rr_param == 0) { + LM_WARN("add_rr_param function is not found, cannot record information " + "about the OSP transaction\n"); + return; + } + + cookie.s = buffer; + + if(isorig == 1) { + cookie.len = snprintf(buffer, sizeof(buffer), + ";%s=%c%llu_%c%s_%c%d_%c%d", OSP_ORIG_COOKIE, + OSP_COOKIE_TRANSID, transid, OSP_COOKIE_SRCIP, uac, + OSP_COOKIE_AUTHTIME, (unsigned int)authtime, + OSP_COOKIE_DSTCOUNT, destinationCount); + } else { + cookie.len = snprintf(buffer, sizeof(buffer), ";%s=%c%llu_%c%s_%c%d", + OSP_TERM_COOKIE, OSP_COOKIE_TRANSID, transid, OSP_COOKIE_SRCIP, + uac, OSP_COOKIE_AUTHTIME, (unsigned int)authtime); + } + + if(cookie.len < 0) { + LM_ERR("failed to create OSP cookie\n"); + return; + } + + LM_DBG("adding RR parameter '%s'\n", buffer); + osp_rr.add_rr_param(msg, &cookie); } /* @@ -152,18 +139,14 @@ static void ospRecordTransaction( * param authtime Request authorization time * param destinationCount Destination count */ -void ospRecordOrigTransaction( - struct sip_msg* msg, - unsigned long long transid, - char* uac, - char* from, - char* to, - time_t authtime, - unsigned destinationCount) +void ospRecordOrigTransaction(struct sip_msg *msg, unsigned long long transid, + char *uac, char *from, char *to, time_t authtime, + unsigned destinationCount) { - int isorig = 1; + int isorig = 1; - ospRecordTransaction(msg, transid, uac, from, to, authtime, isorig, destinationCount); + ospRecordTransaction( + msg, transid, uac, from, to, authtime, isorig, destinationCount); } /* @@ -175,18 +158,14 @@ void ospRecordOrigTransaction( * param to * param authtime Request authorization time */ -void ospRecordTermTransaction( - struct sip_msg* msg, - unsigned long long transid, - char* uac, - char* from, - char* to, - time_t authtime) +void ospRecordTermTransaction(struct sip_msg *msg, unsigned long long transid, + char *uac, char *from, char *to, time_t authtime) { - int isorig = 0; - unsigned destinationCount = 0; /* N/A */ + int isorig = 0; + unsigned destinationCount = 0; /* N/A */ - ospRecordTransaction(msg, transid, uac, from, to, authtime, isorig, destinationCount); + ospRecordTransaction( + msg, transid, uac, from, to, authtime, isorig, destinationCount); } /* @@ -198,207 +177,173 @@ void ospRecordTermTransaction( * param type Usage type * return */ -static int ospReportUsageFromCookie( - struct sip_msg* msg, - char* cookie, - OSPTCALLID* callid, - int release, - OSPE_MSG_ROLETYPES type) +static int ospReportUsageFromCookie(struct sip_msg *msg, char *cookie, + OSPTCALLID *callid, int release, OSPE_MSG_ROLETYPES type) { - char* tmp; - char* token; - char tag; - char* value; - unsigned long long transid = 0; - time_t authtime = 0; - unsigned destinationCount = 0; - time_t duration = 0; - time_t endtime = time(NULL); - int cookieflags = 0; - unsigned releasecode; - char firstvia[OSP_STRBUF_SIZE]; - char from[OSP_STRBUF_SIZE]; - char to[OSP_STRBUF_SIZE]; - char nexthop[OSP_STRBUF_SIZE]; - char* calling; - char* called; - char* originator = NULL; - char* terminator; - char* source; - char srcbuf[OSP_STRBUF_SIZE]; - char* destination; - char dstbuf[OSP_STRBUF_SIZE]; - char* srcdev; - char devbuf[OSP_STRBUF_SIZE]; - OSPTTRANHANDLE transaction = -1; - int errorcode; - - LM_DBG("'%s' type '%d'\n", cookie, type); - if (cookie != NULL) { - for (token = strtok_r(cookie, "_", &tmp); - token; - token = strtok_r(NULL, "_", &tmp)) - { - tag = *token; - value= token + 1; - - switch (tag) { - case OSP_COOKIE_TRANSID: - case OSP_COOKIE_TRANSIDUP: - transid = atoll(value); - cookieflags |= OSP_COOKIEHAS_TRANSID; - break; - case OSP_COOKIE_AUTHTIME: - case OSP_COOKIE_AUTHTIMEUP: - authtime = atoi(value); - duration = endtime - authtime; - cookieflags |= OSP_COOKIEHAS_AUTHTIME; - break; - case OSP_COOKIE_SRCIP: - case OSP_COOKIE_SRCIPUP: - originator = value; - cookieflags |= OSP_COOKIEHAS_SRCIP; - break; - case OSP_COOKIE_DSTCOUNT: - case OSP_COOKIE_DSTCOUNTUP: - destinationCount = (unsigned)atoi(value); - cookieflags |= OSP_COOKIEHAS_DSTCOUNT; - break; - default: - LM_ERR("unexpected tag '%c' / value '%s'\n", tag, value); - break; - } - } - } - - switch (type) { - case OSPC_DESTINATION: - if (cookieflags == OSP_COOKIEHAS_TERMALL) { - releasecode = 10016; - } else { - releasecode = 9016; - } - break; - case OSPC_SOURCE: - case OSPC_OTHER: - case OSPC_UNDEFINED_ROLE: - default: - if (cookieflags == OSP_COOKIEHAS_ORIGALL) { - releasecode = 10016; - } else { - releasecode = 9016; - } - break; - } - - if (releasecode == 9016) { - transid = 0; - originator = NULL; - authtime = 0; - duration = 0; - destinationCount = 0; - } - - ospGetSourceAddress(msg, firstvia, sizeof(firstvia)); - ospGetFromUserpart(msg, from, sizeof(from)); - ospGetToUserpart(msg, to, sizeof(to)); - ospGetNextHop(msg, nexthop, sizeof(nexthop)); - - LM_DBG("first via '%s' from '%s' to '%s' next hop '%s'\n", - firstvia, - from, - to, - nexthop); - - if (release == OSP_RELEASE_ORIG) { - LM_DBG("orig '%s' released the call, call_id '%.*s' transaction_id '%llu'\n", - firstvia, - callid->ospmCallIdLen, - callid->ospmCallIdVal, - transid); - if (originator == NULL) { - originator = firstvia; - } - calling = from; - called = to; - terminator = nexthop; - } else { - release = OSP_RELEASE_TERM; - LM_DBG("term '%s' released the call, call_id '%.*s' transaction_id '%llu'\n", - firstvia, - callid->ospmCallIdLen, - callid->ospmCallIdVal, - transid); - if (originator == NULL) { - originator = nexthop; - } - calling = to; - called = from; - terminator = firstvia; - } - - errorcode = OSPPTransactionNew(_osp_provider, &transaction); - - LM_DBG("created transaction handle '%d' (%d)\n", transaction, errorcode); - - switch (type) { - case OSPC_DESTINATION: - ospConvertAddress(originator, srcbuf, sizeof(srcbuf)); - source = srcbuf; - destination = _osp_device_ip; - srcdev = ""; - break; - case OSPC_SOURCE: - case OSPC_OTHER: - case OSPC_UNDEFINED_ROLE: - default: - source = _osp_device_ip; - ospConvertAddress(terminator, dstbuf, sizeof(dstbuf)); - destination = dstbuf; - ospConvertAddress(originator, devbuf, sizeof(devbuf)); - srcdev = devbuf; - break; - } - - errorcode = OSPPTransactionBuildUsageFromScratch( - transaction, - transid, - type, - source, - destination, - srcdev, - "", - calling, - OSPC_E164, - called, - OSPC_E164, - callid->ospmCallIdLen, - callid->ospmCallIdVal, - (enum OSPEFAILREASON)0, - NULL, - NULL); - - LM_DBG("built usage handle '%d' (%d)\n", transaction, errorcode); - - if ((errorcode == OSPC_ERR_NO_ERROR) && (destinationCount > 0)) { - errorcode = OSPPTransactionSetDestinationCount( - transaction, - destinationCount); - } - - ospReportUsageWrapper( - transaction, - releasecode, - duration, - authtime, - endtime, - 0, - 0, - 0, - 0, - release); - - return errorcode; + char *tmp; + char *token; + char tag; + char *value; + unsigned long long transid = 0; + time_t authtime = 0; + unsigned destinationCount = 0; + time_t duration = 0; + time_t endtime = time(NULL); + int cookieflags = 0; + unsigned releasecode; + char firstvia[OSP_STRBUF_SIZE]; + char from[OSP_STRBUF_SIZE]; + char to[OSP_STRBUF_SIZE]; + char nexthop[OSP_STRBUF_SIZE]; + char *calling; + char *called; + char *originator = NULL; + char *terminator; + char *source; + char srcbuf[OSP_STRBUF_SIZE]; + char *destination; + char dstbuf[OSP_STRBUF_SIZE]; + char *srcdev; + char devbuf[OSP_STRBUF_SIZE]; + OSPTTRANHANDLE transaction = -1; + int errorcode; + + LM_DBG("'%s' type '%d'\n", cookie, type); + if(cookie != NULL) { + for(token = strtok_r(cookie, "_", &tmp); token; + token = strtok_r(NULL, "_", &tmp)) { + tag = *token; + value = token + 1; + + switch(tag) { + case OSP_COOKIE_TRANSID: + case OSP_COOKIE_TRANSIDUP: + transid = atoll(value); + cookieflags |= OSP_COOKIEHAS_TRANSID; + break; + case OSP_COOKIE_AUTHTIME: + case OSP_COOKIE_AUTHTIMEUP: + authtime = atoi(value); + duration = endtime - authtime; + cookieflags |= OSP_COOKIEHAS_AUTHTIME; + break; + case OSP_COOKIE_SRCIP: + case OSP_COOKIE_SRCIPUP: + originator = value; + cookieflags |= OSP_COOKIEHAS_SRCIP; + break; + case OSP_COOKIE_DSTCOUNT: + case OSP_COOKIE_DSTCOUNTUP: + destinationCount = (unsigned)atoi(value); + cookieflags |= OSP_COOKIEHAS_DSTCOUNT; + break; + default: + LM_ERR("unexpected tag '%c' / value '%s'\n", tag, value); + break; + } + } + } + + switch(type) { + case OSPC_DESTINATION: + if(cookieflags == OSP_COOKIEHAS_TERMALL) { + releasecode = 10016; + } else { + releasecode = 9016; + } + break; + case OSPC_SOURCE: + case OSPC_OTHER: + case OSPC_UNDEFINED_ROLE: + default: + if(cookieflags == OSP_COOKIEHAS_ORIGALL) { + releasecode = 10016; + } else { + releasecode = 9016; + } + break; + } + + if(releasecode == 9016) { + transid = 0; + originator = NULL; + authtime = 0; + duration = 0; + destinationCount = 0; + } + + ospGetSourceAddress(msg, firstvia, sizeof(firstvia)); + ospGetFromUserpart(msg, from, sizeof(from)); + ospGetToUserpart(msg, to, sizeof(to)); + ospGetNextHop(msg, nexthop, sizeof(nexthop)); + + LM_DBG("first via '%s' from '%s' to '%s' next hop '%s'\n", firstvia, from, + to, nexthop); + + if(release == OSP_RELEASE_ORIG) { + LM_DBG("orig '%s' released the call, call_id '%.*s' transaction_id " + "'%llu'\n", + firstvia, callid->ospmCallIdLen, callid->ospmCallIdVal, + transid); + if(originator == NULL) { + originator = firstvia; + } + calling = from; + called = to; + terminator = nexthop; + } else { + release = OSP_RELEASE_TERM; + LM_DBG("term '%s' released the call, call_id '%.*s' transaction_id " + "'%llu'\n", + firstvia, callid->ospmCallIdLen, callid->ospmCallIdVal, + transid); + if(originator == NULL) { + originator = nexthop; + } + calling = to; + called = from; + terminator = firstvia; + } + + errorcode = OSPPTransactionNew(_osp_provider, &transaction); + + LM_DBG("created transaction handle '%d' (%d)\n", transaction, errorcode); + + switch(type) { + case OSPC_DESTINATION: + ospConvertAddress(originator, srcbuf, sizeof(srcbuf)); + source = srcbuf; + destination = _osp_device_ip; + srcdev = ""; + break; + case OSPC_SOURCE: + case OSPC_OTHER: + case OSPC_UNDEFINED_ROLE: + default: + source = _osp_device_ip; + ospConvertAddress(terminator, dstbuf, sizeof(dstbuf)); + destination = dstbuf; + ospConvertAddress(originator, devbuf, sizeof(devbuf)); + srcdev = devbuf; + break; + } + + errorcode = OSPPTransactionBuildUsageFromScratch(transaction, transid, type, + source, destination, srcdev, "", calling, OSPC_E164, called, + OSPC_E164, callid->ospmCallIdLen, callid->ospmCallIdVal, + (enum OSPEFAILREASON)0, NULL, NULL); + + LM_DBG("built usage handle '%d' (%d)\n", transaction, errorcode); + + if((errorcode == OSPC_ERR_NO_ERROR) && (destinationCount > 0)) { + errorcode = OSPPTransactionSetDestinationCount( + transaction, destinationCount); + } + + ospReportUsageWrapper(transaction, releasecode, duration, authtime, endtime, + 0, 0, 0, 0, release); + + return errorcode; } /* @@ -408,67 +353,68 @@ static int ospReportUsageFromCookie( * param ignore2 * return MODULE_RETURNCODE_TRUE success, MODULE_RETURNCODE_FALSE failure */ -int ospReportUsage( - struct sip_msg* msg, - char* whorelease, - char* ignore2) +int ospReportUsage(struct sip_msg *msg, char *whorelease, char *ignore2) { - int release; - char* tmp; - char* token; - char parameters[OSP_HEADERBUF_SIZE]; - OSPTCALLID* callid = NULL; - int result = MODULE_RETURNCODE_FALSE; - - ospGetCallId(msg, &callid); - - if (callid != NULL) { - /* Who releases the call first, 0 orig, 1 term */ - if (sscanf(whorelease, "%d", &release) != 1 || (release != OSP_RELEASE_ORIG && release != OSP_RELEASE_TERM)) { - release = OSP_RELEASE_ORIG; - } - LM_DBG("who releases the call first '%d'\n", release); - - if (ospGetRouteParameters(msg, parameters, sizeof(parameters)) == 0) { - for (token = strtok_r(parameters, ";", &tmp); - token; - token = strtok_r(NULL, ";", &tmp)) - { - if (strncmp(token, OSP_ORIG_COOKIE, strlen(OSP_ORIG_COOKIE)) == 0) { - LM_INFO("report orig duration for call_id '%.*s'\n", - callid->ospmCallIdLen, - callid->ospmCallIdVal); - ospReportUsageFromCookie(msg, token + strlen(OSP_ORIG_COOKIE) + 1, callid, release, OSPC_SOURCE); - result = MODULE_RETURNCODE_TRUE; - } else if (strncmp(token, OSP_TERM_COOKIE, strlen(OSP_TERM_COOKIE)) == 0) { - LM_INFO("report term duration for call_id '%.*s'\n", - callid->ospmCallIdLen, - callid->ospmCallIdVal); - ospReportUsageFromCookie(msg, token + strlen(OSP_TERM_COOKIE) + 1, callid, release, OSPC_DESTINATION); - result = MODULE_RETURNCODE_TRUE; - } else { - LM_DBG("ignoring parameter '%s'\n", token); - } - } - } - - if (result == MODULE_RETURNCODE_FALSE) { - LM_DBG("without orig or term OSP information\n"); - LM_INFO("report other duration for call_id '%.*s'\n", - callid->ospmCallIdLen, - callid->ospmCallIdVal); - ospReportUsageFromCookie(msg, NULL, callid, release, OSPC_SOURCE); - result = MODULE_RETURNCODE_TRUE; - } - - OSPPCallIdDelete(&callid); - } - - if (result == MODULE_RETURNCODE_FALSE) { - LM_ERR("failed to report usage\n"); - } - - return result; + int release; + char *tmp; + char *token; + char parameters[OSP_HEADERBUF_SIZE]; + OSPTCALLID *callid = NULL; + int result = MODULE_RETURNCODE_FALSE; + + ospGetCallId(msg, &callid); + + if(callid != NULL) { + /* Who releases the call first, 0 orig, 1 term */ + if(sscanf(whorelease, "%d", &release) != 1 + || (release != OSP_RELEASE_ORIG + && release != OSP_RELEASE_TERM)) { + release = OSP_RELEASE_ORIG; + } + LM_DBG("who releases the call first '%d'\n", release); + + if(ospGetRouteParameters(msg, parameters, sizeof(parameters)) == 0) { + for(token = strtok_r(parameters, ";", &tmp); token; + token = strtok_r(NULL, ";", &tmp)) { + if(strncmp(token, OSP_ORIG_COOKIE, strlen(OSP_ORIG_COOKIE)) + == 0) { + LM_INFO("report orig duration for call_id '%.*s'\n", + callid->ospmCallIdLen, callid->ospmCallIdVal); + ospReportUsageFromCookie(msg, + token + strlen(OSP_ORIG_COOKIE) + 1, callid, + release, OSPC_SOURCE); + result = MODULE_RETURNCODE_TRUE; + } else if(strncmp(token, OSP_TERM_COOKIE, + strlen(OSP_TERM_COOKIE)) + == 0) { + LM_INFO("report term duration for call_id '%.*s'\n", + callid->ospmCallIdLen, callid->ospmCallIdVal); + ospReportUsageFromCookie(msg, + token + strlen(OSP_TERM_COOKIE) + 1, callid, + release, OSPC_DESTINATION); + result = MODULE_RETURNCODE_TRUE; + } else { + LM_DBG("ignoring parameter '%s'\n", token); + } + } + } + + if(result == MODULE_RETURNCODE_FALSE) { + LM_DBG("without orig or term OSP information\n"); + LM_INFO("report other duration for call_id '%.*s'\n", + callid->ospmCallIdLen, callid->ospmCallIdVal); + ospReportUsageFromCookie(msg, NULL, callid, release, OSPC_SOURCE); + result = MODULE_RETURNCODE_TRUE; + } + + OSPPCallIdDelete(&callid); + } + + if(result == MODULE_RETURNCODE_FALSE) { + LM_ERR("failed to report usage\n"); + } + + return result; } /* @@ -479,44 +425,31 @@ int ospReportUsage( * return 0 success, others failure */ static int ospBuildUsageFromDestination( - OSPTTRANHANDLE transaction, - osp_dest* dest, - int lastcode) + OSPTTRANHANDLE transaction, osp_dest *dest, int lastcode) { - int errorcode; - char addr[OSP_STRBUF_SIZE]; - char* source; - char* srcdev; - - if (dest->type == OSPC_SOURCE) { - ospConvertAddress(dest->srcdev, addr, sizeof(addr)); - source = dest->source; - srcdev = addr; - } else { - ospConvertAddress(dest->source, addr, sizeof(addr)); - source = addr; - srcdev = dest->srcdev; - } - - errorcode = OSPPTransactionBuildUsageFromScratch( - transaction, - dest->transid, - dest->type, - source, - dest->host, - srcdev, - dest->destdev, - dest->calling, - OSPC_E164, - dest->origcalled, /* Report original called number */ - OSPC_E164, - dest->callidsize, - dest->callid, - (enum OSPEFAILREASON)lastcode, - NULL, - NULL); - - return errorcode; + int errorcode; + char addr[OSP_STRBUF_SIZE]; + char *source; + char *srcdev; + + if(dest->type == OSPC_SOURCE) { + ospConvertAddress(dest->srcdev, addr, sizeof(addr)); + source = dest->source; + srcdev = addr; + } else { + ospConvertAddress(dest->source, addr, sizeof(addr)); + source = addr; + srcdev = dest->srcdev; + } + + errorcode = OSPPTransactionBuildUsageFromScratch(transaction, dest->transid, + dest->type, source, dest->host, srcdev, dest->destdev, + dest->calling, OSPC_E164, + dest->origcalled, /* Report original called number */ + OSPC_E164, dest->callidsize, dest->callid, + (enum OSPEFAILREASON)lastcode, NULL, NULL); + + return errorcode; } /* @@ -526,22 +459,21 @@ static int ospBuildUsageFromDestination( * return 0 success */ static int ospReportUsageFromDestination( - OSPTTRANHANDLE transaction, - osp_dest* dest) + OSPTTRANHANDLE transaction, osp_dest *dest) { - ospReportUsageWrapper( - transaction, /* In - Transaction handle */ - dest->lastcode, /* In - Release Code */ - 0, /* In - Length of call */ - dest->authtime, /* In - Call start time */ - 0, /* In - Call end time */ - dest->time180, /* In - Call alert time */ - dest->time200, /* In - Call connect time */ - dest->time180 ? 1 : 0, /* In - Is PDD Info present */ - dest->time180 ? dest->time180 - dest->authtime : 0, /* In - Post Dial Delay */ - 0); - - return 0; + ospReportUsageWrapper(transaction, /* In - Transaction handle */ + dest->lastcode, /* In - Release Code */ + 0, /* In - Length of call */ + dest->authtime, /* In - Call start time */ + 0, /* In - Call end time */ + dest->time180, /* In - Call alert time */ + dest->time200, /* In - Call connect time */ + dest->time180 ? 1 : 0, /* In - Is PDD Info present */ + dest->time180 ? dest->time180 - dest->authtime + : 0, /* In - Post Dial Delay */ + 0); + + return 0; } /* @@ -549,62 +481,60 @@ static int ospReportUsageFromDestination( */ void ospReportOrigSetupUsage(void) { - osp_dest* dest = NULL; - osp_dest* lastused = NULL; - struct usr_avp* destavp = NULL; - int_str destval; - OSPTTRANHANDLE transaction = -1; - int lastcode = 0; - int errorcode; + osp_dest *dest = NULL; + osp_dest *lastused = NULL; + struct usr_avp *destavp = NULL; + int_str destval; + OSPTTRANHANDLE transaction = -1; + int lastcode = 0; + int errorcode; struct search_state st; - errorcode = OSPPTransactionNew(_osp_provider, &transaction); + errorcode = OSPPTransactionNew(_osp_provider, &transaction); - for (destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, (int_str)OSP_ORIGDEST_NAME, NULL, &st); - destavp != NULL; - destavp = search_next_avp(&st, 0)) - { - get_avp_val(destavp, &destval); + for(destavp = search_first_avp(AVP_NAME_STR | AVP_VAL_STR, + (int_str)OSP_ORIGDEST_NAME, NULL, &st); + destavp != NULL; destavp = search_next_avp(&st, 0)) { + get_avp_val(destavp, &destval); - /* OSP destination is wrapped in a string */ - dest = (osp_dest*)destval.s.s; + /* OSP destination is wrapped in a string */ + dest = (osp_dest *)destval.s.s; - if (dest->used == 1) { - if (dest->reported == 1) { - LM_DBG("orig setup already reported\n"); - break; - } else { - dest->reported = 1; - } + if(dest->used == 1) { + if(dest->reported == 1) { + LM_DBG("orig setup already reported\n"); + break; + } else { + dest->reported = 1; + } - LM_DBG("iterating through used destination\n"); + LM_DBG("iterating through used destination\n"); - ospDumpDestination(dest); + ospDumpDestination(dest); - lastused = dest; + lastused = dest; - errorcode = ospBuildUsageFromDestination(transaction, dest, lastcode); + errorcode = + ospBuildUsageFromDestination(transaction, dest, lastcode); - lastcode = dest->lastcode; - } else { - LM_DBG("destination has not been used, breaking out\n"); - break; - } - } + lastcode = dest->lastcode; + } else { + LM_DBG("destination has not been used, breaking out\n"); + break; + } + } - if (lastused) { - LM_INFO("report orig setup for call_id '%.*s' transaction_id '%llu'\n", - lastused->callidsize, - lastused->callid, - lastused->transid); - errorcode = ospReportUsageFromDestination(transaction, lastused); - } else { - /* If a Toolkit transaction handle was created, but we did not find + if(lastused) { + LM_INFO("report orig setup for call_id '%.*s' transaction_id '%llu'\n", + lastused->callidsize, lastused->callid, lastused->transid); + errorcode = ospReportUsageFromDestination(transaction, lastused); + } else { + /* If a Toolkit transaction handle was created, but we did not find * any destinations to report, we need to release the handle. Otherwise, * the ospReportUsageFromDestination will release it. */ - OSPPTransactionDelete(transaction); - } + OSPPTransactionDelete(transaction); + } } /* @@ -612,24 +542,23 @@ void ospReportOrigSetupUsage(void) */ void ospReportTermSetupUsage(void) { - osp_dest* dest = NULL; - OSPTTRANHANDLE transaction = -1; - int errorcode; - - if ((dest = ospGetTermDestination())) { - if (dest->reported == 0) { - dest->reported = 1; - LM_INFO("report term setup for call_id '%.*s' transaction_id '%llu'\n", - dest->callidsize, - dest->callid, - dest->transid); - errorcode = OSPPTransactionNew(_osp_provider, &transaction); - errorcode = ospBuildUsageFromDestination(transaction, dest, 0); - errorcode = ospReportUsageFromDestination(transaction, dest); - } else { - LM_DBG("term setup already reported\n"); - } - } else { - LM_ERR("without term setup to report\n"); - } + osp_dest *dest = NULL; + OSPTTRANHANDLE transaction = -1; + int errorcode; + + if((dest = ospGetTermDestination())) { + if(dest->reported == 0) { + dest->reported = 1; + LM_INFO("report term setup for call_id '%.*s' transaction_id " + "'%llu'\n", + dest->callidsize, dest->callid, dest->transid); + errorcode = OSPPTransactionNew(_osp_provider, &transaction); + errorcode = ospBuildUsageFromDestination(transaction, dest, 0); + errorcode = ospReportUsageFromDestination(transaction, dest); + } else { + LM_DBG("term setup already reported\n"); + } + } else { + LM_ERR("without term setup to report\n"); + } } diff --git a/src/modules/osp/usage.h b/src/modules/osp/usage.h index 2431be31581..b1b79dcb0f2 100644 --- a/src/modules/osp/usage.h +++ b/src/modules/osp/usage.h @@ -49,11 +49,13 @@ * Actual conversation duration maybe calculated using connect time (from the call * set up usage) and stop time (from the duration usage). */ -void ospRecordOrigTransaction(struct sip_msg* msg, unsigned long long transid, char* uac, char* from, char* to, time_t authtime, unsigned destinationCount); -void ospRecordTermTransaction(struct sip_msg* msg, unsigned long long transid, char* uac, char* from, char* to, time_t authtime); +void ospRecordOrigTransaction(struct sip_msg *msg, unsigned long long transid, + char *uac, char *from, char *to, time_t authtime, + unsigned destinationCount); +void ospRecordTermTransaction(struct sip_msg *msg, unsigned long long transid, + char *uac, char *from, char *to, time_t authtime); void ospReportOrigSetupUsage(void); void ospReportTermSetupUsage(void); -int ospReportUsage(struct sip_msg* msg, char* whorelease, char* ignore2); +int ospReportUsage(struct sip_msg *msg, char *whorelease, char *ignore2); #endif /* _OSP_MOD_USAGE_H_ */ -