Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
lwip init network interfaces
Good day!
I want to make a client and connect it to netcat simple server.
1) Compilled lib with default settings and #define's (the only thing - i set some debug information on) 2) Linked lib to the project (works fine) 3) Set up virtual machine with ubuntu and made netcat working with
~$ sudo netcat -l -v 7
that means we are listening to port 7 (seven).
now I use ifconfig and get
inet addr:172.17.9.71 Bcast:172.17.11.255 Mask:255.255.252.0
fine for now.
4) ping and telnet work fine - I see that netcat is reachable by them .
my ifconfig stats are
inet addr:172.17.9.165 Bcast:172.17.11.255 Mask:255.255.252.0
5) Now I'm trying to connect to netcat server with my client, written by using lwip
output is :
LWIP_HAVE_LOOPIF = 0 LWIP_HAVE_LOOPIF = 0 tcp_bind: bind to port 55555 tcp_connect to port 7 netif_default = -780756800 netif_is_up(netif_default) = 0 ip_route: No route to 172.17.9.71 connect err = -4 netif_default = -780756800 netif_is_up(netif_default) = 0 ip_route: No route to 172.17.9.71 ip_output: No route to 172.17.9.71 Assertion "mem_free: mem->used" failed at line 339 in ../../../../../lwip/src/core/mem.c
the full listing :
const char *helloworld = "hello world\n"; void hello_end(struct tcp_pcb *pcb, u8_t *state) { tcp_err(pcb, NULL); tcp_recv(pcb, NULL); tcp_sent(pcb, NULL); tcp_poll(pcb, NULL, 0); mem_free(state); } err_t hello_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { u8_t *state = (u8_t *)arg; u16_t len; if (p == NULL) if (*state == 255) /* close send */ hello_end(pcb, state); else /* close not yet send */ *state |= 2; else { len = p->tot_len; pbuf_free(p); tcp_recved(pcb, len); } return ERR_OK; } void hello_err(void *arg, err_t err) { mem_free(arg); } err_t hello_poll_close(void *arg, struct tcp_pcb *pcb) { u8_t *state = (u8_t *)arg; if (tcp_close(pcb) == ERR_OK) { if ((*state & 2) == 2) /* close received */ hello_end(pcb, state); else /* close not yet received */ *state = 255; } return ERR_OK; } err_t hello_connected(void *arg, struct tcp_pcb *pcb, err_t err) { tcp_write(pcb, helloworld, 12, 0); return ERR_OK; } err_t hello_connect() { lwip_init(); u8_t *state; err_t err; struct tcp_pcb *pcb; ip_addr_t ipaddr; IP4_ADDR(&ipaddr, 172,17,9,71); if ((state = mem_malloc(1)) == NULL) { return ERR_MEM; } *state = 1; if ((pcb = tcp_new()) == NULL) { mem_free(state); return ERR_MEM; } tcp_arg(pcb, state); tcp_err(pcb, hello_err); tcp_recv(pcb, hello_recv); tcp_sent(pcb, NULL); tcp_poll(pcb, hello_poll_close, 10); tcp_bind(pcb,IPADDR_ANY, 55555); //Bind ourselvs to the port 55555 and my own adress err = tcp_connect(pcb, &ipaddr, 7, hello_connected); if (err != ERR_OK) { std::cout << "connect err = " << (int)err << std::endl; mem_free(state); tcp_abort(pcb); } getchar(); return ERR_OK; } int main(int argc, char** argv) { err_t err = hello_connect(); if (err != ERR_OK) { std::cout << "2err = " << err << std::endl; } std::cout << "End of Main" << std::endl; return 0; return 0; }
from here I started to think that the problem is that I didnt set up NETIF.
but I don't know HOW.
I think I don't want lwip to create tap0 by default
(it makes
tap0 Link encap:Ethernet HWaddr 86:97:2c:64:b7:78 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::8497:2cff:fe64:b778/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:33 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:500 RX bytes:0 (0.0 B) TX bytes:6555 (6.4 KiB)
)
I want to bind my application to localhost or to eth1 - whatever... How can I do this? What do I do wrong in my application?
By the way - when I do
char *eth = "eht1"; netif_set_default(netif_find(eth));
In my Init function I get this output
LWIP_HAVE_LOOPIF = 0 LWIP_HAVE_LOOPIF = 0 tcpip_thread: PACKET 0x7f72acaa1988 ip_input: packet not for us. tcp_bind: bind to port 55555 tcp_connect to port 7 netif_default = 0
and it falls on
err = tcp_connect(pcb, &ipaddr, 7, hello_connected);
(not passing it...)
Can some 1 help?