drsuapi_SupportedExtensionsExt
[wireshark-sm.git] / epan / wslua / wslua_file_handler.c
blob5807e88c245c8d0139bcd4c5cdb11ed3ad7f223f
1 /*
2 * wslua_file_handler.c
4 * Wireshark's interface to the Lua Programming Language
5 * for custom file reading/writing.
7 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "config.h"
16 #define WS_LOG_DOMAIN LOG_DOMAIN_WSLUA
18 #include "wslua_file_common.h"
20 #include <errno.h>
21 #include <wiretap/file_wrappers.h>
23 /* WSLUA_CONTINUE_MODULE File */
26 WSLUA_CLASS_DEFINE(FileHandler,NOP);
28 A FileHandler object, created by a call to FileHandler.new(arg1, arg2, ...).
29 The FileHandler object lets you create a file-format reader, or writer, or
30 both, by setting your own read_open/read or write_open/write functions.
33 static int filehandler_cb_error_handler(lua_State* L) {
34 const char* error = lua_tostring(L,1);
35 const char* functype = luaL_optstring(L, lua_upvalueindex(1), "UNKNOWN");
36 report_failure("Lua: Error During execution of FileHandler %s callback:\n %s",functype,error);
37 lua_pop(L, 1);
38 return 0;
41 static int push_error_handler(lua_State* L, const char* funcname) {
42 lua_pushstring(L, funcname);
43 lua_pushcclosure(L, filehandler_cb_error_handler, 1);
44 return 1;
48 /* Keep track of registered FileHandlers such that reloading plugins works. */
49 static GSList *registered_file_handlers;
51 /* During file routines, we cannot allow the FileHandler to get deregistered, since
52 that would change the GArray's in file_access.c and hilarity would ensue. So we
53 set this to true right before pcall(), and back to false afterwards */
54 static bool in_routine;
56 static void
57 report_error(int *err, char **err_info, const char *fmt, ...)
59 va_list ap;
60 char *msg;
62 va_start(ap, fmt);
63 msg = ws_strdup_vprintf(fmt, ap);
64 va_end(ap);
65 if (err != NULL) {
66 *err = WTAP_ERR_INTERNAL;
67 *err_info = msg;
68 } else {
69 ws_warning("%s", msg);
70 g_free(msg);
74 /* This does the verification and setup common to all open/read/seek_read/close routines */
75 #define INIT_FILEHANDLER_ROUTINE(name,retval,err,err_info) \
76 if (!fh) { \
77 report_error(err, err_info, "Error in file %s: no Lua FileHandler object", #name); \
78 return retval; \
79 } \
80 if (fh->removed) { \
81 return retval; \
82 } \
83 if (!fh->registered) { \
84 report_error(err, err_info, "Error in file %s: Lua FileHandler is not registered", #name); \
85 return retval; \
86 } \
87 if (!fh->L) { \
88 report_error(err, err_info, "Error in file %s: no FileHandler Lua state", #name); \
89 return retval; \
90 } \
91 if (fh->name##_ref == LUA_NOREF) { \
92 report_error(err, err_info, "Error in file %s: no FileHandler %s routine reference", #name, #name); \
93 return retval; \
94 } \
95 L = fh->L; \
96 lua_settop(L,0); \
97 push_error_handler(L, #name " routine"); \
98 lua_rawgeti(L, LUA_REGISTRYINDEX, fh->name##_ref); \
99 if (!lua_isfunction(L, -1)) { \
100 report_error(err, err_info, "Error in file %s: no FileHandler %s routine function in Lua", #name, #name); \
101 return retval; \
103 /* now guard against deregistering during pcall() */ \
104 in_routine = true
106 #define END_FILEHANDLER_ROUTINE() \
107 /* now allow deregistering again */ \
108 in_routine = false
111 /* LUA_ERRGCMM is in Lua 5.2 only - making it 9 disables it */
112 #ifndef LUA_ERRGCMM
113 #define LUA_ERRGCMM 9
114 #endif
116 #define CASE_ERROR(name,err,err_info) \
117 case LUA_ERRRUN: \
118 report_error(err, err_info, "Run-time error while calling FileHandler %s routine", name); \
119 break; \
120 case LUA_ERRMEM: \
121 report_error(err, err_info, "Memory alloc error while calling FileHandler %s routine", name); \
122 break; \
123 case LUA_ERRERR: \
124 report_error(err, err_info, "Error in error handling while calling FileHandler %s routine", name); \
125 break; \
126 case LUA_ERRGCMM: \
127 report_error(err, err_info, "Error in garbage collector while calling FileHandler %s routine", name); \
128 break; \
129 default: \
130 ws_assert_not_reached(); \
131 break;
133 /* some declarations */
134 static bool
135 wslua_filehandler_read(wtap *wth, wtap_rec *rec, Buffer *buf,
136 int *err, char **err_info, int64_t *offset);
137 static bool
138 wslua_filehandler_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf,
139 int *err, char **err_info);
140 static void
141 wslua_filehandler_close(wtap *wth);
142 static void
143 wslua_filehandler_sequential_close(wtap *wth);
146 /* This is our one-and-only open routine for file handling. When called by
147 * file_access.c, the wtap wth argument has a void* wslua_data that holds the specific
148 * FileHandler for the specific registered file format reader. It has this because
149 * we passed it in when we registered the open routine.
150 * The open_file_* routines should return:
151 * -1 on an I/O error;
152 * 1 if the file they're reading is one of the types it handles;
153 * 0 if the file they're reading isn't the type they're checking for.
154 * If the routine handles this type of file, it should set the "file_type"
155 * field in the "struct wtap" to the type of the file.
157 static wtap_open_return_val
158 wslua_filehandler_open(wtap *wth, int *err, char **err_info)
160 FileHandler fh = (FileHandler)(wth->wslua_data);
161 wtap_open_return_val retval = WTAP_OPEN_NOT_MINE;
162 lua_State* L = NULL;
163 File *fp = NULL;
164 CaptureInfo *fc = NULL;
166 INIT_FILEHANDLER_ROUTINE(read_open,WTAP_OPEN_ERROR,err,err_info);
168 create_wth_priv(L, wth);
170 fp = push_File(L, wth->fh);
171 fc = push_CaptureInfo(L, wth, true);
173 errno = WTAP_ERR_CANT_OPEN;
174 switch ( lua_pcall(L,2,1,1) ) {
175 case 0:
176 retval = (wtap_open_return_val)wslua_optboolint(L,-1,0);
177 break;
178 CASE_ERROR("read_open",err,err_info)
181 END_FILEHANDLER_ROUTINE();
183 (*fp)->expired = true;
184 (*fc)->expired = true;
186 if (retval == WTAP_OPEN_MINE) {
187 /* this is our file type - set the routines and settings into wtap */
189 if (fh->read_ref != LUA_NOREF) {
190 wth->subtype_read = wslua_filehandler_read;
192 else {
193 ws_warning("Lua file format module lacks a read routine");
194 return WTAP_OPEN_NOT_MINE;
197 /* when not having a seek_read routine a default will be used */
198 wth->subtype_seek_read = wslua_filehandler_seek_read;
200 /* it's ok to not have a close routine */
201 if (fh->read_close_ref != LUA_NOREF)
202 wth->subtype_close = wslua_filehandler_close;
203 else
204 wth->subtype_close = NULL;
206 /* it's ok to not have a sequential close routine */
207 if (fh->seq_read_close_ref != LUA_NOREF)
208 wth->subtype_sequential_close = wslua_filehandler_sequential_close;
209 else
210 wth->subtype_sequential_close = NULL;
212 wth->file_type_subtype = fh->file_type;
214 else if (retval == WTAP_OPEN_ERROR) {
215 /* open error - we *must* return an error code! */
216 if (err) {
217 *err = WTAP_ERR_CANT_OPEN;
220 else if (retval == WTAP_OPEN_NOT_MINE) {
221 /* not our file type */
222 remove_wth_priv(L, wth);
224 else {
225 /* not a valid return type */
226 if (err) {
227 *err = WTAP_ERR_INTERNAL;
228 *err_info = ws_strdup_printf("FileHandler read_open routine returned %d", retval);
230 retval = WTAP_OPEN_ERROR;
233 lua_settop(L,0);
234 return retval;
237 static bool
238 wslua_filehandler_read_packet(wtap *wth, FILE_T wth_fh, wtap_rec *rec, Buffer *buf,
239 int *err, char **err_info, int64_t *offset)
241 FileHandler fh = (FileHandler)(wth->wslua_data);
242 int retval = -1;
243 lua_State* L = NULL;
244 File *fp = NULL;
245 CaptureInfo *fc = NULL;
246 FrameInfo *fi = NULL;
248 INIT_FILEHANDLER_ROUTINE(read,false,err,err_info);
250 /* Reset errno */
251 if (err) {
252 *err = errno = 0;
255 wtap_block_unref(rec->block);
256 rec->block = NULL;
258 fp = push_File(L, wth_fh);
259 fc = push_CaptureInfo(L, wth, false);
260 fi = push_FrameInfo(L, rec, buf);
262 switch ( lua_pcall(L,3,1,1) ) {
263 case 0:
265 * Return values for FileHandler:read():
266 * Integer is the number of read bytes.
267 * Boolean false indicates an error.
268 * XXX handling of boolean true is not documented. Currently it will
269 * succeed without advancing data offset. Should it fail instead?
271 if (lua_type(L, -1) == LUA_TNUMBER) {
272 *offset = wslua_toint64(L, -1);
273 retval = 1;
274 break;
276 retval = wslua_optboolint(L,-1,0);
277 break;
278 CASE_ERROR("read",err,err_info)
281 END_FILEHANDLER_ROUTINE();
283 (*fp)->expired = true;
284 (*fc)->expired = true;
285 (*fi)->expired = true;
286 lua_settop(L,0);
288 return (retval == 1);
291 /* The classic wtap read routine. This returns true if it found the next packet,
292 * else false.
293 * If it finds a frame/packet, it should set the pseudo-header info (ie, let Lua set it).
294 * Also Lua needs to set data_offset to the beginning of the line we're returning.
295 * This will be the seek_off parameter when this frame is re-read.
297 static bool
298 wslua_filehandler_read(wtap *wth, wtap_rec *rec, Buffer *buf,
299 int *err, char **err_info, int64_t *offset)
301 return wslua_filehandler_read_packet(wth, wth->fh, rec, buf, err, err_info, offset);
304 static bool
305 wslua_filehandler_seek_read_packet(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf,
306 int *err, char **err_info)
308 FileHandler fh = (FileHandler)(wth->wslua_data);
309 int retval = -1;
310 lua_State* L = NULL;
311 File *fp = NULL;
312 CaptureInfo *fc = NULL;
313 FrameInfo *fi = NULL;
315 INIT_FILEHANDLER_ROUTINE(seek_read,false,err,err_info);
317 /* Reset errno */
318 if (err) {
319 *err = errno = 0;
322 wtap_block_unref(rec->block);
323 rec->block = NULL;
325 fp = push_File(L, wth->random_fh);
326 fc = push_CaptureInfo(L, wth, false);
327 fi = push_FrameInfo(L, rec, buf);
328 lua_pushinteger(L, (lua_Integer)seek_off);
330 switch ( lua_pcall(L,4,1,1) ) {
331 case 0:
333 * Return values for FileHandler:seek_read():
334 * Boolean true for successful parsing, false/nil on error.
335 * Numbers (including zero) are interpreted as success for
336 * compatibility to match FileHandler:seek semantics.
337 * (Other values are unspecified/undocumented, but happen to be
338 * treated as success.)
340 retval = lua_toboolean(L, -1);
341 break;
342 CASE_ERROR("seek_read",err,err_info)
345 END_FILEHANDLER_ROUTINE();
347 (*fp)->expired = true;
348 (*fc)->expired = true;
349 (*fi)->expired = true;
350 lua_settop(L,0);
352 return (retval == 1);
355 /* Default FileHandler:seek_read() implementation.
356 * Do a standard file_seek() and then call FileHandler:read().
358 static bool
359 wslua_filehandler_seek_read_default(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf,
360 int *err, char **err_info)
362 int64_t offset = file_seek(wth->random_fh, seek_off, SEEK_SET, err);
364 if (offset < 0) {
365 return false;
368 return wslua_filehandler_read_packet(wth, wth->random_fh, rec, buf, err, err_info, &offset);
371 /* Classic wtap seek_read function, called by wtap core. This must return true on
372 * success, false on error.
374 static bool
375 wslua_filehandler_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf,
376 int *err, char **err_info)
378 FileHandler fh = (FileHandler)(wth->wslua_data);
380 if (fh->removed) {
381 /* Return success when removed during reloading Lua plugins */
382 return true;
385 if (fh->seek_read_ref != LUA_NOREF) {
386 return wslua_filehandler_seek_read_packet(wth, seek_off, rec, buf, err, err_info);
387 } else {
388 return wslua_filehandler_seek_read_default(wth, seek_off, rec, buf, err, err_info);
392 /* Classic wtap close function, called by wtap core.
394 static void
395 wslua_filehandler_close(wtap *wth)
397 FileHandler fh = (FileHandler)(wth->wslua_data);
398 lua_State* L = NULL;
399 File *fp = NULL;
400 CaptureInfo *fc = NULL;
402 INIT_FILEHANDLER_ROUTINE(read_close,,NULL,NULL);
404 fp = push_File(L, wth->fh);
405 fc = push_CaptureInfo(L, wth, false);
407 switch ( lua_pcall(L,2,1,1) ) {
408 case 0:
409 break;
410 CASE_ERROR("read_close",NULL,NULL)
413 END_FILEHANDLER_ROUTINE();
415 remove_wth_priv(L, wth);
417 (*fp)->expired = true;
418 (*fc)->expired = true;
419 lua_settop(L,0);
421 return;
424 /* Classic wtap sequential close function, called by wtap core.
426 static void
427 wslua_filehandler_sequential_close(wtap *wth)
429 FileHandler fh = (FileHandler)(wth->wslua_data);
430 lua_State* L = NULL;
431 File *fp = NULL;
432 CaptureInfo *fc = NULL;
434 INIT_FILEHANDLER_ROUTINE(seq_read_close,,NULL,NULL);
436 fp = push_File(L, wth->fh);
437 fc = push_CaptureInfo(L, wth, false);
439 switch ( lua_pcall(L,2,1,1) ) {
440 case 0:
441 break;
442 CASE_ERROR("seq_read_close",NULL,NULL)
445 END_FILEHANDLER_ROUTINE();
447 (*fp)->expired = true;
448 (*fc)->expired = true;
449 lua_settop(L,0);
451 return;
455 /* basically a dummy function to use for can_write_encap so that the caller calls
456 * wslua_can_write_encap instead (which will be wslua_filehandler_can_write_encap)
458 static int
459 wslua_dummy_can_write_encap(int encap _U_)
461 return WTAP_ERR_CHECK_WSLUA;
464 /* Similar to the classic wtap can_write_encap function.
465 * This returns 0 if the encap is ok for this file type.
467 static int
468 wslua_filehandler_can_write_encap(int encap, void* data)
470 FileHandler fh = (FileHandler)(data);
471 int retval = WTAP_ERR_UNWRITABLE_ENCAP;
472 lua_State* L = NULL;
474 INIT_FILEHANDLER_ROUTINE(can_write_encap,WTAP_ERR_UNWRITABLE_ENCAP,NULL,NULL);
476 lua_pushinteger(L, encap);
478 switch ( lua_pcall(L,1,1,1) ) {
479 case 0:
480 retval = wslua_optboolint(L,-1,WTAP_ERR_UNWRITABLE_ENCAP);
481 break;
482 CASE_ERROR("can_write_encap",NULL,NULL)
485 END_FILEHANDLER_ROUTINE();
487 /* the retval we got was either a 1 for true, 0 for false, or WTAP_ERR_UNWRITABLE_ENCAP;
488 but can_write_encap() expects 0 to be true/yes */
489 if (retval == 1) {
490 retval = 0;
491 } else if (retval == 0) {
492 retval = WTAP_ERR_UNWRITABLE_ENCAP;
495 return retval;
498 /* some declarations */
499 static bool
500 wslua_filehandler_dump(wtap_dumper *wdh, const wtap_rec *rec,
501 const uint8_t *pd, int *err, char **err_info);
502 static bool
503 wslua_filehandler_dump_finish(wtap_dumper *wdh, int *err, char **err_info);
506 /* The classic wtap dump_open function.
507 * This returns true on success.
509 static bool
510 wslua_filehandler_dump_open(wtap_dumper *wdh, int *err, char **err_info)
512 FileHandler fh = (FileHandler)(wdh->wslua_data);
513 bool retval = false;
514 lua_State* L = NULL;
515 File *fp = NULL;
516 CaptureInfoConst *fc = NULL;
518 INIT_FILEHANDLER_ROUTINE(write_open,0,err,err_info);
520 create_wdh_priv(L, wdh);
522 fp = push_Wdh(L, wdh);
523 fc = push_CaptureInfoConst(L,wdh);
525 /* Reset err */
526 if (err) {
527 *err = 0;
530 switch ( lua_pcall(L,2,1,1) ) {
531 case 0:
532 retval = wslua_optboolint(L,-1,0);
533 break;
534 CASE_ERROR("write_open",err,err_info)
537 END_FILEHANDLER_ROUTINE();
539 (*fp)->expired = true;
540 (*fc)->expired = true;
542 if (retval == true) {
543 /* this is our file type - set the routines and settings into wtap */
545 if (fh->write_ref != LUA_NOREF) {
546 wdh->subtype_write = wslua_filehandler_dump;
548 else {
549 ws_warning("FileHandler was not set with a write function, even though write_open() returned true");
550 return false;
553 /* it's ok to not have a finish routine */
554 if (fh->write_close_ref != LUA_NOREF)
555 wdh->subtype_finish = wslua_filehandler_dump_finish;
556 else
557 wdh->subtype_finish = NULL;
559 else {
560 /* not our file type? */
561 remove_wdh_priv(L, wdh);
564 return retval;
567 /* The classic wtap dump routine. This returns true if it writes the current
568 * packet, else false.
570 static bool
571 wslua_filehandler_dump(wtap_dumper *wdh, const wtap_rec *rec,
572 const uint8_t *pd, int *err, char **err_info)
574 FileHandler fh = (FileHandler)(wdh->wslua_data);
575 int retval = -1;
576 lua_State* L = NULL;
577 File *fp = NULL;
578 CaptureInfoConst *fc = NULL;
579 FrameInfoConst *fi = NULL;
581 INIT_FILEHANDLER_ROUTINE(write,false,err,err_info);
583 /* Reset errno */
584 if (err) {
585 *err = errno = 0;
588 fp = push_Wdh(L, wdh);
589 fc = push_CaptureInfoConst(L,wdh);
590 fi = push_FrameInfoConst(L, rec, pd);
592 errno = WTAP_ERR_CANT_WRITE;
593 switch ( lua_pcall(L,3,1,1) ) {
594 case 0:
595 retval = wslua_optboolint(L,-1,0);
596 break;
597 CASE_ERROR("write",err,err_info)
600 END_FILEHANDLER_ROUTINE();
602 (*fp)->expired = true;
603 (*fc)->expired = true;
604 (*fi)->expired = true;
606 return (retval == 1);
609 /* The classic wtap dump_finish routine. This returns true if it
610 * writes out the last information cleanly, else false.
612 static bool
613 wslua_filehandler_dump_finish(wtap_dumper *wdh, int *err, char **err_info)
615 FileHandler fh = (FileHandler)(wdh->wslua_data);
616 int retval = -1;
617 lua_State* L = NULL;
618 File *fp = NULL;
619 CaptureInfoConst *fc = NULL;
621 INIT_FILEHANDLER_ROUTINE(write_close,false,err,err_info);
623 /* Reset errno */
624 if (err) {
625 *err = errno = 0;
628 fp = push_Wdh(L, wdh);
629 fc = push_CaptureInfoConst(L,wdh);
631 errno = WTAP_ERR_CANT_CLOSE;
632 switch ( lua_pcall(L,2,1,1) ) {
633 case 0:
634 retval = wslua_optboolint(L,-1,0);
635 break;
636 CASE_ERROR("write_close",err,err_info)
639 END_FILEHANDLER_ROUTINE();
641 remove_wdh_priv(L, wdh);
643 (*fp)->expired = true;
644 (*fc)->expired = true;
646 return (retval == 1);
650 * Prototype table of option support.
651 * We start out saying we don't support comments, and we don't mention
652 * other options.
654 static const struct supported_option_type option_type_proto[] = {
655 { OPT_COMMENT, OPTION_NOT_SUPPORTED }
659 * Prototype table of block type support.
660 * We start out saying we only support packets.
662 static const struct supported_block_type block_type_proto[] = {
663 { WTAP_BLOCK_SECTION, BLOCK_NOT_SUPPORTED, 0, NULL },
664 { WTAP_BLOCK_IF_ID_AND_INFO, BLOCK_NOT_SUPPORTED, 0, NULL },
665 { WTAP_BLOCK_NAME_RESOLUTION, BLOCK_NOT_SUPPORTED, 0, NULL },
666 { WTAP_BLOCK_IF_STATISTICS, BLOCK_NOT_SUPPORTED, 0, NULL },
667 { WTAP_BLOCK_DECRYPTION_SECRETS, BLOCK_NOT_SUPPORTED, 0, NULL },
668 { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, 0, NULL },
669 { WTAP_BLOCK_FT_SPECIFIC_REPORT, BLOCK_NOT_SUPPORTED, 0, NULL },
670 { WTAP_BLOCK_FT_SPECIFIC_EVENT, BLOCK_NOT_SUPPORTED, 0, NULL }
673 #define NUM_LISTED_BLOCK_TYPES array_length(block_type_proto)
675 WSLUA_CONSTRUCTOR FileHandler_new(lua_State* L) {
676 /* Creates a new FileHandler */
677 #define WSLUA_ARG_FileHandler_new_DESCRIPTION 1 /* A description of the file type, for display purposes only. E.g., "Wireshark - pcapng" */
678 #define WSLUA_ARG_FileHandler_new_NAME 2 /* The file type name, used to look up the file type in various places. E.g., "pcapng". Note: The name cannot already be in use. */
679 #define WSLUA_ARG_FileHandler_new_INTERNAL_DESCRIPTION 3 /* Descriptive text about this file format, for internal display purposes only */
680 #define WSLUA_ARG_FileHandler_new_TYPE 4 /* The type of FileHandler, "r"/"w"/"rw" for reader/writer/both, include "m" for magic, "s" for strong heuristic */
682 const char* description = luaL_checkstring(L,WSLUA_ARG_FileHandler_new_DESCRIPTION);
683 const char* name = luaL_checkstring(L,WSLUA_ARG_FileHandler_new_NAME);
684 const char* internal_description = luaL_checkstring(L,WSLUA_ARG_FileHandler_new_INTERNAL_DESCRIPTION);
685 const char* type = luaL_checkstring(L,WSLUA_ARG_FileHandler_new_TYPE);
686 FileHandler fh = (FileHandler) g_malloc0(sizeof(struct _wslua_filehandler));
687 struct supported_block_type *supported_blocks;
689 fh->is_reader = (strchr(type,'r') != NULL) ? true : false;
690 fh->is_writer = (strchr(type,'w') != NULL) ? true : false;
692 if (fh->is_reader && wtap_has_open_info(name)) {
693 g_free(fh);
694 return luaL_error(L, "FileHandler.new: '%s' name already exists for a reader!", name);
697 if (fh->is_writer && wtap_name_to_file_type_subtype(name) > -1) {
698 g_free(fh);
699 return luaL_error(L, "FileHandler.new: '%s' name already exists for a writer!", name);
702 fh->type = g_strdup(type);
703 fh->extensions = NULL;
704 fh->finfo.description = g_strdup(description);
705 fh->finfo.name = g_strdup(name);
706 fh->finfo.default_file_extension = NULL;
707 fh->finfo.additional_file_extensions = NULL;
708 fh->finfo.writing_must_seek = false;
709 supported_blocks = (struct supported_block_type *)g_memdup2(&block_type_proto, sizeof block_type_proto);
711 * Add a list of options to the seciton block, interface block, and
712 * packet block, so the file handler can indicate comment support.
714 for (size_t i = 0; i < NUM_LISTED_BLOCK_TYPES; i++) {
715 switch (supported_blocks[i].type) {
717 case WTAP_BLOCK_SECTION:
718 case WTAP_BLOCK_IF_ID_AND_INFO:
719 case WTAP_BLOCK_PACKET:
720 supported_blocks[i].num_supported_options = OPTION_TYPES_SUPPORTED(option_type_proto);
721 supported_blocks[i].supported_options = (struct supported_option_type *)g_memdup2(&option_type_proto, sizeof option_type_proto);
722 break;
724 default:
725 break;
728 fh->finfo.num_supported_blocks = NUM_LISTED_BLOCK_TYPES;
729 fh->finfo.supported_blocks = supported_blocks;
730 fh->finfo.can_write_encap = NULL;
731 fh->finfo.dump_open = NULL;
732 /* this will be set to a new file_type when registered */
733 fh->file_type = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
735 fh->internal_description = g_strdup(internal_description);
736 fh->L = L;
737 fh->read_open_ref = LUA_NOREF;
738 fh->read_ref = LUA_NOREF;
739 fh->seek_read_ref = LUA_NOREF;
740 fh->read_close_ref = LUA_NOREF;
741 fh->seq_read_close_ref = LUA_NOREF;
742 fh->write_open_ref = LUA_NOREF;
743 fh->write_ref = LUA_NOREF;
744 fh->write_close_ref = LUA_NOREF;
745 fh->can_write_encap_ref = LUA_NOREF;
747 fh->registered = false;
749 pushFileHandler(L,fh);
750 WSLUA_RETURN(1); /* The newly created FileHandler object */
753 WSLUA_METAMETHOD FileHandler__tostring(lua_State* L) {
754 /* Generates a string of debug info for the FileHandler */
755 FileHandler fh = toFileHandler(L,1);
757 if (!fh) {
758 lua_pushstring(L,"FileHandler pointer is NULL!");
759 } else {
760 lua_pushfstring(L, "FileHandler(%s): description='%s', internal description='%s', read_open=%d, read=%d, write=%d",
761 fh->finfo.name, fh->finfo.description, fh->internal_description, fh->read_open_ref, fh->read_ref, fh->write_ref);
764 WSLUA_RETURN(1); /* String of debug information. */
767 static int FileHandler__gc(lua_State* L _U_) {
768 /* do NOT free FileHandler, it's never free'd */
769 /* TODO: handle this and other Lua things that should be free'd on exit, in a better way */
770 return 0;
773 /* A Lua File handler must not be expired, and must be either a reader or writer, and
774 * a *reader* one MUST at least define read_open, read, and seek_read funcs; and
775 * a *writer* one MUST at least define can_write_encap, write_open, and write funcs
777 static bool verify_filehandler_complete(FileHandler fh) {
778 return ((fh->is_reader || fh->is_writer) &&
779 (!fh->is_reader ||
780 (fh->is_reader &&
781 fh->read_open_ref != LUA_NOREF &&
782 fh->read_ref != LUA_NOREF)) &&
783 (!fh->is_writer ||
784 (fh->is_writer &&
785 fh->can_write_encap_ref != LUA_NOREF &&
786 fh->write_open_ref != LUA_NOREF &&
787 fh->write_ref != LUA_NOREF)) );
791 WSLUA_FUNCTION wslua_register_filehandler(lua_State* L) {
792 /* Register the FileHandler into Wireshark/TShark, so they can read/write this new format.
793 All functions and settings must be complete before calling this registration function.
794 This function cannot be called inside the reading/writing callback functions. */
795 #define WSLUA_ARG_register_filehandler_FILEHANDLER 1 /* The FileHandler object to be registered */
796 FileHandler fh = checkFileHandler(L,WSLUA_ARG_register_filehandler_FILEHANDLER);
798 if (in_routine)
799 return luaL_error(L,"a FileHandler cannot be registered during reading/writing callback functions");
801 if (fh->registered)
802 return luaL_error(L,"this FileHandler is already registered");
804 if (!verify_filehandler_complete(fh))
805 return luaL_error(L,"this FileHandler is not complete enough to register");
807 if (fh->is_writer) {
808 if (fh->extensions && fh->extensions[0]) {
809 char *extension = g_strdup(fh->extensions);
810 char *extra_extensions = strchr(extension, ';');
811 if (extra_extensions) {
812 /* Split "cap;pcap" -> "cap" and "pcap" */
813 *extra_extensions++ = '\0';
815 fh->finfo.default_file_extension = extension;
816 fh->finfo.additional_file_extensions = extra_extensions;
818 fh->finfo.can_write_encap = wslua_dummy_can_write_encap;
819 fh->finfo.wslua_info = g_new0(wtap_wslua_file_info_t, 1);
820 fh->finfo.wslua_info->wslua_can_write_encap = wslua_filehandler_can_write_encap;
821 fh->finfo.wslua_info->wslua_data = (void*)(fh);
822 fh->finfo.dump_open = wslua_filehandler_dump_open;
825 fh->file_type = wtap_register_file_type_subtype(&(fh->finfo));
827 if (fh->is_reader) {
828 struct open_info oi = { NULL, OPEN_INFO_HEURISTIC, NULL, NULL, NULL, NULL };
829 oi.name = fh->finfo.name;
830 oi.open_routine = wslua_filehandler_open;
831 oi.extensions = fh->extensions;
832 oi.wslua_data = (void*)(fh);
833 if (strchr(fh->type,'m') != NULL) {
834 oi.type = OPEN_INFO_MAGIC;
835 } else {
836 oi.type = OPEN_INFO_HEURISTIC;
838 wtap_register_open_info(&oi, (strchr(fh->type,'s') != NULL));
841 fh->registered = true;
842 registered_file_handlers = g_slist_prepend(registered_file_handlers, fh);
844 lua_pushinteger(L, fh->file_type);
846 WSLUA_RETURN(1); /* the new type number for this file reader/write */
849 static void
850 wslua_deregister_filehandler_work(FileHandler fh)
852 /* undo writing stuff, even if it wasn't a writer */
853 fh->finfo.can_write_encap = NULL;
854 if (fh->finfo.wslua_info) {
855 fh->finfo.wslua_info->wslua_can_write_encap = NULL;
856 fh->finfo.wslua_info->wslua_data = NULL;
857 g_free(fh->finfo.wslua_info);
858 fh->finfo.wslua_info = NULL;
860 g_free((char *)fh->finfo.default_file_extension);
861 fh->finfo.default_file_extension = NULL;
862 fh->finfo.additional_file_extensions = NULL;
863 fh->finfo.dump_open = NULL;
865 if (fh->file_type != WTAP_FILE_TYPE_SUBTYPE_UNKNOWN) {
866 wtap_deregister_file_type_subtype(fh->file_type);
869 if (fh->is_reader && wtap_has_open_info(fh->finfo.name)) {
870 wtap_deregister_open_info(fh->finfo.name);
873 fh->registered = false;
876 WSLUA_FUNCTION wslua_deregister_filehandler(lua_State* L) {
877 /* Deregister the FileHandler from Wireshark/TShark, so it no longer gets used for reading/writing/display.
878 This function cannot be called inside the reading/writing callback functions. */
879 #define WSLUA_ARG_deregister_filehandler_FILEHANDLER 1 /* The FileHandler object to be deregistered */
880 FileHandler fh = checkFileHandler(L,WSLUA_ARG_deregister_filehandler_FILEHANDLER);
882 if (in_routine)
883 return luaL_error(L,"A FileHandler cannot be deregistered during reading/writing callback functions");
885 if (!fh->registered)
886 return 0;
888 wslua_deregister_filehandler_work(fh);
889 registered_file_handlers = g_slist_remove(registered_file_handlers, fh);
891 return 0;
894 /* The following macros generate setter functions for Lua, for the following Lua
895 function references in _wslua_filehandler struct:
896 int read_open_ref;
897 int read_ref;
898 int seek_read_ref;
899 int read_close_ref;
900 int seq_read_close_ref;
901 int can_write_encap_ref;
902 int write_open_ref;
903 int write_ref;
904 int write_close_ref;
907 /* WSLUA_ATTRIBUTE FileHandler_read_open WO The Lua function to be called when Wireshark opens a file for reading.
909 When later called by Wireshark, the Lua function will be given:
910 1. A `File` object
911 2. A `CaptureInfo` object
913 The purpose of the Lua function set to this `read_open` field is to check if the file Wireshark is opening is of its type,
914 for example by checking for magic numbers or trying to parse records in the file, etc. The more can be verified
915 the better, because Wireshark tries all file readers until it finds one that accepts the file, so accepting an
916 incorrect file prevents other file readers from reading their files.
918 The called Lua function should return true if the file is its type (it accepts it), false if not. The Lua
919 function must also set the File offset position (using `file:seek()`) to where it wants it to be for its first
920 `read()` call.
922 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,read_open);
924 /* WSLUA_ATTRIBUTE FileHandler_read WO The Lua function to be called when Wireshark wants to read a packet from the file.
926 When later called by Wireshark, the Lua function will be given:
927 1. A `File` object
928 2. A `CaptureInfo` object
929 3. A `FrameInfo` object
931 The purpose of the Lua function set to this `read` field is to read the next packet from the file, and setting the parsed/read
932 packet into the frame buffer using `FrameInfo.data = foo` or `FrameInfo:read_data(file, frame.captured_length)`.
934 The called Lua function should return the file offset/position number where the packet begins, or false if it hit an
935 error. The file offset will be saved by Wireshark and passed into the set `seek_read()` Lua function later.
937 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,read);
939 /* WSLUA_ATTRIBUTE FileHandler_seek_read WO The Lua function to be called when Wireshark wants to read a packet from the file at the given offset.
941 When later called by Wireshark, the Lua function will be given:
942 1. A `File` object
943 2. A `CaptureInfo` object
944 3. A `FrameInfo` object
945 4. The file offset number previously set by the `read()` function call
947 The called Lua function should return true if the read was successful, or false if it hit an error.
948 Since 2.4.0, a number is also acceptable to signal success, this allows for reuse of `FileHandler:read`:
950 [source,lua]
951 ----
952 local function fh_read(file, capture, frame) ... end
953 myfilehandler.read = fh_read
955 function myfilehandler.seek_read(file, capture, frame, offset)
956 if not file:seek("set", offset) then
957 -- Seeking failed, return failure
958 return false
961 -- Now try to read one frame
962 return fh_read(file, capture, frame)
964 ----
966 Since 3.6.0, it's possible to omit the `FileHandler:seek_read()` function to get a default seek_read implementation.
968 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,seek_read);
970 /* WSLUA_ATTRIBUTE FileHandler_read_close WO The Lua function to be called when Wireshark wants to close the read file completely.
972 When later called by Wireshark, the Lua function will be given:
973 1. A `File` object
974 2. A `CaptureInfo` object
976 It is not necessary to set this field to a Lua function - FileHandler can be registered without doing so - it
977 is available in case there is memory/state to clear in your script when the file is closed. */
978 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,read_close);
980 /* WSLUA_ATTRIBUTE FileHandler_seq_read_close WO The Lua function to be called when Wireshark wants to close the sequentially-read file.
982 When later called by Wireshark, the Lua function will be given:
983 1. A `File` object
984 2. A `CaptureInfo` object
986 It is not necessary to set this field to a Lua
987 function - FileHandler can be registered without doing so - it is available in case there is memory/state to clear in your script
988 when the file is closed for the sequential reading portion. After this point, there will be no more calls to `read()`, only `seek_read()`. */
989 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,seq_read_close);
992 /* WSLUA_ATTRIBUTE FileHandler_can_write_encap WO The Lua function to be called when Wireshark wants to write a file,
993 by checking if this file writer can handle the wtap packet encapsulation(s).
995 When later called by Wireshark, the Lua function will be given a Lua number, which matches one of the encapsulations
996 in the Lua `wtap_encaps` table. This might be the `wtap_encap.PER_PACKET` number, meaning the capture contains multiple
997 encapsulation types, and the file reader should only return true if it can handle multiple encap types in one file. The
998 function will then be called again, once for each encap type in the file, to make sure it can write each one.
1000 If the Lua file writer can write the given type of encapsulation into a file, then it returns the boolean true, else false. */
1001 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,can_write_encap);
1003 /* WSLUA_ATTRIBUTE FileHandler_write_open WO The Lua function to be called when Wireshark opens a file for writing.
1005 When later called by Wireshark, the Lua function will be given:
1006 1. A `File` object
1007 2. A `CaptureInfoConst` object
1009 The purpose of the Lua function set to this `write_open` field is similar to the read_open callback function:
1010 to initialize things necessary for writing the capture to a file. For example, if the output file format has a
1011 file header, then the file header should be written within this write_open function.
1013 The called Lua function should return true on success, or false if it hit an error.
1015 Also make sure to set the `FileHandler.write` (and potentially `FileHandler.write_finish`) functions before
1016 returning true from this function. */
1017 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,write_open);
1019 /* WSLUA_ATTRIBUTE FileHandler_write WO The Lua function to be called when Wireshark wants to write a packet to the file.
1021 When later called by Wireshark, the Lua function will be given:
1022 1. A `File` object
1023 2. A `CaptureInfoConst` object
1024 3. A `FrameInfoConst` object of the current frame/packet to be written
1026 The purpose of the Lua function set to this `write` field is to write the next packet to the file.
1028 The called Lua function should return true on success, or false if it hit an error. */
1029 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,write);
1031 /* WSLUA_ATTRIBUTE FileHandler_write_finish WO The Lua function to be called when Wireshark wants to close the written file.
1033 When later called by Wireshark, the Lua function will be given:
1034 1. A `File` object
1035 2. A `CaptureInfoConst` object
1037 It is not necessary to set this field to a Lua function - `FileHandler` can be registered without doing so - it is available
1038 in case there is memory/state to clear in your script when the file is closed. */
1039 WSLUA_ATTRIBUTE_FUNC_SETTER(FileHandler,write_close);
1041 /* generate other member accessors setters/getters */
1043 /* WSLUA_ATTRIBUTE FileHandler_type RO The internal file type. This is automatically set with a new
1044 number when the FileHandler is registered. */
1045 WSLUA_ATTRIBUTE_NAMED_INTEGER_GETTER(FileHandler,type,file_type);
1047 /* WSLUA_ATTRIBUTE FileHandler_extensions RW One or more semicolon-separated file extensions that this file type usually uses.
1049 For readers using heuristics to determine file type, Wireshark will try the readers of the file's
1050 extension first, before trying other readers. But ultimately Wireshark tries all file readers
1051 for any file extension, until it finds one that accepts the file.
1053 (Since 2.6) For writers, the first extension is used to suggest the default file extension. */
1054 WSLUA_ATTRIBUTE_STRING_GETTER(FileHandler,extensions);
1055 WSLUA_ATTRIBUTE_STRING_SETTER(FileHandler,extensions,true);
1057 /* WSLUA_ATTRIBUTE FileHandler_writing_must_seek RW True if the ability to seek is required when writing
1058 this file format, else false.
1060 This will be checked by Wireshark when writing out to compressed
1061 file formats, because seeking is not possible with compressed files. Usually a file writer only
1062 needs to be able to seek if it needs to go back in the file to change something, such as a block or
1063 file length value earlier in the file. */
1064 WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(FileHandler,writing_must_seek,finfo.writing_must_seek);
1065 WSLUA_ATTRIBUTE_NAMED_BOOLEAN_SETTER(FileHandler,writing_must_seek,finfo.writing_must_seek);
1067 /* WSLUA_ATTRIBUTE FileHandler_writes_name_resolution RW True if the file format supports name resolution
1068 records, else false. */
1069 static inline struct supported_block_type *
1070 safe_cast_away_block_type_const(const struct supported_block_type *arg)
1073 * Cast away constness without a warning; we know we can do this
1074 * because, for Lua file handlers, the table of supported block
1075 * types is in allocated memory, so that we *can* modify it.
1077 * The pointer in the file_type_subtype_info structure is a
1078 * pointer to const because compiled file handlers will
1079 * normally set it to point to a static const structure.
1081 DIAG_OFF_CAST_AWAY_CONST
1082 return (struct supported_block_type *)arg;
1083 DIAG_ON_CAST_AWAY_CONST
1086 WSLUA_ATTRIBUTE_GET(FileHandler,writes_name_resolution,{ \
1087 bool supports_name_resolution = false; \
1088 for (size_t i = 0; i < obj->finfo.num_supported_blocks; i++) { \
1089 /* \
1090 * If WTAP_BLOCK_NAME_RESOLUTION is supported, name \
1091 * resolution is supported. \
1092 */ \
1093 if (obj->finfo.supported_blocks[i].type == WTAP_BLOCK_NAME_RESOLUTION) { \
1094 supports_name_resolution = (obj->finfo.supported_blocks[i].support != BLOCK_NOT_SUPPORTED); \
1095 break; \
1098 lua_pushboolean(L, supports_name_resolution); \
1100 WSLUA_ATTRIBUTE_SET(FileHandler,writes_name_resolution, { \
1101 bool supports_name_resolution; \
1102 if (!lua_isboolean(L,-1) ) \
1103 return luaL_error(L, "FileHandler's attribute`writes_name_resolution' must be a boolean"); \
1104 supports_name_resolution = lua_toboolean(L,-1); \
1105 /* \
1106 * Update support for WTAP_BLOCK_NAME_RESOLUTION; the entry for \
1107 * it should be there. \
1108 */ \
1109 for (size_t i = 0; i < obj->finfo.num_supported_blocks; i++) { \
1110 if (obj->finfo.supported_blocks[i].type == WTAP_BLOCK_NAME_RESOLUTION) { \
1111 struct supported_block_type *supported_blocks;
1112 supported_blocks = safe_cast_away_block_type_const(obj->finfo.supported_blocks); \
1114 supported_blocks[i].support = supports_name_resolution ? ONE_BLOCK_SUPPORTED : BLOCK_NOT_SUPPORTED; \
1115 break; \
1120 /* WSLUA_ATTRIBUTE FileHandler_supported_comment_types RW Set to the bit-wise OR'ed number representing
1121 the type of comments the file writer supports writing, based on the numbers in the `wtap_comments` table. */
1122 static inline struct supported_option_type *
1123 safe_cast_away_option_type_const(const struct supported_option_type *arg)
1126 * Cast away constness without a warning; we know we can do this
1127 * because, for Lua file handlers, the table of supported option
1128 * types is in allocated memory, so that we *can* modify it.
1130 * The pointer in the file_type_subtype_info structure is a
1131 * pointer to const because compiled file handlers will
1132 * normally set it to point to a static const structure.
1134 DIAG_OFF_CAST_AWAY_CONST
1135 return (struct supported_option_type *)arg;
1136 DIAG_ON_CAST_AWAY_CONST
1139 WSLUA_ATTRIBUTE_GET(FileHandler,supported_comment_types,{ \
1140 unsigned supported_comment_types = 0; \
1141 for (size_t i = 0; i < obj->finfo.num_supported_blocks; i++) { \
1142 size_t num_supported_options; \
1143 const struct supported_option_type *supported_options;
1145 /* \
1146 * Is this block type supported? \
1147 */ \
1148 if (obj->finfo.supported_blocks[i].support == BLOCK_NOT_SUPPORTED) { \
1149 /* \
1150 * No - skip it. \
1151 */ \
1152 continue; \
1155 /* \
1156 * Yes - what type of block is it? \
1157 */ \
1158 switch (obj->finfo.supported_blocks[i].type) { \
1160 case WTAP_BLOCK_SECTION: \
1161 /* \
1162 * Section block - does this block type support comments? \
1163 */ \
1164 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1165 supported_options = obj->finfo.supported_blocks[i].supported_options; \
1166 for (size_t j = 0; j < num_supported_options; i++) { \
1167 if (supported_options[i].opt == OPT_COMMENT) { \
1168 if (supported_options[i].support != OPTION_NOT_SUPPORTED) \
1169 supported_comment_types |= WTAP_COMMENT_PER_SECTION; \
1170 break; \
1173 break; \
1175 case WTAP_BLOCK_IF_ID_AND_INFO: \
1176 /* \
1177 * Interface block - does this block type support comments? \
1178 */ \
1179 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1180 supported_options = obj->finfo.supported_blocks[i].supported_options; \
1181 for (size_t j = 0; j < num_supported_options; i++) { \
1182 if (supported_options[i].opt == OPT_COMMENT) { \
1183 if (supported_options[i].support != OPTION_NOT_SUPPORTED) \
1184 supported_comment_types |= WTAP_COMMENT_PER_INTERFACE; \
1185 break; \
1188 break; \
1190 case WTAP_BLOCK_PACKET: \
1191 /* \
1192 * Packet block - does this block type support comments? \
1193 */ \
1194 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1195 supported_options = obj->finfo.supported_blocks[i].supported_options; \
1196 for (size_t j = 0; j < num_supported_options; i++) { \
1197 if (supported_options[i].opt == OPT_COMMENT) { \
1198 if (supported_options[i].support != OPTION_NOT_SUPPORTED) \
1199 supported_comment_types |= WTAP_COMMENT_PER_PACKET; \
1200 break; \
1203 break; \
1205 default: \
1206 break;\
1209 lua_pushinteger(L, (lua_Integer)supported_comment_types); \
1211 WSLUA_ATTRIBUTE_SET(FileHandler,supported_comment_types, { \
1212 unsigned supported_comment_types; \
1213 size_t num_supported_options; \
1214 struct supported_option_type *supported_options; \
1215 if (!lua_isnumber(L,-1) ) \
1216 return luaL_error(L, "FileHandler's attribute`supported_comment_types' must be a number"); \
1217 supported_comment_types = wslua_touint(L,-1); \
1218 /* \
1219 * Update support for comments in the relevant block types; the entries \
1220 * for comments in those types should be there. \
1221 */ \
1222 for (size_t i = 0; i < obj->finfo.num_supported_blocks; i++) { \
1224 /* \
1225 * Is this block type supported? \
1226 */ \
1227 if (obj->finfo.supported_blocks[i].support == BLOCK_NOT_SUPPORTED) { \
1228 /* \
1229 * No - skip it. \
1230 */ \
1231 continue; \
1234 /* \
1235 * Yes - what type of block is it? \
1236 */ \
1237 switch (obj->finfo.supported_blocks[i].type) { \
1239 case WTAP_BLOCK_SECTION: \
1240 /* \
1241 * Section block - update the comment support. \
1242 */ \
1243 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1244 supported_options = safe_cast_away_option_type_const(obj->finfo.supported_blocks[i].supported_options); \
1245 for (size_t j = 0; j < num_supported_options; i++) { \
1246 if (supported_options[i].opt == OPT_COMMENT) { \
1247 supported_options[i].support = \
1248 (supported_comment_types &= WTAP_COMMENT_PER_SECTION) ? \
1249 ONE_OPTION_SUPPORTED : OPTION_NOT_SUPPORTED ; \
1250 break; \
1253 break; \
1255 case WTAP_BLOCK_IF_ID_AND_INFO: \
1256 /* \
1257 * Interface block - does this block type support comments? \
1258 */ \
1259 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1260 supported_options = safe_cast_away_option_type_const(obj->finfo.supported_blocks[i].supported_options); \
1261 for (size_t j = 0; j < num_supported_options; i++) { \
1262 if (supported_options[i].opt == OPT_COMMENT) { \
1263 supported_options[i].support = \
1264 (supported_comment_types &= WTAP_COMMENT_PER_INTERFACE) ? \
1265 ONE_OPTION_SUPPORTED : OPTION_NOT_SUPPORTED ; \
1266 break; \
1269 break; \
1271 case WTAP_BLOCK_PACKET: \
1272 /* \
1273 * Packet block - does this block type support comments? \
1274 */ \
1275 num_supported_options = obj->finfo.supported_blocks[i].num_supported_options; \
1276 supported_options = safe_cast_away_option_type_const(obj->finfo.supported_blocks[i].supported_options); \
1277 for (size_t j = 0; j < num_supported_options; i++) { \
1278 if (supported_options[i].opt == OPT_COMMENT) { \
1279 supported_options[i].support = \
1280 (supported_comment_types &= WTAP_COMMENT_PER_PACKET) ? \
1281 ONE_OPTION_SUPPORTED : OPTION_NOT_SUPPORTED ; \
1282 break; \
1285 break; \
1287 default: \
1288 break;\
1293 /* This table is ultimately registered as a sub-table of the class' metatable,
1294 * and if __index/__newindex is invoked then it calls the appropriate function
1295 * from this table for getting/setting the members.
1297 WSLUA_ATTRIBUTES FileHandler_attributes[] = {
1298 WSLUA_ATTRIBUTE_WOREG(FileHandler,read_open),
1299 WSLUA_ATTRIBUTE_WOREG(FileHandler,read),
1300 WSLUA_ATTRIBUTE_WOREG(FileHandler,seek_read),
1301 WSLUA_ATTRIBUTE_WOREG(FileHandler,read_close),
1302 WSLUA_ATTRIBUTE_WOREG(FileHandler,seq_read_close),
1303 WSLUA_ATTRIBUTE_WOREG(FileHandler,can_write_encap),
1304 WSLUA_ATTRIBUTE_WOREG(FileHandler,write_open),
1305 WSLUA_ATTRIBUTE_WOREG(FileHandler,write),
1306 WSLUA_ATTRIBUTE_WOREG(FileHandler,write_close),
1307 WSLUA_ATTRIBUTE_ROREG(FileHandler,type),
1308 WSLUA_ATTRIBUTE_RWREG(FileHandler,extensions),
1309 WSLUA_ATTRIBUTE_RWREG(FileHandler,writing_must_seek),
1310 WSLUA_ATTRIBUTE_RWREG(FileHandler,writes_name_resolution),
1311 WSLUA_ATTRIBUTE_RWREG(FileHandler,supported_comment_types),
1312 { NULL, NULL, NULL }
1315 WSLUA_METHODS FileHandler_methods[] = {
1316 WSLUA_CLASS_FNREG(FileHandler,new),
1317 { NULL, NULL }
1320 WSLUA_META FileHandler_meta[] = {
1321 WSLUA_CLASS_MTREG(FileHandler,tostring),
1322 { NULL, NULL }
1325 int FileHandler_register(lua_State* L) {
1326 WSLUA_REGISTER_CLASS_WITH_ATTRS(FileHandler);
1327 return 0;
1330 int wslua_deregister_filehandlers(lua_State* L _U_) {
1331 for (GSList *it = registered_file_handlers; it; it = it->next) {
1332 FileHandler fh = (FileHandler)it->data;
1333 wslua_deregister_filehandler_work(fh);
1335 for (size_t i = 0; i < fh->finfo.num_supported_blocks; i++) {
1336 g_free((struct supported_option_type *)fh->finfo.supported_blocks[i].supported_options);
1338 g_free((struct supported_block_type *)fh->finfo.supported_blocks);
1339 g_free((char *)fh->extensions);
1340 g_free((char *)fh->internal_description);
1341 g_free((char *)fh->finfo.description);
1342 g_free((char *)fh->finfo.name);
1343 g_free(fh->type);
1345 memset(fh, 0, sizeof(*fh));
1346 fh->removed = true;
1347 proto_add_deregistered_data(fh);
1349 g_slist_free(registered_file_handlers);
1350 registered_file_handlers = NULL;
1351 return 0;
1356 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1358 * Local variables:
1359 * c-basic-offset: 4
1360 * tab-width: 8
1361 * indent-tabs-mode: nil
1362 * End:
1364 * vi: set shiftwidth=4 tabstop=8 expandtab:
1365 * :indentSize=4:tabSize=8:noTabs=true: