encode_cseq.c

Go to the documentation of this file.
00001 /* $Id: encode_cseq.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/parse_cseq.h"
00045 #include "../../dprint.h"
00046 #include "../../ut.h"
00047 #include "xaddress.h"/*for SLOG*/
00048 
00049 /* Encoder for CSeq header
00050  * Returns the length of the encoded structure in bytes
00051  * FORMAT (byte meanings):
00052  * 1: method_id
00053  * 4: cseqnum in network byte order
00054  * 2: HDR-based ptr to the method number + number length
00055  * 2: HDR-based ptr to the method name + method length
00056  *
00057  */
00058 int encode_cseq(char *hdrstart,int hdrlen,struct cseq_body *body,unsigned char *where)
00059 {
00060    unsigned int cseqnum;
00061    unsigned char i;
00062 
00063    /*which is the first bit set to 1 ? if i==0, the first bit, 
00064     * if i==31, the last, if i==32, none*/
00065    for(i=0;(!(body->method_id & (0x01<<i))) && i<32;i++);
00066    if(i==32)
00067       i=0;
00068    else
00069       i++;
00070    where[0]=i;
00071    if(str2int(&body->number,&cseqnum)<0){
00072       LM_ERR("str2int(cseq number)\n");
00073       return -1;
00074    }
00075    cseqnum=htonl(cseqnum);
00076    memcpy(&where[1],&cseqnum,4);/*I use 4 because 3261 says CSEq num must be 32 bits long*/
00077    where[5]=(unsigned char)(body->number.s-hdrstart);
00078    where[6]=(unsigned char)(body->number.len);
00079    where[7]=(unsigned char)(body->method.s-hdrstart);
00080    where[8]=(unsigned char)(body->method.len);
00081    return 9;
00082 }
00083 
00084 int print_encoded_cseq(FILE *fd,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix)
00085 {
00086    unsigned int cseqnum;
00087    char *what;
00088 
00089    memcpy(&cseqnum,&payload[1],4);
00090    cseqnum=ntohl(cseqnum);
00091    fprintf(fd,"%sCSEQ NUMBER=%d==%.*s\n",prefix,cseqnum,payload[6],&hdr[payload[5]]);
00092    switch(payload[0]){
00093       case 0:
00094     what="UNDEFINED";
00095     break;
00096       case 1:
00097     what="INVITE";
00098     break;
00099       case 2:
00100     what="CANCEL";
00101     break;
00102       case 3:
00103     what="ACK";
00104     break;
00105       case 4:
00106     what="BYE";
00107     break;
00108       case 5:
00109     what="INFO";
00110     break;
00111       case 6:
00112     what="OPTIONS";
00113     break;
00114       case 7:
00115     what="UPDATE";
00116     break;
00117       case 8:
00118     what="REGISTER";
00119     break;
00120       case 9:
00121     what="MESSAGE";
00122     break;
00123       case 10:
00124     what="SUBSCRIBE";
00125     break;
00126       case 11:
00127     what="NOTIFY";
00128     break;
00129       case 12:
00130     what="PRACK";
00131     break;
00132       case 13:
00133     what="REFER";
00134     break;
00135       case 14:
00136     what="OTHER";
00137     break;
00138       default:
00139     what="UNKNOWN?";
00140     break;
00141    }
00142    fprintf(fd,"%sCSEQ METHOD=%s==%.*s\n",prefix,what,payload[8],&hdr[payload[7]]);
00143    return 1;
00144 
00145 }

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