Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Alexandre Westfahl <awestfahl@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. * |
| 6 | * * |
| 7 | * All rights reserved. * |
| 8 | * * |
| 9 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 10 | * permitted provided that the following conditions are met: * |
| 11 | * * |
| 12 | * * Redistributions of source code must retain the above * |
| 13 | * copyright notice, this list of conditions and the * |
| 14 | * following disclaimer. * |
| 15 | * * |
| 16 | * * Redistributions in binary form must reproduce the above * |
| 17 | * copyright notice, this list of conditions and the * |
| 18 | * following disclaimer in the documentation and/or other * |
| 19 | * materials provided with the distribution. * |
| 20 | * * |
| 21 | * * Neither the name of the Teraoka Laboratory nor the * |
| 22 | * names of its contributors may be used to endorse or * |
| 23 | * promote products derived from this software without * |
| 24 | * specific prior written permission of Teraoka Laboratory * |
| 25 | * * |
| 26 | * * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 29 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 31 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 33 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 35 | *********************************************************************************************************/ |
| 36 | #include "app_sip.h" |
| 37 | |
| 38 | //Diameter-SIP server |
| 39 | struct disp_hdl * app_sip_MAR_hdl=NULL; |
| 40 | struct disp_hdl * app_sip_LIR_hdl=NULL; |
| 41 | struct disp_hdl * app_sip_UAR_hdl=NULL; |
| 42 | struct disp_hdl * app_sip_SAR_hdl=NULL; |
| 43 | struct disp_hdl * app_sip_PPA_hdl=NULL; |
| 44 | struct disp_hdl * app_sip_RTA_hdl=NULL; |
| 45 | |
| 46 | //Suscriber Locator |
| 47 | struct disp_hdl * app_sip_SL_LIR_hdl=NULL; |
| 48 | |
| 49 | |
| 50 | |
| 51 | struct disp_hdl * app_sip_default_hdl=NULL; |
| 52 | struct session_handler * ds_sess_hdl; |
| 53 | |
| 54 | //configuration stucture |
| 55 | struct as_conf * as_conf=NULL; |
| 56 | static struct as_conf app_sip_conf; |
| 57 | |
| 58 | //dictionary of SIP |
| 59 | struct app_sip_dict sip_dict; |
| 60 | |
| 61 | int app_sip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act) |
| 62 | { |
| 63 | TRACE_ENTRY("%p %p %p %p %p", msg, avp, sess, opaque, act); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | void dump_config() |
| 69 | { |
| 70 | TRACE_DEBUG(FULL,"***Configuration of Diameter-SIP extension***"); |
| 71 | TRACE_DEBUG(FULL,"# mode: *%d*",as_conf->mode); |
| 72 | TRACE_DEBUG(FULL,"# datasource: *%d*",as_conf->datasource); |
| 73 | TRACE_DEBUG(FULL,"# mysql_login: *%s*",as_conf->mysql_login); |
| 74 | TRACE_DEBUG(FULL,"# mysql_password: *%s*",as_conf->mysql_password); |
| 75 | TRACE_DEBUG(FULL,"# mysql_database: *%s*",as_conf->mysql_database); |
| 76 | TRACE_DEBUG(FULL,"# mysql_server: *%s*",as_conf->mysql_server); |
| 77 | TRACE_DEBUG(FULL,"# mysql_port: *%d*",as_conf->mysql_port); |
| 78 | TRACE_DEBUG(FULL,"# mysql_port: *%s*",as_conf->mysql_prefix); |
| 79 | TRACE_DEBUG(FULL,"***End of Diameter-SIP configuration extension***"); |
| 80 | } |
| 81 | |
| 82 | static int as_conf_init(void) |
| 83 | { |
| 84 | as_conf=&app_sip_conf; |
| 85 | //memset(app_sip_conf, 0, sizeof(struct as_conf)); |
| 86 | |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* entry point */ |
| 92 | int as_entry(char * conffile) |
| 93 | { |
| 94 | TRACE_ENTRY("%p", conffile); |
| 95 | |
| 96 | struct dict_object * app=NULL; |
| 97 | struct disp_when data; |
| 98 | pthread_t rtr_thread, ppr_thread; |
| 99 | |
| 100 | /* Initialize configuration */ |
| 101 | CHECK_FCT( as_conf_init() ); |
| 102 | |
| 103 | |
| 104 | //We parse the configuration file |
| 105 | if (conffile != NULL) { |
| 106 | CHECK_FCT( as_conf_handle(conffile) ); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | TRACE_DEBUG(INFO, "We need a configuration file for Diameter-SIP extension. See doc/ for an example."); |
| 111 | } |
| 112 | |
| 113 | //TODO: replace by configuration file!! |
| 114 | strcpy(as_conf->mysql_prefix,"as_"); |
| 115 | |
| 116 | //We can dump the configuration extracted from app_sip.conf |
| 117 | //dump_config(); |
| 118 | |
| 119 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &app, ENOENT) ); |
| 120 | CHECK_FCT( fd_disp_app_support ( app, NULL, 1, 0 ) ); |
| 121 | |
| 122 | |
| 123 | |
| 124 | //We set useful AVPs |
| 125 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Session-State", &sip_dict.Auth_Session_State, ENOENT) ); |
| 126 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &sip_dict.Auth_Application_Id, ENOENT) ); |
| 127 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Host", &sip_dict.Destination_Host, ENOENT) ); |
| 128 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Realm", &sip_dict.Destination_Realm, ENOENT) ); |
| 129 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sip_dict.Session_Id, ENOENT) ); |
| 130 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host", &sip_dict.Redirect_Host, ENOENT) ); |
| 131 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host-Usage", &sip_dict.Redirect_Host_Usage, ENOENT) ); |
| 132 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Auth-Data-Item", &sip_dict.SIP_Auth_Data_Item, ENOENT) ); |
| 133 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authorization", &sip_dict.SIP_Authorization, ENOENT) ); |
| 134 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authenticate", &sip_dict.SIP_Authenticate, ENOENT) ); |
| 135 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Number-Auth-Items", &sip_dict.SIP_Number_Auth_Items, ENOENT) ); |
| 136 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Scheme", &sip_dict.SIP_Authentication_Scheme, ENOENT) ); |
| 137 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Info", &sip_dict.SIP_Authentication_Info, ENOENT) ); |
| 138 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-URI", &sip_dict.SIP_Server_URI, ENOENT) ); |
| 139 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Capabilities", &sip_dict.SIP_Server_Capabilities, ENOENT) ); |
| 140 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Mandatory-Capability", &sip_dict.SIP_Mandatory_Capability, ENOENT) ); |
| 141 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Optional-Capability", &sip_dict.SIP_Optional_Capability, ENOENT) ); |
| 142 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Method", &sip_dict.SIP_Method, ENOENT) ); |
| 143 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-AOR", &sip_dict.SIP_AOR, ENOENT) ); |
| 144 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Deregistration-Reason", &sip_dict.SIP_Deregistration_Reason, ENOENT) ); |
| 145 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Code", &sip_dict.SIP_Reason_Code, ENOENT) ); |
| 146 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Info", &sip_dict.SIP_Reason_Info, ENOENT) ); |
| 147 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Information", &sip_dict.SIP_Accounting_Information, ENOENT) ); |
| 148 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Server-URI", &sip_dict.SIP_Accounting_Server_URI, ENOENT) ); |
| 149 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Credit-Control-Server-URI", &sip_dict.SIP_Credit_Control_Server_URI, ENOENT) ); |
| 150 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Assignment-Type", &sip_dict.SIP_Server_Assignment_Type, ENOENT) ); |
| 151 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Item-Number", &sip_dict.SIP_Item_Number, ENOENT) ); |
| 152 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Authorization-Type", &sip_dict.SIP_User_Authorization_Type, ENOENT) ); |
| 153 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Supported-User-Data-Type", &sip_dict.SIP_Supported_User_Data_Type, ENOENT) ); |
| 154 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data", &sip_dict.SIP_User_Data, ENOENT) ); |
| 155 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Type", &sip_dict.SIP_User_Data_Type, ENOENT) ); |
| 156 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Contents", &sip_dict.SIP_User_Data_Contents, ENOENT) ); |
| 157 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Already-Available", &sip_dict.SIP_User_Data_Already_Available, ENOENT) ); |
| 158 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Visited-Network-Id", &sip_dict.SIP_Visited_Network_Id, ENOENT) ); |
| 159 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) ); |
| 160 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-URI", &sip_dict.Digest_URI, ENOENT) ); |
| 161 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce", &sip_dict.Digest_Nonce, ENOENT) ); |
| 162 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-CNonce", &sip_dict.Digest_CNonce, ENOENT) ); |
| 163 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Method", &sip_dict.Digest_Method, ENOENT) ); |
| 164 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce-Count", &sip_dict.Digest_Nonce_Count, ENOENT) ); |
| 165 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) ); |
| 166 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response", &sip_dict.Digest_Response, ENOENT) ); |
| 167 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response-Auth", &sip_dict.Digest_Response_Auth, ENOENT) ); |
| 168 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Username", &sip_dict.Digest_Username, ENOENT) ); |
| 169 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Algorithm", &sip_dict.Digest_Algorithm, ENOENT) ); |
| 170 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-QoP", &sip_dict.Digest_QOP, ENOENT) ); |
| 171 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "User-Name", &sip_dict.User_Name, ENOENT) ); |
| 172 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-HA1", &sip_dict.Digest_HA1, ENOENT) ); |
| 173 | |
| 174 | |
| 175 | //Register Application |
| 176 | memset(&data, 0, sizeof(data)); |
| 177 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &data.app, ENOENT) ); |
| 178 | |
| 179 | if(as_conf->mode==1) |
| 180 | { |
| 181 | // **Command Codes |
| 182 | //MAR |
| 183 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Multimedia-Auth-Request", &data.command, ENOENT) ); |
| 184 | CHECK_FCT( fd_disp_register( app_sip_MAR_cb, DISP_HOW_CC, &data, NULL, &app_sip_MAR_hdl ) ); |
| 185 | //RTA |
| 186 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Registration-Termination-Answer", &data.command, ENOENT) ); |
| 187 | CHECK_FCT( fd_disp_register( app_sip_RTA_cb, DISP_HOW_CC, &data, NULL, &app_sip_RTA_hdl ) ); |
| 188 | //PPA |
| 189 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Push-Profile-Answer", &data.command, ENOENT) ); |
| 190 | CHECK_FCT( fd_disp_register( app_sip_PPA_cb, DISP_HOW_CC, &data, NULL, &app_sip_PPA_hdl ) ); |
| 191 | //LIR |
| 192 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) ); |
| 193 | CHECK_FCT( fd_disp_register( app_sip_LIR_cb, DISP_HOW_CC, &data, NULL, &app_sip_LIR_hdl ) ); |
| 194 | //UAR |
| 195 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "User-Authorization-Request", &data.command, ENOENT) ); |
| 196 | CHECK_FCT( fd_disp_register( app_sip_UAR_cb, DISP_HOW_CC, &data, NULL, &app_sip_UAR_hdl ) ); |
| 197 | //SAR |
| 198 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Server-Assignment-Request", &data.command, ENOENT) ); |
| 199 | CHECK_FCT( fd_disp_register( app_sip_SAR_cb, DISP_HOW_CC, &data, NULL, &app_sip_SAR_hdl ) ); |
| 200 | } |
| 201 | if(as_conf->mode==2) |
| 202 | { |
| 203 | //LIR |
| 204 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) ); |
| 205 | CHECK_FCT( fd_disp_register( app_sip_SL_LIR_cb, DISP_HOW_CC, &data, NULL, &app_sip_SL_LIR_hdl ) ); |
| 206 | } |
| 207 | //Callback for unexpected messages |
| 208 | CHECK_FCT( fd_disp_register( app_sip_default_cb, DISP_HOW_APPID, &data, NULL, &app_sip_default_hdl ) ); |
| 209 | |
| 210 | |
| 211 | //We start database connection |
| 212 | if(start_mysql_connection()) |
| 213 | return EINVAL; |
| 214 | |
| 215 | CHECK_FCT(fd_sess_handler_create(&ds_sess_hdl, (void *)free, NULL, NULL)); |
| 216 | |
| 217 | //Creation of thread for Registration Termination |
| 218 | if(pthread_create(&rtr_thread, NULL,rtr_socket, NULL)) |
| 219 | { |
| 220 | TRACE_DEBUG(INFO,"Creation of thread failed, abort!"); |
| 221 | return EINVAL; |
| 222 | } |
| 223 | //Creation of thread for Push Profile |
| 224 | if(pthread_create(&ppr_thread, NULL,ppr_socket, NULL)) |
| 225 | { |
| 226 | TRACE_DEBUG(INFO,"Creation of thread failed, abort!"); |
| 227 | return EINVAL; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | //Cleanup callback |
| 235 | void fd_ext_fini(void) |
| 236 | { |
| 237 | //TODO:unregister other callbacks |
| 238 | |
| 239 | (void) fd_disp_unregister(&app_sip_MAR_hdl, NULL); |
| 240 | CHECK_FCT_DO( fd_sess_handler_destroy(&ds_sess_hdl, NULL),return); |
| 241 | |
| 242 | |
| 243 | //We close database connection |
| 244 | close_mysql_connection(); |
| 245 | |
| 246 | |
| 247 | |
| 248 | TRACE_ENTRY(); |
| 249 | return ; |
| 250 | } |
| 251 | |
| 252 | EXTENSION_ENTRY("app_sip", as_entry, "dict_sip"); |