2018-08-01 07:01:45 +00:00
|
|
|
/* rfc7468.c
|
2018-04-08 22:15:01 +00:00
|
|
|
*
|
|
|
|
* Implements loading of files in the format specified by RFC 7468.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-08-01 07:01:45 +00:00
|
|
|
#include "rfc7468.h"
|
2018-04-08 22:15:01 +00:00
|
|
|
|
|
|
|
#include "file_wrappers.h"
|
|
|
|
#include "wtap-int.h"
|
|
|
|
|
|
|
|
#include <wsutil/buffer.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* 128 bytes should be enough to contain any line. Strictly speaking, 64 is
|
2020-10-10 23:42:05 +00:00
|
|
|
enough, but we provide some leeway to accommodate nonconformant producers and
|
2018-04-08 22:15:01 +00:00
|
|
|
trailing whitespace. The 2 extra bytes are for the trailing newline and NUL
|
|
|
|
terminator. */
|
|
|
|
#define MAX_LINE_LENGTH (128 + 2)
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static int rfc7468_file_type_subtype = -1;
|
|
|
|
|
|
|
|
void register_rfc7468(void);
|
|
|
|
|
2018-04-08 22:15:01 +00:00
|
|
|
static char *read_complete_text_line(char line[MAX_LINE_LENGTH], FILE_T fh, int *err, gchar **err_info)
|
|
|
|
{
|
|
|
|
char *line_end;
|
|
|
|
|
|
|
|
if (!(line_end = file_getsp(line, MAX_LINE_LENGTH, fh))) {
|
|
|
|
*err = file_error(fh, err_info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(line) != (size_t)(line_end - line)) {
|
2018-08-09 10:04:19 +00:00
|
|
|
*err = 0;
|
2018-04-08 22:15:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_end[-1] != '\n' && !file_eof(fh)) {
|
2018-08-09 10:04:19 +00:00
|
|
|
*err = 0;
|
2018-04-08 22:15:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line_end;
|
|
|
|
}
|
|
|
|
|
2018-08-01 04:57:42 +00:00
|
|
|
//
|
2018-08-01 07:01:45 +00:00
|
|
|
// Arbitrary value - we don't want to read all of a huge non-RFC 7468 file
|
2018-08-01 04:57:42 +00:00
|
|
|
// only to find no pre-encapsulation boundary.
|
|
|
|
//
|
|
|
|
#define MAX_EXPLANATORY_TEXT_LINES 20
|
2018-04-08 22:15:01 +00:00
|
|
|
|
2018-08-01 07:01:45 +00:00
|
|
|
wtap_open_return_val rfc7468_open(wtap *wth, int *err, gchar **err_info)
|
2018-04-08 22:15:01 +00:00
|
|
|
{
|
2018-08-01 04:57:42 +00:00
|
|
|
gboolean found_preeb;
|
|
|
|
static const char preeb_begin[] = "-----BEGIN ";
|
|
|
|
char line[MAX_LINE_LENGTH];
|
2018-04-08 22:15:01 +00:00
|
|
|
|
2018-08-01 04:57:42 +00:00
|
|
|
//
|
|
|
|
// Skip up to MAX_EXPLANATORY_TEXT_LINES worth of lines that don't
|
|
|
|
// look like pre-encapsulation boundaries.
|
|
|
|
//
|
|
|
|
found_preeb = FALSE;
|
|
|
|
for (unsigned int i = 0; i < MAX_EXPLANATORY_TEXT_LINES; i++) {
|
|
|
|
if (!read_complete_text_line(line, wth->fh, err, err_info)) {
|
2018-08-09 10:04:19 +00:00
|
|
|
if (*err == 0 || *err == WTAP_ERR_SHORT_READ)
|
2018-08-01 04:57:42 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
2018-04-08 22:15:01 +00:00
|
|
|
|
2018-08-01 04:57:42 +00:00
|
|
|
// Does the line look like a pre-encapsulation boundary?
|
|
|
|
if (memcmp(line, preeb_begin, sizeof preeb_begin - 1) == 0) {
|
|
|
|
// Yes.
|
|
|
|
found_preeb = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_preeb)
|
2018-04-08 22:15:01 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
|
|
|
|
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
wth->file_type_subtype = rfc7468_file_type_subtype;
|
2018-08-01 04:57:42 +00:00
|
|
|
wth->file_encap = WTAP_ENCAP_RFC7468;
|
2018-04-08 22:15:01 +00:00
|
|
|
|
|
|
|
wth->snapshot_length = 0;
|
|
|
|
wth->file_tsprec = WTAP_TSPREC_SEC;
|
|
|
|
|
2018-11-10 22:03:16 +00:00
|
|
|
wth->subtype_read = wtap_full_file_read;
|
|
|
|
wth->subtype_seek_read = wtap_full_file_seek_read;
|
2018-04-08 22:15:01 +00:00
|
|
|
|
|
|
|
return WTAP_OPEN_MINE;
|
|
|
|
}
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static const struct file_type_subtype_info rfc7468_info = {
|
|
|
|
"RFC 7468 files", "rfc7468", NULL, NULL,
|
|
|
|
FALSE, FALSE, 0,
|
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_rfc7468(void)
|
|
|
|
{
|
|
|
|
rfc7468_file_type_subtype = wtap_register_file_type_subtypes(&rfc7468_info,
|
|
|
|
WTAP_FILE_TYPE_SUBTYPE_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2018-04-08 22:15:01 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2018-04-08 22:15:01 +00:00
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|