1 /* 2 * Copyright (c) 2018-2019 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @brief Transmission Control Protocol (TCP) 9 * 10 * - net_tcp_get() is called by net_context_get(AF_INET, SOCK_STREAM, 11 IPPROTO_TCP, ...) and creates struct tcp for the net_context 12 * - net_tcp_listen()/net_tcp_accept() listen/accept 13 * - At the reception of SYN on the listening net_context, a new pair 14 * of net_context/struct tcp registers a new net_conn handle 15 * with the tcp_recv() as a callback 16 * - net_tcp_queue() queues the data for the transmission 17 * - The incoming data is delivered up through the context->recv_cb 18 * - net_tcp_put() closes the connection 19 * 20 * NOTE: The present API is provided in order to make the integration 21 * into the ip stack and the socket layer less intrusive. 22 * 23 * Semantically cleaner use is possible (and might be exposed), 24 * look into the unit test tests/net/tcp for insights. 25 */ 26 27 #ifndef TCP_H 28 #define TCP_H 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <errno.h> 35 #include <sys/types.h> 36 37 /** 38 * @brief Allocate a TCP connection for the net_context 39 * and mutually link the net_context and TCP connection. 40 * 41 * @param context Network context 42 * 43 * @return 0 on success, < 0 on error 44 */ 45 int net_tcp_get(struct net_context *context); 46 47 /** 48 * @brief Close and delete the TCP connection for the net_context 49 * 50 * @param context Network context 51 * 52 * @return 0 on success, < 0 on error 53 */ 54 int net_tcp_put(struct net_context *context); 55 56 /** 57 * @brief Listen for an incoming TCP connection 58 * 59 * @param context Network context 60 * 61 * @return 0 if successful, < 0 on error 62 */ 63 int net_tcp_listen(struct net_context *context); 64 65 /** 66 * @brief Register an accept callback 67 * 68 * @param context Network context 69 * @param cb net_tcp_accept_cb_t callback 70 * @param user_data User data passed as an argument in the callback 71 * 72 * @return 0 if successful, < 0 on error 73 */ 74 int net_tcp_accept(struct net_context *context, net_tcp_accept_cb_t cb, 75 void *user_data); 76 77 /* TODO: split into 2 functions, conn -> context, queue -> send? */ 78 79 /* The following functions are provided solely for the compatibility 80 * with the old TCP 81 */ 82 83 /** 84 * @brief Return struct net_tcp_hdr pointer 85 * 86 * @param pkt Network packet 87 * @param tcp_access Helper variable for accessing TCP header 88 * 89 * @return Pointer to the TCP header on success, NULL on error 90 */ 91 struct net_tcp_hdr *net_tcp_input(struct net_pkt *pkt, 92 struct net_pkt_data_access *tcp_access); 93 /* TODO: net_tcp_input() isn't used by TCP and might be dropped with little 94 * re-factoring 95 */ 96 97 /* No ops, provided for compatibility with the old TCP */ 98 99 #if defined(CONFIG_NET_NATIVE_TCP) 100 void net_tcp_init(void); 101 #else 102 #define net_tcp_init(...) 103 #endif 104 int net_tcp_update_recv_wnd(struct net_context *context, int32_t delta); 105 int net_tcp_finalize(struct net_pkt *pkt, bool force_chksum); 106 107 #if defined(CONFIG_NET_TEST_PROTOCOL) 108 /** 109 * @brief Handle an incoming TCP packet 110 * 111 * This function is provided for the TCP sanity check and will be eventually 112 * dropped. 113 * 114 * @param pkt Network packet 115 */ 116 void tcp_input(struct net_pkt *pkt); 117 #endif 118 119 #ifdef __cplusplus 120 } 121 #endif 122 123 #endif /* TCP_H */ 124