From 8e59a7c6128eefa84b55229c80b40f22a23c0c23 Mon Sep 17 00:00:00 2001 From: Arcao Date: Wed, 8 Feb 2017 21:56:18 +0100 Subject: [PATCH] beginPacket can be used without listening on socket Currently there is bug in WiFiUDP library when you want to use beginPacket(...) without listening on socket (without calling begin(...) first). You can't send any data because socket is not open and also tx_buffer is not allocated which cause crash while writing data to tx_buffer. --- libraries/WiFi/src/WiFiUdp.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libraries/WiFi/src/WiFiUdp.cpp b/libraries/WiFi/src/WiFiUdp.cpp index cb91923a72a..22c3521d20a 100644 --- a/libraries/WiFi/src/WiFiUdp.cpp +++ b/libraries/WiFi/src/WiFiUdp.cpp @@ -130,7 +130,29 @@ int WiFiUDP::beginMulticastPacket(){ int WiFiUDP::beginPacket(){ if(!remote_port) return 0; + + // allocate tx_buffer if is necessary + if(!tx_buffer){ + tx_buffer = new char[1460]; + if(!tx_buffer){ + log_e("could not create tx buffer: %d", errno); + return 0; + } + } + tx_buffer_len = 0; + + // check whereas socket is already open + if (udp_server != -1) + return 1; + + if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){ + log_e("could not create socket: %d", errno); + return 0; + } + + fcntl(udp_server, F_SETFL, O_NONBLOCK); + return 1; }