utils_func.h

Go to the documentation of this file.
00001 /*
00002  * $Id: utils_func.h 5674 2009-03-08 18:35:20Z miconda $
00003  *
00004  * presence module - presence server implementation
00005  *
00006  * Copyright (C) 2006 Voice Sistem S.R.L.
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  *  2006-08-15  initial version (anca)
00027  */
00028 
00029 /*! \file
00030  * \brief Kamailio presence module :: Utility functions
00031  * \ref utils_func.c
00032  * \ingroup presence 
00033  */
00034 
00035 
00036 
00037 #ifndef UTILS_FUNC_H
00038 #define UTILS_FUNC_H
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include "../../mem/mem.h"
00043 #include "../../dprint.h"
00044 #include "../../str.h"
00045 #include "../../parser/msg_parser.h"
00046 
00047 #define LCONTACT_BUF_SIZE 1024
00048 #define BAD_EVENT_CODE 489
00049 
00050 static inline int uandd_to_uri(str user,  str domain, str *out)
00051 {
00052    int size;
00053 
00054    if(out==0)
00055       return -1;
00056 
00057    size = user.len + domain.len+7;
00058    out->s = (char*)pkg_malloc(size);
00059 
00060    if(out->s == NULL)
00061    {
00062       LM_ERR("no more memory\n");
00063       return -1;
00064    }
00065    strcpy(out->s,"sip:");
00066    out->len = 4;
00067    if(user.s!=NULL && user.len>0)
00068    {
00069       memcpy(out->s+out->len, user.s, user.len);
00070       out->len += user.len;
00071       out->s[out->len++] = '@';
00072    }
00073    memcpy(out->s + out->len, domain.s, domain.len);
00074    out->len += domain.len;
00075    out->s[out->len] = '\0';
00076    
00077    return 0;
00078 }
00079 
00080 static inline str* get_local_contact(struct sip_msg* msg)
00081 {
00082    str ip;
00083    char* proto;
00084    int port;
00085    int len;
00086    str* contact;
00087    int plen;
00088 
00089    contact= (str*)pkg_malloc(sizeof(str));
00090    if(contact== NULL)
00091    {
00092       LM_ERR("No more memory\n");
00093       return NULL;
00094    }
00095    contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE);
00096    if(contact->s== NULL)
00097    {
00098       LM_ERR("No more memory\n");
00099       pkg_free(contact);
00100       return NULL;
00101    }
00102 
00103    memset(contact->s, 0, LCONTACT_BUF_SIZE);
00104    contact->len= 0;
00105    
00106    plen = 3;
00107    if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
00108       proto= "udp";
00109    else
00110    if(msg->rcv.proto== PROTO_TLS )
00111          proto= "tls";
00112    else  
00113    if(msg->rcv.proto== PROTO_TCP)
00114       proto= "tcp";
00115    else  
00116    if(msg->rcv.proto== PROTO_SCTP) {
00117       proto= "sctp";
00118       plen = 4;
00119    }
00120    else
00121    {
00122       LM_ERR("unsupported proto\n");
00123       pkg_free(contact->s);
00124       pkg_free(contact);
00125       return NULL;
00126    }  
00127    
00128    ip.s= ip_addr2a(&msg->rcv.dst_ip);
00129    if(ip.s== NULL)
00130    {
00131       LM_ERR("transforming ip_addr to ascii\n");
00132       pkg_free(contact->s);
00133       pkg_free(contact);
00134       return NULL;
00135    }
00136    ip.len= strlen(ip.s);
00137    port = msg->rcv.dst_port;
00138 
00139    if(strncmp(ip.s, "sip:", 4)!=0)
00140    {
00141       strncpy(contact->s, "sip:", 4);
00142       contact->len+= 4;
00143    }  
00144    strncpy(contact->s+contact->len, ip.s, ip.len);
00145    contact->len += ip.len;
00146    if(contact->len> LCONTACT_BUF_SIZE - 21)
00147    {
00148       LM_ERR("buffer overflow\n");
00149       pkg_free(contact->s);
00150       pkg_free(contact);
00151       return NULL;
00152 
00153    }  
00154    len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
00155    if(len< 0)
00156    {
00157       LM_ERR("unsuccessful sprintf\n");
00158       pkg_free(contact->s);
00159       pkg_free(contact);
00160       return NULL;
00161    }  
00162    contact->len+= len;
00163    strncpy(contact->s+ contact->len, proto, plen);
00164    contact->len += plen;
00165    
00166    return contact;
00167    
00168 }
00169 
00170 //str* int_to_str(long int n);
00171 
00172 int a_to_i (char *s,int len);
00173 
00174 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
00175 
00176 int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str);
00177 
00178 #endif
00179 

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