tcp_helper.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "tcp_helper.h"
  2. #include "lwip/tcp.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_bound_pcbs);
  26. tcp_remove(tcp_listen_pcbs.pcbs);
  27. tcp_remove(tcp_active_pcbs);
  28. tcp_remove(tcp_tw_pcbs);
  29. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  30. fail_unless(lwip_stats.memp[MEMP_TCP_PCB_LISTEN].used == 0);
  31. fail_unless(lwip_stats.memp[MEMP_TCP_SEG].used == 0);
  32. fail_unless(lwip_stats.memp[MEMP_PBUF_POOL].used == 0);
  33. }
  34. /** Create a TCP segment usable for passing to tcp_input
  35. * - IP-addresses, ports, seqno and ackno are taken from pcb
  36. * - seqno and ackno can be altered with an offset
  37. */
  38. struct pbuf*
  39. tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset,
  40. u32_t ackno_offset, u8_t headerflags)
  41. {
  42. return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
  43. data, data_len, pcb->rcv_nxt + seqno_offset, pcb->snd_nxt + ackno_offset, headerflags);
  44. }
  45. /** Create a TCP segment usable for passing to tcp_input */
  46. struct pbuf*
  47. tcp_create_segment(struct ip_addr* src_ip, struct ip_addr* dst_ip,
  48. u16_t src_port, u16_t dst_port, void* data, size_t data_len,
  49. u32_t seqno, u32_t ackno, u8_t headerflags)
  50. {
  51. struct pbuf* p;
  52. struct ip_hdr* iphdr;
  53. struct tcp_hdr* tcphdr;
  54. u16_t pbuf_len = sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len;
  55. p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
  56. EXPECT_RETNULL(p != NULL);
  57. EXPECT_RETNULL(p->next == NULL);
  58. memset(p->payload, 0, p->len);
  59. iphdr = p->payload;
  60. /* fill IP header */
  61. iphdr->dest.addr = dst_ip->addr;
  62. iphdr->src.addr = src_ip->addr;
  63. IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, 0);
  64. IPH_LEN_SET(iphdr, htons(p->tot_len));
  65. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  66. pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
  67. tcphdr = p->payload;
  68. tcphdr->src = htons(src_port);
  69. tcphdr->dest = htons(dst_port);
  70. tcphdr->seqno = htonl(seqno);
  71. tcphdr->ackno = htonl(ackno);
  72. TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
  73. TCPH_FLAGS_SET(tcphdr, headerflags);
  74. tcphdr->wnd = htonl(TCP_WND);
  75. /* copy data */
  76. memcpy((char*)tcphdr + sizeof(struct tcp_hdr), data, data_len);
  77. /* calculate checksum */
  78. tcphdr->chksum = inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest),
  79. IP_PROTO_TCP, p->tot_len);
  80. pbuf_header(p, sizeof(struct ip_hdr));
  81. return p;
  82. }
  83. /** Safely bring a tcp_pcb into the requested state */
  84. void
  85. tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, struct ip_addr* local_ip,
  86. struct ip_addr* remote_ip, u16_t local_port, u16_t remote_port)
  87. {
  88. /* @todo: are these all states? */
  89. /* @todo: remove from previous list */
  90. pcb->state = state;
  91. if (state == ESTABLISHED) {
  92. TCP_REG(&tcp_active_pcbs, pcb);
  93. pcb->local_ip.addr = local_ip->addr;
  94. pcb->local_port = local_port;
  95. pcb->remote_ip.addr = remote_ip->addr;
  96. pcb->remote_port = remote_port;
  97. } else if(state == LISTEN) {
  98. TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
  99. pcb->local_ip.addr = local_ip->addr;
  100. pcb->local_port = local_port;
  101. } else if(state == TIME_WAIT) {
  102. TCP_REG(&tcp_tw_pcbs, pcb);
  103. pcb->local_ip.addr = local_ip->addr;
  104. pcb->local_port = local_port;
  105. pcb->remote_ip.addr = remote_ip->addr;
  106. pcb->remote_port = remote_port;
  107. } else {
  108. fail();
  109. }
  110. }
  111. void
  112. test_tcp_counters_err(void* arg, err_t err)
  113. {
  114. struct test_tcp_counters* counters = arg;
  115. EXPECT_RET(arg != NULL);
  116. counters->err_calls++;
  117. counters->last_err = err;
  118. }
  119. static void
  120. test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf* p)
  121. {
  122. struct pbuf* q;
  123. u32_t i, received;
  124. if(counters->expected_data == NULL) {
  125. /* no data to compare */
  126. return;
  127. }
  128. EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
  129. received = counters->recved_bytes;
  130. for(q = p; q != NULL; q = q->next) {
  131. char *data = q->payload;
  132. for(i = 0; i < q->len; i++) {
  133. EXPECT_RET(data[i] == counters->expected_data[received]);
  134. received++;
  135. }
  136. }
  137. EXPECT(received == counters->recved_bytes + p->tot_len);
  138. }
  139. err_t
  140. test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
  141. {
  142. struct test_tcp_counters* counters = arg;
  143. EXPECT_RETX(arg != NULL, ERR_OK);
  144. EXPECT_RETX(pcb != NULL, ERR_OK);
  145. EXPECT_RETX(err == ERR_OK, ERR_OK);
  146. if (p != NULL) {
  147. if (counters->close_calls == 0) {
  148. counters->recv_calls++;
  149. test_tcp_counters_check_rxdata(counters, p);
  150. counters->recved_bytes += p->tot_len;
  151. } else {
  152. counters->recv_calls_after_close++;
  153. counters->recved_bytes_after_close += p->tot_len;
  154. }
  155. pbuf_free(p);
  156. } else {
  157. counters->close_calls++;
  158. }
  159. EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0);
  160. return ERR_OK;
  161. }
  162. /** Allocate a pcb and set up the test_tcp_counters_* callbacks */
  163. struct tcp_pcb*
  164. test_tcp_new_counters_pcb(struct test_tcp_counters* counters)
  165. {
  166. struct tcp_pcb* pcb = tcp_new();
  167. if (pcb != NULL) {
  168. /* set up args and callbacks */
  169. tcp_arg(pcb, counters);
  170. tcp_recv(pcb, test_tcp_counters_recv);
  171. tcp_err(pcb, test_tcp_counters_err);
  172. }
  173. return pcb;
  174. }