Search code examples
ctcpembeddedpacketlwip

Raw LWIP Send TCP Transmission to Static IP


I've got the TCP Echo example working well on my hardware, and yesterday figured out how to get a UDP Broadcast working. After further thought, I've realized is that what I really need is to be able to set up a TCP Connection to a Static IP, the idea being that my hardware can connect to a server of some sort and then use that connection for all its transactions. The difference is that whereas the echo example sets up a passive connection, that binds with the incoming source (as I understand it), I want to initiate the connection deliberately to a known IP.

Based on what I found on Wikia Here Here

I've attempted as a base case to implement a function that can send a packet to a Defined IP. I'm simply trying to send a packet to my PC, and I'm looking for it on Wireshark.

void echo_tx_tcp()
{
  err_t wr_err = ERR_OK;
  struct tcp_pcb *l_tcp_pcb;
  l_tcp_pcb = tcp_new();
  ip_addr_t dest_ip =
  { ((u32_t)0x0C0C0C2BUL) };
  wr_err = tcp_bind(l_tcp_pcb, &dest_ip, 12);
  wr_err = tcp_connect(l_tcp_pcb, &dest_ip, 12, echo_accept);
  tcp_sent(l_tcp_pcb, echo_sent);

  struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 1024, PBUF_RAM);
  unsigned char buffer_send[1024] = "My Name Is TCP";
  p->payload = buffer_send;
  p->len = 1024;
  p->tot_len = 1024;

  wr_err = tcp_write(l_tcp_pcb, p->payload, p->len, 1);
  wr_err = tcp_output(l_tcp_pcb);

  if(wr_err == ERR_OK)
  {
    p->len++;
  }
  return;
}

The last if statement just exists so that I can inspect the wr_err value with a debugger. The err is coming back OK but the packet is not seen on wireshark. My setup is my hardare as well as my PC connected to a router in an isolated manner. The IP Address of the PC locally is 12.12.12.43

Am I missing a step here?


Solution

  • The tcp_write() function will fail and return ERR_MEM if:

    1. The length of the data exceeds the current send buffer size.
    2. The length of the queue of the outgoing segment is larger than the upper limit defined in lwipopts.h.

    The number of bytes available in the output queue can be retrieved with the tcp_sndbuf() function.

    Potential solution(s):

    1. Try again but send less data.
    2. Monitor the amount of space available in the send buffer and only send (more) data when there is space available in the send buffer.

    Suggestions:

    tcp_snd_buf() can be used to find out how much send buffer space is available.

    tcp_sent() can be implemented with callback function, that will be called when send butter space is available.