encode_to_body.c

Go to the documentation of this file.
00001 /* $Id: encode_to_body.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:  xaddress.c
00026  * 
00027  *     Description:  Address manipulation tools
00028  * 
00029  *         Version:  1.0
00030  *         Created:  17/11/05 02:09:44 CET
00031  *        Revision:  none
00032  *        Compiler:  gcc
00033  * 
00034  *          Author:  Elias Baixas (EB), elias@conillera.net
00035  *         Company:  VozTele.com
00036  * 
00037  * =====================================================================================
00038  */
00039 #define _GNU_SOURCE
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include <netinet/in.h>
00043 #include "../../mem/mem.h"
00044 #include "../../parser/msg_parser.h"
00045 #include "../../parser/parse_uri.h"
00046 #include "../../dprint.h"
00047 #include "encode_to_body.h"
00048 #include "encode_uri.h"
00049 #include "encode_header.h"
00050 #include "encode_parameters.h"
00051 #include "xaddress.h"
00052 
00053 
00054 /* Encoder for From and To headers.
00055  * Returns the length of the encoded structure in bytes
00056  * FORMAT (byte meanings):
00057  * 1: flags
00058  *    0x10 :   there is a Display Name
00059  *    0x20 :   there is a tag parameter
00060  *    0x40 :   there are other parameters
00061  * 1: length of the XURI-encoded uri in bytes.
00062  * [2]: optionally, 1 HDR-based ptr to the displayname + the length of the name
00063  * [2]: optionally, 1 HDR-based ptr to the tag + the length of the tag parameter
00064  * N: the XURI-encoded URI.
00065  * [N:] optionally, HDR-based pointers to the different header-parameters
00066  *
00067  */
00068 int encode_to_body(char *hdrstart,int hdrlen,struct to_body *body,unsigned char *where)
00069 {
00070    int i=2,j=0;/* 1*flags + 1*URI_len*/
00071    unsigned char flags=0;
00072    struct sip_uri puri;
00073 
00074    if(body->display.s && body->display.len){
00075       flags|=HAS_DISPLAY_F;
00076       if(body->display.s[0]=='\"'){
00077     body->display.s++;
00078     body->display.len-=2;
00079       }
00080       where[i++]=(unsigned char)(body->display.s-hdrstart);
00081       where[i++]=(unsigned char)(body->display.len);
00082    }
00083    if(body->tag_value.s && body->tag_value.len){
00084       flags|=HAS_TAG_F;
00085       where[i++]=(unsigned char)(body->tag_value.s-hdrstart);
00086       where[i++]=(unsigned char)body->tag_value.len;
00087    }
00088    if (parse_uri(body->uri.s, body->uri.len,&puri) < 0 ) {
00089       LM_ERR("Bad URI in address\n");
00090       return -1;
00091    }else{
00092       if((j=encode_uri2(hdrstart,hdrlen,body->uri,&puri,&where[i]))<0){
00093     LM_ERR("failed to codify the URI\n");
00094     return -1;
00095       }else{
00096     i+=j;
00097       }
00098    }
00099    where[0]=flags;
00100    where[1]=(unsigned char)j;
00101    i+=encode_parameters(&where[i],(void *)body->param_lst,hdrstart,body,'t');
00102 
00103    return i;
00104 }
00105 
00106 int print_encoded_to_body(FILE *fd,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix)
00107 {
00108    int i=2;/* flags + urilength */
00109    unsigned char flags=0;
00110 
00111    flags=payload[0];
00112    fprintf(fd,"%s",prefix);
00113    for(i=0;i<paylen;i++)
00114       fprintf(fd,"%s%d%s",i==0?"BODY CODE=[":":",payload[i],i==paylen-1?"]\n":"");
00115    i=2;
00116    if(flags & HAS_DISPLAY_F){
00117       fprintf(fd,"%sDISPLAY NAME=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]);
00118       i+=2;
00119    }
00120    if(flags & HAS_TAG_F){
00121       fprintf(fd,"%sTAG=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]);
00122       i+=2;
00123    }
00124    if(print_encoded_uri(fd,&payload[i],payload[1],hdr,hdrlen,strcat(prefix,"  "))<0){
00125       fprintf(fd,"Error parsing URI\n");
00126       prefix[strlen(prefix)-2]=0;
00127       return -1;
00128    }
00129    prefix[strlen(prefix)-2]=0;
00130    print_encoded_parameters(fd,&payload[i+payload[1]],hdr,paylen-i-payload[1],prefix);
00131    return 0;
00132 }
00133 
00134 /**
00135  * dumps to FD a NBO int which is the header length, the header,
00136  * an NBO int which is the payload length, and the payload.
00137  *
00138  * hdr is the header,
00139  * hdrlen is the header length,
00140  * payload is the payload,
00141  * paylen is the payload length,
00142  * fd is the file descriptor to which to dump,
00143  * segregationLevel is wether only URIS must be dumped or all the header code.
00144  *
00145  * return 0 on success, <0 on error
00146  */
00147 int dump_to_body_test(char *hdr,int hdrlen,unsigned char* payload,int paylen,FILE* fd,char segregationLevel)
00148 {
00149    int i=2;/* flags + urilength */
00150    unsigned char flags=0;
00151 
00152    flags=payload[0];
00153    if(!segregationLevel){
00154       return dump_standard_hdr_test(hdr,hdrlen,payload,paylen,fd);
00155    }
00156    i=2;
00157    if(flags & HAS_DISPLAY_F){
00158       i+=2;
00159    }
00160    if(flags & HAS_TAG_F){
00161       i+=2;
00162    }
00163    if(!(segregationLevel & JUNIT) && (segregationLevel & ONLY_URIS)){
00164      return dump_standard_hdr_test(hdr,hdrlen,&payload[i],payload[1],fd);
00165    }
00166    if((segregationLevel & JUNIT) && (segregationLevel & ONLY_URIS)){
00167      return print_uri_junit_tests(hdr,hdrlen,&payload[i],payload[1],fd,1,"");
00168    }
00169    return 0;
00170 }

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6