modules_k/registrar/common.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
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 
00037 #include <string.h> 
00038 #include "../../dprint.h"
00039 #include "rerrno.h"
00040 #include "reg_mod.h"
00041 #include "common.h"
00042 #include "config.h"
00043 
00044 #define MAX_AOR_LEN 256
00045 
00049 int extract_aor(str* _uri, str* _a, sip_uri_t *_pu)
00050 {
00051         static char aor_buf[MAX_AOR_LEN];
00052         str tmp;
00053         sip_uri_t turi;
00054         sip_uri_t *puri;
00055         int user_len;
00056         str *uri;
00057         str realm_prefix;
00058         
00059         memset(aor_buf, 0, MAX_AOR_LEN);
00060         uri=_uri;
00061 
00062         if(_pu!=NULL)
00063                 puri = _pu;
00064         else
00065                 puri = &turi;
00066 
00067         if (parse_uri(uri->s, uri->len, puri) < 0) {
00068                 rerrno = R_AOR_PARSE;
00069                 LM_ERR("failed to parse Address of Record\n");
00070                 return -1;
00071         }
00072         
00073         if ( (puri->user.len + puri->host.len + 1) > MAX_AOR_LEN
00074         || puri->user.len > USERNAME_MAX_SIZE
00075         ||  puri->host.len > DOMAIN_MAX_SIZE ) {
00076                 rerrno = R_AOR_LEN;
00077                 LM_ERR("Address Of Record too long\n");
00078                 return -2;
00079         }
00080 
00081         _a->s = aor_buf;
00082         _a->len = puri->user.len;
00083 
00084         if (un_escape(&puri->user, _a) < 0) {
00085                 rerrno = R_UNESCAPE;
00086                 LM_ERR("failed to unescape username\n");
00087                 return -3;
00088         }
00089 
00090         user_len = _a->len;
00091 
00092         if (reg_use_domain) {
00093                 if (user_len)
00094                         aor_buf[_a->len++] = '@';
00095                 /* strip prefix (if defined) */
00096                 realm_prefix.s = cfg_get(registrar, registrar_cfg, realm_pref).s;
00097                 realm_prefix.len = cfg_get(registrar, registrar_cfg, realm_pref).len;
00098                 if (realm_prefix.len && realm_prefix.len<puri->host.len &&
00099                 (memcmp(realm_prefix.s, puri->host.s, realm_prefix.len)==0) ) {
00100                         memcpy(aor_buf + _a->len, puri->host.s + realm_prefix.len,
00101                                         puri->host.len - realm_prefix.len);
00102                         _a->len += puri->host.len - realm_prefix.len;
00103                 } else {
00104                         memcpy(aor_buf + _a->len, puri->host.s, puri->host.len);
00105                         _a->len += puri->host.len;
00106                 }
00107         }
00108 
00109         if (cfg_get(registrar, registrar_cfg, case_sensitive) && user_len) {
00110                 tmp.s = _a->s + user_len + 1;
00111                 tmp.len = _a->s + _a->len - tmp.s;
00112                 strlower(&tmp);
00113         } else {
00114                 strlower(_a);
00115         }
00116 
00117         return 0;
00118 }