util.c

Go to the documentation of this file.
00001 /*
00002  * $Id: util.c 5299 2008-12-04 18:12:33Z henningw $
00003  *
00004  * XMPP Module
00005  * This file is part of Kamailio, a free SIP server.
00006  *
00007  * Copyright (C) 2006 Voice Sistem S.R.L.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * Author: Andreea Spirea
00024  *
00025  */
00026 /*! \file
00027  * \brief Kamailio XMPP module - utilities
00028  *  \ingroup xmpp
00029  */
00030 
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034 
00035 #include "xmpp.h"
00036 #include "../../parser/parse_uri.h"
00037 
00038 /*! \brief decode sip:user*domain1\@domain2 -> user\@domain1 
00039    \note In many kinds of gateway scenarios, the % sign is a common character used
00040       See the MSN XMPP transports for an example.
00041  */
00042 char *decode_uri_sip_xmpp(char *uri)
00043 {
00044    struct sip_uri puri;
00045    static char buf[512];
00046    char *p;
00047 
00048    if (!uri)
00049       return NULL;
00050    if (parse_uri(uri, strlen(uri), &puri) < 0) {
00051       LM_ERR("failed to parse URI\n");
00052       return NULL;
00053    }
00054    strncpy(buf, puri.user.s, sizeof(buf));
00055    buf[puri.user.len] = 0;
00056    
00057    /* replace domain separator */
00058    if ((p = strchr(buf, domain_separator)))
00059       *p = '@';
00060 
00061    return buf;
00062 }
00063 
00064 /*! \brief  encode sip:user\@domain -> user*domain\@xmpp_domain */
00065 char *encode_uri_sip_xmpp(char *uri)
00066 {
00067    struct sip_uri puri;
00068    static char buf[512];
00069 
00070    if (!uri)
00071       return NULL;
00072    if (parse_uri(uri, strlen(uri), &puri) < 0) {
00073       LM_ERR("failed to parse URI\n");
00074       return NULL;
00075    }
00076    snprintf(buf, sizeof(buf), "%.*s%c%.*s@%s",
00077       puri.user.len, puri.user.s,
00078       domain_separator,
00079       puri.host.len, puri.host.s,
00080       xmpp_domain);
00081    return buf;
00082 }
00083 
00084 /*! \brief  decode user*domain1\@domain2 -> sip:user\@domain1 */
00085 char *decode_uri_xmpp_sip(char *jid)
00086 {
00087    static char buf[512];
00088    char *p;
00089 
00090    if (!jid)
00091       return NULL;
00092    snprintf(buf, sizeof(buf), "sip:%s", jid);
00093 
00094    /* strip off resource */
00095    if ((p = strchr(buf, '/')))
00096       *p = 0;
00097    /* strip off domain */
00098    if ((p = strchr(buf, '@')))
00099       *p = 0;
00100    /* replace domain separator */
00101    if ((p = strchr(buf, domain_separator)))
00102       *p = '@';
00103 
00104    return buf;
00105 }
00106 
00107 /*! \brief  encode user\@domain -> sip:user*domain\@gateway_domain */
00108 char *encode_uri_xmpp_sip(char *jid)
00109 {
00110    static char buf[512];
00111    char *p;
00112 
00113    if (!jid)
00114       return NULL;
00115    /* TODO: maybe not modify jid? */
00116    if ((p = strchr(jid, '/')))
00117       *p = 0;
00118    if ((p = strchr(jid, '@')))
00119       *p = domain_separator;
00120    snprintf(buf, sizeof(buf), "sip:%s@%s", jid, gateway_domain);
00121    return buf;
00122 }
00123 
00124 char *extract_domain(char *jid)
00125 {
00126    char *p;
00127    
00128    if ((p = strchr(jid, '/')))
00129       *p = 0;
00130    if ((p = strchr(jid, '@'))) {
00131       *p++ = 0;
00132       return p;
00133    }
00134    return p;
00135 }
00136 
00137 char *random_secret(void)
00138 {
00139    static char secret[41];
00140    int i, r;
00141 
00142         for (i = 0; i < 40; i++) {
00143             r = (int) (36.0 * rand() / RAND_MAX);
00144             secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
00145         }
00146         secret[40] = '\0';
00147 
00148    return secret;
00149 }
00150 
00151 char *db_key(char *secret, char *domain, char *id)
00152 {
00153    char buf[1024];
00154    char *hash;
00155    
00156    snprintf(buf, sizeof(buf), "%s", secret);
00157    hash = shahash(buf);
00158 
00159    snprintf(buf, sizeof(buf), "%s%s", hash, domain);
00160    hash = shahash(buf);
00161 
00162    snprintf(buf, sizeof(buf), "%s%s", hash, id);
00163    hash = shahash(buf);
00164    return hash;
00165 }
00166 

Generated on Thu May 24 22:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6