utils_func.h

Go to the documentation of this file.
00001 /*
00002  * $Id$
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 Vamanu)
00027  */
00028 
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 #include "../../parser/parse_event.h"
00047 
00048 #define LCONTACT_BUF_SIZE 1024
00049 #define BAD_EVENT_CODE 489
00050 
00051 
00052 #define EVENT_DIALOG_SLA(ev) \
00053         ((ev)->type == EVENT_DIALOG \
00054                 && ((ev)->params.hooks.event_dialog.sla \
00055                         || (ev)->params.hooks.event_dialog.ma))
00056 
00057 
00058 static inline int uandd_to_uri(str user,  str domain, str *out)
00059 {
00060         int size;
00061 
00062         if(out==0)
00063                 return -1;
00064 
00065         size = user.len + domain.len+7;
00066         out->s = (char*)pkg_malloc(size);
00067 
00068         if(out->s == NULL)
00069         {
00070                 LM_ERR("no more memory\n");
00071                 return -1;
00072         }
00073         strcpy(out->s,"sip:");
00074         out->len = 4;
00075         if(user.s!=NULL && user.len>0)
00076         {
00077                 memcpy(out->s+out->len, user.s, user.len);
00078                 out->len += user.len;
00079                 out->s[out->len++] = '@';
00080         }
00081         memcpy(out->s + out->len, domain.s, domain.len);
00082         out->len += domain.len;
00083         out->s[out->len] = '\0';
00084         
00085         return 0;
00086 }
00087 
00088 static inline int ps_fill_local_contact(struct sip_msg* msg, str *contact)
00089 {
00090         str ip;
00091         char* proto;
00092         int port;
00093         int len;
00094         int plen;
00095 
00096         contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE);
00097         if(contact->s== NULL)
00098         {
00099                 LM_ERR("No more memory\n");
00100                 goto error;
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                 goto error;
00124         }       
00125         
00126         ip.s= ip_addr2a(&msg->rcv.dst_ip);
00127         if(ip.s== NULL)
00128         {
00129                 LM_ERR("transforming ip_addr to ascii\n");
00130                 goto error;
00131         }
00132         ip.len= strlen(ip.s);
00133         port = msg->rcv.dst_port;
00134 
00135         if(strncmp(ip.s, "sip:", 4)!=0)
00136         {
00137                 strncpy(contact->s, "sip:", 4);
00138                 contact->len+= 4;
00139         }       
00140         strncpy(contact->s+contact->len, ip.s, ip.len);
00141         contact->len += ip.len;
00142         if(contact->len> LCONTACT_BUF_SIZE - 21)
00143         {
00144                 LM_ERR("buffer overflow\n");
00145                 goto error;
00146 
00147         }       
00148         len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
00149         if(len< 0)
00150         {
00151                 LM_ERR("unsuccessful sprintf\n");
00152                 goto error;
00153         }       
00154         contact->len+= len;
00155         strncpy(contact->s+ contact->len, proto, plen);
00156         contact->len += plen;
00157         
00158         return 0;
00159 error:
00160         if(contact->s!=NULL)
00161                 pkg_free(contact->s);
00162         contact->s = 0;
00163         contact->len = 0;
00164         return -1;
00165 }
00166 
00167 //str* int_to_str(long int n);
00168 
00169 int a_to_i (char *s,int len);
00170 
00171 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
00172 
00173 int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str);
00174 
00175 #endif
00176