This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconvert_util.h
109 lines (92 loc) · 3.48 KB
/
convert_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright(c) 2019, Tessares S.A.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CONVERT_UTIL_H_
#define _CONVERT_UTIL_H_
#include "convert.h"
#ifndef UNUSED
#ifdef __GNUC__
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif
#endif
/* The TLVs in the converter headers are padded to align to 4
* bytes. These macros are helper functions to compute the expected
* padded length. Eg. returns 4 for 3, 8 for 5, etc.
*/
#define CONVERT_PADDING 4
#define __CONVERT_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
#define CONVERT_ALIGN(bytes) __CONVERT_ALIGN(bytes, CONVERT_PADDING)
enum {
_CONVERT_F_INFO = 0,
_CONVERT_F_CONNECT,
_CONVERT_F_EXTENDED_TCP_HDR,
_CONVERT_F_SUPPORTED_TCP_EXT,
_CONVERT_F_COOKIE,
_CONVERT_F_ERROR,
_CONVERT_F_MAX,
};
enum {
CONVERT_F_INFO = (1 << _CONVERT_F_INFO),
CONVERT_F_CONNECT = (1 << _CONVERT_F_CONNECT),
CONVERT_F_EXTENDED_TCP_HDR = (1 << _CONVERT_F_EXTENDED_TCP_HDR),
CONVERT_F_SUPPORTED_TCP_EXT = (1 << _CONVERT_F_SUPPORTED_TCP_EXT),
CONVERT_F_COOKIE = (1 << _CONVERT_F_COOKIE),
CONVERT_F_ERROR = (1 << _CONVERT_F_ERROR),
};
struct convert_opts {
uint8_t flags;
/* if CONVERT_F_CONNECT is set in flags
* The sin_port and sin_addr members shall be in network byte order.
*/
struct sockaddr_in6 remote_addr;
/* if CONVERT_F_ERROR is set in flags */
uint8_t error_code;
/* if CONVERT_F_EXTENDED_TCP_HDR is set in flags */
uint8_t * tcp_options;
size_t tcp_options_len;
/* if CONVERT_F_COOKIE is set in flags */
uint8_t * cookie_data;
size_t cookie_len;
/* TODO extend to support more TLVs. */
};
void
convert_free_opts(struct convert_opts *opts);
int
convert_parse_header(const uint8_t *buff, size_t buff_len, size_t *tlvs_length);
/* Returns a pointer to struct convert_opts, which must be freed by the caller
* using convert_free_opts(). Returns NULL upon failure.
*/
struct convert_opts *
convert_parse_tlvs(const uint8_t *buff, size_t buff_len);
ssize_t
convert_write(uint8_t *buff, size_t buff_len, const struct convert_opts *opts);
#endif