BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file |
||
3 | * @defgroup altcp Application layered TCP |
||
4 | * @ingroup callbackstyle_api |
||
5 | * Application layered TCP connection API (to be used from TCPIP thread)\n |
||
6 | * This interface mimics the tcp callback API to the application while preventing |
||
7 | * direct linking (much like virtual functions). |
||
8 | * This way, an application can make use of other application layer protocols |
||
9 | * on top of TCP without knowing the details (e.g. TLS, proxy connection). |
||
10 | * |
||
11 | * This file contains the common functions for altcp to work. |
||
12 | */ |
||
13 | |||
14 | /* |
||
15 | * Copyright (c) 2017 Simon Goldschmidt |
||
16 | * All rights reserved. |
||
17 | * |
||
18 | * Redistribution and use in source and binary forms, with or without modification, |
||
19 | * are permitted provided that the following conditions are met: |
||
20 | * |
||
21 | * 1. Redistributions of source code must retain the above copyright notice, |
||
22 | * this list of conditions and the following disclaimer. |
||
23 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
24 | * this list of conditions and the following disclaimer in the documentation |
||
25 | * and/or other materials provided with the distribution. |
||
26 | * 3. The name of the author may not be used to endorse or promote products |
||
27 | * derived from this software without specific prior written permission. |
||
28 | * |
||
29 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
||
30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
31 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
||
32 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
33 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
||
34 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
37 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
||
38 | * OF SUCH DAMAGE. |
||
39 | * |
||
40 | * This file is part of the lwIP TCP/IP stack. |
||
41 | * |
||
42 | * Author: Simon Goldschmidt <goldsimon@gmx.de> |
||
43 | * |
||
44 | */ |
||
45 | |||
46 | #include "lwip/opt.h" |
||
47 | |||
48 | #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */ |
||
49 | |||
50 | #include "lwip/altcp.h" |
||
51 | #include "lwip/priv/altcp_priv.h" |
||
52 | #include "lwip/tcp.h" |
||
53 | #include "lwip/mem.h" |
||
54 | |||
55 | #include <string.h> |
||
56 | |||
57 | extern const struct altcp_functions altcp_tcp_functions; |
||
58 | |||
59 | struct altcp_pcb * |
||
60 | altcp_alloc(void) |
||
61 | { |
||
62 | struct altcp_pcb *ret = (struct altcp_pcb *)memp_malloc(MEMP_ALTCP_PCB); |
||
63 | if (ret != NULL) { |
||
64 | memset(ret, 0, sizeof(struct altcp_pcb)); |
||
65 | } |
||
66 | return ret; |
||
67 | } |
||
68 | |||
69 | void |
||
70 | altcp_free(struct altcp_pcb *conn) |
||
71 | { |
||
72 | if (conn) { |
||
73 | if (conn->fns && conn->fns->dealloc) { |
||
74 | conn->fns->dealloc(conn); |
||
75 | } |
||
76 | memp_free(MEMP_ALTCP_PCB, conn); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @ingroup altcp |
||
82 | * @see tcp_arg() |
||
83 | */ |
||
84 | void |
||
85 | altcp_arg(struct altcp_pcb *conn, void *arg) |
||
86 | { |
||
87 | if (conn) { |
||
88 | conn->arg = arg; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @ingroup altcp |
||
94 | * @see tcp_accept() |
||
95 | */ |
||
96 | void |
||
97 | altcp_accept(struct altcp_pcb *conn, altcp_accept_fn accept) |
||
98 | { |
||
99 | if (conn != NULL) { |
||
100 | conn->accept = accept; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @ingroup altcp |
||
106 | * @see tcp_recv() |
||
107 | */ |
||
108 | void |
||
109 | altcp_recv(struct altcp_pcb *conn, altcp_recv_fn recv) |
||
110 | { |
||
111 | if (conn) { |
||
112 | conn->recv = recv; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @ingroup altcp |
||
118 | * @see tcp_sent() |
||
119 | */ |
||
120 | void |
||
121 | altcp_sent(struct altcp_pcb *conn, altcp_sent_fn sent) |
||
122 | { |
||
123 | if (conn) { |
||
124 | conn->sent = sent; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @ingroup altcp |
||
130 | * @see tcp_poll() |
||
131 | */ |
||
132 | void |
||
133 | altcp_poll(struct altcp_pcb *conn, altcp_poll_fn poll, u8_t interval) |
||
134 | { |
||
135 | if (conn) { |
||
136 | conn->poll = poll; |
||
137 | conn->pollinterval = interval; |
||
138 | if (conn->fns && conn->fns->set_poll) { |
||
139 | conn->fns->set_poll(conn, interval); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @ingroup altcp |
||
146 | * @see tcp_err() |
||
147 | */ |
||
148 | void |
||
149 | altcp_err(struct altcp_pcb *conn, altcp_err_fn err) |
||
150 | { |
||
151 | if (conn) { |
||
152 | conn->err = err; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /* Generic functions calling the "virtual" ones */ |
||
157 | |||
158 | /** |
||
159 | * @ingroup altcp |
||
160 | * @see tcp_recved() |
||
161 | */ |
||
162 | void |
||
163 | altcp_recved(struct altcp_pcb *conn, u16_t len) |
||
164 | { |
||
165 | if (conn && conn->fns && conn->fns->recved) { |
||
166 | conn->fns->recved(conn, len); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @ingroup altcp |
||
172 | * @see tcp_bind() |
||
173 | */ |
||
174 | err_t |
||
175 | altcp_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port) |
||
176 | { |
||
177 | if (conn && conn->fns && conn->fns->bind) { |
||
178 | return conn->fns->bind(conn, ipaddr, port); |
||
179 | } |
||
180 | return ERR_VAL; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @ingroup altcp |
||
185 | * @see tcp_connect() |
||
186 | */ |
||
187 | err_t |
||
188 | altcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected) |
||
189 | { |
||
190 | if (conn && conn->fns && conn->fns->connect) { |
||
191 | return conn->fns->connect(conn, ipaddr, port, connected); |
||
192 | } |
||
193 | return ERR_VAL; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @ingroup altcp |
||
198 | * @see tcp_listen_with_backlog_and_err() |
||
199 | */ |
||
200 | struct altcp_pcb * |
||
201 | altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u8_t backlog, err_t *err) |
||
202 | { |
||
203 | if (conn && conn->fns && conn->fns->listen) { |
||
204 | return conn->fns->listen(conn, backlog, err); |
||
205 | } |
||
206 | return NULL; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @ingroup altcp |
||
211 | * @see tcp_abort() |
||
212 | */ |
||
213 | void |
||
214 | altcp_abort(struct altcp_pcb *conn) |
||
215 | { |
||
216 | if (conn && conn->fns && conn->fns->abort) { |
||
217 | conn->fns->abort(conn); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @ingroup altcp |
||
223 | * @see tcp_close() |
||
224 | */ |
||
225 | err_t |
||
226 | altcp_close(struct altcp_pcb *conn) |
||
227 | { |
||
228 | if (conn && conn->fns && conn->fns->close) { |
||
229 | return conn->fns->close(conn); |
||
230 | } |
||
231 | return ERR_VAL; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @ingroup altcp |
||
236 | * @see tcp_shutdown() |
||
237 | */ |
||
238 | err_t |
||
239 | altcp_shutdown(struct altcp_pcb *conn, int shut_rx, int shut_tx) |
||
240 | { |
||
241 | if (conn && conn->fns && conn->fns->shutdown) { |
||
242 | return conn->fns->shutdown(conn, shut_rx, shut_tx); |
||
243 | } |
||
244 | return ERR_VAL; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @ingroup altcp |
||
249 | * @see tcp_write() |
||
250 | */ |
||
251 | err_t |
||
252 | altcp_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t apiflags) |
||
253 | { |
||
254 | if (conn && conn->fns && conn->fns->write) { |
||
255 | return conn->fns->write(conn, dataptr, len, apiflags); |
||
256 | } |
||
257 | return ERR_VAL; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @ingroup altcp |
||
262 | * @see tcp_output() |
||
263 | */ |
||
264 | err_t |
||
265 | altcp_output(struct altcp_pcb *conn) |
||
266 | { |
||
267 | if (conn && conn->fns && conn->fns->output) { |
||
268 | return conn->fns->output(conn); |
||
269 | } |
||
270 | return ERR_VAL; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @ingroup altcp |
||
275 | * @see tcp_mss() |
||
276 | */ |
||
277 | u16_t |
||
278 | altcp_mss(struct altcp_pcb *conn) |
||
279 | { |
||
280 | if (conn && conn->fns && conn->fns->mss) { |
||
281 | return conn->fns->mss(conn); |
||
282 | } |
||
283 | return 0; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @ingroup altcp |
||
288 | * @see tcp_sndbuf() |
||
289 | */ |
||
290 | u16_t |
||
291 | altcp_sndbuf(struct altcp_pcb *conn) |
||
292 | { |
||
293 | if (conn && conn->fns && conn->fns->sndbuf) { |
||
294 | return conn->fns->sndbuf(conn); |
||
295 | } |
||
296 | return 0; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @ingroup altcp |
||
301 | * @see tcp_sndqueuelen() |
||
302 | */ |
||
303 | u16_t |
||
304 | altcp_sndqueuelen(struct altcp_pcb *conn) |
||
305 | { |
||
306 | if (conn && conn->fns && conn->fns->sndqueuelen) { |
||
307 | return conn->fns->sndqueuelen(conn); |
||
308 | } |
||
309 | return 0; |
||
310 | } |
||
311 | |||
312 | void |
||
313 | altcp_nagle_disable(struct altcp_pcb *conn) |
||
314 | { |
||
315 | if (conn && conn->fns && conn->fns->nagle_disable) { |
||
316 | conn->fns->nagle_disable(conn); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | void |
||
321 | altcp_nagle_enable(struct altcp_pcb *conn) |
||
322 | { |
||
323 | if (conn && conn->fns && conn->fns->nagle_enable) { |
||
324 | conn->fns->nagle_enable(conn); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | int |
||
329 | altcp_nagle_disabled(struct altcp_pcb *conn) |
||
330 | { |
||
331 | if (conn && conn->fns && conn->fns->nagle_disabled) { |
||
332 | return conn->fns->nagle_disabled(conn); |
||
333 | } |
||
334 | return 0; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @ingroup altcp |
||
339 | * @see tcp_setprio() |
||
340 | */ |
||
341 | void |
||
342 | altcp_setprio(struct altcp_pcb *conn, u8_t prio) |
||
343 | { |
||
344 | if (conn && conn->fns && conn->fns->setprio) { |
||
345 | conn->fns->setprio(conn, prio); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | err_t |
||
350 | altcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port) |
||
351 | { |
||
352 | if (conn && conn->fns && conn->fns->addrinfo) { |
||
353 | return conn->fns->addrinfo(conn, local, addr, port); |
||
354 | } |
||
355 | return ERR_VAL; |
||
356 | } |
||
357 | |||
358 | ip_addr_t * |
||
359 | altcp_get_ip(struct altcp_pcb *conn, int local) |
||
360 | { |
||
361 | if (conn && conn->fns && conn->fns->getip) { |
||
362 | return conn->fns->getip(conn, local); |
||
363 | } |
||
364 | return NULL; |
||
365 | } |
||
366 | |||
367 | u16_t |
||
368 | altcp_get_port(struct altcp_pcb *conn, int local) |
||
369 | { |
||
370 | if (conn && conn->fns && conn->fns->getport) { |
||
371 | return conn->fns->getport(conn, local); |
||
372 | } |
||
373 | return 0; |
||
374 | } |
||
375 | |||
376 | #ifdef LWIP_DEBUG |
||
377 | enum tcp_state |
||
378 | altcp_dbg_get_tcp_state(struct altcp_pcb *conn) |
||
379 | { |
||
380 | if (conn && conn->fns && conn->fns->dbg_get_tcp_state) { |
||
381 | return conn->fns->dbg_get_tcp_state(conn); |
||
382 | } |
||
383 | return CLOSED; |
||
384 | } |
||
385 | #endif |
||
386 | |||
387 | /* Default implementations for the "virtual" functions */ |
||
388 | |||
389 | void |
||
390 | altcp_default_set_poll(struct altcp_pcb *conn, u8_t interval) |
||
391 | { |
||
392 | if (conn && conn->inner_conn) { |
||
393 | altcp_poll(conn->inner_conn, conn->poll, interval); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | void |
||
398 | altcp_default_recved(struct altcp_pcb *conn, u16_t len) |
||
399 | { |
||
400 | if (conn && conn->inner_conn) { |
||
401 | altcp_recved(conn->inner_conn, len); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | err_t |
||
406 | altcp_default_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port) |
||
407 | { |
||
408 | if (conn && conn->inner_conn) { |
||
409 | return altcp_bind(conn->inner_conn, ipaddr, port); |
||
410 | } |
||
411 | return ERR_VAL; |
||
412 | } |
||
413 | |||
414 | err_t |
||
415 | altcp_default_shutdown(struct altcp_pcb *conn, int shut_rx, int shut_tx) |
||
416 | { |
||
417 | if (conn && conn->inner_conn) { |
||
418 | return altcp_shutdown(conn->inner_conn, shut_rx, shut_tx); |
||
419 | } |
||
420 | return ERR_VAL; |
||
421 | } |
||
422 | |||
423 | err_t |
||
424 | altcp_default_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t apiflags) |
||
425 | { |
||
426 | if (conn && conn->inner_conn) { |
||
427 | return altcp_write(conn->inner_conn, dataptr, len, apiflags); |
||
428 | } |
||
429 | return ERR_VAL; |
||
430 | } |
||
431 | |||
432 | err_t |
||
433 | altcp_default_output(struct altcp_pcb *conn) |
||
434 | { |
||
435 | if (conn && conn->inner_conn) { |
||
436 | return altcp_output(conn->inner_conn); |
||
437 | } |
||
438 | return ERR_VAL; |
||
439 | } |
||
440 | |||
441 | u16_t |
||
442 | altcp_default_mss(struct altcp_pcb *conn) |
||
443 | { |
||
444 | if (conn && conn->inner_conn) { |
||
445 | return altcp_mss(conn->inner_conn); |
||
446 | } |
||
447 | return 0; |
||
448 | } |
||
449 | |||
450 | u16_t |
||
451 | altcp_default_sndbuf(struct altcp_pcb *conn) |
||
452 | { |
||
453 | if (conn && conn->inner_conn) { |
||
454 | return altcp_sndbuf(conn->inner_conn); |
||
455 | } |
||
456 | return 0; |
||
457 | } |
||
458 | |||
459 | u16_t |
||
460 | altcp_default_sndqueuelen(struct altcp_pcb *conn) |
||
461 | { |
||
462 | if (conn && conn->inner_conn) { |
||
463 | return altcp_sndqueuelen(conn->inner_conn); |
||
464 | } |
||
465 | return 0; |
||
466 | } |
||
467 | |||
468 | void |
||
469 | altcp_default_nagle_disable(struct altcp_pcb *conn) |
||
470 | { |
||
471 | if (conn && conn->inner_conn) { |
||
472 | altcp_nagle_disable(conn->inner_conn); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | void |
||
477 | altcp_default_nagle_enable(struct altcp_pcb *conn) |
||
478 | { |
||
479 | if (conn && conn->inner_conn) { |
||
480 | altcp_nagle_enable(conn->inner_conn); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | int |
||
485 | altcp_default_nagle_disabled(struct altcp_pcb *conn) |
||
486 | { |
||
487 | if (conn && conn->inner_conn) { |
||
488 | return altcp_nagle_disabled(conn->inner_conn); |
||
489 | } |
||
490 | return 0; |
||
491 | } |
||
492 | |||
493 | void |
||
494 | altcp_default_setprio(struct altcp_pcb *conn, u8_t prio) |
||
495 | { |
||
496 | if (conn && conn->inner_conn) { |
||
497 | altcp_setprio(conn->inner_conn, prio); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | void |
||
502 | altcp_default_dealloc(struct altcp_pcb *conn) |
||
503 | { |
||
504 | LWIP_UNUSED_ARG(conn); |
||
505 | /* nothing to do */ |
||
506 | } |
||
507 | |||
508 | err_t |
||
509 | altcp_default_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port) |
||
510 | { |
||
511 | if (conn && conn->inner_conn) { |
||
512 | return altcp_get_tcp_addrinfo(conn->inner_conn, local, addr, port); |
||
513 | } |
||
514 | return ERR_VAL; |
||
515 | } |
||
516 | |||
517 | ip_addr_t * |
||
518 | altcp_default_get_ip(struct altcp_pcb *conn, int local) |
||
519 | { |
||
520 | if (conn && conn->inner_conn) { |
||
521 | return altcp_get_ip(conn->inner_conn, local); |
||
522 | } |
||
523 | return NULL; |
||
524 | } |
||
525 | |||
526 | u16_t |
||
527 | altcp_default_get_port(struct altcp_pcb *conn, int local) |
||
528 | { |
||
529 | if (conn && conn->inner_conn) { |
||
530 | return altcp_get_port(conn->inner_conn, local); |
||
531 | } |
||
532 | return 0; |
||
533 | } |
||
534 | |||
535 | #ifdef LWIP_DEBUG |
||
536 | enum tcp_state |
||
537 | altcp_default_dbg_get_tcp_state(struct altcp_pcb *conn) |
||
538 | { |
||
539 | if (conn && conn->inner_conn) { |
||
540 | return altcp_dbg_get_tcp_state(conn->inner_conn); |
||
541 | } |
||
542 | return CLOSED; |
||
543 | } |
||
544 | #endif |
||
545 | |||
546 | |||
547 | #endif /* LWIP_ALTCP */ |