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 | |
| 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 | #include <errno.h> |
| 51 | |
| 52 | struct rtrsipaor |
| 53 | { |
| 54 | char username[200]; |
| 55 | char sip_aor1[200]; |
| 56 | char sip_aor2[200]; |
| 57 | char sip_aor3[200]; |
| 58 | char strreason[200]; |
| 59 | char desthost[200]; |
| 60 | int reason; |
| 61 | }; |
| 62 | |
| 63 | int main (int argc, char **argv) |
| 64 | { |
| 65 | SOCKET sock; |
| 66 | SOCKADDR_IN sin; |
| 67 | struct rtrsipaor rtrsip; |
| 68 | int numaor=0,i=0; |
| 69 | int port=666; |
| 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 | |
| 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 if(strcmp(argv[i],"-p")==0) |
| 218 | { |
| 219 | |
| 220 | if(sscanf(argv[i+1],"%d", &port)!=1) |
| 221 | { |
| 222 | fprintf(stderr,"Incorrect port number!\n"); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | i++; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | fprintf(stderr,"Unknown argument: %s\n",argv[i]); |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | fprintf(stderr,"Unknown argument: %s\n",argv[i]); |
| 237 | i++; |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | //If no SIP-AOR provided, we remove all |
| 243 | if(numaor<1) |
| 244 | { |
| 245 | fprintf(stderr,"All SIP-AOR of %s will be deregistrated.\n",rtrsip.username); |
| 246 | } |
| 247 | |
| 248 | //We want a username |
| 249 | if(strlen(rtrsip.username)==0) |
| 250 | { |
| 251 | fprintf(stderr,"You must provide a username!\n"); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | if(rtrsip.desthost[0]=='\0') |
| 256 | { |
| 257 | fprintf(stderr,"You must provide the hostname of SIP-Server!\n"); |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | //We set the port number |
| 262 | sin.sin_port = htons(port); |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | fprintf(stderr,"*%s*\n",rtrsip.username); |
| 267 | fprintf(stderr,"*%s*\n",rtrsip.sip_aor1); |
| 268 | fprintf(stderr,"*%s*\n",rtrsip.sip_aor2); |
| 269 | fprintf(stderr,"*%s*\n",rtrsip.sip_aor3); |
| 270 | fprintf(stderr,"*%d*\n",rtrsip.reason); |
| 271 | fprintf(stderr,"*%s*\n",rtrsip.strreason); |
| 272 | |
| 273 | //return 0; |
| 274 | */ |
| 275 | |
| 276 | |
| 277 | //TODO: check args |
| 278 | if(!connect(sock, (SOCKADDR*)&sin, sizeof(sin))) |
| 279 | { |
| 280 | fprintf(stderr,"Connexion succeed!\n"); |
| 281 | |
| 282 | |
| 283 | if(send(sock, &rtrsip, sizeof(struct rtrsipaor), 0)) |
| 284 | fprintf(stderr,"sent OK!\n"); |
| 285 | else |
| 286 | fprintf(stderr,"not sent\n"); |
| 287 | |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | fprintf(stderr,"Unable to connect\n"); |
| 292 | } |
| 293 | |
| 294 | close(sock); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |