cmpapi.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00028 #include "../../parser/parse_uri.h"
00029 #include "../../parser/parse_param.h"
00030 #include "cmpapi.h"
00031 
00032 int cmp_str(str *s1, str *s2)
00033 {
00034         int ret = 0;
00035         int len = 0;
00036         if(s1->len==0 && s2->len==0)
00037                 return 0;
00038         if(s1->len==0)
00039                 return -1;
00040         if(s2->len==0)
00041                 return 1;
00042         len = (s1->len<s2->len)?s1->len:s2->len;
00043         ret = strncmp(s1->s, s2->s, len);
00044         if(ret==0)
00045         {
00046                 if(s1->len==s2->len)
00047                         return 0;
00048                 if(s1->len<s2->len)
00049                         return -1;
00050                 return 1;
00051         }
00052         return ret;
00053 }
00054 
00055 int cmpi_str(str *s1, str *s2)
00056 {
00057         int ret = 0;
00058         int len = 0;
00059         if(s1->len==0 && s2->len==0)
00060                 return 0;
00061         if(s1->len==0)
00062                 return -1;
00063         if(s2->len==0)
00064                 return 1;
00065         len = (s1->len<s2->len)?s1->len:s2->len;
00066         ret = strncasecmp(s1->s, s2->s, len);
00067         if(ret==0)
00068         {
00069                 if(s1->len==s2->len)
00070                         return 0;
00071                 if(s1->len<s2->len)
00072                         return -1;
00073                 return 1;
00074         }
00075         return ret;
00076 }
00077 
00078 int cmp_hdrname_str(str *s1, str *s2)
00079 {
00080         /* todo: parse hdr name and compare with short/long alternative */
00081         return cmpi_str(s1, s2);
00082 }
00083 
00084 int cmp_hdrname_strzn(str *s1, char *s2, size_t n)
00085 {
00086         str s;
00087         s.s = s2;
00088         s.len = n;
00089         return cmpi_str(s1, &s);
00090 }
00091 
00092 int cmp_str_params(str *s1, str *s2)
00093 {
00094         param_t* pl1 = NULL;
00095         param_hooks_t phooks1;
00096         param_t *pit1=NULL;
00097         param_t* pl2 = NULL;
00098         param_hooks_t phooks2;
00099         param_t *pit2=NULL;
00100         
00101         if (parse_params(s1, CLASS_ANY, &phooks1, &pl1)<0)
00102                 return -1;
00103         if (parse_params(s2, CLASS_ANY, &phooks2, &pl2)<0)
00104                 return -1;
00105         for (pit1 = pl1; pit1; pit1=pit1->next)
00106         {
00107                 for (pit2 = pl2; pit2; pit2=pit2->next)
00108                 {
00109                         if (pit1->name.len==pit2->name.len
00110                                 && strncasecmp(pit1->name.s, pit2->name.s, pit2->name.len)==0)
00111                         {
00112                                 if(pit1->body.len!=pit2->body.len
00113                                                 || strncasecmp(pit1->body.s, pit2->body.s,
00114                                                         pit2->body.len)!=0)
00115                                         return 1;
00116                         }
00117                 }
00118         }
00119         return 0;
00120 }
00121 
00129 int cmp_uri(struct sip_uri *uri1, struct sip_uri *uri2)
00130 {
00131         if(uri1->type!=uri2->type)
00132                 return 1;
00133         /* quick check for length */
00134         if(uri1->user.len!=uri2->user.len
00135                         || uri1->host.len!=uri2->host.len
00136                         || uri1->port.len!=uri2->port.len
00137                         || uri1->passwd.len!=uri2->passwd.len)
00138                 return 1;
00139         if(cmp_str(&uri1->user, &uri2->user)!=0)
00140                 return 1;
00141         if(cmp_str(&uri1->port, &uri2->port)!=0)
00142                 return 1;
00143         if(cmp_str(&uri1->passwd, &uri2->passwd)!=0)
00144                 return 1;
00145         if(cmpi_str(&uri1->host, &uri2->host)!=0)
00146                 return 1;
00147         /* if no params, we are done */
00148         if(uri1->params.len==0 && uri2->params.len==0)
00149                 return 0;
00150         if(uri1->params.len==0)
00151         {
00152                 if(uri2->user_param.len!=0)
00153                         return 1;
00154                 if(uri2->ttl.len!=0)
00155                         return 1;
00156                 if(uri2->method.len!=0)
00157                         return 1;
00158                 if(uri2->maddr.len!=0)
00159                         return 1;
00160         }
00161         if(uri2->params.len==0)
00162         {
00163                 if(uri1->user_param.len!=0)
00164                         return 1;
00165                 if(uri1->ttl.len!=0)
00166                         return 1;
00167                 if(uri1->method.len!=0)
00168                         return 1;
00169                 if(uri1->maddr.len!=0)
00170                         return 1;
00171         }
00172         return cmp_str_params(&uri1->params, &uri2->params);
00173 }
00174 
00181 int cmp_uri_str(str *s1, str *s2)
00182 {
00183         struct sip_uri uri1;
00184         struct sip_uri uri2;
00185 
00186         /* todo: parse uri and compare the parts */
00187         if(parse_uri(s1->s, s1->len, &uri1)!=0)
00188                 return -1;
00189         if(parse_uri(s2->s, s2->len, &uri2)!=0)
00190                 return -1;
00191         return cmp_uri(&uri1, &uri2);
00192 }
00193 
00202 int cmp_aor(struct sip_uri *uri1, struct sip_uri *uri2)
00203 {
00204         /* quick check for length */
00205         if(uri1->user.len!=uri2->user.len
00206                         || uri1->host.len!=uri2->host.len)
00207                 return 1;
00208         if(cmp_str(&uri1->user, &uri2->user)!=0)
00209                 return 1;
00210         if(cmp_str(&uri1->port, &uri2->port)!=0)
00211         {
00212                 if(uri1->port.len==0 && uri2->port_no!=5060)
00213                         return 1;
00214                 if(uri2->port.len==0 && uri1->port_no!=5060)
00215                         return 1;
00216         }
00217         if(cmpi_str(&uri1->host, &uri2->host)!=0)
00218                 return 1;
00219         return 0;
00220 }
00221 
00228 int cmp_aor_str(str *s1, str *s2)
00229 {
00230         struct sip_uri uri1;
00231         struct sip_uri uri2;
00232 
00233         /* todo: parse uri and compare the parts */
00234         if(parse_uri(s1->s, s1->len, &uri1)!=0)
00235                 return -1;
00236         if(parse_uri(s2->s, s2->len, &uri2)!=0)
00237                 return -1;
00238         return cmp_aor(&uri1, &uri2);
00239 }
00240