1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/* $Id$
*
* Copyright (C) 2006-2007 VozTelecom Sistemas S.L
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* =====================================================================================
*
* Filename: utils.c
*
* Description:
*
* Version: 1.0
* Created: 19/01/06 15:50:33 CET
* Revision: none
* Compiler: gcc
*
* Author: Elias Baixas (EB), elias@conillera.net
* Company: VozTele.com
*
* =====================================================================================
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include "../../parser/msg_parser.h"
#include "../../parser/parse_via.h"
#include "../../parser/parse_uri.h"
#include "../../parser/parse_from.h"
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "encode_header.h"
#include "encode_msg.h"
#include "utils.h"
#define MAX_ERROR 32
static inline int memstr(char *haystack,int hlen,char *needle,int nlen);
int buffered_printer(FILE* infd)
{
int i,k=0,retval;
char *missatge=0,*myerror="";
struct sip_msg msg;
static char mybuffer[1400];
static int end=0,last=0;
while((i=fread(&mybuffer[last],1,1400-last,infd))==1400-last){
if((end=memstr(mybuffer,last+i,"\n\n\n",3))<0){
last+=i;
return 0;
}else{
end+=3;
while(end<1400 && (mybuffer[end]=='\n' || mybuffer[end]=='.' || mybuffer[end]=='\r'))
end++;
if((missatge=pkg_malloc(end))==0){
myerror="Out of memory !!\n";
goto error;
}
memset(missatge,0,end);
memcpy(missatge,mybuffer,end);
memset(&msg,0,sizeof(struct sip_msg));
msg.buf=missatge;
msg.len=end;
if(!parse_msg(msg.buf,msg.len,&msg))
print_msg_info(stdout,&msg);
printf("PARSED:%d,last=%d,end=%d\n",k++,last,end);
free_sip_msg(&msg);
pkg_free(missatge);
memmove(mybuffer,&mybuffer[end],1400-end);
last=1400-end;
}
}
retval=0;
goto exit;
error:
printf("Error on %s",myerror);
retval=1;
exit:
if(missatge)
pkg_free(missatge);
return retval;
}
int coded_buffered_printer(FILE* infd)
{
int i,lastlast;
char spaces[50];
static char mybuffer[1500];
static int size=0,last=0;
memcpy(spaces," ",2);
do{
lastlast=1500-last;
i=fread(&mybuffer[last],1,lastlast,infd);
printf("read i=%d\n",i);
if(i==0)
break;
if(size==0){
size=GET_PAY_SIZE(mybuffer);
printf("size=%d\n",size);
last+=i;
}
if(last>=size){
printf("should print message: last=%d, size=%d\n",last,size);
if(print_encoded_msg(stdout,mybuffer,spaces)<0){
printf("Unable to print encoded msg\n");
return -1;
}
if(last>size){
memmove(mybuffer,&mybuffer[size],last-size);
last=last-size;
}else
last=0;
size=0;
}
}while(i>0 && i==lastlast);
if(i==0)
return 0;
else
return 1;
}
int print_msg_info(FILE* fd,struct sip_msg* msg)
{
char *payload=0;
char *prefix=0;
int retval=-1;
if((prefix=pkg_malloc(500))==0){
printf("OUT OF MEMORY !!!\n");
return -1;
}
memset(prefix,0,500);
strcpy(prefix," ");
if(parse_headers(msg,HDR_EOH_F,0)<0)
goto error;
if(!(payload=pkg_malloc(MAX_ENCODED_MSG + MAX_MESSAGE_LEN)))
goto error;
if(encode_msg(msg,payload,MAX_ENCODED_MSG + MAX_MESSAGE_LEN)<0){
printf("Unable to encode msg\n");
goto error;
}
if(print_encoded_msg(fd,payload,prefix)<0){
printf("Unable to print encoded msg\n");
pkg_free(payload);
goto error;
}
pkg_free(payload);
retval =0;
error:
if(prefix)
pkg_free(prefix);
return retval;
}
static inline int memstr(char *haystack,int hlen,char *needle,int nlen)
{
int i=0;
if(nlen>hlen)
return -1;
while(i<=(hlen-nlen) && (haystack[i]!=needle[0] || memcmp(&haystack[i],needle,nlen)))
i++;
if(i>(hlen-nlen))
return -1;
else
return i;
}
|