Module: sip-router Branch: janakj/kcore Commit: 06e7f4bab16c3f78686282d67ab16fb2065f01e4 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06e7f4ba...
Author: Jan Janak jan@iptel.org Committer: Jan Janak jan@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 +
Hi Jan,
I think you should push the kcore to the master branch, it is easier to access and update by others and prepare some docs to show others how to test the sip-router and kamailio modules.
Thanks, Daniel
On 03/03/2009 05:23 PM, Jan Janak wrote:
Module: sip-router Branch: janakj/kcore Commit: 06e7f4bab16c3f78686282d67ab16fb2065f01e4 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06e7f4ba...
[...]
Daniel,
I am still far from being finished with the kcore library, for now you can just merge the branch janakj/kcore into the master branch (or whatever branch you are working on).
Jan.
On 05-03 20:33, Daniel-Constantin Mierla wrote:
Hi Jan,
I think you should push the kcore to the master branch, it is easier to access and update by others and prepare some docs to show others how to test the sip-router and kamailio modules.
Thanks, Daniel
On 03/03/2009 05:23 PM, Jan Janak wrote:
Module: sip-router Branch: janakj/kcore Commit: 06e7f4bab16c3f78686282d67ab16fb2065f01e4 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06e7f4ba...
[...]
Hi Jan,
On 03/05/2009 08:41 PM, Jan Janak wrote:
Daniel,
I am still far from being finished with the kcore library, for now you can just merge the branch janakj/kcore into the master branch (or whatever branch you are working on).
I know it is not finished, but others may want to add to it and thought this is the best do to. Maybe we should synchronize not to work on same code.
I plan to review the extra features we have in the core for the config file and to merge in the sip-router. There are bits of code to be pushed in kcore.
Cheers, Daniel
Jan.
On 05-03 20:33, Daniel-Constantin Mierla wrote:
Hi Jan,
I think you should push the kcore to the master branch, it is easier to access and update by others and prepare some docs to show others how to test the sip-router and kamailio modules.
Thanks, Daniel
On 03/03/2009 05:23 PM, Jan Janak wrote:
Module: sip-router Branch: janakj/kcore Commit: 06e7f4bab16c3f78686282d67ab16fb2065f01e4 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06e7f4ba...
[...]
On 05-03 20:47, Daniel-Constantin Mierla wrote:
Hi Jan,
On 03/05/2009 08:41 PM, Jan Janak wrote:
Daniel,
I am still far from being finished with the kcore library, for now you can just merge the branch janakj/kcore into the master branch (or whatever branch you are working on).
I know it is not finished, but others may want to add to it and thought this is the best do to. Maybe we should synchronize not to work on same code.
I plan to review the extra features we have in the core for the config file and to merge in the sip-router. There are bits of code to be pushed in kcore.
Yeah, good point. Let me publish my whole repository somehow. I have a big repository with one branch per kamailio module and I did a lot of updates using scripts on all the modules at once. You can base your work on what I have done so far, this could save you some tedious work.
I'll try to publish it tomorrow.
Jan.
On 03/05/2009 09:25 PM, Jan Janak wrote:
On 05-03 20:47, Daniel-Constantin Mierla wrote:
Hi Jan,
On 03/05/2009 08:41 PM, Jan Janak wrote:
Daniel,
I am still far from being finished with the kcore library, for now you can just merge the branch janakj/kcore into the master branch (or whatever branch you are working on).
I know it is not finished, but others may want to add to it and thought this is the best do to. Maybe we should synchronize not to work on same code.
I plan to review the extra features we have in the core for the config file and to merge in the sip-router. There are bits of code to be pushed in kcore.
Yeah, good point. Let me publish my whole repository somehow. I have a big repository with one branch per kamailio module and I did a lot of updates using scripts on all the modules at once. You can base your work on what I have done so far, this could save you some tedious work.
Thanks, take your time, heavy work here is planned for next week.
Cheers, Daniel
I'll try to publish it tomorrow.
Jan.
sr-dev mailing list sr-dev@lists.sip-router.org http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev
On 05-03 20:25, Jan Janak wrote:
On 05-03 20:47, Daniel-Constantin Mierla wrote:
Hi Jan,
On 03/05/2009 08:41 PM, Jan Janak wrote:
Daniel,
I am still far from being finished with the kcore library, for now you can just merge the branch janakj/kcore into the master branch (or whatever branch you are working on).
I know it is not finished, but others may want to add to it and thought this is the best do to. Maybe we should synchronize not to work on same code.
I plan to review the extra features we have in the core for the config file and to merge in the sip-router. There are bits of code to be pushed in kcore.
Yeah, good point. Let me publish my whole repository somehow. I have a big repository with one branch per kamailio module and I did a lot of updates using scripts on all the modules at once. You can base your work on what I have done so far, this could save you some tedious work.
I'll try to publish it tomorrow.
Which reminds me that I didn't really continue publishing the patches on the mailing list because I figured that this would generate too much traffic and people would get lots in that. But I am continuing with my effort to make k. modules compile with the sip-router core.
Instead I'm gonna publish the repository tomorrow and then people can generate patches for modules they are interested in themselves.
Jan.