aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/wtap_opttypes.h
blob: 56d0c29e19e89473a24e3f8c6b973adecaeaf3f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* wtap_opttypes.h
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 2001 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef WTAP_OPT_TYPES_H
#define WTAP_OPT_TYPES_H

#include "ws_symbol_export.h"

struct wtap_optionblock;
typedef struct wtap_optionblock *wtap_optionblock_t;

/* Currently supported option blocks */
typedef enum {
    WTAP_OPTION_BLOCK_IF_DESCR = 0,
    WTAP_OPTION_BLOCK_IF_STATS,
    WTAP_OPTION_BLOCK_NG_SECTION,
    WTAP_OPTION_BLOCK_NG_NRB,
    WTAP_OPTION_BLOCK_END_OF_LIST
} wtap_optionblock_type_t;

/* Currently supported option types */
typedef enum {
    WTAP_OPTTYPE_UINT8,
    WTAP_OPTTYPE_UINT64,
    WTAP_OPTTYPE_STRING,
    WTAP_OPTTYPE_CUSTOM
} wtap_opttype_e;

typedef enum {
    WTAP_OPTTYPE_SUCCESS = 0,
    WTAP_OPTTYPE_NOT_FOUND = -1,
    WTAP_OPTTYPE_TYPE_MISMATCH = -2,
    WTAP_OPTTYPE_ALREADY_EXISTS = -3
} wtap_opttype_return_val;

typedef void (*wtap_opttype_free_custom_func)(void* data);

struct wtap_opttype_custom
{
    void* data;
    guint size;
    wtap_opttype_free_custom_func free_func;
};

typedef union {
    guint8 uint8val;
    guint64 uint64val;
    char *stringval;
    struct wtap_opttype_custom customval;
} wtap_option_type;

struct wtap_dumper;

typedef void (*wtap_block_create_func)(wtap_optionblock_t block);
typedef void (*wtap_mand_free_func)(wtap_optionblock_t block);
typedef void (*wtap_mand_copy_func)(wtap_optionblock_t dest_block, wtap_optionblock_t src_block);
typedef gboolean (*wtap_write_func)(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err);

typedef guint32 (*wtap_opttype_option_write_size)(wtap_option_type* data); /**< does the option have data worth writing (Ex string option != NULL */
typedef gboolean (*wtap_opttype_option_write)(struct wtap_dumper* wdh, wtap_option_type* data, int *err); /**< does the option have data worth writing (Ex string option != NULL */

typedef struct wtap_optblock_reg {
    const char *name;                /**< name of option */
    const char *description;         /**< human-readable description of option */
    wtap_opttype_e type;             /**< type of that option */
    wtap_opttype_option_write_size write_size_func; /**< Size of option in file (0 to not write option) */
    wtap_opttype_option_write write_func; /**< write option data to dumper */
    wtap_option_type option;         /**< pointer to variable storing the value */
    wtap_option_type default_val;    /**< the default value of the option */
} wtap_optblock_reg_t;

/** Initialize option block types.
 *
 * This is currently just a placeholder as nothing needs to be
 * initialized yet.  Should handle "registration" when code is
 * refactored to do so.
 */
void wtap_opttypes_initialize(void);

/** Create an option block by type
 *
 * Return a newly allocated option block with default options provided
 *
 * @param[in] block_type Option block type to be created
 * @return Newly allocated option block
 */
WS_DLL_PUBLIC wtap_optionblock_t wtap_optionblock_create(int block_type);

/** Free an option block
 *
 * Needs to be called to clean up any allocated option block
 *
 * @param[in] block Block to be freed
 */
WS_DLL_PUBLIC void wtap_optionblock_free(wtap_optionblock_t block);

/** Provide mandatory data of an option block
 *
 * @param[in] block Block to retrieve mandatory data
 * @return Option block mandatory data.  Structure varies based on option block type
 */
WS_DLL_PUBLIC void* wtap_optionblock_get_mandatory_data(wtap_optionblock_t block);

/** Add an option to the option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] option structure explaining it
 * @return 0 if successful
 */
int wtap_optionblock_add_option(wtap_optionblock_t block, guint option_id, wtap_optblock_reg_t* option);

/** Set string option value to an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] value New value of option
 * @param[in] value_length Maximum length of string to copy.
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_set_option_string(wtap_optionblock_t block, guint option_id, char* value, gsize value_length);

/** Set printf-styled string option value to an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] format printf like format string
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_set_option_string_format(wtap_optionblock_t block, guint option_id, const char *format, ...)
                                                            G_GNUC_PRINTF(3,4);

/** Get string option value from an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[out] value Returned value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_get_option_string(wtap_optionblock_t block, guint option_id, char** value);

/** Set UINT64 option value to an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] value New value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_set_option_uint64(wtap_optionblock_t block, guint option_id, guint64 value);

/** Get UINT64 option value from an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[out] value Returned value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_get_option_uint64(wtap_optionblock_t block, guint option_id, guint64* value);

/** Set UINT8 option value to an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] value New value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_set_option_uint8(wtap_optionblock_t block, guint option_id, guint8 value);

/** Get UINT8 option value from an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[out] value Returned value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_get_option_uint8(wtap_optionblock_t block, guint option_id, guint8* value);

/** Set a "custom" option value to an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[in] value New value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_set_option_custom(wtap_optionblock_t block, guint option_id, void* value);

/** Get a "custom" option value from an option block
 *
 * @param[in] block Block to add option
 * @param[in] option_id Identifier value for option
 * @param[out] value Returned value of option
 * @return 0 if successful
 */
WS_DLL_PUBLIC int wtap_optionblock_get_option_custom(wtap_optionblock_t block, guint option_id, void** value);

/** Copy an option block to another.
 *
 * Any options that are in the destination but not the source are not removed.
 * Options that are just in source will be added to destination
 *
 * @param[in] dest_block Block to be copied to
 * @param[in] src_block Block to be copied from
 */
void wtap_optionblock_copy_options(wtap_optionblock_t dest_block, wtap_optionblock_t src_block);

/** Write an option block
 *
 * Will write all mandatory data as well as "valid" options
 *
 * @param[in] wdh writing assistant
 * @param[in] block Block to be written
 * @param[in] err Any errors that occurred
 * @return TRUE if successful, FALSE will populate err
 */
gboolean wtap_optionblock_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err);

/* Some utility functions for option types */

guint32 wtap_opttype_write_size_string(wtap_option_type* data);
gboolean wtap_opttype_write_data_string(struct wtap_dumper* wdh, wtap_option_type* data, int *err);

/* if option value = 0, write size = 0, otherwise 4 */
guint32 wtap_opttype_write_uint8_not0(wtap_option_type* data);
gboolean wtap_opttype_write_data_uint8(struct wtap_dumper* wdh, wtap_option_type* data, int *err);

/* if option value = 0, write size = 0, otherwise 8 */
guint32 wtap_opttype_write_uint64_not0(wtap_option_type* data);
/* if option value = -1 (0xFFFFFFFFFFFFFFFF), write size = 0, otherwise 8 */
guint32 wtap_opttype_write_uint64_not_minus1(wtap_option_type* data);
gboolean wtap_opttype_write_data_uint64(struct wtap_dumper* wdh, wtap_option_type* data, int *err);

WS_DLL_PUBLIC int wtap_opttype_register_custom_block_type(const char* name, const char* description, wtap_block_create_func create,
                                                wtap_write_func write_func, wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand);


#endif /* WTAP_OPT_TYPES_H */