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 | #include <sys/types.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <arpa/inet.h> |
| 41 | #include <unistd.h> |
| 42 | typedef int SOCKET; |
| 43 | typedef struct sockaddr_in SOCKADDR_IN; |
| 44 | typedef struct sockaddr SOCKADDR; |
| 45 | |
| 46 | //Procedure which always wait for data on socket |
| 47 | void *ppr_socket(void *arg) |
| 48 | { |
| 49 | |
| 50 | SOCKET sock; |
| 51 | SOCKADDR_IN sin, csin; |
| 52 | struct pprsipaor pprsip; |
| 53 | int rcvbytes=0; |
| 54 | sock = socket(AF_INET, SOCK_STREAM, 0); |
| 55 | sin.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 56 | sin.sin_family = AF_INET; |
| 57 | sin.sin_port = htons(as_conf->ppr_port); |
| 58 | socklen_t sinsize = sizeof(csin); |
| 59 | int accepted=0; |
| 60 | |
| 61 | |
| 62 | if(!bind(sock, (SOCKADDR*)&sin, sizeof(sin))) |
| 63 | { |
| 64 | if(listen(sock,1)) |
| 65 | { |
| 66 | TRACE_DEBUG(INFO,"ERROR on listen!"); |
| 67 | } |
| 68 | |
| 69 | while(1) |
| 70 | { |
| 71 | accepted=accept(sock, (struct sockaddr *)&csin,&sinsize); |
| 72 | if(accepted>-1) |
| 73 | { |
| 74 | rcvbytes=recv(accepted, &pprsip, sizeof(struct pprsipaor),0); |
| 75 | |
| 76 | if(rcvbytes>-1) |
| 77 | { |
| 78 | //We received something, we can send an PPR |
| 79 | app_sip_PPR_cb(&pprsip); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | } |
| 86 | else |
| 87 | TRACE_DEBUG(INFO,"Can't create socket!"); |
| 88 | |
| 89 | |
| 90 | pthread_exit(NULL); |
| 91 | |
| 92 | } |
| 93 | //Called to send a PPR |
| 94 | int app_sip_PPR_cb(struct pprsipaor *structure) |
| 95 | { |
| 96 | /* |
| 97 | TRACE_ENTRY("%p", structure); |
| 98 | |
| 99 | int got_username=0; |
| 100 | int got_streason=0; |
| 101 | int num_aor=0;//How many SIP-AOR? |
| 102 | struct dict_object * rtr_model=NULL; |
| 103 | struct msg * message=NULL; |
| 104 | struct avp *groupedavp=NULL, *avp=NULL; |
| 105 | struct session *sess=NULL; |
| 106 | union avp_value value; |
| 107 | |
| 108 | //We must check that we have all needed value in structure |
| 109 | if(structure.username[0]!='\0') |
| 110 | got_username=1; |
| 111 | |
| 112 | if(structure.sip_aor1[0]!='\0') |
| 113 | { |
| 114 | num_aor++; |
| 115 | if(structure.sip_aor2[0]!='\0') |
| 116 | { |
| 117 | num_aor++; |
| 118 | if(structure.sip_aor3[0]!='\0') |
| 119 | num_aor++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if(structure.strreason!='\0') |
| 124 | got_streason=1; |
| 125 | |
| 126 | |
| 127 | TRACE_DEBUG(INFO,"We have %d SIP_AOR",num_aor); |
| 128 | |
| 129 | if((got_username + num_aor)==0) |
| 130 | { |
| 131 | //We must have a least a SIP_AOR or a Username |
| 132 | TRACE_DEBUG(INFO,"Can not proceed because there is no SIP_AOR or Username"); |
| 133 | return EINVAL; |
| 134 | } |
| 135 | if(structure.reason<0) |
| 136 | { |
| 137 | //We must have a least a SIP_AOR or a Username |
| 138 | TRACE_DEBUG(INFO,"Incorrect Reason-Code"); |
| 139 | return EINVAL; |
| 140 | } |
| 141 | |
| 142 | if(structure.desthost[0]=='\0') |
| 143 | { |
| 144 | //We must have a least a SIP_AOR or a Username |
| 145 | TRACE_DEBUG(INFO,"No Destination_Host was provided!"); |
| 146 | return EINVAL; |
| 147 | } |
| 148 | //Create the base message for an RTR |
| 149 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Registration-Termination-Request", &rtr_model, ENOENT) ); |
| 150 | CHECK_FCT( fd_msg_new (rtr_model, 0, &message)); |
| 151 | |
| 152 | // Create a new session |
| 153 | { |
| 154 | CHECK_FCT( fd_sess_new( &sess, fd_g_config->cnf_diamid, "app_sip", 7 )); |
| 155 | char * sid; |
| 156 | CHECK_FCT( fd_sess_getsid ( sess, &sid )); |
| 157 | CHECK_FCT( fd_msg_avp_new ( sip_dict.Session_Id, 0, &avp )); |
| 158 | value.os.data = (uint8_t *)sid; |
| 159 | value.os.len = strlen(sid); |
| 160 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value )); |
| 161 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_FIRST_CHILD, avp )); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | //Auth_Session_State |
| 166 | { |
| 167 | CHECK_FCT( fd_msg_avp_new ( sip_dict.Auth_Session_State, 0, &avp ) ); |
| 168 | value.i32=1; |
| 169 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 170 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 171 | } |
| 172 | |
| 173 | //Origin_Host & Origin_Realm |
| 174 | CHECK_FCT( fd_msg_add_origin ( message, 0 )); |
| 175 | |
| 176 | //Destination_Host |
| 177 | { |
| 178 | CHECK_FCT( fd_msg_avp_new ( sip_dict.Destination_Host, 0, &avp ) ); |
| 179 | value.os.data=(unsigned char *)structure.desthost; |
| 180 | value.os.len=(size_t)strlen(structure.desthost); |
| 181 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 182 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | //SIP Deregistration Reason (Grouped AVP) |
| 187 | { |
| 188 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Deregistration_Reason, 0, &groupedavp ) ); |
| 189 | |
| 190 | //Reason Code |
| 191 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Reason_Code, 0, &avp ) ); |
| 192 | value.i32=structure.reason; |
| 193 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 194 | CHECK_FCT( fd_msg_avp_add( groupedavp, MSG_BRW_LAST_CHILD, avp ) ); |
| 195 | |
| 196 | if(got_streason) |
| 197 | { |
| 198 | //Reason Info |
| 199 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Reason_Info, 0, &avp ) ); |
| 200 | value.os.data=(unsigned char *)structure.strreason; |
| 201 | value.os.len=(size_t)strlen(structure.strreason); |
| 202 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 203 | CHECK_FCT( fd_msg_avp_add( groupedavp, MSG_BRW_LAST_CHILD, avp ) ); |
| 204 | } |
| 205 | |
| 206 | //We add the grouped AVP to the message |
| 207 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, groupedavp ) ); |
| 208 | } |
| 209 | |
| 210 | //Username |
| 211 | { |
| 212 | if(got_username) |
| 213 | { |
| 214 | CHECK_FCT( fd_msg_avp_new ( sip_dict.User_Name, 0, &avp ) ); |
| 215 | value.os.data=(unsigned char *)structure.username; |
| 216 | value.os.len=(size_t)strlen(structure.username); |
| 217 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 218 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | //SIP_AOR |
| 223 | { |
| 224 | if(num_aor>0) |
| 225 | { |
| 226 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) ); |
| 227 | value.os.data=(unsigned char *)structure.sip_aor1; |
| 228 | value.os.len=(size_t)strlen(structure.sip_aor1); |
| 229 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 230 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 231 | if(num_aor>1) |
| 232 | { |
| 233 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) ); |
| 234 | value.os.data=(unsigned char *)structure.sip_aor2; |
| 235 | value.os.len=(size_t)strlen(structure.sip_aor2); |
| 236 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 237 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 238 | if(num_aor>2) |
| 239 | { |
| 240 | CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) ); |
| 241 | value.os.data=(unsigned char *)structure.sip_aor3; |
| 242 | value.os.len=(size_t)strlen(structure.sip_aor3); |
| 243 | CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) ); |
| 244 | CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) ); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | fd_msg_dump_walk(INFO,message); |
| 251 | CHECK_FCT( fd_msg_send( &message, NULL, NULL )); |
| 252 | */ |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | //Called when an PPA arrive |
| 257 | int app_sip_PPA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, void * opaque, enum disp_action * act) |
| 258 | { |
| 259 | //TODO: PPA reception |
| 260 | /* |
| 261 | //TODO:remove unused variables |
| 262 | struct msg *ans, *qry; |
| 263 | struct avp *avp, *a2, *authdataitem; |
| 264 | struct msg_hdr * header = NULL; |
| 265 | struct avp_hdr * avphdr=NULL, *avpheader=NULL, *avpheader_auth=NULL,*digestheader=NULL; |
| 266 | union avp_value val; |
| 267 | int found_cnonce=0; |
| 268 | struct avp * tempavp=NULL,*sipAuthentication=NULL,*sipAuthenticate=NULL; |
| 269 | char * result; |
| 270 | int idx=0, idx2=0, number_of_auth_items=0,i=0; |
| 271 | //Flags and variables for Database |
| 272 | int sipurinotstored=0, authenticationpending=0, querylen=0, usernamelen=0; |
| 273 | char *query=NULL,*username=NULL; |
| 274 | |
| 275 | |
| 276 | |
| 277 | TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act); |
| 278 | |
| 279 | if (msg == NULL) |
| 280 | return EINVAL; |
| 281 | |
| 282 | |
| 283 | // Create answer header |
| 284 | qry = *msg; |
| 285 | CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) ); |
| 286 | ans = *msg; |
| 287 | |
| 288 | |
| 289 | // Add the Auth-Session-State AVP |
| 290 | { |
| 291 | |
| 292 | CHECK_FCT( fd_msg_search_avp ( qry, sip_dict.Auth_Session_State, &avp) ); |
| 293 | CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) ); |
| 294 | |
| 295 | CHECK_FCT( fd_msg_avp_new ( sip_dict.Auth_Session_State, 0, &avp ) ); |
| 296 | CHECK_FCT( fd_msg_avp_setvalue( avp, avphdr->avp_value ) ); |
| 297 | CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, avp ) ); |
| 298 | } |
| 299 | |
| 300 | CHECK_FCT( fd_msg_search_avp ( qry, sip_dict.SIP_Deregistration_Reason, &avp) ); |
| 301 | CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) ); |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | */ |
| 316 | return 0; |
| 317 | |
| 318 | } |
| 319 | |