tcp_helper.c 9.9 KB

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