From 2758993bb2a30cda21c03bcbbcb85d2cf8e9d2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Date: Sat, 20 Oct 2007 11:36:21 +0000 Subject: [PATCH] Robustify network protocols with more error checking. --- protocolext.c | 24 +++++++++++++++++++----- protocolhttp.c | 14 ++++++++++---- protocoludp.c | 22 +++++++++++++++++----- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/protocolext.c b/protocolext.c index 6f5c37a..a88962e 100644 --- a/protocolext.c +++ b/protocolext.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolext.c,v 1.10 2007/10/19 22:56:25 rahrenbe Exp $ + * $Id: protocolext.c,v 1.11 2007/10/20 11:36:21 ajhseppa Exp $ */ #include @@ -187,6 +187,11 @@ void cIptvProtocolExt::TerminateCommand(void) int cIptvProtocolExt::Read(unsigned char* *BufferAddr) { //debug("cIptvProtocolExt::Read()\n"); + // Error out if socket not initialized + if (socketDesc <= 0) { + error("ERROR: Invalid socket in %s\n", __FUNCTION__); + return -1; + } socklen_t addrlen = sizeof(sockAddr); // Set argument point to read buffer *BufferAddr = readBuffer; @@ -203,14 +208,21 @@ int cIptvProtocolExt::Read(unsigned char* *BufferAddr) if (retval < 0) { char tmp[64]; error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return -1; + return retval; } // Check if data available else if (retval) { // Read data from socket - int len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, - (struct sockaddr *)&sockAddr, &addrlen); - if ((len > 0) && (readBuffer[0] == 0x47)) { + int len = 0; + if (isActive) + len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, + (struct sockaddr *)&sockAddr, &addrlen); + if (len < 0) { + char tmp[64]; + error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp))); + return len; + } + else if ((len > 0) && (readBuffer[0] == 0x47)) { // Set argument point to read buffer *BufferAddr = &readBuffer[0]; return len; @@ -256,6 +268,7 @@ bool cIptvProtocolExt::Open(void) OpenSocket(); // Execute the external command ExecuteCommand(); + isActive = true; return true; } @@ -266,6 +279,7 @@ bool cIptvProtocolExt::Close(void) CloseSocket(); // Terminate the external script TerminateCommand(); + isActive = false; return true; } diff --git a/protocolhttp.c b/protocolhttp.c index 6e4337e..8da27fb 100644 --- a/protocolhttp.c +++ b/protocolhttp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolhttp.c,v 1.12 2007/10/19 22:18:55 rahrenbe Exp $ + * $Id: protocolhttp.c,v 1.13 2007/10/20 11:36:21 ajhseppa Exp $ */ #include @@ -307,6 +307,11 @@ bool cIptvProtocolHttp::ProcessHeaders(void) int cIptvProtocolHttp::Read(unsigned char* *BufferAddr) { //debug("cIptvProtocolHttp::Read()\n"); + // Error out if socket not initialized + if (socketDesc <= 0) { + error("ERROR: Invalid socket in %s\n", __FUNCTION__); + return -1; + } socklen_t addrlen = sizeof(sockAddr); // Set argument point to read buffer *BufferAddr = readBuffer; @@ -323,13 +328,14 @@ int cIptvProtocolHttp::Read(unsigned char* *BufferAddr) if (retval < 0) { char tmp[64]; error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return -1; + return retval; } // Check if data available else if (retval) { // Read data from socket - return recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, - (struct sockaddr *)&sockAddr, &addrlen); + if (isActive) + return recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, + (struct sockaddr *)&sockAddr, &addrlen); } return 0; } diff --git a/protocoludp.c b/protocoludp.c index 7d32209..016dbf9 100644 --- a/protocoludp.c +++ b/protocoludp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocoludp.c,v 1.13 2007/10/19 22:18:55 rahrenbe Exp $ + * $Id: protocoludp.c,v 1.14 2007/10/20 11:36:21 ajhseppa Exp $ */ #include @@ -155,6 +155,11 @@ bool cIptvProtocolUdp::DropMulticast(void) int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) { //debug("cIptvProtocolUdp::Read()\n"); + // Error out if socket not initialized + if (socketDesc <= 0) { + error("ERROR: Invalid socket in %s\n", __FUNCTION__); + return -1; + } socklen_t addrlen = sizeof(sockAddr); // Set argument point to read buffer *BufferAddr = readBuffer; @@ -171,14 +176,21 @@ int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) if (retval < 0) { char tmp[64]; error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return -1; + return retval; } // Check if data available else if (retval) { + int len = 0; // Read data from socket - int len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, - (struct sockaddr *)&sockAddr, &addrlen); - if ((len > 0) && (readBuffer[0] == 0x47)) { + if (isActive) + len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, + (struct sockaddr *)&sockAddr, &addrlen); + if (len < 0) { + char tmp[64]; + error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp))); + return len; + } + else if ((len > 0) && (readBuffer[0] == 0x47)) { // Set argument point to read buffer *BufferAddr = &readBuffer[0]; return len;