utils_func.c

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 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <ctype.h>
00039 #include "../../mem/mem.h"
00040 #include "../../dprint.h"
00041 #include "../../ut.h"
00042 #include "../../data_lump_rpl.h"
00043 #include "utils_func.h"
00044 #include "event_list.h"
00045 #include "presence.h"
00046 
00047 
00048 static const char base64digits[] =
00049 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00050 
00051 #define BAD     -1
00052 static const char base64val[] = {
00053 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
00054 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
00055 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
00056 52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
00057 BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
00058 15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
00059 BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
00060 41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
00061 };
00062 #define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
00063 
00064 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
00065 {
00066         for (; inlen >= 3; inlen -= 3)
00067         {
00068                 *out++ = base64digits[in[0] >> 2];
00069                 *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
00070                 *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
00071                 *out++ = base64digits[in[2] & 0x3f];
00072                 in += 3;
00073         }
00074 
00075         if (inlen > 0)
00076         {
00077                 unsigned char fragment;
00078 
00079                 *out++ = base64digits[in[0] >> 2];
00080                 fragment = (in[0] << 4) & 0x30;
00081 
00082                 if (inlen > 1)
00083                         fragment |= in[1] >> 4;
00084 
00085                 *out++ = base64digits[fragment];
00086                 *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
00087                 *out++ = '=';
00088         }
00089                 *out = '\0';
00090                 
00091 }
00092 
00093 int a_to_i (char *s,int len)
00094 {
00095         int n = 0, i= 0;
00096         
00097         while( i<len  )         
00098                 n=n*10+( s[i++] -'0');
00099         
00100         return n;
00101 }
00102 
00103 int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str)
00104 {
00105         if(reply_code== BAD_EVENT_CODE)
00106         {
00107                 str hdr_append;
00108                 char buffer[256];
00109                 int i;
00110                 pres_ev_t* ev= EvList->events;
00111 
00112                 hdr_append.s = buffer;
00113                 hdr_append.s[0]='\0';
00114                 hdr_append.len = sprintf(hdr_append.s, "Allow-Events: ");
00115                 if(hdr_append.len < 0)
00116                 {
00117                         LM_ERR("unsuccessful sprintf\n");
00118                         return -1;
00119                 }
00120 
00121                 for(i= 0; i< EvList->ev_count; i++)
00122                 {
00123                         if(i> 0)
00124                         {
00125                                 memcpy(hdr_append.s+ hdr_append.len, ", ", 2);
00126                                 hdr_append.len+= 2;
00127                         }       
00128                         memcpy(hdr_append.s+ hdr_append.len, ev->name.s, ev->name.len );
00129                         hdr_append.len+= ev->name.len ;
00130                         ev= ev->next;
00131                 }
00132                 memcpy(hdr_append.s+ hdr_append.len, CRLF, CRLF_LEN);
00133                 hdr_append.len+=  CRLF_LEN;
00134                 hdr_append.s[hdr_append.len]= '\0';
00135                 
00136                 if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
00137                 {
00138                         LM_ERR("unable to add lump_rl\n");
00139                         return -1;
00140                 }
00141         }
00142 
00143         if (slb.freply(msg, reply_code, &reply_str) < 0)
00144         {
00145                 LM_ERR("sending %d %.*s reply\n", reply_code, reply_str.len,
00146                                 reply_str.s);
00147                 return -1;
00148         }
00149         return 0;
00150 
00151 }
00152