Skip to content

Instantly share code, notes, and snippets.

@malachib
Created December 31, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malachib/3ff06a8a629ad863246b6e01a5d1510d to your computer and use it in GitHub Desktop.
Save malachib/3ff06a8a629ad863246b6e01a5d1510d to your computer and use it in GitHub Desktop.
Unexpected corruption
struct netconn *conn = netconn_new_with_proto_and_callback(NETCONN_UDP, 0, NULL);
char buf[512];
if(!conn)
{
cerr << "Failed to allocate socket";
abort();
}
netconn_bind(conn, IP_ADDR_ANY, 5000);
netconn_set_recvtimeout(conn, 0); // blocking
struct netbuf* nbFrom = netbuf_new();
netconn_recv(conn, &nbFrom);
void* _recvBuf;
u16_t recvLen = 0;
const size_t offset = 50;
err_t err = netbuf_data(nbFrom, &_recvBuf, &recvLen);
clog << "Status: " << (uint16_t)err << endl;
clog << "Got " << recvLen << " bytes" << endl;
debugPrintBuffer((char*)_recvBuf, recvLen);
clog << endl;
memset(buf, 0, sizeof(buf));
char* offsetbuf = buf + offset;
sprintf(offsetbuf, "Test send! (got: %d bytes)", recvLen);
size_t buflen = strlen(offsetbuf);
struct netbuf* nb = netbuf_new();
netbuf_ref(nb, offsetbuf, buflen);
clog << "About to send" << endl;
clog << "buffer pre offset: ";
debugPrintBuffer((char*)buf, offset);
clog << endl;
netconn_sendto(conn, nb, netbuf_fromaddr(nbFrom), 5001);
clog << "buffer pre offset: ";
debugPrintBuffer((char*)buf, offset);
clog << endl;
clog << "Sent" << endl;
netbuf_delete(nb);
netbuf_delete(nbFrom);
netconn_disconnect(conn);
netconn_delete(conn);
@malachib
Copy link
Author

malachib commented Jan 1, 2017

Perhaps it's cause I'm not waiting long enough for the TX async to complete sending, as alluded to here http://savannah.nongnu.org/bugs/?44805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment