1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * Test client's and server's common components.
4 */
5
6#ifndef TEST_COMMON_H
7#define TEST_COMMON_H 1
8
9#if __linux__
10#   include <net/if.h>  /* For IFNAMSIZ */
11#endif
12
13#ifdef WIN32
14#include <winsock2.h>
15#include <ws2tcpip.h>
16#endif
17
18struct lsquic_engine;
19struct lsquic_engine_settings;
20struct lsquic_out_spec;
21struct event_base;
22struct event;
23struct packets_in;
24struct lsquic_conn;
25struct prog;
26struct reader_ctx;
27struct lsxpack_header;
28
29#ifndef WIN32
30#   define SOCKOPT_VAL int
31#   define SOCKET_TYPE int
32#   define CLOSE_SOCKET close
33#   define CHAR_CAST
34#else
35#   define SOCKOPT_VAL DWORD
36#   define SOCKET_TYPE SOCKET
37#   define CLOSE_SOCKET closesocket
38#   define CHAR_CAST (char *)
39#endif
40
41enum sport_flags
42{
43#if LSQUIC_DONTFRAG_SUPPORTED
44    SPORT_FRAGMENT_OK       = (1 << 0),
45#endif
46    SPORT_SET_SNDBUF        = (1 << 1), /* SO_SNDBUF */
47    SPORT_SET_RCVBUF        = (1 << 2), /* SO_RCVBUF */
48    SPORT_SERVER            = (1 << 3),
49    SPORT_CONNECT           = (1 << 4),
50};
51
52struct service_port {
53    TAILQ_ENTRY(service_port)  next_sport;
54#ifndef WIN32
55    int                        fd;
56#else
57    SOCKET                        fd;
58#endif
59#if __linux__
60    uint32_t                   n_dropped;
61    int                        drop_init;
62    char                       if_name[IFNAMSIZ];
63#endif
64    struct event              *ev;
65    struct lsquic_engine      *engine;
66    void                      *conn_ctx;
67    char                       host[80];
68    struct sockaddr_storage    sas;
69    struct sockaddr_storage    sp_local_addr;
70    struct packets_in         *packs_in;
71    enum sport_flags           sp_flags;
72    SOCKOPT_VAL                sp_sndbuf;   /* If SPORT_SET_SNDBUF is set */
73    SOCKOPT_VAL                sp_rcvbuf;   /* If SPORT_SET_RCVBUF is set */
74    struct prog               *sp_prog;
75    unsigned char             *sp_token_buf;
76    size_t                     sp_token_sz;
77};
78
79TAILQ_HEAD(sport_head, service_port);
80
81struct service_port *
82sport_new (const char *optarg, struct prog *);
83
84void
85sport_destroy (struct service_port *);
86
87int
88sport_init_server (struct service_port *, struct lsquic_engine *,
89                   struct event_base *);
90
91int
92sport_init_client (struct service_port *, struct lsquic_engine *,
93                   struct event_base *);
94
95int
96sport_packets_out (void *ctx, const struct lsquic_out_spec *, unsigned count);
97
98int
99sport_set_token (struct service_port *, const char *);
100
101int
102set_engine_option (struct lsquic_engine_settings *,
103                   int *version_cleared, const char *name_value);
104
105struct packout_buf;
106
107struct packout_buf_allocator
108{
109    unsigned                    n_out,      /* Number of buffers outstanding */
110                                max;        /* Maximum outstanding.  Zero mean no limit */
111    SLIST_HEAD(, packout_buf)   free_packout_bufs;
112};
113
114void
115pba_init (struct packout_buf_allocator *, unsigned max);
116
117void *
118pba_allocate (void *packout_buf_allocator, void*, lsquic_conn_ctx_t *, unsigned short, char);
119
120void
121pba_release (void *packout_buf_allocator, void *, void *obj, char);
122
123void
124pba_cleanup (struct packout_buf_allocator *);
125
126void
127print_conn_info (const struct lsquic_conn *conn);
128
129size_t
130test_reader_size (void *void_ctx);
131
132size_t
133test_reader_read (void *void_ctx, void *buf, size_t count);
134
135struct reader_ctx *
136create_lsquic_reader_ctx (const char *filename);
137
138void
139destroy_lsquic_reader_ctx (struct reader_ctx *ctx);
140
141#define STRINGIFY(x) #x
142#define TOSTRING(x) STRINGIFY(x)
143#define LITESPEED_ID "lsquic" "/" TOSTRING(LSQUIC_MAJOR_VERSION) "." \
144            TOSTRING(LSQUIC_MINOR_VERSION) "." TOSTRING(LSQUIC_PATCH_VERSION)
145
146struct header_buf
147{
148    unsigned    off;
149    char        buf[UINT16_MAX];
150};
151
152int
153header_set_ptr (struct lsxpack_header *hdr, struct header_buf *header_buf,
154                const char *name, size_t name_len,
155                const char *val, size_t val_len);
156
157#endif
158