Skip to content

Commit

Permalink
fmt: add text2pcap format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 7, 2024
1 parent 14a3236 commit 2f29d38
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ set(SRCS
src/fmt/regex.c
src/fmt/str.c
src/fmt/str_error.c
src/fmt/text2pcap.c
src/fmt/time.c
src/fmt/unicode.c

Expand Down
10 changes: 10 additions & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,13 @@ void fmt_param_apply(const struct pl *pl, fmt_param_h *ph, void *arg);
int utf8_encode(struct re_printf *pf, const char *str);
int utf8_decode(struct re_printf *pf, const struct pl *pl);
size_t utf8_byteseq(char u[4], unsigned cp);


/* text2pcap */
struct re_text2pcap {
bool in;
const struct mbuf *mb;
char *id;
};

int re_text2pcap(struct re_printf *pf, struct re_text2pcap *pcap);
37 changes: 37 additions & 0 deletions src/fmt/text2pcap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <sys/time.h>
#include <time.h>

#include <re_types.h>
#include <re_fmt.h>
#include <re_mbuf.h>


int re_text2pcap(struct re_printf *pf, struct re_text2pcap *pcap)
{
struct timeval tv;
time_t t = time(NULL);
struct tm *tm = localtime(&t);

Check failure

Code scanning / CodeQL

Use of potentially dangerous function Critical

Call to 'localtime' is potentially dangerous.
char ts[10] = {0};

if (!pcap)
return EINVAL;

uint8_t *buf = mbuf_buf(pcap->mb);
if (!buf)
return EINVAL;

gettimeofday(&tv, NULL);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);

re_hprintf(pf, "%s %s.%06ld 000000", pcap->in ? "I" : "O", ts,
tv.tv_usec);

size_t sz = mbuf_get_left(pcap->mb);
for (size_t i = 0; i < sz; i++) {
re_hprintf(pf, " %02x", buf[i]);
}

re_hprintf(pf, " %s\r\n", pcap->id);

return 0;
}
27 changes: 27 additions & 0 deletions test/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,3 +1158,30 @@ int test_fmt_hexdump(void)

return 0;
}


int test_text2pcap(void)
{
char test[64];
struct mbuf *mb;
int err = 0;

mb = mbuf_alloc(1);
if (!mb)
return ENOMEM;

mbuf_write_u8(mb, 42);
mbuf_write_u8(mb, 23);

mbuf_set_pos(mb, 0);

struct re_text2pcap pcap = {.id = "test", .in = true, .mb = mb};

int ret = re_snprintf(test, sizeof(test), "%H", re_text2pcap, &pcap);

TEST_EQUALS(37, ret);

out:
mem_deref(mb);
return err;
}
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static const struct test tests[] = {
TEST(test_sys_getenv),
TEST(test_tcp),
TEST(test_telev),
TEST(test_text2pcap),
#ifdef USE_TLS
TEST(test_tls),
TEST(test_tls_ec),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ int test_sys_fs_fopen(void);
int test_sys_getenv(void);
int test_tcp(void);
int test_telev(void);
int test_text2pcap(void);
int test_thread(void);
int test_thread_cnd_timedwait(void);
int test_tmr_jiffies(void);
Expand Down

0 comments on commit 2f29d38

Please sign in to comment.