Mercurial > hg > freeDiameter
comparison extensions/app_sip/tools/rtrsipaor.c @ 432:533188d2e6cc
Corrected bugs on SAR/SAA; added SQL files
author | Alexandre Westfahl <awestfahl@freediameter.net> |
---|---|
date | Wed, 28 Jul 2010 03:14:11 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
431:1bb1bc417efe | 432:533188d2e6cc |
---|---|
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 | |
37 | |
38 // This file is separated from the source code because it is a separate command which will call registration termination function in Diameter-SIP | |
39 #include <sys/types.h> | |
40 #include <sys/socket.h> | |
41 #include <netinet/in.h> | |
42 #include <arpa/inet.h> | |
43 #include <unistd.h> | |
44 typedef int SOCKET; | |
45 typedef struct sockaddr_in SOCKADDR_IN; | |
46 typedef struct sockaddr SOCKADDR; | |
47 #include <stdio.h> | |
48 #include <stdlib.h> | |
49 #include <string.h> | |
50 #define PORT 666 | |
51 #include <errno.h> | |
52 | |
53 struct rtrsipaor | |
54 { | |
55 char username[200]; | |
56 char sip_aor1[200]; | |
57 char sip_aor2[200]; | |
58 char sip_aor3[200]; | |
59 char strreason[200]; | |
60 char desthost[200]; | |
61 int reason; | |
62 }; | |
63 | |
64 int main (int argc, char **argv) | |
65 { | |
66 SOCKET sock; | |
67 SOCKADDR_IN sin; | |
68 struct rtrsipaor rtrsip; | |
69 int numaor=0,i=0; | |
70 | |
71 sock = socket(AF_INET, SOCK_STREAM, 0); | |
72 sin.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
73 sin.sin_family = AF_INET; | |
74 sin.sin_port = htons(PORT); | |
75 | |
76 //We initialize the structure | |
77 rtrsip.username[0]='\0'; | |
78 rtrsip.sip_aor1[0]='\0'; | |
79 rtrsip.sip_aor2[0]='\0'; | |
80 rtrsip.sip_aor3[0]='\0'; | |
81 rtrsip.strreason[0]='\0'; | |
82 rtrsip.desthost[0]='\0'; | |
83 rtrsip.reason=-1; | |
84 | |
85 | |
86 //Start of arguments check | |
87 if(argc<3) | |
88 { | |
89 fprintf(stderr,"Missing arguments! You must at least provide a username.\n"); | |
90 return 1; | |
91 } | |
92 | |
93 | |
94 for (i=1;i<argc;i++) | |
95 { | |
96 //We must check if it is a value or the name | |
97 if(strncmp(argv[i],"-",1)==0) | |
98 { | |
99 if(strcmp(argv[i],"-u")==0) | |
100 { | |
101 //Username | |
102 if(strlen(argv[i+1])<199) | |
103 { | |
104 strcpy(rtrsip.username,argv[i+1]); | |
105 //We must not check the value | |
106 i++; | |
107 } | |
108 else | |
109 { | |
110 fprintf(stderr,"Username is too long!\n"); | |
111 } | |
112 } | |
113 else if(strcmp(argv[i],"-a")==0) | |
114 { | |
115 i++; | |
116 int j=i; | |
117 | |
118 for(j=i;j<argc;j++) | |
119 { | |
120 | |
121 if(strncmp(argv[i],"-",1)!=0) | |
122 { | |
123 if(strlen(argv[i])>199) | |
124 { | |
125 fprintf(stderr,"SIP-AOR is too long!\n"); | |
126 } | |
127 else if(strncmp(argv[i],"sip",3)!=0) | |
128 { | |
129 //Bad format of SIP-AOR | |
130 fprintf(stderr,"A SIP-AOR must start by 'sip:' or 'sips:'. Aborting...\n"); | |
131 return 1; | |
132 } | |
133 else | |
134 { | |
135 if(numaor<3) | |
136 { | |
137 switch(numaor) | |
138 { | |
139 case 0: | |
140 strcpy(rtrsip.sip_aor1,argv[i]); | |
141 break; | |
142 case 1: | |
143 strcpy(rtrsip.sip_aor2,argv[i]); | |
144 break; | |
145 case 2: | |
146 strcpy(rtrsip.sip_aor3,argv[i]); | |
147 break; | |
148 } | |
149 numaor++; | |
150 } | |
151 else | |
152 { | |
153 fprintf(stderr,"You can not provide more than 3 SIP-AOR at the same time!\n"); | |
154 break; | |
155 } | |
156 } | |
157 i=j+1; | |
158 } | |
159 else | |
160 { | |
161 //We have a new argument | |
162 i--; | |
163 break; | |
164 } | |
165 } | |
166 } | |
167 else if(strcmp(argv[i],"-r")==0) | |
168 { | |
169 | |
170 if(strlen(argv[i+1])>199) | |
171 { | |
172 fprintf(stderr,"Deregistration reason is too long!\n"); | |
173 } | |
174 else | |
175 { | |
176 strcpy(rtrsip.strreason,argv[i+1]); | |
177 } | |
178 i++; | |
179 } | |
180 else if(strcmp(argv[i],"-h")==0) | |
181 { | |
182 //Remote SIP Server | |
183 if(strlen(argv[i+1])>199) | |
184 { | |
185 fprintf(stderr,"Host is too long!\n"); | |
186 } | |
187 else if(strlen(argv[i+1])<5) | |
188 { | |
189 fprintf(stderr,"Host is too short!\n"); | |
190 } | |
191 else | |
192 { | |
193 strcpy(rtrsip.desthost,argv[i+1]); | |
194 } | |
195 i++; | |
196 } | |
197 else if(strcmp(argv[i],"-pt")==0) | |
198 { | |
199 //Permanent Termination | |
200 rtrsip.reason=0; | |
201 } | |
202 else if(strcmp(argv[i],"-nssa")==0) | |
203 { | |
204 //New SIP Server Assigned | |
205 rtrsip.reason=1; | |
206 } | |
207 else if(strcmp(argv[i],"-ssc")==0) | |
208 { | |
209 //SIP Server Change | |
210 rtrsip.reason=2; | |
211 } | |
212 else if(strcmp(argv[i],"-rss")==0) | |
213 { | |
214 //Remote SIP Server | |
215 rtrsip.reason=3; | |
216 } | |
217 else | |
218 { | |
219 fprintf(stderr,"Unknown argument: %s\n",argv[i]); | |
220 } | |
221 } | |
222 else | |
223 { | |
224 fprintf(stderr,"Unknown argument: %s\n",argv[i]); | |
225 i++; | |
226 } | |
227 | |
228 } | |
229 | |
230 //If no SIP-AOR provided, we remove all | |
231 if(numaor<1) | |
232 { | |
233 fprintf(stderr,"All SIP-AOR of %s will be deregistrated.\n",rtrsip.username); | |
234 } | |
235 | |
236 //We want a username | |
237 if(strlen(rtrsip.username)==0) | |
238 { | |
239 fprintf(stderr,"You must provide a username!\n"); | |
240 return 1; | |
241 } | |
242 | |
243 if(rtrsip.desthost[0]=='\0') | |
244 { | |
245 fprintf(stderr,"You must provide the hostname of SIP-Server!\n"); | |
246 return 1; | |
247 } | |
248 | |
249 | |
250 | |
251 | |
252 /* | |
253 fprintf(stderr,"*%s*\n",rtrsip.username); | |
254 fprintf(stderr,"*%s*\n",rtrsip.sip_aor1); | |
255 fprintf(stderr,"*%s*\n",rtrsip.sip_aor2); | |
256 fprintf(stderr,"*%s*\n",rtrsip.sip_aor3); | |
257 fprintf(stderr,"*%d*\n",rtrsip.reason); | |
258 fprintf(stderr,"*%s*\n",rtrsip.strreason); | |
259 | |
260 //return 0; | |
261 */ | |
262 | |
263 | |
264 //TODO: check args | |
265 if(!connect(sock, (SOCKADDR*)&sin, sizeof(sin))) | |
266 { | |
267 fprintf(stderr,"Connexion succeed!\n"); | |
268 | |
269 | |
270 if(send(sock, &rtrsip, sizeof(struct rtrsipaor), 0)) | |
271 fprintf(stderr,"sent OK!\n"); | |
272 else | |
273 fprintf(stderr,"not sent\n"); | |
274 | |
275 } | |
276 else | |
277 { | |
278 fprintf(stderr,"Unable to connect\n"); | |
279 } | |
280 | |
281 close(sock); | |
282 | |
283 return 0; | |
284 } | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 |