xjab_base.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xjab_base.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * eXtended JABber module - Jabber connections pool
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <sys/types.h>
00029 #include <sys/ipc.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 
00033 #include "../../mem/mem.h"
00034 #include "../../mem/shm_mem.h"
00035 #include "../../parser/parse_uri.h"
00036 
00037 #include "xjab_base.h"
00038 #include "mdefines.h"
00039 
00040 /**
00041  * get the hash code - based on Andrei's function
00042  * 
00043  */
00044 int xj_get_hash(str *x, str *y)
00045 {
00046    char* p;
00047    register unsigned v;
00048    register unsigned h;
00049 
00050    if(!x && !y)
00051       return 0;
00052    h=0;
00053    if(x)
00054    {
00055       for (p=x->s; p<=(x->s+x->len-4); p+=4)
00056       {
00057          v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
00058          h+=v^(v>>3);
00059       }
00060       v=0;
00061       for (;p<(x->s+x->len); p++)
00062       { 
00063          v<<=8; 
00064          v+=*p;
00065       }
00066       h+=v^(v>>3);
00067    }
00068    if(y)
00069    {
00070       for (p=y->s; p<=(y->s+y->len-4); p+=4)
00071       {
00072          v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
00073          h+=v^(v>>3);
00074       }
00075    
00076       v=0;
00077       for (;p<(y->s+y->len); p++)
00078       { 
00079          v<<=8; 
00080          v+=*p;
00081       }
00082       h+=v^(v>>3);
00083    }
00084    h=((h)+(h>>11))+((h>>13)+(h>>23));
00085    
00086    return (h)?h:1;
00087 }
00088 
00089 /**
00090  * function used to compare two xj_jkey elements
00091  */
00092 int xj_jkey_cmp(void *x, void *y)
00093 {
00094    int n;
00095    xj_jkey a, b;
00096    a = (xj_jkey)x;
00097    b = (xj_jkey)y;
00098    if(a == NULL || a->id == NULL || a->id->s == NULL)
00099        return -1;
00100    if(b == NULL || b->id == NULL || b->id->s == NULL)
00101        return 1;
00102    // LM_DBG("comparing <%.*s> / <%.*s>\n", ((str *)a)->len,
00103    //       ((str *)a)->s, ((str *)b)->len, ((str *)b)->s);
00104    if(a->hash != b->hash)
00105       return (a->hash < b->hash)?-1:1;
00106    
00107    if(a->id->len != b->id->len)
00108       return (a->id->len < b->id->len)?-1:1;
00109    
00110    n=strncmp(a->id->s,b->id->s,a->id->len);
00111    
00112    if(n!=0)
00113       return (n<0)?-1:1;
00114    
00115    return 0;
00116 }
00117 /**
00118  * free the information from a jkey
00119  */
00120 void xj_jkey_free_p(void *p)
00121 {
00122    if(p == NULL)
00123       return;
00124    if(((xj_jkey)p)->id != NULL)
00125    {
00126       if(((xj_jkey)p)->id->s != NULL)
00127          _M_SHM_FREE(((xj_jkey)p)->id->s);
00128       _M_SHM_FREE(((xj_jkey)p)->id);
00129    }
00130    _M_SHM_FREE(p);
00131 }
00132 
00133 /**
00134  * free a pointer to a t_jab_sipmsg structure
00135  * > element where points 'from' MUST be deliberated separated
00136  */
00137 void xj_sipmsg_free(xj_sipmsg jsmsg)
00138 {
00139    if(jsmsg == NULL)
00140       return;
00141    if(jsmsg->to.s != NULL)
00142       _M_SHM_FREE(jsmsg->to.s);
00143    // the key is deallocated when the connection is closed
00144 // if(jsmsg->jkey->id->s != NULL)
00145 //    _M_SHM_FREE(jsmsg->from->id->s);
00146    if(jsmsg->msg.s != NULL)
00147       _M_SHM_FREE(jsmsg->msg.s);
00148    _M_SHM_FREE(jsmsg);
00149 }
00150 
00151 int xj_extract_aor(str* u, int t)
00152 {
00153    struct sip_uri puri;
00154 
00155    if(!u)
00156       return -1;
00157    if (parse_uri(u->s, u->len, &puri) < 0)
00158    {
00159       LM_ERR("failed to parse URI\n");
00160       return -1;
00161    }
00162    
00163    if(t == 1)
00164       u->s = puri.user.s;
00165    u->len = puri.host.s + puri.host.len - u->s;
00166    return 0;
00167 }
00168 
00169 

Generated on Fri May 25 00:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6