seas/utils.c

Go to the documentation of this file.
00001 /* $Id: utils.c 5192 2008-11-13 10:18:48Z eliasbaixas $
00002  *
00003  * Copyright (C) 2006-2007 VozTelecom Sistemas S.L
00004  *
00005  * This file is part of Kamailio, a free SIP server.
00006  *
00007  * Kamailio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version
00011  *
00012  * Kamailio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License 
00018  * along with this program; if not, write to the Free Software 
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 /*
00023  * =====================================================================================
00024  * 
00025  *        Filename:  utils.c
00026  * 
00027  *     Description:  
00028  * 
00029  *         Version:  1.0
00030  *         Created:  19/01/06 15:50:33 CET
00031  *        Revision:  none
00032  *        Compiler:  gcc
00033  * 
00034  *          Author:  Elias Baixas (EB), elias@conillera.net
00035  *         Company:  VozTele.com
00036  * 
00037  * =====================================================================================
00038  */
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041 #include <fcntl.h>
00042 #include <unistd.h>
00043 #include <stdlib.h>
00044 #define _GNU_SOURCE
00045 #include <stdio.h>
00046 #include <syslog.h>
00047 #include <string.h>
00048 
00049 #include "../../parser/msg_parser.h"
00050 #include "../../parser/parse_via.h"
00051 #include "../../parser/parse_uri.h"
00052 #include "../../parser/parse_from.h"
00053 #include "../../mem/mem.h"
00054 #include "../../dprint.h"
00055 #include "encode_header.h"
00056 #include "encode_msg.h"
00057 #include "utils.h"
00058 
00059 #define MAX_ERROR 32
00060 
00061 static inline int memstr(char *haystack,int hlen,char *needle,int nlen);
00062 
00063 int buffered_printer(FILE* infd)
00064 {
00065    int i,k=0,retval;
00066    char *missatge=0,*myerror="";
00067    struct sip_msg msg;
00068    static char mybuffer[1400];
00069    static int end=0,last=0;
00070 
00071    while((i=fread(&mybuffer[last],1,1400-last,infd))==1400-last){
00072       if((end=memstr(mybuffer,last+i,"\n\n\n",3))<0){
00073     last+=i;
00074     return 0;
00075       }else{
00076     end+=3;
00077     while(end<1400 && (mybuffer[end]=='\n' || mybuffer[end]=='.' || mybuffer[end]=='\r'))
00078        end++;
00079     if((missatge=pkg_malloc(end))==0){
00080        myerror="Out of memory !!\n";
00081        goto error;
00082     }
00083     memset(missatge,0,end);
00084     memcpy(missatge,mybuffer,end);
00085     memset(&msg,0,sizeof(struct sip_msg));
00086     msg.buf=missatge;
00087     msg.len=end;
00088     if(!parse_msg(msg.buf,msg.len,&msg))
00089        print_msg_info(stdout,&msg);
00090     printf("PARSED:%d,last=%d,end=%d\n",k++,last,end);
00091     free_sip_msg(&msg);
00092     pkg_free(missatge);
00093     memmove(mybuffer,&mybuffer[end],1400-end);
00094     last=1400-end;
00095       }
00096    }
00097    retval=0;
00098    goto exit;
00099 error:
00100    printf("Error on %s",myerror);
00101    retval=1;
00102 exit:
00103    if(missatge)
00104       pkg_free(missatge);
00105    return retval;
00106 }
00107 
00108 int coded_buffered_printer(FILE* infd)
00109 {
00110    int i,lastlast;
00111    char spaces[50];
00112    static char mybuffer[1500];
00113    static int size=0,last=0;
00114 
00115    memcpy(spaces," ",2);
00116 
00117    do{
00118       lastlast=1500-last;
00119       i=fread(&mybuffer[last],1,lastlast,infd);
00120       printf("read i=%d\n",i);
00121       if(i==0)
00122     break;
00123       if(size==0){
00124     size=GET_PAY_SIZE(mybuffer);
00125     printf("size=%d\n",size);
00126     last+=i;
00127       }
00128       if(last>=size){
00129     printf("should print message: last=%d, size=%d\n",last,size);
00130     if(print_encoded_msg(stdout,mybuffer,spaces)<0){
00131        printf("Unable to print encoded msg\n");
00132        return -1;
00133     }
00134     if(last>size){
00135        memmove(mybuffer,&mybuffer[size],last-size);
00136        last=last-size;
00137     }else
00138        last=0;
00139     size=0;
00140       }
00141    }while(i>0 && i==lastlast);
00142 
00143    if(i==0)
00144       return 0;
00145    else
00146       return 1;
00147 }
00148 
00149 int print_msg_info(FILE* fd,struct sip_msg* msg)
00150 {
00151    char *payload=0;
00152    char *prefix=0;
00153    int retval=-1;
00154    if((prefix=pkg_malloc(500))==0){
00155       printf("OUT OF MEMORY !!!\n");
00156       return -1;
00157    }
00158    memset(prefix,0,500);
00159    strcpy(prefix,"  ");
00160 
00161    if(parse_headers(msg,HDR_EOH_F,0)<0)
00162       goto error;
00163    if(!(payload=pkg_malloc(MAX_ENCODED_MSG + MAX_MESSAGE_LEN)))
00164       goto error;
00165    if(encode_msg(msg,payload,MAX_ENCODED_MSG + MAX_MESSAGE_LEN)<0){
00166       printf("Unable to encode msg\n");
00167       goto error;
00168    }
00169    if(print_encoded_msg(fd,payload,prefix)<0){
00170       printf("Unable to print encoded msg\n");
00171       pkg_free(payload);
00172       goto error;
00173    }
00174    pkg_free(payload);
00175    retval =0;
00176 error:
00177    if(prefix)
00178       pkg_free(prefix);
00179    return retval;
00180 }
00181 
00182 static inline int memstr(char *haystack,int hlen,char *needle,int nlen)
00183 {
00184    int i=0;
00185 
00186    if(nlen>hlen)
00187       return -1;
00188    while(i<=(hlen-nlen) && (haystack[i]!=needle[0] || memcmp(&haystack[i],needle,nlen)))
00189       i++;
00190    if(i>(hlen-nlen))
00191       return -1;
00192    else
00193       return i;
00194 }

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