diff options
author | Neal H. Walfield <neal@pep.foundation> | 2018-09-10 11:00:31 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2018-09-10 11:17:00 +0200 |
commit | da075409e3d0632ee113f4a9c988151799a455da (patch) | |
tree | e351d793fcbf14d98712bff46137336a07af4c9d /ffi/examples | |
parent | 7818f0b843153ff8d681c78a92cde2bb331934fc (diff) |
ffi: Provide an FFI to work with sq_packet_parser_result_t's.
- 4486871 added the sq_packet_parser_result_t to the FFI and fixed
sq_packet_parser_from_reader, etc. to return it instead of a
sq_packet_parser_t. That change didn't provide any methods for
working with sq_packet_parser_result_t's, and it made
ffi/examples/parser.c no longer compile (although parser.c no
longer worked since we changed the PacketParser code to work with
PacketParserResults's in c9ee0ac).
- Add sq_packet_parser_result_packet_parser,
sq_packet_parser_result_eof, and sq_packet_parser_result_free to
be able to work with sq_packet_parser_result_t's.
- Fix parser.c so that it not only compiles, but also works again.
Diffstat (limited to 'ffi/examples')
-rw-r--r-- | ffi/examples/parser.c | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/ffi/examples/parser.c b/ffi/examples/parser.c index 798f01cf..3cceea26 100644 --- a/ffi/examples/parser.c +++ b/ffi/examples/parser.c @@ -23,7 +23,8 @@ main (int argc, char **argv) sq_status_t rc; sq_error_t err; sq_context_t ctx; - sq_packet_parser_t pp, ppo; + sq_packet_parser_result_t ppr; + sq_packet_parser_t pp; if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); @@ -45,30 +46,38 @@ main (int argc, char **argv) if (b == MAP_FAILED) error (1, errno, "mmap"); - pp = sq_packet_parser_from_bytes (ctx, b, st.st_size); - if (pp == NULL) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); - } - size_t n = 0; time_t start = time (NULL); time_t elapsed; size_t tens_of_s = 0; - while (pp) + + ppr = sq_packet_parser_from_bytes (ctx, b, st.st_size); + while (ppr && (pp = sq_packet_parser_result_packet_parser (ppr))) { - sq_packet_t p; - rc = sq_packet_parser_next (ctx, pp, &p, NULL, &ppo, NULL); + // Get a reference to the packet that is currently being parsed. + sq_packet_t p = sq_packet_parser_packet (pp); + + if (sq_packet_tag(p) == SQ_TAG_LITERAL) + { + // Stream the packet here. + } + + // Finish parsing the current packet (returned in p), and read + // the header of the next packet (returned in ppr). + rc = sq_packet_parser_next (ctx, pp, &p, NULL, &ppr, NULL); if (rc) { err = sq_context_last_error (ctx); error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); } - pp = ppo; + + // We now own p. If we want, we can save it in some structure. + // This would be useful when collecting PKESK packets. Either + // way, we need to free it when we are done. n += 1; + sq_packet_free (p); elapsed = time (NULL) - start; @@ -81,10 +90,17 @@ main (int argc, char **argv) tens_of_s = elapsed / 10; } } + if (ppr == NULL) + { + err = sq_context_last_error (ctx); + error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); + } + fprintf (stderr, "Parsed %ld packets in %ld seconds, %.2f packets/s.\n", n, elapsed, (double) n / (double) elapsed); - sq_packet_parser_free (pp); + sq_packet_parser_result_free (ppr); + sq_context_free (ctx); munmap (b, st.st_size); return 0; |