encode_content_length.c

Go to the documentation of this file.
00001 /* $Id: encode_content_length.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:  encode_content_length.c
00026  * 
00027  *     Description:  Function to encode content-length headers.
00028  * 
00029  *         Version:  1.0
00030  *         Created:  21/11/05 02:02:58 CET
00031  *        Revision:  none
00032  *        Compiler:  gcc
00033  * 
00034  *          Author:  Elias Baixas (EB), elias@conillera.net
00035  *         Company:  VozTele.com
00036  * 
00037  * =====================================================================================
00038  */
00039 
00040 #define _GNU_SOURCE
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <netinet/in.h>
00044 
00045 /*
00046  * Encodes a content-length header.
00047  * encoding is as follows:
00048  * 1: length of the payload.
00049  * N: Network-Byte-Ordered(little endian) of the
00050  * multibyte number represeting the length (now, it is
00051  * a long integer)
00052  */
00053 int encode_contentlength(char *hdr,int hdrlen,long int len,char *where)
00054 {
00055    long int longint;
00056 
00057    longint = htonl(len);
00058    where[0]=sizeof(long int);
00059    memcpy(&where[1],&longint,sizeof(long int));
00060    return 1+sizeof(long int);
00061 
00062 }
00063 
00064 int print_encoded_contentlength(FILE *fd,char *hdr,int hdrlen,unsigned char *payload,int paylen,char *prefix)
00065 {
00066    long int content_length;
00067    int i;
00068 
00069    memcpy(&content_length,&payload[1],payload[0]);
00070    content_length=ntohl(content_length);
00071 
00072    fprintf(fd,"%s",prefix);
00073    for(i=0;i<paylen;i++)
00074       fprintf(fd,"%s%d%s",i==0?"ENCODED CONTENT LENGTH BODY:[":":",payload[i],i==paylen-1?"]\n":"");
00075    fprintf(fd,"%s  CONTENT LENGTH=[%d]\n",prefix,(int)content_length);
00076    return 1;
00077 }
00078 

Generated on Tue May 22 16:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6