From 73ef11624a331ece478e2e3383c760dd4b95df3c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 2 Feb 2012 18:45:43 +0100 Subject: [PATCH] always handle EAGAIN, EWOULDBLOCK and EINTR after recv()/recvfrom() calls --- miniupnpd/Changelog.txt | 5 ++++- miniupnpd/minissdp.c | 14 +++++++++++--- miniupnpd/natpmp.c | 13 ++++++++++--- miniupnpd/upnpevents.c | 15 +++++++++++---- miniupnpd/upnphttp.c | 24 ++++++++++++++++++------ 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/miniupnpd/Changelog.txt b/miniupnpd/Changelog.txt index b339b84..e7e389d 100644 --- a/miniupnpd/Changelog.txt +++ b/miniupnpd/Changelog.txt @@ -1,4 +1,7 @@ -$Id: Changelog.txt,v 1.248 2012/01/20 21:57:45 nanard Exp $ +$Id: Changelog.txt,v 1.249 2012/02/01 11:13:29 nanard Exp $ + +2012/02/01: + always handle EAGAIN, EWOULDBLOCK and EINTR after recv()/recvfrom() calls 2012/01/20: Always #include before #include (for OpenBSD) diff --git a/miniupnpd/minissdp.c b/miniupnpd/minissdp.c index 378d842..ed6cd08 100644 --- a/miniupnpd/minissdp.c +++ b/miniupnpd/minissdp.c @@ -1,7 +1,7 @@ -/* $Id: minissdp.c,v 1.27 2011/05/23 12:39:41 nanard Exp $ */ +/* $Id: minissdp.c,v 1.28 2012/02/01 11:13:30 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2006-2011 Thomas Bernard + * (c) 2006-2012 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "config.h" #include "upnpdescstrings.h" @@ -403,7 +404,14 @@ ProcessSSDPRequest(int s, unsigned short port) (struct sockaddr *)&sendername, &len_r); if(n < 0) { - syslog(LOG_ERR, "recvfrom(udp): %m"); + /* EAGAIN, EWOULDBLOCK, EINTR : silently ignore (try again next time) + * other errors : log to LOG_ERR */ + if(errno != EAGAIN && + errno != EWOULDBLOCK && + errno != EINTR) + { + syslog(LOG_ERR, "recvfrom(udp): %m"); + } return; } ProcessSSDPData(s, bufr, n, (struct sockaddr *)&sendername, port); diff --git a/miniupnpd/natpmp.c b/miniupnpd/natpmp.c index 3e427cf..cafd404 100644 --- a/miniupnpd/natpmp.c +++ b/miniupnpd/natpmp.c @@ -1,6 +1,6 @@ -/* $Id: natpmp.c,v 1.26 2011/07/15 07:48:26 nanard Exp $ */ +/* $Id: natpmp.c,v 1.27 2012/02/01 11:13:30 nanard Exp $ */ /* MiniUPnP project - * (c) 2007-2010 Thomas Bernard + * (c) 2007-2012 Thomas Bernard * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -116,7 +117,13 @@ void ProcessIncomingNATPMPPacket(int s) n = recvfrom(s, req, sizeof(req), 0, (struct sockaddr *)&senderaddr, &senderaddrlen); if(n<0) { - syslog(LOG_ERR, "recvfrom(natpmp): %m"); + /* EAGAIN, EWOULDBLOCK and EINTR : silently ignore (retry next time) + * other errors : log to LOG_ERR */ + if(errno != EAGAIN && + errno != EWOULDBLOCK && + errno != EINTR) { + syslog(LOG_ERR, "recvfrom(natpmp): %m"); + } return; } if(!inet_ntop(AF_INET, &senderaddr.sin_addr, diff --git a/miniupnpd/upnpevents.c b/miniupnpd/upnpevents.c index be95a78..887a124 100644 --- a/miniupnpd/upnpevents.c +++ b/miniupnpd/upnpevents.c @@ -1,7 +1,7 @@ -/* $Id: upnpevents.c,v 1.17 2011/06/27 11:24:00 nanard Exp $ */ +/* $Id: upnpevents.c,v 1.18 2012/02/01 11:13:30 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2008-2011 Thomas Bernard + * (c) 2008-2012 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -390,12 +390,19 @@ static void upnp_event_recv(struct upnp_event_notify * obj) int n; n = recv(obj->s, obj->buffer, obj->buffersize, 0); if(n<0) { - syslog(LOG_ERR, "%s: recv(): %m", "upnp_event_recv"); - obj->state = EError; + if(errno != EAGAIN && + errno != EWOULDBLOCK && + errno != EINTR) { + syslog(LOG_ERR, "%s: recv(): %m", "upnp_event_recv"); + obj->state = EError; + } return; } syslog(LOG_DEBUG, "%s: (%dbytes) %.*s", "upnp_event_recv", n, n, obj->buffer); + /* TODO : do something with the data recevied ? + * right now, n (number of bytes received) is ignored + * We may need to recv() more bytes. */ obj->state = EFinished; if(obj->sub) obj->sub->seq++; diff --git a/miniupnpd/upnphttp.c b/miniupnpd/upnphttp.c index 96d4455..70e21fe 100644 --- a/miniupnpd/upnphttp.c +++ b/miniupnpd/upnphttp.c @@ -1,8 +1,8 @@ -/* $Id: upnphttp.c,v 1.65 2012/01/20 21:45:57 nanard Exp $ */ +/* $Id: upnphttp.c,v 1.66 2012/02/01 11:13:30 nanard Exp $ */ /* Project : miniupnp * Website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * Author : Thomas Bernard - * Copyright (c) 2005-2011 Thomas Bernard + * Copyright (c) 2005-2012 Thomas Bernard * This software is subject to the conditions detailed in the * LICENCE file included in this distribution. * */ @@ -554,8 +554,14 @@ Process_upnphttp(struct upnphttp * h) n = recv(h->socket, buf, 2048, 0); if(n<0) { - syslog(LOG_ERR, "recv (state0): %m"); - h->state = 100; + if(errno != EAGAIN && + errno != EWOULDBLOCK && + errno != EINTR) + { + syslog(LOG_ERR, "recv (state0): %m"); + h->state = 100; + } + /* if errno is EAGAIN, EWOULDBLOCK or EINTR, try again later */ } else if(n==0) { @@ -584,8 +590,14 @@ Process_upnphttp(struct upnphttp * h) n = recv(h->socket, buf, 2048, 0); if(n<0) { - syslog(LOG_ERR, "recv (state1): %m"); - h->state = 100; + if(errno != EAGAIN && + errno != EWOULDBLOCK && + errno != EINTR) + { + syslog(LOG_ERR, "recv (state1): %m"); + h->state = 100; + } + /* if errno is EAGAIN, EWOULDBLOCK or EINTR, try again later */ } else if(n==0) {