From 280e61e1fc4d602b9562e23e9ed1560cb9ccd97e Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 7 Nov 2022 11:53:56 +0100 Subject: [PATCH] Make EthernetInterface code more robust --- EthernetInterface.cpp | 8 +++++++- EthernetInterface.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp index 2668ece..8dd4d31 100644 --- a/EthernetInterface.cpp +++ b/EthernetInterface.cpp @@ -163,6 +163,10 @@ bool EthernetInterface::checkLink() { } void EthernetInterface::loop2() { + if (!outboundRing) { // no idea to call loop2() if we can't handle outgoing data in it + if (Diag::ETHERNET) DIAG(F("No outboundRing")); + return; + } // get client from the server EthernetClient client = server->accept(); @@ -217,7 +221,9 @@ void EthernetInterface::loop2() { // handle at most 1 outbound transmission int socketOut=outboundRing->read(); - if (socketOut>=0) { + if (socketOut >= MAX_SOCK_NUM) { + DIAG(F("Ethernet outboundRing socket=%d error"), socketOut); + } else if (socketOut >= 0) { int count=outboundRing->count(); if (Diag::ETHERNET) DIAG(F("Ethernet reply socket=%d, count=:%d"), socketOut,count); for(;count>0;count--) clients[socketOut].write(outboundRing->read()); diff --git a/EthernetInterface.h b/EthernetInterface.h index e9b6c60..8078c3f 100644 --- a/EthernetInterface.h +++ b/EthernetInterface.h @@ -62,7 +62,7 @@ class EthernetInterface { ~EthernetInterface(); void loop2(); bool checkLink(); - EthernetServer * server; + EthernetServer * server = NULL; EthernetClient clients[MAX_SOCK_NUM]; // accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv RingStream * outboundRing = NULL;