1
0

tcp_helper.c 9.8 KB

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