tcp_helper.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "tcp_helper.h"
  2. #include "lwip/priv/tcp_priv.h"
  3. #include "lwip/stats.h"
  4. #include "lwip/pbuf.h"
  5. #include "lwip/inet_chksum.h"
  6. #include "lwip/ip_addr.h"
  7. #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
  8. #error "This tests needs TCP- and MEMP-statistics enabled"
  9. #endif
  10. /** Remove all pcbs on the given list. */
  11. static void
  12. tcp_remove(struct tcp_pcb* pcb_list)
  13. {
  14. struct tcp_pcb *pcb = pcb_list;
  15. struct tcp_pcb *pcb2;
  16. while(pcb != NULL) {
  17. pcb2 = pcb;
  18. pcb = pcb->next;
  19. tcp_abort(pcb2);
  20. }
  21. }
  22. /** Remove all pcbs on listen-, active- and time-wait-list (bound- isn't exported). */
  23. void
  24. tcp_remove_all(void)
  25. {
  26. tcp_remove(tcp_listen_pcbs.pcbs);
  27. tcp_remove(tcp_active_pcbs);
  28. tcp_remove(tcp_tw_pcbs);
  29. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  30. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
  31. fail_unless(MEMP_STATS_GET(used, MEMP_TCP_SEG) == 0);
  32. fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
  33. }
  34. /** Create a TCP segment usable for passing to tcp_input */
  35. static struct pbuf*
  36. tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
  37. u16_t src_port, u16_t dst_port, void* data, size_t data_len,
  38. u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd)
  39. {
  40. struct pbuf *p, *q;
  41. struct ip_hdr* iphdr;
  42. struct tcp_hdr* tcphdr;
  43. u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
  44. LWIP_ASSERT("data_len too big", data_len <= 0xFFFF);
  45. p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
  46. EXPECT_RETNULL(p != NULL);
  47. /* first pbuf must be big enough to hold the headers */
  48. EXPECT_RETNULL(p->len >= (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
  49. if (data_len > 0) {
  50. /* first pbuf must be big enough to hold at least 1 data byte, too */
  51. EXPECT_RETNULL(p->len > (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
  52. }
  53. for(q = p; q != NULL; q = q->next) {
  54. memset(q->payload, 0, q->len);
  55. }
  56. iphdr = (struct ip_hdr*)p->payload;
  57. /* fill IP header */
  58. iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
  59. iphdr->src.addr = ip_2_ip4(src_ip)->addr;
  60. IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
  61. IPH_TOS_SET(iphdr, 0);
  62. IPH_LEN_SET(iphdr, htons(p->tot_len));
  63. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  64. /* let p point to TCP header */
  65. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  66. tcphdr = (struct tcp_hdr*)p->payload;
  67. tcphdr->src = htons(src_port);
  68. tcphdr->dest = htons(dst_port);
  69. tcphdr->seqno = htonl(seqno);
  70. tcphdr->ackno = htonl(ackno);
  71. TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
  72. TCPH_FLAGS_SET(tcphdr, headerflags);
  73. tcphdr->wnd = htons(wnd);
  74. if (data_len > 0) {
  75. /* let p point to TCP data */
  76. pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr));
  77. /* copy data */
  78. pbuf_take(p, data, (u16_t)data_len);
  79. /* let p point to TCP header again */
  80. pbuf_header(p, sizeof(struct tcp_hdr));
  81. }
  82. /* calculate checksum */
  83. tcphdr->chksum = ip_chksum_pseudo(p,
  84. IP_PROTO_TCP, p->tot_len, src_ip, dst_ip);
  85. pbuf_header(p, sizeof(struct ip_hdr));
  86. return p;
  87. }
  88. /** Create a TCP segment usable for passing to tcp_input */
  89. struct pbuf*
  90. tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip,
  91. u16_t src_port, u16_t dst_port, void* data, size_t data_len,
  92. u32_t seqno, u32_t ackno, u8_t headerflags)
  93. {
  94. return tcp_create_segment_wnd(src_ip, dst_ip, src_port, dst_port, data,
  95. data_len, seqno, ackno, headerflags, TCP_WND);
  96. }
  97. /** Create a TCP segment usable for passing to tcp_input
  98. * - IP-addresses, ports, seqno and ackno are taken from pcb
  99. * - seqno and ackno can be altered with an offset
  100. */
  101. struct pbuf*
  102. tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset,
  103. u32_t ackno_offset, u8_t headerflags)
  104. {
  105. return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
  106. data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags);
  107. }
  108. /** Create a TCP segment usable for passing to tcp_input
  109. * - IP-addresses, ports, seqno and ackno are taken from pcb
  110. * - seqno and ackno can be altered with an offset
  111. * - TCP window can be adjusted
  112. */
  113. struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len,
  114. u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd)
  115. {
  116. return tcp_create_segment_wnd(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
  117. data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags, wnd);
  118. }
  119. /** Safely bring a tcp_pcb into the requested state */
  120. void
  121. tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip,
  122. ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
  123. {
  124. u32_t iss;
  125. /* @todo: are these all states? */
  126. /* @todo: remove from previous list */
  127. pcb->state = state;
  128. iss = tcp_next_iss(pcb);
  129. pcb->snd_wl2 = iss;
  130. pcb->snd_nxt = iss;
  131. pcb->lastack = iss;
  132. pcb->snd_lbb = iss;
  133. if (state == ESTABLISHED) {
  134. TCP_REG(&tcp_active_pcbs, pcb);
  135. ip_addr_copy(pcb->local_ip, *local_ip);
  136. pcb->local_port = local_port;
  137. ip_addr_copy(pcb->remote_ip, *remote_ip);
  138. pcb->remote_port = remote_port;
  139. } else if(state == LISTEN) {
  140. TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
  141. ip_addr_copy(pcb->local_ip, *local_ip);
  142. pcb->local_port = local_port;
  143. } else if(state == TIME_WAIT) {
  144. TCP_REG(&tcp_tw_pcbs, pcb);
  145. ip_addr_copy(pcb->local_ip, *local_ip);
  146. pcb->local_port = local_port;
  147. ip_addr_copy(pcb->remote_ip, *remote_ip);
  148. pcb->remote_port = remote_port;
  149. } else {
  150. fail();
  151. }
  152. }
  153. void
  154. test_tcp_counters_err(void* arg, err_t err)
  155. {
  156. struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
  157. EXPECT_RET(arg != NULL);
  158. counters->err_calls++;
  159. counters->last_err = err;
  160. }
  161. static void
  162. test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf* p)
  163. {
  164. struct pbuf* q;
  165. u32_t i, received;
  166. if(counters->expected_data == NULL) {
  167. /* no data to compare */
  168. return;
  169. }
  170. EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
  171. received = counters->recved_bytes;
  172. for(q = p; q != NULL; q = q->next) {
  173. char *data = (char*)q->payload;
  174. for(i = 0; i < q->len; i++) {
  175. EXPECT_RET(data[i] == counters->expected_data[received]);
  176. received++;
  177. }
  178. }
  179. EXPECT(received == counters->recved_bytes + p->tot_len);
  180. }
  181. err_t
  182. test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  183. {
  184. struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
  185. EXPECT_RETX(arg != NULL, ERR_OK);
  186. EXPECT_RETX(pcb != NULL, ERR_OK);
  187. EXPECT_RETX(err == ERR_OK, ERR_OK);
  188. if (p != NULL) {
  189. if (counters->close_calls == 0) {
  190. counters->recv_calls++;
  191. test_tcp_counters_check_rxdata(counters, p);
  192. counters->recved_bytes += p->tot_len;
  193. } else {
  194. counters->recv_calls_after_close++;
  195. counters->recved_bytes_after_close += p->tot_len;
  196. }
  197. pbuf_free(p);
  198. } else {
  199. counters->close_calls++;
  200. }
  201. EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0);
  202. return ERR_OK;
  203. }
  204. /** Allocate a pcb and set up the test_tcp_counters_* callbacks */
  205. struct tcp_pcb*
  206. test_tcp_new_counters_pcb(struct test_tcp_counters* counters)
  207. {
  208. struct tcp_pcb* pcb = tcp_new();
  209. if (pcb != NULL) {
  210. /* set up args and callbacks */
  211. tcp_arg(pcb, counters);
  212. tcp_recv(pcb, test_tcp_counters_recv);
  213. tcp_err(pcb, test_tcp_counters_err);
  214. pcb->snd_wnd = TCP_WND;
  215. pcb->snd_wnd_max = TCP_WND;
  216. }
  217. return pcb;
  218. }
  219. /** Calls tcp_input() after adjusting current_iphdr_dest */
  220. void test_tcp_input(struct pbuf *p, struct netif *inp)
  221. {
  222. struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
  223. /* these lines are a hack, don't use them as an example :-) */
  224. ip_addr_copy_from_ip4(*ip_current_dest_addr(), iphdr->dest);
  225. ip_addr_copy_from_ip4(*ip_current_src_addr(), iphdr->src);
  226. ip_current_netif() = inp;
  227. ip_data.current_ip4_header = iphdr;
  228. /* since adding IPv6, p->payload must point to tcp header, not ip header */
  229. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  230. tcp_input(p, inp);
  231. ip_addr_set_zero(ip_current_dest_addr());
  232. ip_addr_set_zero(ip_current_src_addr());
  233. ip_current_netif() = NULL;
  234. ip_data.current_ip4_header = NULL;
  235. }
  236. static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p,
  237. const ip4_addr_t *ipaddr)
  238. {
  239. struct test_tcp_txcounters *txcounters = (struct test_tcp_txcounters*)netif->state;
  240. LWIP_UNUSED_ARG(ipaddr);
  241. if (txcounters != NULL)
  242. {
  243. txcounters->num_tx_calls++;
  244. txcounters->num_tx_bytes += p->tot_len;
  245. if (txcounters->copy_tx_packets) {
  246. struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  247. err_t err;
  248. EXPECT(p_copy != NULL);
  249. err = pbuf_copy(p_copy, p);
  250. EXPECT(err == ERR_OK);
  251. if (txcounters->tx_packets == NULL) {
  252. txcounters->tx_packets = p_copy;
  253. } else {
  254. pbuf_cat(txcounters->tx_packets, p_copy);
  255. }
  256. }
  257. }
  258. return ERR_OK;
  259. }
  260. void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
  261. ip_addr_t *ip_addr, ip_addr_t *netmask)
  262. {
  263. struct netif *n;
  264. memset(netif, 0, sizeof(struct netif));
  265. if (txcounters != NULL) {
  266. memset(txcounters, 0, sizeof(struct test_tcp_txcounters));
  267. netif->state = txcounters;
  268. }
  269. netif->output = test_tcp_netif_output;
  270. netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
  271. ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
  272. ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
  273. for (n = netif_list; n != NULL; n = n->next) {
  274. if (n == netif) {
  275. return;
  276. }
  277. }
  278. netif->next = NULL;
  279. netif_list = netif;
  280. }