registrar/common.c

Go to the documentation of this file.
00001 /*
00002  * $Id: common.c 4892 2008-09-11 17:44:28Z henningw $
00003  *
00004  * Common stuff
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  * History
00025  * ------
00026  * 2003-02-14 un-escaping added (janakj)
00027  * 2006-09-19 AOR may be provided via an AVP instead of being fetched
00028  *            from URI (bogdan)
00029 */
00030 
00031 /*!
00032  * \file
00033  * \brief SIP registrar module
00034  * \ingroup registrar
00035  */
00036 
00037 #include <string.h> 
00038 #include "../../dprint.h"
00039 #include "../../parser/parse_uri.h"
00040 #include "rerrno.h"
00041 #include "reg_mod.h"
00042 #include "common.h"
00043 
00044 
00045 #define MAX_AOR_LEN 256
00046 
00047 /*! \brief
00048  * Extract Address of Record
00049  */
00050 int extract_aor(str* _uri, str* _a)
00051 {
00052    static char aor_buf[MAX_AOR_LEN];
00053    memset(aor_buf, 0, MAX_AOR_LEN);
00054 
00055    str tmp;
00056    struct sip_uri puri;
00057    int user_len;
00058    int_str avp_val;
00059    struct usr_avp *avp;
00060    str *uri;
00061 
00062    if (aor_avp_name.n!=0) {
00063       avp = search_first_avp( aor_avp_type, aor_avp_name, &avp_val, 0);
00064       if (avp && is_avp_str_val(avp)) {
00065          uri = &avp_val.s;
00066       } else {
00067          uri = _uri;
00068       }
00069    } else {
00070       uri=_uri;
00071    }
00072 
00073    if (parse_uri(uri->s, uri->len, &puri) < 0) {
00074       rerrno = R_AOR_PARSE;
00075       LM_ERR("failed to parse Address of Record\n");
00076       return -1;
00077    }
00078    
00079    if ( (puri.user.len + puri.host.len + 1) > MAX_AOR_LEN
00080    || puri.user.len > USERNAME_MAX_SIZE
00081    ||  puri.host.len > DOMAIN_MAX_SIZE ) {
00082       rerrno = R_AOR_LEN;
00083       LM_ERR("Address Of Record too long\n");
00084       return -2;
00085    }
00086 
00087    _a->s = aor_buf;
00088    _a->len = puri.user.len;
00089 
00090    if (un_escape(&puri.user, _a) < 0) {
00091       rerrno = R_UNESCAPE;
00092       LM_ERR("failed to unescape username\n");
00093       return -3;
00094    }
00095 
00096    user_len = _a->len;
00097 
00098    if (reg_use_domain) {
00099       if (user_len)
00100          aor_buf[_a->len++] = '@';
00101       /* strip prefix (if defined) */
00102       if (realm_prefix.len && realm_prefix.len<puri.host.len &&
00103       (memcmp(realm_prefix.s, puri.host.s, realm_prefix.len)==0) ) {
00104          memcpy(aor_buf + _a->len, puri.host.s + realm_prefix.len,
00105                puri.host.len - realm_prefix.len);
00106          _a->len += puri.host.len - realm_prefix.len;
00107       } else {
00108          memcpy(aor_buf + _a->len, puri.host.s, puri.host.len);
00109          _a->len += puri.host.len;
00110       }
00111    }
00112 
00113    if (case_sensitive && user_len) {
00114       tmp.s = _a->s + user_len + 1;
00115       tmp.len = _a->s + _a->len - tmp.s;
00116       strlower(&tmp);
00117    } else {
00118       strlower(_a);
00119    }
00120 
00121    return 0;
00122 }

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