(Trivial) remove commented out calls to tvb_free().
[obnox/wireshark/wip.git] / plugins / m2m / wimax_tlv.c
1 /* wimax_tlv.c
2  * WiMax TLV handling functions
3  *
4  * Copyright (c) 2007 by Intel Corporation.
5  *
6  * Author: Lu Pan <lu.pan@intel.com>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1999 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*************************************************************/
30 /*   ----------------------- NOTE -------------------------  */
31 /* There is an identical copy of this file, wimax_tlv.c, in  */
32 /* both .../plugins/m2m and .../plugins/wimax.  If either    */
33 /* one needs to be modified, please be sure to copy the file */
34 /* to the sister directory and check it in there also.       */
35 /* This prevents having to generate a complicated            */
36 /* Makefile.nmake in .../plugins/m2m.                        */
37 /*************************************************************/
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <plugins/wimax/wimax_tlv.h>
44
45 /*************************************************************/
46 /* init_tlv_info()                                           */
47 /* retrive the tlv information from specified tvb and offset */
48 /* parameter:                                                */
49 /*   this - pointer of a tlv information data structure      */
50 /* return:                                                   */
51 /*   0-success                                               */
52 /*   !=0-the invalid size of the TLV length (failed)         */
53 /*************************************************************/
54 gint init_tlv_info(tlv_info_t *this, tvbuff_t *tvb, gint offset)
55 {
56         guint tlv_len;
57
58         /* get TLV type */
59         this->type = (guint8)tvb_get_guint8( tvb, offset );
60         /* get TLV length */
61         tlv_len = (guint)tvb_get_guint8( tvb, (offset + 1) );
62         /* set the TLV value offset */
63         this->value_offset = 2;
64         /* adjust for multiple-byte TLV length */
65         if((tlv_len & WIMAX_TLV_EXTENDED_LENGTH_MASK) != 0)
66         {       /* multiple bytes TLV length */
67                 this->length_type = 1;
68                 /* get the size of the TLV length */
69                 tlv_len = (tlv_len & WIMAX_TLV_LENGTH_MASK);
70                 this->size_of_length = tlv_len;
71                 /* update the TLV value offset */
72                 this->value_offset += tlv_len;
73                 switch (tlv_len)
74                 {
75                         case 0:
76                                 this->length = 0;  /* no length */
77                         break;
78                         case 1:
79                                 this->length = (gint32)tvb_get_guint8( tvb, (offset + 2) ); /* 8 bit */
80                         break;
81                         case 2:
82                                 this->length = (gint32)tvb_get_ntohs( tvb, (offset + 2) ); /* 16 bit */
83                         break;
84                         case 3:
85                                 this->length = (gint32)tvb_get_ntoh24( tvb, (offset + 2) ); /* 24 bit */
86                         break;
87                         case 4:
88                                 this->length = (gint32)tvb_get_ntohl( tvb, (offset + 2) ); /* 32 bit */
89                         break;
90                         default:
91                                 /* mark invalid tlv */
92                                 this->valid = 0;
93                                 /* failed, return the invalid size of the tlv length */
94                                 return (gint)tlv_len;
95                         break;
96                 }
97         }
98         else    /* single byte length */
99         {
100                 this->length_type = 0;
101                 this->size_of_length = 0;
102                 this->length = (gint32)tlv_len;
103         }
104         /* mark valid tlv */
105         this->valid = 1;
106         /* success */
107         return 0;
108 }
109
110 /*************************************************************/
111 /* get_tlv_type()                                            */
112 /* get the tlv type of the specified tlv information         */
113 /* parameter:                                                */
114 /*   this - pointer of a tlv information data structure      */
115 /* return:                                                   */
116 /*   >=0 - TLV type                                           */
117 /*   =-1 - invalid tlv info                                  */
118 /*************************************************************/
119 gint get_tlv_type(tlv_info_t *this)
120 {
121         if(this->valid)
122                 return (gint)this->type;
123         return -1;
124 }
125
126 /**************************************************************/
127 /* get_tlv_size_of_length()                                   */
128 /* get the size of tlv length of the specified tlv information*/
129 /* parameter:                                                 */
130 /*   this - pointer of a tlv information data structure       */
131 /* return:                                                    */
132 /*   >=0 - the size of TLV length                              */
133 /*   =-1 - invalid tlv info                                   */
134 /**************************************************************/
135 gint get_tlv_size_of_length(tlv_info_t *this)
136 {
137         if(this->valid)
138                 return (gint)this->size_of_length;
139         return -1;
140 }
141
142 /*************************************************************/
143 /* get_tlv_length()                                          */
144 /* get the tlv length of the specified tlv information       */
145 /* parameter:                                                */
146 /*   this - pointer of a tlv information data structure      */
147 /* return:                                                   */
148 /*   >=0 - TLV length                                         */
149 /*   =-1 - invalid tlv info                                  */
150 /*************************************************************/
151 gint32 get_tlv_length(tlv_info_t *this)
152 {
153         if(this->valid)
154                 return (gint32)this->length;
155         return -1;
156 }
157
158 /*************************************************************/
159 /* get_tlv_value_offset()                                    */
160 /* get the tlv value offset of the specified tlv information */
161 /* parameter:                                                */
162 /*   this - pointer of a tlv information data structure      */
163 /* return:                                                   */
164 /*   >0 - TLV value offset in byte                           */
165 /*   =-1 - invalid tlv info                                  */
166 /*************************************************************/
167 gint get_tlv_value_offset(tlv_info_t *this)
168 {
169         if(this->valid)
170                 return (gint)this->value_offset;
171         return -1;
172 }
173
174 /*************************************************************/
175 /* get_tlv_length_type()                                     */
176 /* get the tlv length type of the specified tlv information  */
177 /* parameter:                                                */
178 /*   this - pointer of a tlv information data structure      */
179 /* return:                                                   */
180 /*   0 - single byte TLV length                              */
181 /*   1 - multiple bytes TLV length                           */
182 /*************************************************************/
183 gint get_tlv_length_type(tlv_info_t *this)
184 {
185         if(this->valid)
186                 return (gint)this->length_type;
187         return -1;
188 }