altcp_tcp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * @file
  3. * Application layered TCP connection API (to be used from TCPIP thread)
  4. *
  5. * This interface mimics the tcp callback API to the application while preventing
  6. * direct linking (much like virtual functions).
  7. * This way, an application can make use of other application layer protocols
  8. * on top of TCP without knowing the details (e.g. TLS, proxy connection).
  9. *
  10. * This file contains the base implementation calling into tcp.
  11. */
  12. /*
  13. * Copyright (c) 2017 Simon Goldschmidt
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  30. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. *
  38. * This file is part of the lwIP TCP/IP stack.
  39. *
  40. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  41. *
  42. */
  43. #include "lwip/opt.h"
  44. #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */
  45. #include "lwip/altcp.h"
  46. #include "lwip/altcp_tcp.h"
  47. #include "lwip/priv/altcp_priv.h"
  48. #include "lwip/tcp.h"
  49. #include "lwip/priv/tcp_priv.h"
  50. #include "lwip/mem.h"
  51. #include <string.h>
  52. #define ALTCP_TCP_ASSERT_CONN(conn) do { \
  53. LWIP_ASSERT("conn->inner_conn == NULL", (conn)->inner_conn == NULL); \
  54. LWIP_UNUSED_ARG(conn); /* for LWIP_NOASSERT */ } while(0)
  55. #define ALTCP_TCP_ASSERT_CONN_PCB(conn, tpcb) do { \
  56. LWIP_ASSERT("pcb mismatch", (conn)->state == tpcb); \
  57. LWIP_UNUSED_ARG(tpcb); /* for LWIP_NOASSERT */ \
  58. ALTCP_TCP_ASSERT_CONN(conn); } while(0)
  59. /* Variable prototype, the actual declaration is at the end of this file
  60. since it contains pointers to static functions declared here */
  61. extern const struct altcp_functions altcp_tcp_functions;
  62. static void altcp_tcp_setup(struct altcp_pcb *conn, struct tcp_pcb *tpcb);
  63. /* callback functions for TCP */
  64. static err_t
  65. altcp_tcp_accept(void *arg, struct tcp_pcb *new_tpcb, err_t err)
  66. {
  67. struct altcp_pcb *listen_conn = (struct altcp_pcb *)arg;
  68. if (new_tpcb && listen_conn && listen_conn->accept) {
  69. /* create a new altcp_conn to pass to the next 'accept' callback */
  70. struct altcp_pcb *new_conn = altcp_alloc();
  71. if (new_conn == NULL) {
  72. return ERR_MEM;
  73. }
  74. altcp_tcp_setup(new_conn, new_tpcb);
  75. return listen_conn->accept(listen_conn->arg, new_conn, err);
  76. }
  77. return ERR_ARG;
  78. }
  79. static err_t
  80. altcp_tcp_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  81. {
  82. struct altcp_pcb *conn = (struct altcp_pcb *)arg;
  83. if (conn) {
  84. ALTCP_TCP_ASSERT_CONN_PCB(conn, tpcb);
  85. if (conn->connected) {
  86. return conn->connected(conn->arg, conn, err);
  87. }
  88. }
  89. return ERR_OK;
  90. }
  91. static err_t
  92. altcp_tcp_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  93. {
  94. struct altcp_pcb *conn = (struct altcp_pcb *)arg;
  95. if (conn) {
  96. ALTCP_TCP_ASSERT_CONN_PCB(conn, tpcb);
  97. if (conn->recv) {
  98. return conn->recv(conn->arg, conn, p, err);
  99. }
  100. }
  101. if (p != NULL) {
  102. /* prevent memory leaks */
  103. pbuf_free(p);
  104. }
  105. return ERR_OK;
  106. }
  107. static err_t
  108. altcp_tcp_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  109. {
  110. struct altcp_pcb *conn = (struct altcp_pcb *)arg;
  111. if (conn) {
  112. ALTCP_TCP_ASSERT_CONN_PCB(conn, tpcb);
  113. if (conn->sent) {
  114. return conn->sent(conn->arg, conn, len);
  115. }
  116. }
  117. return ERR_OK;
  118. }
  119. static err_t
  120. altcp_tcp_poll(void *arg, struct tcp_pcb *tpcb)
  121. {
  122. struct altcp_pcb *conn = (struct altcp_pcb *)arg;
  123. if (conn) {
  124. ALTCP_TCP_ASSERT_CONN_PCB(conn, tpcb);
  125. if (conn->poll) {
  126. return conn->poll(conn->arg, conn);
  127. }
  128. }
  129. return ERR_OK;
  130. }
  131. static void
  132. altcp_tcp_err(void *arg, err_t err)
  133. {
  134. struct altcp_pcb *conn = (struct altcp_pcb *)arg;
  135. if (conn) {
  136. conn->state = NULL; /* already freed */
  137. if (conn->err) {
  138. conn->err(conn->arg, err);
  139. }
  140. altcp_free(conn);
  141. }
  142. }
  143. /* setup functions */
  144. static void
  145. altcp_tcp_remove_callbacks(struct tcp_pcb *tpcb)
  146. {
  147. tcp_arg(tpcb, NULL);
  148. if (tpcb->state != LISTEN) {
  149. tcp_recv(tpcb, NULL);
  150. tcp_sent(tpcb, NULL);
  151. tcp_err(tpcb, NULL);
  152. tcp_poll(tpcb, NULL, tpcb->pollinterval);
  153. }
  154. }
  155. static void
  156. altcp_tcp_setup_callbacks(struct altcp_pcb *conn, struct tcp_pcb *tpcb)
  157. {
  158. tcp_arg(tpcb, conn);
  159. /* this might be called for LISTN when close fails... */
  160. if (tpcb->state != LISTEN) {
  161. tcp_recv(tpcb, altcp_tcp_recv);
  162. tcp_sent(tpcb, altcp_tcp_sent);
  163. tcp_err(tpcb, altcp_tcp_err);
  164. /* tcp_poll is set when interval is set by application */
  165. }
  166. }
  167. static void
  168. altcp_tcp_setup(struct altcp_pcb *conn, struct tcp_pcb *tpcb)
  169. {
  170. altcp_tcp_setup_callbacks(conn, tpcb);
  171. conn->state = tpcb;
  172. conn->fns = &altcp_tcp_functions;
  173. }
  174. struct altcp_pcb *
  175. altcp_tcp_new_ip_type(u8_t ip_type)
  176. {
  177. /* Allocate the tcp pcb first to invoke the priority handling code
  178. if we're out of pcbs */
  179. struct tcp_pcb *tpcb = tcp_new_ip_type(ip_type);
  180. if (tpcb != NULL) {
  181. struct altcp_pcb *ret = altcp_alloc();
  182. if (ret != NULL) {
  183. altcp_tcp_setup(ret, tpcb);
  184. return ret;
  185. } else {
  186. /* altcp_pcb allocation failed -> free the tcp_pcb too */
  187. tcp_close(tpcb);
  188. }
  189. }
  190. return NULL;
  191. }
  192. /** altcp_tcp allocator function fitting to @ref altcp_allocator_t / @ref altcp_new.
  193. *
  194. * arg pointer is not used for TCP.
  195. */
  196. struct altcp_pcb *
  197. altcp_tcp_alloc(void *arg, u8_t ip_type)
  198. {
  199. LWIP_UNUSED_ARG(arg);
  200. return altcp_tcp_new_ip_type(ip_type);
  201. }
  202. struct altcp_pcb *
  203. altcp_tcp_wrap(struct tcp_pcb *tpcb)
  204. {
  205. if (tpcb != NULL) {
  206. struct altcp_pcb *ret = altcp_alloc();
  207. if (ret != NULL) {
  208. altcp_tcp_setup(ret, tpcb);
  209. return ret;
  210. }
  211. }
  212. return NULL;
  213. }
  214. /* "virtual" functions calling into tcp */
  215. static void
  216. altcp_tcp_set_poll(struct altcp_pcb *conn, u8_t interval)
  217. {
  218. if (conn != NULL) {
  219. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  220. ALTCP_TCP_ASSERT_CONN(conn);
  221. tcp_poll(pcb, altcp_tcp_poll, interval);
  222. }
  223. }
  224. static void
  225. altcp_tcp_recved(struct altcp_pcb *conn, u16_t len)
  226. {
  227. if (conn != NULL) {
  228. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  229. ALTCP_TCP_ASSERT_CONN(conn);
  230. tcp_recved(pcb, len);
  231. }
  232. }
  233. static err_t
  234. altcp_tcp_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port)
  235. {
  236. struct tcp_pcb *pcb;
  237. if (conn == NULL) {
  238. return ERR_VAL;
  239. }
  240. ALTCP_TCP_ASSERT_CONN(conn);
  241. pcb = (struct tcp_pcb *)conn->state;
  242. return tcp_bind(pcb, ipaddr, port);
  243. }
  244. static err_t
  245. altcp_tcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected)
  246. {
  247. struct tcp_pcb *pcb;
  248. if (conn == NULL) {
  249. return ERR_VAL;
  250. }
  251. ALTCP_TCP_ASSERT_CONN(conn);
  252. conn->connected = connected;
  253. pcb = (struct tcp_pcb *)conn->state;
  254. return tcp_connect(pcb, ipaddr, port, altcp_tcp_connected);
  255. }
  256. static struct altcp_pcb *
  257. altcp_tcp_listen(struct altcp_pcb *conn, u8_t backlog, err_t *err)
  258. {
  259. struct tcp_pcb *pcb;
  260. struct tcp_pcb *lpcb;
  261. if (conn == NULL) {
  262. return NULL;
  263. }
  264. ALTCP_TCP_ASSERT_CONN(conn);
  265. pcb = (struct tcp_pcb *)conn->state;
  266. lpcb = tcp_listen_with_backlog_and_err(pcb, backlog, err);
  267. if (lpcb != NULL) {
  268. conn->state = lpcb;
  269. tcp_accept(lpcb, altcp_tcp_accept);
  270. return conn;
  271. }
  272. return NULL;
  273. }
  274. static void
  275. altcp_tcp_abort(struct altcp_pcb *conn)
  276. {
  277. if (conn != NULL) {
  278. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  279. ALTCP_TCP_ASSERT_CONN(conn);
  280. if (pcb) {
  281. tcp_abort(pcb);
  282. }
  283. }
  284. }
  285. static err_t
  286. altcp_tcp_close(struct altcp_pcb *conn)
  287. {
  288. struct tcp_pcb *pcb;
  289. if (conn == NULL) {
  290. return ERR_VAL;
  291. }
  292. ALTCP_TCP_ASSERT_CONN(conn);
  293. pcb = (struct tcp_pcb *)conn->state;
  294. if (pcb) {
  295. err_t err;
  296. tcp_poll_fn oldpoll = pcb->poll;
  297. altcp_tcp_remove_callbacks(pcb);
  298. err = tcp_close(pcb);
  299. if (err != ERR_OK) {
  300. /* not closed, set up all callbacks again */
  301. altcp_tcp_setup_callbacks(conn, pcb);
  302. /* poll callback is not included in the above */
  303. tcp_poll(pcb, oldpoll, pcb->pollinterval);
  304. return err;
  305. }
  306. conn->state = NULL; /* unsafe to reference pcb after tcp_close(). */
  307. }
  308. altcp_free(conn);
  309. return ERR_OK;
  310. }
  311. static err_t
  312. altcp_tcp_shutdown(struct altcp_pcb *conn, int shut_rx, int shut_tx)
  313. {
  314. struct tcp_pcb *pcb;
  315. if (conn == NULL) {
  316. return ERR_VAL;
  317. }
  318. ALTCP_TCP_ASSERT_CONN(conn);
  319. pcb = (struct tcp_pcb *)conn->state;
  320. return tcp_shutdown(pcb, shut_rx, shut_tx);
  321. }
  322. static err_t
  323. altcp_tcp_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t apiflags)
  324. {
  325. struct tcp_pcb *pcb;
  326. if (conn == NULL) {
  327. return ERR_VAL;
  328. }
  329. ALTCP_TCP_ASSERT_CONN(conn);
  330. pcb = (struct tcp_pcb *)conn->state;
  331. return tcp_write(pcb, dataptr, len, apiflags);
  332. }
  333. static err_t
  334. altcp_tcp_output(struct altcp_pcb *conn)
  335. {
  336. struct tcp_pcb *pcb;
  337. if (conn == NULL) {
  338. return ERR_VAL;
  339. }
  340. ALTCP_TCP_ASSERT_CONN(conn);
  341. pcb = (struct tcp_pcb *)conn->state;
  342. return tcp_output(pcb);
  343. }
  344. static u16_t
  345. altcp_tcp_mss(struct altcp_pcb *conn)
  346. {
  347. struct tcp_pcb *pcb;
  348. if (conn == NULL) {
  349. return 0;
  350. }
  351. ALTCP_TCP_ASSERT_CONN(conn);
  352. pcb = (struct tcp_pcb *)conn->state;
  353. return tcp_mss(pcb);
  354. }
  355. static u16_t
  356. altcp_tcp_sndbuf(struct altcp_pcb *conn)
  357. {
  358. struct tcp_pcb *pcb;
  359. if (conn == NULL) {
  360. return 0;
  361. }
  362. ALTCP_TCP_ASSERT_CONN(conn);
  363. pcb = (struct tcp_pcb *)conn->state;
  364. return tcp_sndbuf(pcb);
  365. }
  366. static u16_t
  367. altcp_tcp_sndqueuelen(struct altcp_pcb *conn)
  368. {
  369. struct tcp_pcb *pcb;
  370. if (conn == NULL) {
  371. return 0;
  372. }
  373. ALTCP_TCP_ASSERT_CONN(conn);
  374. pcb = (struct tcp_pcb *)conn->state;
  375. return tcp_sndqueuelen(pcb);
  376. }
  377. static void
  378. altcp_tcp_nagle_disable(struct altcp_pcb *conn)
  379. {
  380. if (conn && conn->state) {
  381. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  382. ALTCP_TCP_ASSERT_CONN(conn);
  383. tcp_nagle_disable(pcb);
  384. }
  385. }
  386. static void
  387. altcp_tcp_nagle_enable(struct altcp_pcb *conn)
  388. {
  389. if (conn && conn->state) {
  390. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  391. ALTCP_TCP_ASSERT_CONN(conn);
  392. tcp_nagle_enable(pcb);
  393. }
  394. }
  395. static int
  396. altcp_tcp_nagle_disabled(struct altcp_pcb *conn)
  397. {
  398. if (conn && conn->state) {
  399. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  400. ALTCP_TCP_ASSERT_CONN(conn);
  401. return tcp_nagle_disabled(pcb);
  402. }
  403. return 0;
  404. }
  405. static void
  406. altcp_tcp_setprio(struct altcp_pcb *conn, u8_t prio)
  407. {
  408. if (conn != NULL) {
  409. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  410. ALTCP_TCP_ASSERT_CONN(conn);
  411. tcp_setprio(pcb, prio);
  412. }
  413. }
  414. #if LWIP_TCP_KEEPALIVE
  415. static void
  416. altcp_tcp_keepalive_disable(struct altcp_pcb *conn)
  417. {
  418. if (conn && conn->state) {
  419. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  420. ALTCP_TCP_ASSERT_CONN(conn);
  421. ip_reset_option(pcb, SOF_KEEPALIVE);
  422. }
  423. }
  424. static void
  425. altcp_tcp_keepalive_enable(struct altcp_pcb *conn, u32_t idle, u32_t intvl, u32_t cnt)
  426. {
  427. if (conn && conn->state) {
  428. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  429. ALTCP_TCP_ASSERT_CONN(conn);
  430. ip_set_option(pcb, SOF_KEEPALIVE);
  431. pcb->keep_idle = idle ? idle : TCP_KEEPIDLE_DEFAULT;
  432. pcb->keep_intvl = intvl ? intvl : TCP_KEEPINTVL_DEFAULT;
  433. pcb->keep_cnt = cnt ? cnt : TCP_KEEPCNT_DEFAULT;
  434. }
  435. }
  436. #endif
  437. static void
  438. altcp_tcp_dealloc(struct altcp_pcb *conn)
  439. {
  440. LWIP_UNUSED_ARG(conn);
  441. ALTCP_TCP_ASSERT_CONN(conn);
  442. /* no private state to clean up */
  443. }
  444. static err_t
  445. altcp_tcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port)
  446. {
  447. if (conn) {
  448. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  449. ALTCP_TCP_ASSERT_CONN(conn);
  450. return tcp_tcp_get_tcp_addrinfo(pcb, local, addr, port);
  451. }
  452. return ERR_VAL;
  453. }
  454. static ip_addr_t *
  455. altcp_tcp_get_ip(struct altcp_pcb *conn, int local)
  456. {
  457. if (conn) {
  458. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  459. ALTCP_TCP_ASSERT_CONN(conn);
  460. if (pcb) {
  461. if (local) {
  462. return &pcb->local_ip;
  463. } else {
  464. return &pcb->remote_ip;
  465. }
  466. }
  467. }
  468. return NULL;
  469. }
  470. static u16_t
  471. altcp_tcp_get_port(struct altcp_pcb *conn, int local)
  472. {
  473. if (conn) {
  474. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  475. ALTCP_TCP_ASSERT_CONN(conn);
  476. if (pcb) {
  477. if (local) {
  478. return pcb->local_port;
  479. } else {
  480. return pcb->remote_port;
  481. }
  482. }
  483. }
  484. return 0;
  485. }
  486. #ifdef LWIP_DEBUG
  487. static enum tcp_state
  488. altcp_tcp_dbg_get_tcp_state(struct altcp_pcb *conn)
  489. {
  490. if (conn) {
  491. struct tcp_pcb *pcb = (struct tcp_pcb *)conn->state;
  492. ALTCP_TCP_ASSERT_CONN(conn);
  493. if (pcb) {
  494. return pcb->state;
  495. }
  496. }
  497. return CLOSED;
  498. }
  499. #endif
  500. const struct altcp_functions altcp_tcp_functions = {
  501. altcp_tcp_set_poll,
  502. altcp_tcp_recved,
  503. altcp_tcp_bind,
  504. altcp_tcp_connect,
  505. altcp_tcp_listen,
  506. altcp_tcp_abort,
  507. altcp_tcp_close,
  508. altcp_tcp_shutdown,
  509. altcp_tcp_write,
  510. altcp_tcp_output,
  511. altcp_tcp_mss,
  512. altcp_tcp_sndbuf,
  513. altcp_tcp_sndqueuelen,
  514. altcp_tcp_nagle_disable,
  515. altcp_tcp_nagle_enable,
  516. altcp_tcp_nagle_disabled,
  517. altcp_tcp_setprio,
  518. altcp_tcp_dealloc,
  519. altcp_tcp_get_tcp_addrinfo,
  520. altcp_tcp_get_ip,
  521. altcp_tcp_get_port
  522. #if LWIP_TCP_KEEPALIVE
  523. , altcp_tcp_keepalive_disable
  524. , altcp_tcp_keepalive_enable
  525. #endif
  526. #ifdef LWIP_DEBUG
  527. , altcp_tcp_dbg_get_tcp_state
  528. #endif
  529. };
  530. #endif /* LWIP_ALTCP */