-
Notifications
You must be signed in to change notification settings - Fork 19
/
neat.h
314 lines (265 loc) · 11.4 KB
/
neat.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef NEAT_H
#define NEAT_H
// Avoid additional includes for SWIG
#ifndef SWIG
#include <sys/types.h>
#include <netinet/in.h>
#include <uv.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
// TODO: this __attribute__ feature supposedly works with both clang and
// modern gcc compilers. Could be moved to a cmake test for better
// portability.
#ifndef SWIG
#define NEAT_EXTERN __attribute__ ((__visibility__ ("default")))
#else
// SWIG doesnt like the above definition
#define NEAT_EXTERN extern
#endif
//Maps directly to libuv contants
typedef enum {
NEAT_RUN_DEFAULT = 0,
NEAT_RUN_ONCE,
NEAT_RUN_NOWAIT
} neat_run_mode;
struct neat_ctx; // global
struct neat_flow; // one per connection
typedef uint64_t neat_error_code;
NEAT_EXTERN int neat_get_socket_fd(struct neat_flow *nf);
NEAT_EXTERN neat_error_code neat_get_stack(struct neat_flow *flow, void *ptr, size_t *size);
NEAT_EXTERN struct neat_ctx *neat_init_ctx();
NEAT_EXTERN neat_error_code neat_start_event_loop(struct neat_ctx *nc, neat_run_mode run_mode);
NEAT_EXTERN uv_loop_t *neat_get_event_loop(struct neat_ctx *ctx);
NEAT_EXTERN void neat_stop_event_loop(struct neat_ctx *nc);
NEAT_EXTERN int neat_get_backend_fd(struct neat_ctx *nc);
NEAT_EXTERN int neat_get_backend_timeout(struct neat_ctx *nc);
NEAT_EXTERN void neat_free_ctx(struct neat_ctx *nc);
NEAT_EXTERN void neat_log_level(struct neat_ctx *ctx, uint8_t level);
NEAT_EXTERN uint8_t neat_log_file(struct neat_ctx *ctx, const char* file_name);
struct neat_flow_operations;
typedef neat_error_code (*neat_flow_operations_fx)(struct neat_flow_operations *);
// Additional callbacks from D.1.2 sect. 3.2/3.3:
// Callback handler function prototypes
// Not including ctx/flow pointers, flow_ops struct has those as well
// as status code
//(struct neat_flow_operations *ops, int ecn, uint32_t rate)
typedef void (*neat_cb_flow_slowdown_t)(struct neat_flow_operations *, int, uint32_t);
//(struct neat_flow_operations *flowops, uint32_t new_rate)
typedef void (*neat_cb_flow_rate_hint_t)(struct neat_flow_operations *, uint32_t);
//struct neat_flow_operations *flowops, int context, const unsigned char *unsent
typedef void (*neat_cb_send_failure_t)(struct neat_flow_operations *, int, const unsigned char *);
struct neat_flow_operations {
// Used by the Python shim
uint32_t preconnection_id;
uint32_t rendezvous_id;
uint32_t connection_id;
uint32_t parent_id;
uint32_t clone_id;
void *userData;
neat_error_code status;
uint16_t stream_id;
int transport_protocol;
neat_flow_operations_fx on_connected;
neat_flow_operations_fx on_error;
neat_flow_operations_fx on_readable;
neat_flow_operations_fx on_writable;
neat_flow_operations_fx on_all_written;
neat_flow_operations_fx on_network_status_changed;
neat_flow_operations_fx on_aborted;
neat_flow_operations_fx on_timeout;
neat_flow_operations_fx on_close;
neat_flow_operations_fx on_parameters;
neat_cb_send_failure_t on_send_failure;
neat_cb_flow_slowdown_t on_slowdown;
neat_cb_flow_rate_hint_t on_rate_hint;
char *label;
struct neat_ctx *ctx;
struct neat_flow *flow;
};
NEAT_EXTERN void set_initiate_timer(struct neat_ctx *nc, struct neat_flow *flow, struct neat_flow_operations *ops, float timeout);
NEAT_EXTERN void set_ops_user_data(struct neat_flow_operations *ops, unsigned char* data);
NEAT_EXTERN unsigned char* get_ops_user_data(struct neat_flow_operations *ops);
NEAT_EXTERN void neat_get_max_buffer_sizes(struct neat_flow *flow, int *send, int *recv);
enum neat_tlv_type {
NEAT_TYPE_INTEGER = 0,
NEAT_TYPE_FLOAT,
NEAT_TYPE_STRING,
};
typedef enum neat_tlv_type neat_tlv_type;
enum neat_tlv_tag {
NEAT_TAG_STREAM_ID = 0,
NEAT_TAG_STREAM_COUNT,
NEAT_TAG_LOCAL_NAME,
NEAT_TAG_LOCAL_ADDRESS,
NEAT_TAG_SERVICE_NAME,
NEAT_TAG_CONTEXT,
NEAT_TAG_PARTIAL_RELIABILITY_METHOD,
NEAT_TAG_PARTIAL_RELIABILITY_VALUE,
NEAT_TAG_PARTIAL_MESSAGE_RECEIVED,
NEAT_TAG_PARTIAL_SEQNUM,
NEAT_TAG_UNORDERED,
NEAT_TAG_UNORDERED_SEQNUM,
NEAT_TAG_DESTINATION_IP_ADDRESS,
NEAT_TAG_PRIORITY,
NEAT_TAG_FLOW_GROUP,
NEAT_TAG_CC_ALGORITHM,
NEAT_TAG_TRANSPORT_STACK,
NEAT_TAG_CHANNEL_NAME,
NEAT_TAG_LAST
};
typedef enum neat_tlv_tag neat_tlv_tag;
struct neat_tlv {
neat_tlv_tag tag;
neat_tlv_type type;
union {
int integer;
char *string;
float real;
} value;
};
NEAT_EXTERN struct neat_flow *neat_new_flow(struct neat_ctx *ctx);
NEAT_EXTERN neat_error_code neat_set_operations(struct neat_ctx *ctx,
struct neat_flow *flow,
struct neat_flow_operations *ops);
NEAT_EXTERN neat_error_code neat_get_stats(struct neat_ctx *ctx, char **neat_stats);
NEAT_EXTERN neat_error_code neat_open(struct neat_ctx *mgr, struct neat_flow *flow,
const char *name, uint16_t port,
struct neat_tlv optional[], unsigned int opt_count);
NEAT_EXTERN neat_error_code neat_read(struct neat_ctx *ctx, struct neat_flow *flow,
unsigned char *buffer, uint32_t amt, uint32_t *actualAmt,
struct neat_tlv optional[], unsigned int opt_count);
NEAT_EXTERN neat_error_code neat_write(struct neat_ctx *ctx, struct neat_flow *flow,
const unsigned char *buffer, uint32_t amt,
struct neat_tlv optional[], unsigned int opt_count);
NEAT_EXTERN neat_error_code neat_get_property(struct neat_ctx *ctx, struct neat_flow *flow,
const char* name, void *ptr, size_t *size);
NEAT_EXTERN neat_error_code neat_set_property(struct neat_ctx *ctx, struct neat_flow *flow,
const char* properties);
NEAT_EXTERN neat_error_code neat_accept(struct neat_ctx *ctx, struct neat_flow *flow,
uint16_t port, struct neat_tlv optional[], unsigned int opt_count);
NEAT_EXTERN neat_error_code neat_shutdown(struct neat_ctx *ctx, struct neat_flow *flow);
NEAT_EXTERN neat_error_code neat_close(struct neat_ctx *ctx, struct neat_flow *flow);
NEAT_EXTERN neat_error_code neat_abort(struct neat_ctx *ctx, struct neat_flow *flow);
NEAT_EXTERN int neat_getlpaddrs(struct neat_ctx *ctx, struct neat_flow *flow, struct sockaddr** addrs, const int local);
NEAT_EXTERN void neat_freelpaddrs(struct sockaddr* addrs);
NEAT_EXTERN neat_error_code neat_change_timeout(struct neat_ctx *ctx, struct neat_flow *flow,
unsigned int seconds);
NEAT_EXTERN neat_error_code neat_set_primary_dest(struct neat_ctx *ctx, struct neat_flow *flow,
const char *name);
NEAT_EXTERN neat_error_code neat_set_checksum_coverage(struct neat_ctx *ctx, struct neat_flow *flow,
unsigned int send_coverage, unsigned int receive_coverage);
// The filename should be a PEM file with both cert and key
NEAT_EXTERN neat_error_code neat_secure_identity(struct neat_ctx *ctx, struct neat_flow *flow,
const char *filename, int pemType);
NEAT_EXTERN neat_error_code neat_set_qos(struct neat_ctx *ctx,
struct neat_flow *flow, uint8_t qos);
NEAT_EXTERN int neat_get_qos(struct neat_ctx *ctx, struct neat_flow *flow);
NEAT_EXTERN neat_error_code neat_set_ecn(struct neat_ctx *ctx,
struct neat_flow *flow, uint8_t ecn);
NEAT_EXTERN neat_error_code neat_set_low_watermark(struct neat_ctx *ctx, struct neat_flow *flow, uint32_t watermark);
#if defined(WEBRTC_SUPPORT)
NEAT_EXTERN neat_error_code neat_send_remote_parameters(struct neat_ctx *ctx, struct neat_flow *flow, char* params);
#endif
#define NEAT_ERROR_OK (0)
#define NEAT_OK NEAT_ERROR_OK
#define NEAT_ERROR_WOULD_BLOCK (1)
#define NEAT_ERROR_BAD_ARGUMENT (2)
#define NEAT_ERROR_IO (3)
#define NEAT_ERROR_DNS (4)
#define NEAT_ERROR_INTERNAL (5)
#define NEAT_ERROR_SECURITY (6)
#define NEAT_ERROR_UNABLE (7)
#define NEAT_ERROR_MESSAGE_TOO_BIG (8)
#define NEAT_ERROR_REMOTE (9)
#define NEAT_ERROR_OUT_OF_MEMORY (10)
#define NEAT_INVALID_STREAM (-1)
#define NEAT_LOG_OFF (0)
#define NEAT_LOG_ERROR (1)
#define NEAT_LOG_WARNING (2)
#define NEAT_LOG_INFO (3)
#define NEAT_LOG_DEBUG (4)
#define NEAT_OPTARGS (__optional_arguments)
#define NEAT_OPTARGS_COUNT (__optional_argument_count)
#define NEAT_OPTARGS_MAX (NEAT_TAG_LAST)
#define NEAT_OPTARGS_INIT() \
do { \
NEAT_OPTARGS_COUNT = 0; \
} while (0);
#define NEAT_OPTARGS_RESET NEAT_OPTARGS_INIT
#define NEAT_CERT_NONE 0
#define NEAT_CERT_PEM 1
#define NEAT_KEY_PEM 2
#define NEAT_CERT_KEY_PEM 3
#ifdef assert
#define NEAT_OPTARGS_DECLARE(max) \
struct neat_tlv __optargs_buffer[max]; \
struct neat_tlv *NEAT_OPTARGS = &__optargs_buffer[0]; \
unsigned int NEAT_OPTARGS_COUNT; \
unsigned int __optional_arguments_limit = max;
#define NEAT_OPTARG_INT(tagname, val) \
do { \
assert(NEAT_OPTARGS_COUNT < __optional_arguments_limit);\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.integer = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_INTEGER;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#define NEAT_OPTARG_FLOAT(tagname, val) \
do { \
assert(NEAT_OPTARGS_COUNT < __optional_arguments_limit);\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.real = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_FLOAT;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#define NEAT_OPTARG_STRING(tagname, val) \
do { \
assert(NEAT_OPTARGS_COUNT < __optional_arguments_limit);\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.string = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_STRING;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#else
#define NEAT_OPTARGS_DECLARE(max) \
struct neat_tlv __optargs_buffer[max]; \
struct neat_tlv *NEAT_OPTARGS = &__optargs_buffer[0]; \
unsigned int NEAT_OPTARGS_COUNT;
#define NEAT_OPTARG_INT(tagname, val) \
do { \
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.integer = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_INTEGER;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#define NEAT_OPTARG_FLOAT(tagname, val) \
do { \
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.real = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_FLOAT;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#define NEAT_OPTARG_STRING(tagname, val) \
do { \
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].value.string = val;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].type = NEAT_TYPE_STRING;\
NEAT_OPTARGS[NEAT_OPTARGS_COUNT].tag = tagname;\
NEAT_OPTARGS_COUNT++;\
} while (0);
#endif // ifdef assert else
typedef enum {
NEAT_STACK_UDP = 1,
NEAT_STACK_UDPLITE,
NEAT_STACK_TCP,
NEAT_STACK_MPTCP,
NEAT_STACK_SCTP,
NEAT_STACK_SCTP_UDP,
NEAT_STACK_WEBRTC
} neat_protocol_stack_type;
// cleanup extern "C"
#ifdef __cplusplus
}
#endif
#endif // guard bars