tcp_helper.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "tcp_helper.h"
  2. #include "lwip/tcp_impl.h"
  3. #include "lwip/stats.h"
  4. #include "lwip/pbuf.h"
  5. #include "lwip/inet_chksum.h"
  6. #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
  7. #error "This tests needs TCP- and MEMP-statistics enabled"
  8. #endif
  9. /** Remove all pcbs on the given list. */
  10. static void
  11. tcp_remove(struct tcp_pcb* pcb_list)
  12. {
  13. struct tcp_pcb *pcb = pcb_list;
  14. struct tcp_pcb *pcb2;
  15. while(pcb != NULL) {
  16. pcb2 = pcb;
  17. pcb = pcb->next;
  18. tcp_abort(pcb2);
  19. }
  20. }
  21. /** Remove all pcbs on listen-, active- and time-wait-list (bound- isn't exported). */
  22. void
  23. tcp_remove_all(void)
  24. {
  25. tcp_remove(tcp_listen_pcbs.pcbs);
  26. tcp_remove(tcp_active_pcbs);
  27. tcp_remove(tcp_tw_pcbs);
  28. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  29. fail_unless(lwip_stats.memp[MEMP_TCP_PCB_LISTEN].used == 0);
  30. fail_unless(lwip_stats.memp[MEMP_TCP_SEG].used == 0);
  31. fail_unless(lwip_stats.memp[MEMP_PBUF_POOL].used == 0);
  32. }
  33. /** Create a TCP segment usable for passing to tcp_input
  34. * - IP-addresses, ports, seqno and ackno are taken from pcb
  35. * - seqno and ackno can be altered with an offset
  36. */
  37. struct pbuf*
  38. tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset,
  39. u32_t ackno_offset, u8_t headerflags)
  40. {
  41. return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
  42. data, data_len, pcb->rcv_nxt + seqno_offset, pcb->snd_nxt + ackno_offset, headerflags);
  43. }
  44. /** Create a TCP segment usable for passing to tcp_input */
  45. struct pbuf*
  46. tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip,
  47. u16_t src_port, u16_t dst_port, void* data, size_t data_len,
  48. u32_t seqno, u32_t ackno, u8_t headerflags)
  49. {
  50. struct pbuf* p;
  51. struct ip_hdr* iphdr;
  52. struct tcp_hdr* tcphdr;
  53. u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
  54. p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
  55. EXPECT_RETNULL(p != NULL);
  56. EXPECT_RETNULL(p->next == NULL);
  57. memset(p->payload, 0, p->len);
  58. iphdr = p->payload;
  59. /* fill IP header */
  60. iphdr->dest.addr = dst_ip->addr;
  61. iphdr->src.addr = src_ip->addr;
  62. IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, 0);
  63. IPH_LEN_SET(iphdr, htons(p->tot_len));
  64. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  65. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  66. tcphdr = 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(TCP_WND);
  74. /* copy data */
  75. memcpy((char*)tcphdr + sizeof(struct tcp_hdr), data, data_len);
  76. /* calculate checksum */
  77. tcphdr->chksum = inet_chksum_pseudo(p, src_ip, dst_ip,
  78. IP_PROTO_TCP, p->tot_len);
  79. pbuf_header(p, sizeof(struct ip_hdr));
  80. return p;
  81. }
  82. /** Safely bring a tcp_pcb into the requested state */
  83. void
  84. tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip,
  85. ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
  86. {
  87. /* @todo: are these all states? */
  88. /* @todo: remove from previous list */
  89. pcb->state = state;
  90. if (state == ESTABLISHED) {
  91. TCP_REG(&tcp_active_pcbs, pcb);
  92. pcb->local_ip.addr = local_ip->addr;
  93. pcb->local_port = local_port;
  94. pcb->remote_ip.addr = remote_ip->addr;
  95. pcb->remote_port = remote_port;
  96. } else if(state == LISTEN) {
  97. TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
  98. pcb->local_ip.addr = local_ip->addr;
  99. pcb->local_port = local_port;
  100. } else if(state == TIME_WAIT) {
  101. TCP_REG(&tcp_tw_pcbs, pcb);
  102. pcb->local_ip.addr = local_ip->addr;
  103. pcb->local_port = local_port;
  104. pcb->remote_ip.addr = remote_ip->addr;
  105. pcb->remote_port = remote_port;
  106. } else {
  107. fail();
  108. }
  109. }
  110. void
  111. test_tcp_counters_err(void* arg, err_t err)
  112. {
  113. struct test_tcp_counters* counters = arg;
  114. EXPECT_RET(arg != NULL);
  115. counters->err_calls++;
  116. counters->last_err = err;
  117. }
  118. static void
  119. test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf* p)
  120. {
  121. struct pbuf* q;
  122. u32_t i, received;
  123. if(counters->expected_data == NULL) {
  124. /* no data to compare */
  125. return;
  126. }
  127. EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
  128. received = counters->recved_bytes;
  129. for(q = p; q != NULL; q = q->next) {
  130. char *data = q->payload;
  131. for(i = 0; i < q->len; i++) {
  132. EXPECT_RET(data[i] == counters->expected_data[received]);
  133. received++;
  134. }
  135. }
  136. EXPECT(received == counters->recved_bytes + p->tot_len);
  137. }
  138. err_t
  139. test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  140. {
  141. struct test_tcp_counters* counters = arg;
  142. EXPECT_RETX(arg != NULL, ERR_OK);
  143. EXPECT_RETX(pcb != NULL, ERR_OK);
  144. EXPECT_RETX(err == ERR_OK, ERR_OK);
  145. if (p != NULL) {
  146. if (counters->close_calls == 0) {
  147. counters->recv_calls++;
  148. test_tcp_counters_check_rxdata(counters, p);
  149. counters->recved_bytes += p->tot_len;
  150. } else {
  151. counters->recv_calls_after_close++;
  152. counters->recved_bytes_after_close += p->tot_len;
  153. }
  154. pbuf_free(p);
  155. } else {
  156. counters->close_calls++;
  157. }
  158. EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0);
  159. return ERR_OK;
  160. }
  161. /** Allocate a pcb and set up the test_tcp_counters_* callbacks */
  162. struct tcp_pcb*
  163. test_tcp_new_counters_pcb(struct test_tcp_counters* counters)
  164. {
  165. struct tcp_pcb* pcb = tcp_new();
  166. if (pcb != NULL) {
  167. /* set up args and callbacks */
  168. tcp_arg(pcb, counters);
  169. tcp_recv(pcb, test_tcp_counters_recv);
  170. tcp_err(pcb, test_tcp_counters_err);
  171. }
  172. return pcb;
  173. }
  174. /** Calls tcp_input() after adjusting current_iphdr_dest */
  175. void test_tcp_input(struct pbuf *p, struct netif *inp)
  176. {
  177. struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
  178. ip_addr_copy(current_iphdr_dest, iphdr->dest);
  179. ip_addr_copy(current_iphdr_src, iphdr->src);
  180. current_netif = inp;
  181. current_header = iphdr;
  182. tcp_input(p, inp);
  183. current_iphdr_dest.addr = 0;
  184. current_iphdr_src.addr = 0;
  185. current_netif = NULL;
  186. current_header = NULL;
  187. }