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