mini_parse.c revision a74702c6
1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * Convert from our hexdump format to binary:
4 *
5 * perl -p -ne 's/^[[:xdigit:]]+\s+//;s~  \|.*~~;s/\s+//g;s/([[:xdigit:]]{2})/chr hex$1/ge'
6 */
7
8#include <assert.h>
9#include <stdio.h>
10#include <stdint.h>
11#include <stdlib.h>
12#include <string.h>
13#ifndef WIN32
14#include <fcntl.h>
15#include <unistd.h>
16#else
17#include "getopt.h"
18#endif
19
20#include <sys/queue.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include "lsquic.h"
25#include "lsquic_types.h"
26#include "lsquic_int_types.h"
27#include "lsquic_packet_common.h"
28#include "lsquic_packet_gquic.h"
29#include "lsquic_packet_in.h"
30#include "lsquic_packet_out.h"
31#include "lsquic_mm.h"
32#include "lsquic_hash.h"
33#include "lsquic_conn.h"
34#include "lsquic_rtt.h"
35#include "lsquic_mini_conn.h"
36#include "lsquic_engine_public.h"
37#include "lsquic_util.h"
38#include "lsquic_logger.h"
39#include "lsquic_str.h"
40
41
42int
43lsquic_enc_session_decrypt (void *enc_session_p, enum lsquic_version version,
44           uint8_t path_id, uint64_t pack_num,
45           unsigned char *buf, size_t *header_len, size_t data_len,
46           unsigned char *diversification_nonce,
47           unsigned char *buf_out, size_t max_out_len, size_t *out_len)
48{
49    memcpy(buf_out, buf, *header_len + data_len);
50    *out_len = data_len;
51    return 0;
52}
53
54
55int
56main (int argc, char **argv)
57{
58    int opt;
59    int fd = STDIN_FILENO;
60    struct lsquic_engine_public enpub;
61    struct lsquic_conn *lconn;
62    lsquic_packet_in_t packet_in;
63    unsigned char buf[0x1000];
64    ssize_t packet_sz;
65    enum lsquic_version ver = LSQVER_043;
66
67    memset(&enpub, 0, sizeof(enpub));
68    lsquic_mm_init(&enpub.enp_mm);
69
70    lsquic_log_to_fstream(stderr, 0);
71    lsq_log_levels[LSQLM_MINI_CONN] = LSQ_LOG_DEBUG;
72    lsq_log_levels[LSQLM_NOMODULE]  = LSQ_LOG_DEBUG;
73
74    while (-1 != (opt = getopt(argc, argv, "v:f:h")))
75    {
76        switch (opt)
77        {
78        case 'v':
79            ver = atoi(optarg);
80            break;
81        case 'f':
82            fd = open(optarg, O_RDONLY);
83            if (fd < 0)
84            {
85                perror("open");
86                exit(EXIT_FAILURE);
87            }
88            break;
89        case 'h':
90            fprintf(stderr, "usage: %s [-v version] [-f input-file]\n", argv[0]);
91            exit(EXIT_SUCCESS);
92        default:
93            exit(EXIT_FAILURE);
94        }
95    }
96
97    packet_sz = read(fd, buf, sizeof(buf));
98    if (packet_sz < 0)
99    {
100        perror("read");
101        exit(EXIT_FAILURE);
102    }
103
104    memset(&packet_in, 0, sizeof(packet_in));
105    packet_in.pi_packno = 1;
106    packet_in.pi_header_sz = 0;
107    packet_in.pi_data_sz = packet_sz;
108    packet_in.pi_data = buf;
109    packet_in.pi_refcnt = 1;
110
111    lconn = lsquic_mini_conn_new(&enpub, &packet_in, ver);
112    lconn->cn_if->ci_packet_in(lconn, &packet_in);
113
114    exit(EXIT_SUCCESS);
115}
116