regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / wslua / wslua_proto_field.c
bloba5331c94a40ba81fc9d6c05cb8848aebf120b05f
1 /*
2 * wslua_proto_field.c
4 * Wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
8 * (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
9 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include "config.h"
20 #include <epan/tfs.h>
21 #include <epan/unit_strings.h>
23 #include "wslua.h"
25 /* WSLUA_CONTINUE_MODULE Proto */
28 WSLUA_CLASS_DEFINE(ProtoField,FAIL_ON_NULL("null ProtoField"));
29 /* A Protocol field (to be used when adding items to the dissection tree).
30 It must be registered via being added to a `Proto.fields` table. */
32 static const wslua_ft_types_t ftenums[] = {
33 {"ftypes.NONE", FT_NONE},
34 {"ftypes.BOOLEAN", FT_BOOLEAN},
35 {"ftypes.CHAR", FT_CHAR},
36 {"ftypes.UINT8", FT_UINT8},
37 {"ftypes.UINT16", FT_UINT16},
38 {"ftypes.UINT24", FT_UINT24},
39 {"ftypes.UINT32", FT_UINT32},
40 {"ftypes.UINT64", FT_UINT64},
41 {"ftypes.INT8", FT_INT8},
42 {"ftypes.INT16", FT_INT16},
43 {"ftypes.INT24", FT_INT24},
44 {"ftypes.INT32", FT_INT32},
45 {"ftypes.INT64", FT_INT64},
46 {"ftypes.FLOAT", FT_FLOAT},
47 {"ftypes.DOUBLE", FT_DOUBLE},
48 {"ftypes.ABSOLUTE_TIME", FT_ABSOLUTE_TIME},
49 {"ftypes.RELATIVE_TIME", FT_RELATIVE_TIME},
50 {"ftypes.STRING", FT_STRING},
51 {"ftypes.STRINGZ", FT_STRINGZ},
52 {"ftypes.ETHER", FT_ETHER},
53 {"ftypes.BYTES", FT_BYTES},
54 {"ftypes.UINT_BYTES", FT_UINT_BYTES},
55 {"ftypes.IPv4", FT_IPv4},
56 {"ftypes.IPv6", FT_IPv6},
57 {"ftypes.IPXNET", FT_IPXNET},
58 {"ftypes.FRAMENUM", FT_FRAMENUM},
59 {"ftypes.GUID", FT_GUID},
60 {"ftypes.OID", FT_OID},
61 {"ftypes.SYSTEM_ID", FT_SYSTEM_ID},
62 {"ftypes.REL_OID", FT_REL_OID},
63 {"ftypes.EUI64", FT_EUI64},
64 {"ftypes.FCWWN", FT_FCWWN},
65 {NULL, FT_NONE}
68 static enum ftenum get_ftenum(const char* type) {
69 const wslua_ft_types_t* ts;
70 for (ts = ftenums; ts->str; ts++) {
71 if ( g_str_equal(ts->str,type) ) {
72 return ts->id;
75 return FT_NONE;
78 static const char* ftenum_to_string(enum ftenum ft) {
79 const wslua_ft_types_t* ts;
80 for (ts = ftenums; ts->str; ts++) {
81 if ( ts->id == ft ) {
82 return ts->str;
85 return NULL;
88 struct field_display_string_t {
89 const char* str;
90 unsigned base;
94 * This table is primarily used to convert from string representation
95 * to int representation in string_to_base().
96 * Some string values are added for backward compatibility.
98 static const struct field_display_string_t base_displays[] = {
99 {"base.NONE", BASE_NONE},
100 {"base.DEC", BASE_DEC},
101 {"base.HEX", BASE_HEX},
102 {"base.OCT", BASE_OCT},
103 {"base.DEC_HEX", BASE_DEC_HEX},
104 {"base.HEX_DEC", BASE_HEX_DEC},
105 {"base.UNIT_STRING", BASE_UNIT_STRING},
106 /* Byte separators */
107 {"base.DOT", SEP_DOT},
108 {"base.DASH", SEP_DASH},
109 {"base.COLON", SEP_COLON},
110 {"base.SPACE", SEP_SPACE},
111 /* for FT_BOOLEAN, how wide the parent bitfield is */
112 {"8",8},
113 {"16",16},
114 {"24",24},
115 {"32",32},
116 /* FT_ABSOLUTE_TIME */
117 {"base.LOCAL", ABSOLUTE_TIME_LOCAL},
118 {"base.UTC", ABSOLUTE_TIME_UTC},
119 {"base.DOY_UTC", ABSOLUTE_TIME_DOY_UTC},
120 {"LOCAL", ABSOLUTE_TIME_LOCAL}, /* for backward compatibility */
121 {"UTC", ABSOLUTE_TIME_UTC}, /* for backward compatibility */
122 {"DOY_UTC", ABSOLUTE_TIME_DOY_UTC}, /* for backward compatibility */
123 {NULL,0}
126 static const char* base_to_string(unsigned base) {
127 const struct field_display_string_t* b;
128 for (b=base_displays;b->str;b++) {
129 if ( base == b->base)
130 return b->str;
132 return NULL;
135 static unsigned string_to_base(const char* str) {
136 const struct field_display_string_t* b;
137 for (b=base_displays;b->str;b++) {
138 if ( g_str_equal(str,b->str))
139 return b->base;
141 return BASE_NONE;
144 static void cleanup_range_string(GArray *rs) {
145 range_string *rs32 = (range_string *)(void *)(rs->data);
147 while (rs32->strptr) {
148 g_free((char *)rs32->strptr);
149 rs32++;
151 g_array_free(rs, true);
154 static range_string * range_string_from_table(lua_State* L, int idx) {
155 GArray* rs;
156 range_string* rs32;
158 if (lua_isnil(L,idx)) {
159 return NULL;
160 } else if (!lua_istable(L,idx)) {
161 luaL_argerror(L,idx,"must be a table");
162 return NULL;
166 * The first parameter set to true means give us a zero-filled
167 * terminal entry.
169 rs = g_array_new(true,true,sizeof(range_string));
171 lua_pushnil(L);
173 while (lua_next(L, idx) != 0) {
174 int inner_idx;
175 int key_count = 0;
176 range_string r = {0,0,NULL};
178 if (!lua_istable(L, -1)) {
179 cleanup_range_string(rs);
180 luaL_argerror(L, idx, "All values of a table used as a range_string must be tables");
181 return NULL;
185 * Now process the table ... it must have three elements,
186 * the min value, the max, both integers and a string.
188 * However, they are each separate items in the table and we
189 * ignore their keys.
191 inner_idx = lua_gettop(L);
192 lua_pushnil(L);
195 * First two elements must be numbers, third is a string
197 while (lua_next(L, inner_idx) != 0) {
198 if (++key_count > 3) {
199 break;
202 switch (key_count) {
203 case 1:
204 case 2:
205 if (!lua_isnumber(L, -1)) {
206 cleanup_range_string(rs);
207 luaL_argerror(L, idx, "First two elements of a range string value must be integers");
208 return NULL;
210 if (key_count == 1) /* We incremented it above */
211 r.value_min = wslua_touint64(L, -1);
212 else
213 r.value_max = wslua_touint64(L, -1);
214 break;
216 case 3:
217 if (lua_type(L, -1) != LUA_TSTRING) {
218 cleanup_range_string(rs);
219 luaL_argerror(L, idx, "Third element of a range string value must be a string");
220 return NULL;
222 r.strptr = g_strdup(lua_tostring(L,-1));
224 * We append the value here to avoid a mem leak if there
225 * are more than three entries in the table.
227 g_array_append_val(rs,r);
228 break;
231 lua_pop(L, 1);
234 if (key_count != 3) {
235 cleanup_range_string(rs);
236 luaL_argerror(L, idx, "Values of a range string must be tables with exactly three elements");
237 return NULL;
240 lua_pop(L, 1);
243 rs32 = (range_string*)(void*)g_array_free(rs, false);
245 return rs32;
248 static value_string* value_string_from_table(lua_State* L, int idx) {
249 GArray* vs;
250 value_string* vs32;
252 if (lua_isnil(L,idx)) {
253 return NULL;
254 } else if (!lua_istable(L,idx)) {
255 luaL_argerror(L,idx,"must be a table");
256 return NULL;
260 * The first parameter set to true means give us a zero-filled
261 * terminal entry.
263 vs = g_array_new(true,true,sizeof(value_string));
265 lua_pushnil(L);
267 while (lua_next(L, idx) != 0) {
268 value_string v = {0,NULL};
270 if (! lua_isnumber(L,-2)) {
271 vs32 = (value_string *)(void *)vs->data;
272 while (vs32->strptr) {
273 g_free((char *)vs32->strptr);
274 vs32++;
276 g_array_free(vs,true);
277 luaL_argerror(L,idx,"All keys of a table used as value_string must be integers");
278 return NULL;
281 if (! lua_isstring(L,-1)) {
282 vs32 = (value_string *)(void *)vs->data;
283 while (vs32->strptr) {
284 g_free((char *)vs32->strptr);
285 vs32++;
287 g_array_free(vs,true);
288 luaL_argerror(L,idx,"All values of a table used as value_string must be strings");
289 return NULL;
292 v.value = wslua_touint32(L,-2);
293 v.strptr = g_strdup(lua_tostring(L,-1));
295 g_array_append_val(vs,v);
297 lua_pop(L, 1);
300 vs32 = (value_string*)(void*)g_array_free(vs, false);
302 return vs32;
305 static val64_string* val64_string_from_table(lua_State* L, int idx) {
306 GArray* vs;
307 val64_string* vs64;
309 if (lua_isnil(L,idx)) {
310 return NULL;
311 } else if (!lua_istable(L,idx)) {
312 luaL_argerror(L,idx,"must be a table");
313 return NULL;
317 * The first parameter set to true means give us a zero-filled
318 * terminal entry.
320 vs = g_array_new(true,true,sizeof(val64_string));
322 lua_pushnil(L);
324 while (lua_next(L, idx) != 0) {
325 val64_string v = {0,NULL};
327 if (! lua_isnumber(L,-2)) {
328 vs64 = (val64_string *)(void *)vs->data;
329 while (vs64->strptr) {
330 g_free((char *)vs64->strptr);
331 vs64++;
333 g_array_free(vs,true);
334 luaL_argerror(L,idx,"All keys of a table used as value string must be integers");
335 return NULL;
338 if (! lua_isstring(L,-1)) {
339 vs64 = (val64_string *)(void *)vs->data;
340 while (vs64->strptr) {
341 g_free((char *)vs64->strptr);
342 vs64++;
344 g_array_free(vs,true);
345 luaL_argerror(L,idx,"All values of a table used as value string must be strings");
346 return NULL;
349 v.value = wslua_touint64(L, -2);
350 v.strptr = g_strdup(lua_tostring(L,-1));
352 g_array_append_val(vs,v);
354 lua_pop(L, 1);
357 vs64 = (val64_string*)(void*)g_array_free(vs, false);
359 return vs64;
362 static true_false_string* true_false_string_from_table(lua_State* L, int idx) {
363 true_false_string* tfs;
364 char *true_string;
365 char *false_string;
367 if (lua_isnil(L,idx)) {
368 return NULL;
369 } else if (!lua_istable(L,idx)) {
370 luaL_argerror(L,idx,"must be a table");
371 return NULL;
374 true_string = g_strdup("True");
375 false_string = g_strdup("False");
377 lua_pushnil(L);
379 while (lua_next(L, idx)) {
381 if (! lua_isnumber(L,-2)) {
382 g_free (true_string);
383 g_free (false_string);
384 luaL_argerror(L,idx,"All keys of a table used as true_false_string must be integers");
385 return NULL;
388 if (! lua_isstring(L,-1)) {
389 g_free (true_string);
390 g_free (false_string);
391 luaL_argerror(L,idx,"All values of a table used as true_false_string must be strings");
392 return NULL;
395 /* Arrays in Lua start with index number 1 */
396 switch (lua_tointeger(L,-2)) {
397 case 1:
398 g_free(true_string);
399 true_string = g_strdup(lua_tostring(L,-1));
400 break;
401 case 2:
402 g_free(false_string);
403 false_string = g_strdup(lua_tostring(L,-1));
404 break;
405 default:
406 g_free (true_string);
407 g_free (false_string);
408 luaL_argerror(L,idx,"The true_false_string table can have maximum two strings with key value 1 and 2");
409 return NULL;
412 lua_pop(L, 1);
415 tfs = g_new(true_false_string, 1);
416 tfs->true_string = true_string;
417 tfs->false_string = false_string;
419 return tfs;
422 static uint64_t get_mask(lua_State* L, int idx, uint64_t default_value) {
423 uint64_t mask = default_value;
425 switch(lua_type(L, idx)) {
426 case LUA_TNUMBER:
427 mask = wslua_optuint64(L, idx, (lua_Number)default_value);
428 break;
429 case LUA_TSTRING:
430 case LUA_TUSERDATA:
431 mask = getUInt64(L,idx);
432 break;
433 case LUA_TNIL:
434 case LUA_TNONE:
435 break;
436 default:
437 luaL_argerror(L,idx,"MASK field must be a number, UInt64 or string");
438 break;
440 return mask;
443 static unit_name_string* unit_name_string_from_table(lua_State* L, int idx) {
444 unit_name_string* units;
446 if (lua_isnil(L,idx)) {
447 return NULL;
448 } else if (!lua_istable(L,idx)) {
449 luaL_argerror(L,idx,"must be a table");
450 return NULL;
453 units = g_new0(unit_name_string, 1);
455 lua_pushnil(L);
457 while (lua_next(L, idx)) {
459 if (! lua_isnumber(L,-2)) {
460 g_free(units->singular);
461 g_free(units->plural);
462 g_free(units);
463 luaL_argerror(L,idx,"All keys of a table used as unit name must be integers");
464 return NULL;
467 if (! lua_isstring(L,-1)) {
468 g_free(units->singular);
469 g_free(units->plural);
470 g_free(units);
471 luaL_argerror(L,idx,"All values of a table used as unit name must be strings");
472 return NULL;
475 /* Arrays in Lua start with index number 1 */
476 switch (lua_tointeger(L,-2)) {
477 case 1:
478 g_free((char *)units->singular);
479 units->singular = g_strdup(lua_tostring(L,-1));
480 break;
481 case 2:
482 g_free((char *)units->plural);
483 units->plural = g_strdup(lua_tostring(L,-1));
484 break;
485 default:
486 g_free(units->singular);
487 g_free(units->plural);
488 g_free(units);
489 luaL_argerror(L,idx,"The unit name table can have maximum two strings with key value 1 and 2");
490 return NULL;
493 lua_pop(L, 1);
496 if (!units->singular) {
497 g_free(units->plural);
498 g_free(units);
499 luaL_argerror(L,idx,"The unit name table must have a singular entry (key value 1)");
500 return NULL;
503 return units;
506 static const char* check_field_name(lua_State* L, const int abbr_idx, const enum ftenum type) {
507 const char* abbr = luaL_checkstring(L,abbr_idx);
508 const header_field_info* hfinfo = NULL;
510 if (!abbr[0]) {
511 luaL_argerror(L, abbr_idx, "Empty field name abbreviation");
512 return NULL;
515 if (proto_check_field_name(abbr)) {
516 luaL_argerror(L, abbr_idx, "Invalid char in abbrev");
517 return NULL;
520 hfinfo = proto_registrar_get_byname(abbr);
522 if (hfinfo && !ftype_similar_types(type, hfinfo->type)) {
523 luaL_argerror(L, abbr_idx, "A field of an incompatible ftype with this abbrev already exists");
524 return NULL;
527 return abbr;
530 WSLUA_CONSTRUCTOR ProtoField_new(lua_State* L) {
531 /* Creates a new <<lua_class_ProtoField,`ProtoField`>> object to be used for a protocol field. */
532 #define WSLUA_ARG_ProtoField_new_NAME 1 /* Actual name of the field (the string that
533 appears in the tree). */
534 #define WSLUA_ARG_ProtoField_new_ABBR 2 /* Filter name of the field (the string that
535 is used in filters). */
536 #define WSLUA_ARG_ProtoField_new_TYPE 3 /* Field Type: one of: `ftypes.BOOLEAN`, `ftypes.CHAR`, `ftypes.UINT8`,
537 `ftypes.UINT16`, `ftypes.UINT24`, `ftypes.UINT32`, `ftypes.UINT64`, `ftypes.INT8`,
538 `ftypes.INT16`, `ftypes.INT24`, `ftypes.INT32`, `ftypes.INT64`, `ftypes.FLOAT`,
539 `ftypes.DOUBLE` , `ftypes.ABSOLUTE_TIME`, `ftypes.RELATIVE_TIME`, `ftypes.STRING`,
540 `ftypes.STRINGZ`, `ftypes.UINT_STRING`, `ftypes.ETHER`, `ftypes.BYTES`,
541 `ftypes.UINT_BYTES`, `ftypes.IPv4`, `ftypes.IPv6`, `ftypes.IPXNET`, `ftypes.FRAMENUM`,
542 `ftypes.PCRE`, `ftypes.GUID`, `ftypes.OID`, `ftypes.PROTOCOL`, `ftypes.REL_OID`,
543 `ftypes.SYSTEM_ID`, `ftypes.EUI64` or `ftypes.NONE`.
545 #define WSLUA_OPTARG_ProtoField_new_VALUESTRING 4 /* A table containing the text that
546 corresponds to the values, or a table containing tables of range string values that
547 corresponds to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name
548 for the values if base is `base.UNIT_STRING`, or one of `frametype.NONE`, `frametype.REQUEST`,
549 `frametype.RESPONSE`, `frametype.ACK` or `frametype.DUP_ACK` if field type is ftypes.FRAMENUM. */
550 #define WSLUA_OPTARG_ProtoField_new_BASE 5 /* The representation, one of: `base.NONE`, `base.DEC`,
551 `base.HEX`, `base.OCT`, `base.DEC_HEX`,
552 `base.HEX_DEC`, `base.UNIT_STRING` or
553 `base.RANGE_STRING`. */
554 #define WSLUA_OPTARG_ProtoField_new_MASK 6 /* The bitmask to be used. */
555 #define WSLUA_OPTARG_ProtoField_new_DESCRIPTION 7 /* The description of the field. */
557 ProtoField f;
558 int nargs = lua_gettop(L);
559 const char* name = luaL_checkstring(L,WSLUA_ARG_ProtoField_new_NAME);
560 const char* abbr = NULL;
561 enum ftenum type;
562 enum ft_framenum_type framenum_type = FT_FRAMENUM_NONE;
563 range_string *rs32 = NULL;
564 value_string *vs32 = NULL;
565 val64_string *vs64 = NULL;
566 true_false_string *tfs = NULL;
567 unit_name_string *uns = NULL;
568 unsigned base;
569 uint64_t mask = get_mask(L,WSLUA_OPTARG_ProtoField_new_MASK, 0x0);
570 const char *blob = luaL_optstring(L,WSLUA_OPTARG_ProtoField_new_DESCRIPTION,NULL);
571 bool base_unit_string = false;
572 bool base_range_string = false;
574 if (!name[0]) {
575 WSLUA_ARG_ERROR(ProtoField_new,NAME,"cannot be an empty string");
576 return 0;
579 if (lua_isnumber(L,WSLUA_ARG_ProtoField_new_TYPE)) {
580 type = (enum ftenum)luaL_checkinteger(L,WSLUA_ARG_ProtoField_new_TYPE);
581 } else {
582 type = get_ftenum(luaL_checkstring(L,WSLUA_ARG_ProtoField_new_TYPE));
585 abbr = check_field_name(L,WSLUA_ARG_ProtoField_new_ABBR,type);
587 if (lua_isnumber(L, WSLUA_OPTARG_ProtoField_new_BASE)) {
588 base = (unsigned)luaL_optinteger(L, WSLUA_OPTARG_ProtoField_new_BASE, BASE_NONE);
589 } else {
590 base = string_to_base(luaL_optstring(L, WSLUA_OPTARG_ProtoField_new_BASE, "BASE_NONE"));
593 switch (type) {
594 case FT_FRAMENUM:
595 if (base != BASE_NONE) {
596 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"FRAMENUM must use base.NONE");
597 return 0;
599 if (mask) {
600 WSLUA_OPTARG_ERROR(ProtoField_new,MASK,"FRAMENUM can not have a bitmask");
601 return 0;
603 if (nargs >= WSLUA_OPTARG_ProtoField_new_VALUESTRING && !lua_isnil(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING)) {
604 framenum_type = (enum ft_framenum_type) luaL_checkinteger(L, 4);
605 if (framenum_type >= FT_FRAMENUM_NUM_TYPES) {
606 WSLUA_OPTARG_ERROR(ProtoField_new,VALUESTRING,"Invalid frametype");
607 return 0;
610 break;
611 case FT_CHAR:
612 if (nargs < WSLUA_OPTARG_ProtoField_new_BASE || lua_isnil(L, WSLUA_OPTARG_ProtoField_new_BASE)) {
613 base = BASE_OCT; /* Default base for characters (BASE_HEX instead?) */
615 if (base & BASE_UNIT_STRING) {
616 WSLUA_OPTARG_ERROR(ProtoField_new, BASE, "Character type can not use base.UNIT_STRING");
617 return 0;
619 /* FALLTHRU */
620 case FT_UINT8:
621 case FT_UINT16:
622 case FT_UINT24:
623 case FT_UINT32:
624 case FT_UINT64:
625 case FT_INT8:
626 case FT_INT16:
627 case FT_INT24:
628 case FT_INT32:
629 case FT_INT64:
630 if (base & BASE_UNIT_STRING) {
631 base_unit_string = true;
632 base &= ~BASE_UNIT_STRING;
634 if (base & BASE_RANGE_STRING) {
635 base_range_string = true;
636 base &= ~BASE_RANGE_STRING;
638 if (base_unit_string && base_range_string) {
639 WSLUA_OPTARG_ERROR(ProtoField_new, BASE, "Only one of base.UNIT_STRING and base.RANGE_STRING can be specified");
640 return 0;
642 if (type != FT_CHAR && base == BASE_NONE) {
643 base = BASE_DEC; /* Default base for integer */
645 if (type == FT_CHAR) {
646 if (base != BASE_NONE && base != BASE_HEX && base != BASE_OCT) {
647 luaL_argerror(L, 3, "Base must be either base.NONE, base.HEX or base.OCT");
648 return 0;
650 } else if ((base != BASE_DEC) &&
651 (type == FT_INT8 || type == FT_INT16 || type == FT_INT24 || type == FT_INT32 || type == FT_INT64))
653 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be either base.DEC or base.UNIT_STRING");
654 return 0;
655 } else if (base < BASE_DEC || base > BASE_HEX_DEC) {
656 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be either base.DEC, base.HEX, base.OCT,"
657 " base.DEC_HEX, base.HEX_DEC or base.UNIT_STRING");
658 return 0;
660 if (nargs >= WSLUA_OPTARG_ProtoField_new_VALUESTRING) {
661 if (base_unit_string) {
662 uns = unit_name_string_from_table(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING);
663 } else if (base_range_string) {
664 rs32 = range_string_from_table(L, WSLUA_OPTARG_ProtoField_new_VALUESTRING);
665 } else if (type == FT_UINT64 || type == FT_INT64) {
666 vs64 = val64_string_from_table(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING);
667 } else {
668 vs32 = value_string_from_table(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING);
671 if (type == FT_CHAR && base == BASE_NONE && rs32 == NULL && vs32 == NULL) {
672 luaL_argerror(L, 3, "Base base.NONE must be used with a valuestring");
673 return 0;
675 break;
676 case FT_BOOLEAN:
677 if (mask == 0x0 && base != BASE_NONE) {
678 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be base.NONE if bitmask is zero.");
679 return 0;
681 if (mask != 0x0 && (base < 1 || base > 64)) {
682 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be between 1 and 64 if bitmask is non-zero.");
683 return 0;
685 if (nargs >= WSLUA_OPTARG_ProtoField_new_VALUESTRING && !lua_isnil(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING)) {
686 tfs = true_false_string_from_table(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING);
688 break;
689 case FT_ABSOLUTE_TIME:
690 if (base == BASE_NONE) {
691 base = ABSOLUTE_TIME_LOCAL; /* Default base for FT_ABSOLUTE_TIME */
692 } else if (!FIELD_DISPLAY_IS_ABSOLUTE_TIME(base)) {
693 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be either base.LOCAL, base.UTC, or base.DOY_UTC");
694 return 0;
696 if (mask) {
697 WSLUA_OPTARG_ERROR(ProtoField_new,MASK,"ABSOLUTE_TIME can not have a bitmask");
698 return 0;
700 break;
701 case FT_STRING:
702 case FT_STRINGZ:
703 if (base != BASE_NONE) {
704 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Display must be base.NONE");
705 return 0;
707 if (mask) {
708 WSLUA_OPTARG_ERROR(ProtoField_new,MASK,"This type can not have a bitmask");
709 return 0;
711 break;
712 case FT_BYTES:
713 case FT_UINT_BYTES:
714 if (base != BASE_NONE && (base < SEP_DOT || base > SEP_SPACE)) {
715 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Display must be either base.NONE, base.DOT, base.DASH, base.COLON or base.SPACE");
716 return 0;
718 if (mask) {
719 WSLUA_OPTARG_ERROR(ProtoField_new,MASK,"This type can not have a bitmask");
720 return 0;
722 break;
723 case FT_FLOAT:
724 case FT_DOUBLE:
725 if (base & BASE_UNIT_STRING) {
726 base_unit_string = true;
727 base &= ~BASE_UNIT_STRING;
729 if (nargs >= WSLUA_OPTARG_ProtoField_new_VALUESTRING) {
730 uns = unit_name_string_from_table(L,WSLUA_OPTARG_ProtoField_new_VALUESTRING);
732 /* FALLTHRU */
733 case FT_NONE:
734 case FT_IPv4:
735 case FT_IPv6:
736 case FT_IPXNET:
737 case FT_ETHER:
738 case FT_RELATIVE_TIME:
739 case FT_GUID:
740 case FT_OID:
741 case FT_PROTOCOL:
742 case FT_SYSTEM_ID:
743 case FT_REL_OID:
744 case FT_EUI64:
745 case FT_VINES:
746 case FT_FCWWN:
747 if (base != BASE_NONE) {
748 WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be base.NONE");
749 return 0;
751 if (mask) {
752 WSLUA_OPTARG_ERROR(ProtoField_new,MASK,"This type can not have a bitmask");
753 return 0;
755 break;
756 /* TODO: not handled yet */
757 case FT_UINT40:
758 case FT_UINT48:
759 case FT_UINT56:
760 case FT_INT40:
761 case FT_INT48:
762 case FT_INT56:
763 case FT_IEEE_11073_SFLOAT:
764 case FT_IEEE_11073_FLOAT:
765 case FT_UINT_STRING:
766 case FT_AX25:
767 case FT_STRINGZPAD:
768 case FT_STRINGZTRUNC:
769 WSLUA_ARG_ERROR(ProtoField_new,TYPE,"Unsupported ProtoField field type");
770 break;
771 default:
772 WSLUA_ARG_ERROR(ProtoField_new,TYPE,"Invalid ProtoField field type");
773 break;
776 if (base_unit_string && !uns) {
777 WSLUA_OPTARG_ERROR(ProtoField_new,VALUESTRING, "Base contains base.UNIT_STRING but no table was provided");
778 return 0;
781 if (base_range_string && !rs32) {
782 WSLUA_OPTARG_ERROR(ProtoField_new, VALUESTRING, "Base contains bas.RANGE_STRING but no table was provided")
783 return 0;
786 f = g_new(wslua_field_t,1);
788 f->hfid = -2;
789 f->ett = -1;
790 f->name = g_strdup(name);
791 f->abbrev = g_strdup(abbr);
792 f->type = type;
793 f->base = base;
794 if (tfs) {
795 f->vs = TFS(tfs);
796 } else if (vs32) {
797 f->vs = VALS(vs32);
798 } else if (rs32) {
799 f->base |= BASE_RANGE_STRING;
800 f->vs = RVALS(rs32);
801 } else if (vs64) {
802 /* Indicate that we are using val64_string */
803 f->base |= BASE_VAL64_STRING;
804 f->vs = VALS64(vs64);
805 } else if (uns) {
806 f->base |= BASE_UNIT_STRING;
807 f->vs = uns;
808 } else if (framenum_type) {
809 f->vs = FRAMENUM_TYPE(framenum_type);
810 } else {
811 f->vs = NULL;
813 if (f->vs) {
814 lua_pushvalue(L, WSLUA_OPTARG_ProtoField_new_VALUESTRING);
815 f->valuestring_ref = luaL_ref(L, LUA_REGISTRYINDEX);
816 } else {
817 f->valuestring_ref = LUA_NOREF;
819 f->mask = mask;
820 if (blob && strcmp(blob, f->name) != 0) {
821 f->blob = g_strdup(blob);
822 } else {
823 f->blob = NULL;
826 pushProtoField(L,f);
828 WSLUA_RETURN(1); /* The newly created <<lua_class_ProtoField,`ProtoField`>> object. */
831 static int ProtoField_integer(lua_State* L, enum ftenum type) {
832 ProtoField f;
833 const char* abbr = check_field_name(L,1,type);
834 const char* name = luaL_optstring(L,2,abbr);
835 unsigned default_base = (type == FT_FRAMENUM) ? BASE_NONE : ((type == FT_CHAR) ? BASE_OCT : BASE_DEC);
836 unsigned base = (unsigned)luaL_optinteger(L, 3, default_base);
837 enum ft_framenum_type framenum_type = FT_FRAMENUM_NONE;
838 value_string* vs32 = NULL;
839 range_string* rs32 = NULL;
840 val64_string* vs64 = NULL;
841 unit_name_string* uns = NULL;
842 uint64_t mask = get_mask(L,5,0);
843 const char* blob = luaL_optstring(L,6,NULL);
844 bool base_unit_string = false;
845 bool base_range_string = false;
847 if (!name[0]) {
848 luaL_argerror(L, 2, "cannot be an empty string");
849 return 0;
852 if (type == FT_CHAR && base & BASE_UNIT_STRING) {
853 luaL_argerror(L, 3, "Character type can not use base.UNIT_STRING");
854 return 0;
857 if (base & BASE_UNIT_STRING) {
858 base_unit_string = true;
859 base &= ~BASE_UNIT_STRING;
860 if (base == BASE_NONE) {
861 base = BASE_DEC;
865 if (base & BASE_RANGE_STRING) {
866 base_range_string = true;
867 base &= ~BASE_RANGE_STRING;
868 if (type != FT_CHAR && base == BASE_NONE) {
869 base = BASE_DEC;
873 if (base_unit_string && base_range_string) {
874 luaL_argerror(L, 3, "Only one of base.RANGE_STRING and base.UNIT_STRING can be specified");
875 return 0;
878 if (lua_gettop(L) > 3 && !lua_isnil(L, 4)) {
879 if (type == FT_FRAMENUM) {
880 framenum_type = (enum ft_framenum_type) luaL_checkinteger(L, 4);
881 if (framenum_type >= FT_FRAMENUM_NUM_TYPES) {
882 luaL_argerror(L, 4, "Invalid frametype");
883 return 0;
885 } else if (base_unit_string) {
886 uns = unit_name_string_from_table(L,4);
887 } else if (base_range_string) {
888 rs32 = range_string_from_table(L, 4);
889 } else if (type == FT_UINT64 || type == FT_INT64) {
890 vs64 = val64_string_from_table(L,4);
891 } else {
892 vs32 = value_string_from_table(L,4);
896 if (type == FT_FRAMENUM) {
897 if (base != BASE_NONE)
898 luaL_argerror(L, 3, "FRAMENUM must use base.NONE");
899 else if (mask)
900 luaL_argerror(L, 5, "FRAMENUM can not have a bitmask");
901 } else if (type == FT_CHAR) {
902 if (base != BASE_NONE && base != BASE_HEX && base != BASE_OCT) {
903 luaL_argerror(L, 3, "Base must be either base.NONE, base.HEX or base.OCT");
904 return 0;
906 if (base == BASE_NONE && rs32 == NULL && vs32 == NULL) {
907 luaL_argerror(L, 3, "Base base.NONE must be used with a valuestring");
908 return 0;
910 } else if ((base != BASE_DEC) &&
911 (type == FT_INT8 || type == FT_INT16 || type == FT_INT24 || type == FT_INT32 || type == FT_INT64)) {
912 luaL_argerror(L, 3, "Base must be either base.DEC or base.UNIT_STRING");
913 return 0;
914 } else if (base < BASE_DEC || base > BASE_HEX_DEC) {
915 luaL_argerror(L, 3, "Base must be either base.DEC, base.HEX, base.OCT,"
916 " base.DEC_HEX, base.HEX_DEC or base.UNIT_STRING");
917 return 0;
920 if (base_unit_string && !uns) {
921 luaL_argerror(L, 4, "Base contains base.UNIT_STRING but no table was given");
922 return 0;
925 if (base_range_string && !rs32) {
926 luaL_argerror(L, 4, "Base contains base.RANGE_STRING but no table was given");
927 return 0;
930 f = g_new(wslua_field_t,1);
932 f->hfid = -2;
933 f->ett = -1;
934 f->name = g_strdup(name);
935 f->abbrev = g_strdup(abbr);
936 f->type = type;
937 f->base = base;
938 if (vs64) {
939 /* Indicate that we are using val64_string */
940 f->base |= BASE_VAL64_STRING;
941 f->vs = VALS64(vs64);
942 } else if (rs32) {
943 f->base |= BASE_RANGE_STRING;
944 f->vs = rs32;
945 } else if (vs32) {
946 f->vs = VALS(vs32);
947 } else if (uns) {
948 f->base |= BASE_UNIT_STRING;
949 f->vs = uns;
950 } else if (framenum_type) {
951 f->vs = FRAMENUM_TYPE(framenum_type);
952 } else {
953 f->vs = NULL;
955 if (f->vs) {
956 lua_pushvalue(L, 4);
957 f->valuestring_ref = luaL_ref(L, LUA_REGISTRYINDEX);
958 } else {
959 f->valuestring_ref = LUA_NOREF;
961 f->mask = mask;
962 if (blob && strcmp(blob, f->name) != 0) {
963 f->blob = g_strdup(blob);
964 } else {
965 f->blob = NULL;
968 pushProtoField(L,f);
970 return 1;
973 #define PROTOFIELD_INTEGER(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_integer(L,FT); }
974 /* _WSLUA_CONSTRUCTOR_ ProtoField_char Creates a <<lua_class_ProtoField,`ProtoField`>> of an 8-bit ASCII character. */
975 /* WSLUA_ARG_ProtoField_char_ABBR Abbreviated name of the field (the string used in filters). */
976 /* WSLUA_OPTARG_ProtoField_char_NAME Actual name of the field (the string that appears in the tree). */
977 /* WSLUA_OPTARG_ProtoField_char_BASE One of `base.NONE`, `base.HEX`, `base.OCT` or `base.RANGE_STRING`. */
978 /* WSLUA_OPTARG_ProtoField_char_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`. */
979 /* WSLUA_OPTARG_ProtoField_char_MASK Integer mask of this field. */
980 /* WSLUA_OPTARG_ProtoField_char_DESCRIPTION Description of the field. */
981 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
983 /* _WSLUA_CONSTRUCTOR_ ProtoField_uint8 Creates a <<lua_class_ProtoField,`ProtoField`>> of an unsigned 8-bit integer (i.e., a byte). */
984 /* WSLUA_ARG_ProtoField_uint8_ABBR Abbreviated name of the field (the string used in filters). */
985 /* WSLUA_OPTARG_ProtoField_uint8_NAME Actual name of the field (the string that appears in the tree). */
986 /* WSLUA_OPTARG_ProtoField_uint8_BASE One of `base.DEC`, `base.HEX` or `base.OCT`, `base.DEC_HEX`, `base.HEX_DEC`, `base.UNIT_STRING` or `base.RANGE_STRING`. */
987 /* WSLUA_OPTARG_ProtoField_uint8_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing the unit name for the values if base is `base.UNIT_STRING`. */
988 /* WSLUA_OPTARG_ProtoField_uint8_MASK Integer, String or UInt64 mask of this field. */
989 /* WSLUA_OPTARG_ProtoField_uint8_DESCRIPTION Description of the field. */
990 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
992 /* _WSLUA_CONSTRUCTOR_ ProtoField_uint16 Creates a <<lua_class_ProtoField,`ProtoField`>> of an unsigned 16-bit integer. */
993 /* WSLUA_ARG_ProtoField_uint16_ABBR Abbreviated name of the field (the string used in filters). */
994 /* WSLUA_OPTARG_ProtoField_uint16_NAME Actual name of the field (the string that appears in the tree). */
995 /* WSLUA_OPTARG_ProtoField_uint16_BASE One of `base.DEC`, `base.HEX`, `base.OCT`, `base.DEC_HEX`, `base.HEX_DEC`, `base.UNIT_STRING` or `base.RANGE_STRING`. */
996 /* WSLUA_OPTARG_ProtoField_uint16_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
997 /* WSLUA_OPTARG_ProtoField_uint16_MASK Integer, String or UInt64 mask of this field. */
998 /* WSLUA_OPTARG_ProtoField_uint16_DESCRIPTION Description of the field. */
999 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1001 /* _WSLUA_CONSTRUCTOR_ ProtoField_uint24 Creates a <<lua_class_ProtoField,`ProtoField`>> of an unsigned 24-bit integer. */
1002 /* WSLUA_ARG_ProtoField_uint24_ABBR Abbreviated name of the field (the string used in filters). */
1003 /* WSLUA_OPTARG_ProtoField_uint24_NAME Actual name of the field (the string that appears in the tree). */
1004 /* WSLUA_OPTARG_ProtoField_uint24_BASE One of `base.DEC`, `base.HEX`, `base.OCT`, `base.DEC_HEX`, `base.HEX_DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1005 /* WSLUA_OPTARG_ProtoField_uint24_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing the unit name for the values if base is `base.UNIT_STRING`. */
1006 /* WSLUA_OPTARG_ProtoField_uint24_MASK Integer, String or UInt64 mask of this field. */
1007 /* WSLUA_OPTARG_ProtoField_uint24_DESCRIPTION Description of the field. */
1008 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1010 /* _WSLUA_CONSTRUCTOR_ ProtoField_uint32 Creates a <<lua_class_ProtoField,`ProtoField`>> of an unsigned 32-bit integer. */
1011 /* WSLUA_ARG_ProtoField_uint32_ABBR Abbreviated name of the field (the string used in filters). */
1012 /* WSLUA_OPTARG_ProtoField_uint32_NAME Actual name of the field (the string that appears in the tree). */
1013 /* WSLUA_OPTARG_ProtoField_uint32_BASE One of `base.DEC`, `base.HEX`, `base.OCT`, `base.DEC_HEX`, `base.HEX_DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1014 /* WSLUA_OPTARG_ProtoField_uint32_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing the unit name for the values if base is `base.UNIT_STRING`. */
1015 /* WSLUA_OPTARG_ProtoField_uint32_MASK Integer, String or UInt64 mask of this field. */
1016 /* WSLUA_OPTARG_ProtoField_uint32_DESCRIPTION Description of the field. */
1017 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1019 /* _WSLUA_CONSTRUCTOR_ ProtoField_uint64 Creates a <<lua_class_ProtoField,`ProtoField`>> of an unsigned 64-bit integer. */
1020 /* WSLUA_ARG_ProtoField_uint64_ABBR Abbreviated name of the field (the string used in filters). */
1021 /* WSLUA_OPTARG_ProtoField_uint64_NAME Actual name of the field (the string that appears in the tree). */
1022 /* WSLUA_OPTARG_ProtoField_uint64_BASE One of `base.DEC`, `base.HEX`, `base.OCT`, `base.DEC_HEX`, `base.HEX_DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1023 /* WSLUA_OPTARG_ProtoField_uint64_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing the unit name for the values if base is `base.UNIT_STRING`. */
1024 /* WSLUA_OPTARG_ProtoField_uint64_MASK Integer, String or UInt64 mask of this field. */
1025 /* WSLUA_OPTARG_ProtoField_uint64_DESCRIPTION Description of the field. */
1026 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1028 /* _WSLUA_CONSTRUCTOR_ ProtoField_int8 Creates a <<lua_class_ProtoField,`ProtoField`>> of a signed 8-bit integer (i.e., a byte). */
1029 /* WSLUA_ARG_ProtoField_int8_ABBR Abbreviated name of the field (the string used in filters). */
1030 /* WSLUA_OPTARG_ProtoField_int8_NAME Actual name of the field (the string that appears in the tree). */
1031 /* WSLUA_OPTARG_ProtoField_int8_BASE One of `base.DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1032 /* WSLUA_OPTARG_ProtoField_int8_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
1033 /* WSLUA_OPTARG_ProtoField_int8_MASK Integer, String or UInt64 mask of this field. */
1034 /* WSLUA_OPTARG_ProtoField_int8_DESCRIPTION Description of the field. */
1035 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1037 /* _WSLUA_CONSTRUCTOR_ ProtoField_int16 Creates a <<lua_class_ProtoField,`ProtoField`>> of a signed 16-bit integer. */
1038 /* WSLUA_ARG_ProtoField_int16_ABBR Abbreviated name of the field (the string used in filters). */
1039 /* WSLUA_OPTARG_ProtoField_int16_NAME Actual name of the field (the string that appears in the tree). */
1040 /* WSLUA_OPTARG_ProtoField_int16_BASE One of `base.DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1041 /* WSLUA_OPTARG_ProtoField_int16_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
1042 /* WSLUA_OPTARG_ProtoField_int16_MASK Integer, String or UInt64 mask of this field. */
1043 /* WSLUA_OPTARG_ProtoField_int16_DESCRIPTION Description of the field. */
1044 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1046 /* _WSLUA_CONSTRUCTOR_ ProtoField_int24 Creates a <<lua_class_ProtoField,`ProtoField`>> of a signed 24-bit integer. */
1047 /* WSLUA_ARG_ProtoField_int24_ABBR Abbreviated name of the field (the string used in filters). */
1048 /* WSLUA_OPTARG_ProtoField_int24_NAME Actual name of the field (the string that appears in the tree). */
1049 /* WSLUA_OPTARG_ProtoField_int24_BASE One of `base.DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1050 /* WSLUA_OPTARG_ProtoField_int24_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
1051 /* WSLUA_OPTARG_ProtoField_int24_MASK Integer, String or UInt64 mask of this field. */
1052 /* WSLUA_OPTARG_ProtoField_int24_DESCRIPTION Description of the field. */
1053 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1055 /* _WSLUA_CONSTRUCTOR_ ProtoField_int32 Creates a <<lua_class_ProtoField,`ProtoField`>> of a signed 32-bit integer. */
1056 /* WSLUA_ARG_ProtoField_int32_ABBR Abbreviated name of the field (the string used in filters). */
1057 /* WSLUA_OPTARG_ProtoField_int32_NAME Actual name of the field (the string that appears in the tree). */
1058 /* WSLUA_OPTARG_ProtoField_int32_BASE One of `base.DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1059 /* WSLUA_OPTARG_ProtoField_int32_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
1060 /* WSLUA_OPTARG_ProtoField_int32_MASK Integer, String or UInt64 mask of this field. */
1061 /* WSLUA_OPTARG_ProtoField_int32_DESCRIPTION Description of the field. */
1062 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1064 /* _WSLUA_CONSTRUCTOR_ ProtoField_int64 Creates a <<lua_class_ProtoField,`ProtoField`>> of a signed 64-bit integer. */
1065 /* WSLUA_ARG_ProtoField_int64_ABBR Abbreviated name of the field (the string used in filters). */
1066 /* WSLUA_OPTARG_ProtoField_int64_NAME Actual name of the field (the string that appears in the tree). */
1067 /* WSLUA_OPTARG_ProtoField_int64_BASE One of `base.DEC`, `base.UNIT_STRING`, or `base.RANGE_STRING`. */
1068 /* WSLUA_OPTARG_ProtoField_int64_VALUESTRING A table containing the text that corresponds to the values, or a table containing tables of range string values that correspond to the values ({min, max, "string"}) if the base is `base.RANGE_STRING`, or a table containing unit name for the values if base is `base.UNIT_STRING`. */
1069 /* WSLUA_OPTARG_ProtoField_int64_MASK Integer, String or UInt64 mask of this field. */
1070 /* WSLUA_OPTARG_ProtoField_int64_DESCRIPTION Description of the field. */
1071 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1073 /* _WSLUA_CONSTRUCTOR_ ProtoField_framenum Creates a <<lua_class_ProtoField,`ProtoField`>> for a frame number (for hyperlinks between frames). */
1074 /* WSLUA_ARG_ProtoField_framenum_ABBR Abbreviated name of the field (the string used in filters). */
1075 /* WSLUA_OPTARG_ProtoField_framenum_NAME Actual name of the field (the string that appears in the tree). */
1076 /* WSLUA_OPTARG_ProtoField_framenum_BASE Only `base.NONE` is supported for framenum. */
1077 /* WSLUA_OPTARG_ProtoField_framenum_FRAMETYPE One of `frametype.NONE`, `frametype.REQUEST`, `frametype.RESPONSE`, `frametype.ACK` or `frametype.DUP_ACK`. */
1078 /* WSLUA_OPTARG_ProtoField_framenum_MASK Integer, String or UInt64 mask of this field, which must be 0 for framenum. */
1079 /* WSLUA_OPTARG_ProtoField_framenum_DESCRIPTION Description of the field. */
1080 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1082 PROTOFIELD_INTEGER(char,FT_CHAR)
1083 PROTOFIELD_INTEGER(uint8,FT_UINT8)
1084 PROTOFIELD_INTEGER(uint16,FT_UINT16)
1085 PROTOFIELD_INTEGER(uint24,FT_UINT24)
1086 PROTOFIELD_INTEGER(uint32,FT_UINT32)
1087 PROTOFIELD_INTEGER(uint64,FT_UINT64)
1088 PROTOFIELD_INTEGER(int8,FT_INT8)
1089 PROTOFIELD_INTEGER(int16,FT_INT16)
1090 PROTOFIELD_INTEGER(int24,FT_INT24)
1091 PROTOFIELD_INTEGER(int32,FT_INT32)
1092 PROTOFIELD_INTEGER(int64,FT_INT64)
1093 PROTOFIELD_INTEGER(framenum,FT_FRAMENUM)
1095 static int ProtoField_boolean(lua_State* L, enum ftenum type) {
1096 ProtoField f;
1097 const char* abbr = check_field_name(L,1,type);
1098 const char* name = luaL_optstring(L,2,abbr);
1099 unsigned base = (unsigned)luaL_optinteger(L, 3, BASE_NONE);
1100 true_false_string* tfs = NULL;
1101 uint64_t mask = get_mask(L,5,0);
1102 const char* blob = luaL_optstring(L,6,NULL);
1104 if (!name[0]) {
1105 luaL_argerror(L, 2, "cannot be an empty string");
1106 return 0;
1109 if (mask == 0x0 && base != BASE_NONE) {
1110 luaL_argerror(L,3,"Fieldbase (fielddisplay) must be base.NONE"
1111 " if bitmask is zero.");
1112 return 0;
1115 if (mask != 0x0 && (base < 1 || base > 64)) {
1116 luaL_argerror(L,3,"Fieldbase (fielddisplay) must be between 1 and 64"
1117 " if bitmask is non-zero.");
1118 return 0;
1121 if (lua_gettop(L) > 3 && !lua_isnil(L,4)) {
1122 tfs = true_false_string_from_table(L,4);
1125 f = g_new(wslua_field_t,1);
1127 f->hfid = -2;
1128 f->ett = -1;
1129 f->name = g_strdup(name);
1130 f->abbrev = g_strdup(abbr);
1131 f->type = type;
1132 f->base = base;
1133 f->vs = TFS(tfs);
1134 if (f->vs) {
1135 lua_pushvalue(L, 4);
1136 f->valuestring_ref = luaL_ref(L, LUA_REGISTRYINDEX);
1137 } else {
1138 f->valuestring_ref = LUA_NOREF;
1140 f->mask = mask;
1141 if (blob && strcmp(blob, f->name) != 0) {
1142 f->blob = g_strdup(blob);
1143 } else {
1144 f->blob = NULL;
1147 pushProtoField(L,f);
1149 return 1;
1152 #define PROTOFIELD_BOOL(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_boolean(L,FT); }
1153 /* _WSLUA_CONSTRUCTOR_ ProtoField_bool Creates a <<lua_class_ProtoField,`ProtoField`>> for a boolean true/false value. */
1154 /* WSLUA_ARG_ProtoField_bool_ABBR Abbreviated name of the field (the string used in filters). */
1155 /* WSLUA_OPTARG_ProtoField_bool_NAME Actual name of the field (the string that appears in the tree). */
1156 /* WSLUA_OPTARG_ProtoField_bool_DISPLAY How wide the parent bitfield is (`base.NONE` is used for NULL-value). */
1157 /* WSLUA_OPTARG_ProtoField_bool_VALUESTRING A table containing the text that corresponds to the values. */
1158 /* WSLUA_OPTARG_ProtoField_bool_MASK Integer, String or UInt64 mask of this field. */
1159 /* WSLUA_OPTARG_ProtoField_bool_DESCRIPTION Description of the field. */
1160 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1162 PROTOFIELD_BOOL(bool,FT_BOOLEAN)
1164 static int ProtoField_time(lua_State* L,enum ftenum type) {
1165 ProtoField f;
1166 const char* abbr = check_field_name(L,1,type);
1167 const char* name = luaL_optstring(L,2,abbr);
1168 unsigned base = (unsigned)luaL_optinteger(L,3,ABSOLUTE_TIME_LOCAL);
1169 const char* blob = luaL_optstring(L,4,NULL);
1171 if (!name[0]) {
1172 luaL_argerror(L, 2, "cannot be an empty string");
1173 return 0;
1176 if (type == FT_ABSOLUTE_TIME) {
1177 if (!FIELD_DISPLAY_IS_ABSOLUTE_TIME(base)) {
1178 luaL_argerror(L, 3, "Base must be either base.LOCAL, base.UTC, or base.DOY_UTC");
1179 return 0;
1183 f = g_new(wslua_field_t,1);
1185 f->hfid = -2;
1186 f->ett = -1;
1187 f->name = g_strdup(name);
1188 f->abbrev = g_strdup(abbr);
1189 f->type = type;
1190 f->base = base;
1191 f->vs = NULL;
1192 f->valuestring_ref = LUA_NOREF;
1193 f->mask = 0;
1194 if (blob && strcmp(blob, f->name) != 0) {
1195 f->blob = g_strdup(blob);
1196 } else {
1197 f->blob = NULL;
1200 pushProtoField(L,f);
1202 return 1;
1205 #define PROTOFIELD_TIME(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_time(L,FT); }
1206 /* _WSLUA_CONSTRUCTOR_ ProtoField_absolute_time Creates a <<lua_class_ProtoField,`ProtoField`>> of a time_t structure value. */
1207 /* WSLUA_ARG_ProtoField_absolute_time_ABBR Abbreviated name of the field (the string used in filters). */
1208 /* WSLUA_OPTARG_ProtoField_absolute_time_NAME Actual name of the field (the string that appears in the tree). */
1209 /* WSLUA_OPTARG_ProtoField_absolute_time_BASE One of `base.LOCAL`, `base.UTC` or `base.DOY_UTC`. */
1210 /* WSLUA_OPTARG_ProtoField_absolute_time_DESCRIPTION Description of the field. */
1211 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1213 /* _WSLUA_CONSTRUCTOR_ ProtoField_relative_time Creates a <<lua_class_ProtoField,`ProtoField`>> of a time_t structure value. */
1214 /* WSLUA_ARG_ProtoField_relative_time_ABBR Abbreviated name of the field (the string used in filters). */
1215 /* WSLUA_OPTARG_ProtoField_relative_time_NAME Actual name of the field (the string that appears in the tree). */
1216 /* WSLUA_OPTARG_ProtoField_relative_time_DESCRIPTION Description of the field. */
1217 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1220 PROTOFIELD_TIME(absolute_time,FT_ABSOLUTE_TIME)
1222 static int ProtoField_floating(lua_State* L,enum ftenum type) {
1223 ProtoField f;
1224 const char* abbr = check_field_name(L,1,type);
1225 const char* name = luaL_optstring(L,2,abbr);
1226 unit_name_string* uns = NULL;
1227 const char* blob;
1229 if (!name[0]) {
1230 luaL_argerror(L, 2, "cannot be an empty string");
1231 return 0;
1234 if (lua_istable(L, 3)) {
1235 uns = unit_name_string_from_table(L,3);
1236 blob = luaL_optstring(L,4,NULL);
1237 } else {
1238 blob = luaL_optstring(L,3,NULL);
1241 f = g_new(wslua_field_t,1);
1243 f->hfid = -2;
1244 f->ett = -1;
1245 f->name = g_strdup(name);
1246 f->abbrev = g_strdup(abbr);
1247 f->type = type;
1248 if (uns) {
1249 f->base = BASE_NONE | BASE_UNIT_STRING;
1250 f->vs = uns;
1251 lua_pushvalue(L, 3);
1252 f->valuestring_ref = luaL_ref(L, LUA_REGISTRYINDEX);
1253 } else {
1254 f->base = BASE_NONE;
1255 f->vs = NULL;
1256 f->valuestring_ref = LUA_NOREF;
1258 f->mask = 0;
1259 if (blob && strcmp(blob, f->name) != 0) {
1260 f->blob = g_strdup(blob);
1261 } else {
1262 f->blob = NULL;
1265 pushProtoField(L,f);
1267 return 1;
1270 #define PROTOFIELD_FLOATING(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_floating(L,FT); }
1271 /* _WSLUA_CONSTRUCTOR_ ProtoField_float Creates a <<lua_class_ProtoField,`ProtoField`>> of a floating point number (4 bytes). */
1272 /* WSLUA_ARG_ProtoField_float_ABBR Abbreviated name of the field (the string used in filters). */
1273 /* WSLUA_OPTARG_ProtoField_float_NAME Actual name of the field (the string that appears in the tree). */
1274 /* WSLUA_OPTARG_ProtoField_float_VALUESTRING A table containing unit name for the values. */
1275 /* WSLUA_OPTARG_ProtoField_float_DESCRIPTION Description of the field. */
1276 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1278 /* _WSLUA_CONSTRUCTOR_ ProtoField_double Creates a <<lua_class_ProtoField,`ProtoField`>> of a double-precision floating point (8 bytes). */
1279 /* WSLUA_ARG_ProtoField_double_ABBR Abbreviated name of the field (the string used in filters). */
1280 /* WSLUA_OPTARG_ProtoField_double_NAME Actual name of the field (the string that appears in the tree). */
1281 /* WSLUA_OPTARG_ProtoField_double_VALUESTRING A table containing unit name for the values. */
1282 /* WSLUA_OPTARG_ProtoField_double_DESCRIPTION Description of the field. */
1283 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1285 PROTOFIELD_FLOATING(float,FT_FLOAT)
1286 PROTOFIELD_FLOATING(double,FT_DOUBLE)
1288 static int ProtoField_other_display(lua_State* L,enum ftenum type) {
1289 ProtoField f;
1290 const char* abbr = check_field_name(L,1,type);
1291 const char* name = luaL_optstring(L,2,abbr);
1292 unsigned base = BASE_NONE;
1293 const char* blob;
1295 if (!name[0]) {
1296 luaL_argerror(L, 2, "cannot be an empty string");
1297 return 0;
1300 if (lua_isnumber(L, 3)) {
1301 base = (unsigned)luaL_optinteger(L,3,BASE_NONE);
1302 if (type == FT_STRING || type == FT_STRINGZ) {
1303 if (base != BASE_NONE) {
1304 luaL_argerror(L, 3, "Display must be base.NONE");
1305 return 0;
1307 } else if (type == FT_BYTES || type == FT_UINT_BYTES) {
1308 if (base != BASE_NONE && (base < SEP_DOT || base > SEP_SPACE)) {
1309 luaL_argerror(L, 3, "Display must be either base.NONE, base.DOT, base.DASH, base.COLON or base.SPACE");
1310 return 0;
1314 blob = luaL_optstring(L,4,NULL);
1315 } else {
1316 blob = luaL_optstring(L,3,NULL);
1319 f = g_new(wslua_field_t,1);
1321 f->hfid = -2;
1322 f->ett = -1;
1323 f->name = g_strdup(name);
1324 f->abbrev = g_strdup(abbr);
1325 f->type = type;
1326 f->base = base;
1327 f->vs = NULL;
1328 f->valuestring_ref = LUA_NOREF;
1329 f->mask = 0;
1330 if (blob && strcmp(blob, f->name) != 0) {
1331 f->blob = g_strdup(blob);
1332 } else {
1333 f->blob = NULL;
1336 pushProtoField(L,f);
1338 return 1;
1341 #define PROTOFIELD_OTHER_DISPLAY(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_other_display(L,FT); }
1342 /* _WSLUA_CONSTRUCTOR_ ProtoField_string Creates a <<lua_class_ProtoField,`ProtoField`>> of a string value. */
1343 /* WSLUA_ARG_ProtoField_string_ABBR Abbreviated name of the field (the string used in filters). */
1344 /* WSLUA_OPTARG_ProtoField_string_NAME Actual name of the field (the string that appears in the tree). */
1345 /* WSLUA_OPTARG_ProtoField_string_DISPLAY One of `base.ASCII` or `base.UNICODE`. */
1346 /* WSLUA_OPTARG_ProtoField_string_DESCRIPTION Description of the field. */
1347 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1349 /* _WSLUA_CONSTRUCTOR_ ProtoField_stringz Creates a <<lua_class_ProtoField,`ProtoField`>> of a zero-terminated string value. */
1350 /* WSLUA_ARG_ProtoField_stringz_ABBR Abbreviated name of the field (the string used in filters). */
1351 /* WSLUA_OPTARG_ProtoField_stringz_NAME Actual name of the field (the string that appears in the tree). */
1352 /* WSLUA_OPTARG_ProtoField_stringz_DISPLAY One of `base.ASCII` or `base.UNICODE`. */
1353 /* WSLUA_OPTARG_ProtoField_stringz_DESCRIPTION Description of the field. */
1354 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1356 /* _WSLUA_CONSTRUCTOR_ ProtoField_bytes Creates a <<lua_class_ProtoField,`ProtoField`>> for an arbitrary number of bytes. */
1357 /* WSLUA_ARG_ProtoField_bytes_ABBR Abbreviated name of the field (the string used in filters). */
1358 /* WSLUA_OPTARG_ProtoField_bytes_NAME Actual name of the field (the string that appears in the tree). */
1359 /* WSLUA_OPTARG_ProtoField_bytes_DISPLAY One of `base.NONE`, `base.DOT`, `base.DASH`, `base.COLON` or `base.SPACE`. */
1360 /* WSLUA_OPTARG_ProtoField_bytes_DESCRIPTION Description of the field. */
1361 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1363 /* _WSLUA_CONSTRUCTOR_ ProtoField_ubytes Creates a <<lua_class_ProtoField,`ProtoField`>> for an arbitrary number of unsigned bytes. */
1364 /* WSLUA_ARG_ProtoField_ubytes_ABBR Abbreviated name of the field (the string used in filters). */
1365 /* WSLUA_OPTARG_ProtoField_ubytes_NAME Actual name of the field (the string that appears in the tree). */
1366 /* WSLUA_OPTARG_ProtoField_ubytes_DISPLAY One of `base.NONE`, `base.DOT`, `base.DASH`, `base.COLON` or `base.SPACE`. */
1367 /* WSLUA_OPTARG_ProtoField_ubytes_DESCRIPTION Description of the field. */
1368 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1371 PROTOFIELD_OTHER_DISPLAY(string,FT_STRING)
1372 PROTOFIELD_OTHER_DISPLAY(stringz,FT_STRINGZ)
1373 PROTOFIELD_OTHER_DISPLAY(bytes,FT_BYTES)
1374 PROTOFIELD_OTHER_DISPLAY(ubytes,FT_UINT_BYTES)
1376 static int ProtoField_other(lua_State* L,enum ftenum type) {
1377 ProtoField f;
1378 const char* abbr = check_field_name(L,1,type);
1379 const char* name = luaL_optstring(L,2,abbr);
1380 const char* blob = luaL_optstring(L,3,NULL);
1382 if (!name[0]) {
1383 luaL_argerror(L, 2, "cannot be an empty string");
1384 return 0;
1387 f = g_new(wslua_field_t,1);
1389 f->hfid = -2;
1390 f->ett = -1;
1391 f->name = g_strdup(name);
1392 f->abbrev = g_strdup(abbr);
1393 f->type = type;
1394 f->base = BASE_NONE;
1395 f->vs = NULL;
1396 f->valuestring_ref = LUA_NOREF;
1397 f->mask = 0;
1398 if (blob && strcmp(blob, f->name) != 0) {
1399 f->blob = g_strdup(blob);
1400 } else {
1401 f->blob = NULL;
1404 pushProtoField(L,f);
1406 return 1;
1409 #define PROTOFIELD_OTHER(lower,FT) static int ProtoField_##lower(lua_State* L) { return ProtoField_other(L,FT); }
1410 /* _WSLUA_CONSTRUCTOR_ ProtoField_none Creates a <<lua_class_ProtoField,`ProtoField`>> of an unstructured type. */
1411 /* WSLUA_ARG_ProtoField_none_ABBR Abbreviated name of the field (the string used in filters). */
1412 /* WSLUA_OPTARG_ProtoField_none_NAME Actual name of the field (the string that appears in the tree). */
1413 /* WSLUA_OPTARG_ProtoField_none_DESCRIPTION Description of the field. */
1414 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1416 /* _WSLUA_CONSTRUCTOR_ ProtoField_ipv4 Creates a <<lua_class_ProtoField,`ProtoField`>> of an IPv4 address (4 bytes). */
1417 /* WSLUA_ARG_ProtoField_ipv4_ABBR Abbreviated name of the field (the string used in filters). */
1418 /* WSLUA_OPTARG_ProtoField_ipv4_NAME Actual name of the field (the string that appears in the tree). */
1419 /* WSLUA_OPTARG_ProtoField_ipv4_DESCRIPTION Description of the field. */
1420 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1422 /* _WSLUA_CONSTRUCTOR_ ProtoField_ipv6 Creates a <<lua_class_ProtoField,`ProtoField`>> of an IPv6 address (16 bytes). */
1423 /* WSLUA_ARG_ProtoField_ipv6_ABBR Abbreviated name of the field (the string used in filters). */
1424 /* WSLUA_OPTARG_ProtoField_ipv6_NAME Actual name of the field (the string that appears in the tree). */
1425 /* WSLUA_OPTARG_ProtoField_ipv6_DESCRIPTION Description of the field. */
1426 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1428 /* _WSLUA_CONSTRUCTOR_ ProtoField_ether Creates a <<lua_class_ProtoField,`ProtoField`>> of an Ethernet address (6 bytes). */
1429 /* WSLUA_ARG_ProtoField_ether_ABBR Abbreviated name of the field (the string used in filters). */
1430 /* WSLUA_OPTARG_ProtoField_ether_NAME Actual name of the field (the string that appears in the tree). */
1431 /* WSLUA_OPTARG_ProtoField_ether_DESCRIPTION Description of the field. */
1432 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1434 /* _WSLUA_CONSTRUCTOR_ ProtoField_guid Creates a <<lua_class_ProtoField,`ProtoField`>> for a Globally Unique IDentifier (GUID). */
1435 /* WSLUA_ARG_ProtoField_guid_ABBR Abbreviated name of the field (the string used in filters). */
1436 /* WSLUA_OPTARG_ProtoField_guid_NAME Actual name of the field (the string that appears in the tree). */
1437 /* WSLUA_OPTARG_ProtoField_guid_DESCRIPTION Description of the field. */
1438 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1440 /* _WSLUA_CONSTRUCTOR_ ProtoField_oid Creates a <<lua_class_ProtoField,`ProtoField`>> for an ASN.1 Organizational IDentified (OID). */
1441 /* WSLUA_ARG_ProtoField_oid_ABBR Abbreviated name of the field (the string used in filters). */
1442 /* WSLUA_OPTARG_ProtoField_oid_NAME Actual name of the field (the string that appears in the tree). */
1443 /* WSLUA_OPTARG_ProtoField_oid_DESCRIPTION Description of the field. */
1444 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1446 /* _WSLUA_CONSTRUCTOR_ ProtoField_protocol Creates a <<lua_class_ProtoField,`ProtoField`>> for a sub-protocol. */
1447 /* WSLUA_ARG_ProtoField_protocol_ABBR Abbreviated name of the field (the string used in filters). */
1448 /* WSLUA_OPTARG_ProtoField_protocol_NAME Actual name of the field (the string that appears in the tree). */
1449 /* WSLUA_OPTARG_ProtoField_protocol_DESCRIPTION Description of the field. */
1450 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1452 /* _WSLUA_CONSTRUCTOR_ ProtoField_rel_oid Creates a <<lua_class_ProtoField,`ProtoField`>> for an ASN.1 Relative-OID. */
1453 /* WSLUA_ARG_ProtoField_rel_oid_ABBR Abbreviated name of the field (the string used in filters). */
1454 /* WSLUA_OPTARG_ProtoField_rel_oid_NAME Actual name of the field (the string that appears in the tree). */
1455 /* WSLUA_OPTARG_ProtoField_rel_oid_DESCRIPTION Description of the field. */
1456 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1458 /* _WSLUA_CONSTRUCTOR_ ProtoField_systemid Creates a <<lua_class_ProtoField,`ProtoField`>> for an OSI System ID. */
1459 /* WSLUA_ARG_ProtoField_systemid_ABBR Abbreviated name of the field (the string used in filters). */
1460 /* WSLUA_OPTARG_ProtoField_systemid_NAME Actual name of the field (the string that appears in the tree). */
1461 /* WSLUA_OPTARG_ProtoField_systemid_DESCRIPTION Description of the field. */
1462 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1464 /* _WSLUA_CONSTRUCTOR_ ProtoField_eui64 Creates a <<lua_class_ProtoField,`ProtoField`>> for an EUI64. */
1465 /* WSLUA_ARG_ProtoField_eui64_ABBR Abbreviated name of the field (the string used in filters). */
1466 /* WSLUA_OPTARG_ProtoField_eui64_NAME Actual name of the field (the string that appears in the tree). */
1467 /* WSLUA_OPTARG_ProtoField_eui64_DESCRIPTION Description of the field. */
1468 /* _WSLUA_RETURNS_ A <<lua_class_ProtoField,`ProtoField`>> object to be added to a table set to the <<lua_class_attrib_proto_fields,`Proto.fields`>> attribute. */
1470 PROTOFIELD_OTHER(none,FT_NONE)
1471 PROTOFIELD_OTHER(ipv4,FT_IPv4)
1472 PROTOFIELD_OTHER(ipv6,FT_IPv6)
1473 PROTOFIELD_OTHER(ipx,FT_IPXNET)
1474 PROTOFIELD_OTHER(ether,FT_ETHER)
1475 PROTOFIELD_OTHER(relative_time,FT_RELATIVE_TIME)
1476 PROTOFIELD_OTHER(guid,FT_GUID)
1477 PROTOFIELD_OTHER(oid,FT_OID)
1478 PROTOFIELD_OTHER(protocol,FT_PROTOCOL)
1479 PROTOFIELD_OTHER(rel_oid,FT_REL_OID)
1480 PROTOFIELD_OTHER(systemid,FT_SYSTEM_ID)
1481 PROTOFIELD_OTHER(eui64,FT_EUI64)
1483 /* WSLUA_ATTRIBUTE ProtoField_type RO The type of the field.
1485 @since 4.3.0
1487 WSLUA_ATTRIBUTE_INTEGER_GETTER(ProtoField,type);
1489 /* WSLUA_ATTRIBUTE ProtoField_abbr RO The abbreviated name of the field.
1491 @since 4.3.0
1493 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(ProtoField,abbr,abbrev);
1495 /* WSLUA_ATTRIBUTE ProtoField_name RO The actual name of the field.
1497 @since 4.3.0
1499 WSLUA_ATTRIBUTE_STRING_GETTER(ProtoField,name);
1501 /* WSLUA_ATTRIBUTE ProtoField_base RO The base of the field.
1503 @since 4.3.0
1505 WSLUA_ATTRIBUTE_INTEGER_GETTER(ProtoField,base);
1507 /* WSLUA_ATTRIBUTE ProtoField_valuestring RO The valuestring of the field.
1509 @since 4.3.0
1511 WSLUA_METAMETHOD ProtoField_get_valuestring(lua_State* L) {
1512 ProtoField f = checkProtoField(L,1);
1513 if (f->valuestring_ref != LUA_NOREF) {
1514 lua_rawgeti(L, LUA_REGISTRYINDEX, f->valuestring_ref);
1515 } else {
1516 lua_pushnil(L);
1518 return 1;
1521 /* WSLUA_ATTRIBUTE ProtoField_mask RO The mask of the field.
1523 @since 4.3.0
1525 WSLUA_ATTRIBUTE_INTEGER_GETTER(ProtoField,mask);
1527 /* WSLUA_ATTRIBUTE ProtoField_description RO The description of this field.
1529 @since 4.3.0
1531 WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(ProtoField,description,blob);
1533 WSLUA_METAMETHOD ProtoField__tostring(lua_State* L) {
1534 /* Returns a string with info about a protofield (for debugging purposes). */
1535 ProtoField f = checkProtoField(L,1);
1536 char* s = ws_strdup_printf("ProtoField(%i): %s %s %s %s %p %.16" PRIu64 "x %s",
1537 f->hfid,f->name,f->abbrev,
1538 ftenum_to_string(f->type),
1539 base_to_string(f->base),
1540 f->vs,f->mask,f->blob);
1541 lua_pushstring(L,s);
1542 g_free(s);
1543 return 1;
1546 static int ProtoField__gc(lua_State* L) {
1547 ProtoField f = toProtoField(L,1);
1550 * Initialized to -2 in ProtoField_new,
1551 * changed to -1 in Proto_commit and subsequently replaced by
1552 * an allocated number in proto_register_field_array.
1553 * Reset to -2 again in wslua_deregister_protocols.
1555 if (f->hfid != -2) {
1556 /* Only free unregistered and deregistered ProtoField */
1557 return 0;
1560 /* Note: name, abbrev, blob and vs will be NULL after Proto deregistration. */
1561 g_free(f->name);
1562 g_free(f->abbrev);
1563 g_free(f->blob);
1564 proto_free_field_strings(f->type, f->base, f->vs);
1565 if (f->valuestring_ref != LUA_NOREF) {
1566 luaL_unref(L, LUA_REGISTRYINDEX, f->valuestring_ref);
1568 g_free(f);
1570 return 0;
1573 WSLUA_METHODS ProtoField_methods[] = {
1574 WSLUA_CLASS_FNREG(ProtoField,new),
1575 WSLUA_CLASS_FNREG(ProtoField,none),
1576 WSLUA_CLASS_FNREG(ProtoField,char),
1577 WSLUA_CLASS_FNREG(ProtoField,uint8),
1578 WSLUA_CLASS_FNREG(ProtoField,uint16),
1579 WSLUA_CLASS_FNREG(ProtoField,uint24),
1580 WSLUA_CLASS_FNREG(ProtoField,uint32),
1581 WSLUA_CLASS_FNREG(ProtoField,uint64),
1582 WSLUA_CLASS_FNREG(ProtoField,int8),
1583 WSLUA_CLASS_FNREG(ProtoField,int16),
1584 WSLUA_CLASS_FNREG(ProtoField,int24),
1585 WSLUA_CLASS_FNREG(ProtoField,int32),
1586 WSLUA_CLASS_FNREG(ProtoField,int64),
1587 WSLUA_CLASS_FNREG(ProtoField,framenum),
1588 WSLUA_CLASS_FNREG(ProtoField,ipv4),
1589 WSLUA_CLASS_FNREG(ProtoField,ipv6),
1590 WSLUA_CLASS_FNREG(ProtoField,ipx),
1591 WSLUA_CLASS_FNREG(ProtoField,ether),
1592 WSLUA_CLASS_FNREG(ProtoField,bool),
1593 WSLUA_CLASS_FNREG(ProtoField,float),
1594 WSLUA_CLASS_FNREG(ProtoField,double),
1595 WSLUA_CLASS_FNREG(ProtoField,absolute_time),
1596 WSLUA_CLASS_FNREG(ProtoField,relative_time),
1597 WSLUA_CLASS_FNREG(ProtoField,string),
1598 WSLUA_CLASS_FNREG(ProtoField,stringz),
1599 WSLUA_CLASS_FNREG(ProtoField,bytes),
1600 WSLUA_CLASS_FNREG(ProtoField,ubytes),
1601 WSLUA_CLASS_FNREG(ProtoField,guid),
1602 WSLUA_CLASS_FNREG(ProtoField,oid),
1603 WSLUA_CLASS_FNREG(ProtoField,protocol),
1604 WSLUA_CLASS_FNREG(ProtoField,rel_oid),
1605 WSLUA_CLASS_FNREG(ProtoField,systemid),
1606 WSLUA_CLASS_FNREG(ProtoField,eui64),
1607 { NULL, NULL }
1610 /* This table is ultimately registered as a sub-table of the class' metatable,
1611 * and if __index/__newindex is invoked then it calls the appropriate function
1612 * from this table for getting/setting the members.
1614 WSLUA_ATTRIBUTES ProtoField_attributes[] = {
1615 WSLUA_ATTRIBUTE_ROREG(ProtoField,type),
1616 WSLUA_ATTRIBUTE_ROREG(ProtoField,abbr),
1617 WSLUA_ATTRIBUTE_ROREG(ProtoField,name),
1618 WSLUA_ATTRIBUTE_ROREG(ProtoField,base),
1619 WSLUA_ATTRIBUTE_ROREG(ProtoField,valuestring),
1620 WSLUA_ATTRIBUTE_ROREG(ProtoField,mask),
1621 WSLUA_ATTRIBUTE_ROREG(ProtoField,description),
1622 { NULL, NULL, NULL }
1625 WSLUA_META ProtoField_meta[] = {
1626 WSLUA_CLASS_MTREG(ProtoField,tostring),
1627 { NULL, NULL }
1630 int ProtoField_register(lua_State* L) {
1631 WSLUA_REGISTER_CLASS_WITH_ATTRS(ProtoField);
1632 return 0;
1637 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1639 * Local variables:
1640 * c-basic-offset: 4
1641 * tab-width: 8
1642 * indent-tabs-mode: nil
1643 * End:
1645 * vi: set shiftwidth=4 tabstop=8 expandtab:
1646 * :indentSize=4:tabSize=8:noTabs=true: