Module: sip-router
Branch: janakj/kcore
Commit: 06e7f4bab16c3f78686282d67ab16fb2065f01e4
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06e7f4b…
Author: Jan Janak <jan(a)iptel.org>
Committer: Jan Janak <jan(a)iptel.org>
Date: Tue Mar 3 16:22:22 2009 +0100
Adding cmpapi.[ch] from kamailio core.
---
lib/kcore/cmpapi.c | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/kcore/cmpapi.h | 57 ++++++++++++
2 files changed, 297 insertions(+), 0 deletions(-)
diff --git a/lib/kcore/cmpapi.c b/lib/kcore/cmpapi.c
new file mode 100644
index 0000000..8223052
--- /dev/null
+++ b/lib/kcore/cmpapi.c
@@ -0,0 +1,240 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*!
+ * \file
+ * \brief comparison functions
+ */
+
+#include "../../parser/parse_uri.h"
+#include "../../parser/parse_param.h"
+#include "cmpapi.h"
+
+int cmp_str(str *s1, str *s2)
+{
+ int ret = 0;
+ int len = 0;
+ if(s1->len==0 && s2->len==0)
+ return 0;
+ if(s1->len==0)
+ return -1;
+ if(s2->len==0)
+ return 1;
+ len = (s1->len<s2->len)?s1->len:s2->len;
+ ret = strncmp(s1->s, s2->s, len);
+ if(ret==0)
+ {
+ if(s1->len==s2->len)
+ return 0;
+ if(s1->len<s2->len)
+ return -1;
+ return 1;
+ }
+ return ret;
+}
+
+int cmpi_str(str *s1, str *s2)
+{
+ int ret = 0;
+ int len = 0;
+ if(s1->len==0 && s2->len==0)
+ return 0;
+ if(s1->len==0)
+ return -1;
+ if(s2->len==0)
+ return 1;
+ len = (s1->len<s2->len)?s1->len:s2->len;
+ ret = strncasecmp(s1->s, s2->s, len);
+ if(ret==0)
+ {
+ if(s1->len==s2->len)
+ return 0;
+ if(s1->len<s2->len)
+ return -1;
+ return 1;
+ }
+ return ret;
+}
+
+int cmp_hdrname_str(str *s1, str *s2)
+{
+ /* todo: parse hdr name and compare with short/long alternative */
+ return cmpi_str(s1, s2);
+}
+
+int cmp_hdrname_strzn(str *s1, char *s2, size_t n)
+{
+ str s;
+ s.s = s2;
+ s.len = n;
+ return cmpi_str(s1, &s);
+}
+
+int cmp_str_params(str *s1, str *s2)
+{
+ param_t* pl1 = NULL;
+ param_hooks_t phooks1;
+ param_t *pit1=NULL;
+ param_t* pl2 = NULL;
+ param_hooks_t phooks2;
+ param_t *pit2=NULL;
+
+ if (parse_params(s1, CLASS_ANY, &phooks1, &pl1)<0)
+ return -1;
+ if (parse_params(s2, CLASS_ANY, &phooks2, &pl2)<0)
+ return -1;
+ for (pit1 = pl1; pit1; pit1=pit1->next)
+ {
+ for (pit2 = pl2; pit2; pit2=pit2->next)
+ {
+ if (pit1->name.len==pit2->name.len
+ && strncasecmp(pit1->name.s, pit2->name.s, pit2->name.len)==0)
+ {
+ if(pit1->body.len!=pit2->body.len
+ || strncasecmp(pit1->body.s, pit2->body.s,
+ pit2->body.len)!=0)
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Compare SIP URI as per RFC3261, 19.1.4
+ * return:
+ * - 0: match
+ * - >0: no match
+ * - <0: error
+ */
+int cmp_uri(struct sip_uri *uri1, struct sip_uri *uri2)
+{
+ if(uri1->type!=uri2->type)
+ return 1;
+ /* quick check for length */
+ if(uri1->user.len!=uri2->user.len
+ || uri1->host.len!=uri2->host.len
+ || uri1->port.len!=uri2->port.len
+ || uri1->passwd.len!=uri2->passwd.len)
+ return 1;
+ if(cmp_str(&uri1->user, &uri2->user)!=0)
+ return 1;
+ if(cmp_str(&uri1->port, &uri2->port)!=0)
+ return 1;
+ if(cmp_str(&uri1->passwd, &uri2->passwd)!=0)
+ return 1;
+ if(cmpi_str(&uri1->host, &uri2->host)!=0)
+ return 1;
+ /* if no params, we are done */
+ if(uri1->params.len==0 && uri2->params.len==0)
+ return 0;
+ if(uri1->params.len==0)
+ {
+ if(uri2->user_param.len!=0)
+ return 1;
+ if(uri2->ttl.len!=0)
+ return 1;
+ if(uri2->method.len!=0)
+ return 1;
+ if(uri2->maddr.len!=0)
+ return 1;
+ }
+ if(uri2->params.len==0)
+ {
+ if(uri1->user_param.len!=0)
+ return 1;
+ if(uri1->ttl.len!=0)
+ return 1;
+ if(uri1->method.len!=0)
+ return 1;
+ if(uri1->maddr.len!=0)
+ return 1;
+ }
+ return cmp_str_params(&uri1->params, &uri2->params);
+}
+
+/**
+ * return:
+ * - 0: match
+ * - >0: no match
+ * - <0: error
+ */
+int cmp_uri_str(str *s1, str *s2)
+{
+ struct sip_uri uri1;
+ struct sip_uri uri2;
+
+ /* todo: parse uri and compare the parts */
+ if(parse_uri(s1->s, s1->len, &uri1)!=0)
+ return -1;
+ if(parse_uri(s2->s, s2->len, &uri2)!=0)
+ return -1;
+ return cmp_uri(&uri1, &uri2);
+}
+
+/**
+ * Compare SIP AoR
+ * - match user, host and port (if port missing, assume 5060)
+ * return:
+ * - 0: match
+ * - >0: no match
+ * - <0: error
+ */
+int cmp_aor(struct sip_uri *uri1, struct sip_uri *uri2)
+{
+ /* quick check for length */
+ if(uri1->user.len!=uri2->user.len
+ || uri1->host.len!=uri2->host.len)
+ return 1;
+ if(cmp_str(&uri1->user, &uri2->user)!=0)
+ return 1;
+ if(cmp_str(&uri1->port, &uri2->port)!=0)
+ {
+ if(uri1->port.len==0 && uri2->port_no!=5060)
+ return 1;
+ if(uri2->port.len==0 && uri1->port_no!=5060)
+ return 1;
+ }
+ if(cmpi_str(&uri1->host, &uri2->host)!=0)
+ return 1;
+ return 0;
+}
+
+/**
+ * return:
+ * - 0: match
+ * - >0: no match
+ * - <0: error
+ */
+int cmp_aor_str(str *s1, str *s2)
+{
+ struct sip_uri uri1;
+ struct sip_uri uri2;
+
+ /* todo: parse uri and compare the parts */
+ if(parse_uri(s1->s, s1->len, &uri1)!=0)
+ return -1;
+ if(parse_uri(s2->s, s2->len, &uri2)!=0)
+ return -1;
+ return cmp_aor(&uri1, &uri2);
+}
+
diff --git a/lib/kcore/cmpapi.h b/lib/kcore/cmpapi.h
new file mode 100644
index 0000000..89038f8
--- /dev/null
+++ b/lib/kcore/cmpapi.h
@@ -0,0 +1,57 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*!
+ * \file
+ * \brief comparison functions
+ */
+
+#ifndef _CMPAPI_H_
+#define _CMPAPI_H_
+
+#include <string.h>
+#include "../../str.h"
+
+/**
+ * Comparison functions
+ * - return:
+ * - < 0 - less
+ * - ==0 - equal
+ * - > 0 - greater
+ * cmp_str* - for strings does case sensitive comparison
+ * cmpi_str* - for strings does case insensitive comparison
+ *
+ */
+
+#define cmp_strz(a, b) strcmp((a), (b))
+#define cmpi_strz(a, b) strcasecmp((a), (b))
+
+int cmp_str(str *s1, str *s2);
+int cmpi_str(str *s1, str *s2);
+
+int cmp_hdrname_str(str *s1, str *s2);
+int cmp_hdrname_strzn(str *s1, char *s2, size_t n);
+int cmp_uri_str(str *s1, str *s2);
+int cmp_aor_str(str *s1, str *s2);
+
+#endif
+