linux - Bridging ethernet traffic between two interfaces (USB-CDC ECM and Ethernet) using an mbed LPC1768 -
i'm working on project have lpc1768 mbed device. can connect device usb computer. device has working outgoing ethernet connection can read using mbed library.
on embedded device, internet traffic enters usb-cdc ecm protocol. want give packets working ethernet interface. have following code:
#include "mbed.h" #include "usbcdc_ecm.h" #include "rtos.h" extern "c"{ #include <stdint.h> } ethernet eth; // create buffer #define eth_mtu 1514 static uint8_t buf[eth_mtu]; static char ethbuf[eth_mtu]; int main(void) { usbcdc_ecm usb = usbcdc_ecm(0x0525, 0xa4a1, 1); wait(1); int rx_i = 0; while(1) { int size = eth.receive(); if(size > 0) { eth.read(ethbuf, size); printf("read ethernet: %d\r\n", size); // send data usb uint8_t *usb_data = (uint8_t *)malloc(sizeof(uint8_t) * size); memcpy(ethbuf, usb_data, size); int sent = 0; while(sent < size) { int to_send = 64; if ((size - sent) < to_send) { to_send = size - sent; } int res = usb.send(usb_data + sent, to_send); printf("result sending usb: %d\r\n", res); sent += to_send; } if ((size % 64) == 0) { usb.send(usb_data, 0); } } bool ret = usb.readep_nb(buf + rx_i, (uint32_t *)&size); if(!ret) { continue; } rx_i += size; if(size < 64) { printf("received frame size: %d\r\n", rx_i); // send frame on ethernet char *ethernet_data = (char *)malloc(sizeof(char) * rx_i); memcpy(buf, ethernet_data, rx_i); eth.write(ethernet_data, rx_i); int result = eth.send(); printf("result: %d\r\n", result); rx_i = 0; } wait(0.05); } }
so in each loop, read ethernet interface , pass these packages usb-cdc ecm (64 bytes @ time). next, read incoming traffic usb side , pass traffic ethernet interface.
this not working. i've used wireshark analyze traffic looks strange , packets seems corrupted. payload same. occurs traffic internet computer.
does know goes wrong here? pass packets other interface , don't modify them. below, i've added wireshark output.
it appears me memcpy()
calls wrong way around.
void *memcpy(void *dest, const void *src, size_t n);
destination first, source second argument.
Comments
Post a Comment