-
Notifications
You must be signed in to change notification settings - Fork 19
/
neat_usrsctp.c
187 lines (163 loc) · 6 KB
/
neat_usrsctp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usrsctp.h>
#include <unistd.h>
#include <stdarg.h>
#include "neat.h"
#include "neat_internal.h"
#include "neat_addr.h"
#include "neat_usrsctp.h"
#include "neat_usrsctp_internal.h"
#define MCLBYTES 2048
static void
nt_usrsctp_sctp4_readable(uv_poll_t *handle, int status, int events)
{
//nt_log(NEAT_LOG_DEBUG, "%s(status=%d, events=%d)", __func__, status, events);
if (status < 0) {
//nt_log(NEAT_LOG_ERROR, "%s: socket not readable", __func__);
return;
}
usrsctp_recv_function_sctp4();
}
static void
nt_usrsctp_udpsctp4_readable(uv_poll_t *handle, int status, int events)
{
//printf("neat_usrsctp_udpsctp4_readable\n");
if (status < 0) {
//nt_log(NEAT_LOG_ERROR, "%s: socket not readable", __func__);
return;
}
usrsctp_recv_function_udpsctp4();
}
static void
nt_usrsctp_sctp6_readable(uv_poll_t *handle, int status, int events)
{
if (status < 0) {
//nt_log(NEAT_LOG_ERROR, "%s: socket not readable", __func__);
return;
}
usrsctp_recv_function_sctp6();
}
static void
nt_usrsctp_udpsctp6_readable(uv_poll_t *handle, int status, int events)
{
if (status < 0) {
//nt_log(NEAT_LOG_ERROR, "%s: socket not readable", __func__);
return;
}
usrsctp_recv_function_udpsctp6();
}
void
nt_usrsctp_cleanup(struct neat_ctx *ctx)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (usr_intern.num_ctx == 1) {
if (usr_intern.s4_fd >= 0) {
close(usr_intern.s4_fd);
}
if (usr_intern.us4_fd >= 0) {
close(usr_intern.us4_fd);
}
if (usr_intern.s6_fd >= 0) {
close(usr_intern.s6_fd);
}
if (usr_intern.us6_fd >= 0) {
close(usr_intern.us6_fd);
}
}
}
void
nt_handle_usrsctp_timeout(uv_timer_t *handle)
{
usrsctp_handle_timers(10);
}
void
nt_usrsctp_init(struct neat_ctx *ctx)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (usr_intern.num_ctx == 0) {
/* TODO: fix this call to neat_log_usrsctp */
usrsctp_init(SCTP_UDP_TUNNELING_PORT, NULL, neat_log_usrsctp);
usr_intern.s4_fd = usrsctp_open_sctp4_socket();
nt_log(ctx, NEAT_LOG_DEBUG, "sctp4_fd=%d", usr_intern.s4_fd);
usr_intern.us4_fd = usrsctp_open_udpsctp4_socket();
nt_log(ctx, NEAT_LOG_DEBUG, "udpsctp4_fd=%d", usr_intern.us4_fd);
usr_intern.s6_fd = usrsctp_open_sctp6_socket();
nt_log(ctx, NEAT_LOG_DEBUG, "sctp6_fd=%d", usr_intern.s6_fd);
usr_intern.us6_fd = usrsctp_open_udpsctp6_socket();
nt_log(ctx, NEAT_LOG_DEBUG, "udpsctp6_fd=%d", usr_intern.us6_fd);
usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
}
}
struct neat_ctx*
nt_usrsctp_init_ctx(struct neat_ctx *ctx)
{
int ret;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
ctx->cleanup = nt_usrsctp_cleanup;
uv_timer_init(ctx->loop, &(ctx->usrsctp_timer_handle));
ctx->usrsctp_timer_handle.data = ctx;
uv_timer_start(&(ctx->usrsctp_timer_handle), nt_handle_usrsctp_timeout, 10, 10);
if (usr_intern.s4_fd != -1) {
if ((ret = uv_poll_init(ctx->loop, &(ctx->uv_sctp4_handle), usr_intern.s4_fd)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't initialize uv_sctp4_handle (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
ctx->uv_sctp4_handle.data = ctx;
if ((ret = uv_poll_start(&(ctx->uv_sctp4_handle),
UV_READABLE,
nt_usrsctp_sctp4_readable)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't start receiving sctp4 readable events (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
}
if (usr_intern.us4_fd != -1) {
if ((ret = uv_poll_init(ctx->loop, &(ctx->uv_udpsctp4_handle), usr_intern.us4_fd)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't initialize uv_udpsctp4_handle (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
ctx->uv_udpsctp4_handle.data = ctx;
if ((ret = uv_poll_start(&(ctx->uv_udpsctp4_handle),
UV_READABLE,
nt_usrsctp_udpsctp4_readable)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't start receiving udpsctp4 readable events (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
}
if (usr_intern.s6_fd != -1) {
if ((ret = uv_poll_init(ctx->loop, &(ctx->uv_sctp6_handle), usr_intern.s6_fd)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't initialize uv_sctp6_handle (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
ctx->uv_sctp6_handle.data = ctx;
if ((ret = uv_poll_start(&(ctx->uv_sctp6_handle),
UV_READABLE,
nt_usrsctp_sctp6_readable)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't start receiving sctp4 readable events (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
}
if (usr_intern.us6_fd != -1) {
if ((ret = uv_poll_init(ctx->loop, &(ctx->uv_udpsctp6_handle), usr_intern.us6_fd)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't initialize uv_udpsctp6_handle (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
ctx->uv_udpsctp6_handle.data = ctx;
if ((ret = uv_poll_start(&(ctx->uv_udpsctp6_handle),
UV_READABLE,
nt_usrsctp_udpsctp6_readable)) < 0) {
nt_log(ctx, NEAT_LOG_ERROR, "%s: can't start receiving udpsctp6 readable events (%s)", __func__, uv_strerror(ret));
nt_usrsctp_cleanup(ctx);
return NULL;
}
}
return ctx;
}