diff --git a/miniupnpd/upnpevents.c b/miniupnpd/upnpevents.c index 887a124..28d090d 100644 --- a/miniupnpd/upnpevents.c +++ b/miniupnpd/upnpevents.c @@ -1,4 +1,4 @@ -/* $Id: upnpevents.c,v 1.18 2012/02/01 11:13:30 nanard Exp $ */ +/* $Id: upnpevents.c,v 1.19 2012/02/06 16:21:24 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * (c) 2008-2012 Thomas Bernard @@ -16,13 +16,13 @@ #include #include #include -#include #include #include "config.h" #include "upnpevents.h" #include "miniupnpdpath.h" #include "upnpglobalvars.h" #include "upnpdescgen.h" +#include "upnputils.h" #ifdef ENABLE_EVENTS /*enum subscriber_service_enum { @@ -184,7 +184,7 @@ static void upnp_event_create_notify(struct subscriber * sub) { struct upnp_event_notify * obj; - int flags; + obj = calloc(1, sizeof(struct upnp_event_notify)); if(!obj) { syslog(LOG_ERR, "%s: calloc(): %m", "upnp_event_create_notify"); @@ -202,13 +202,8 @@ upnp_event_create_notify(struct subscriber * sub) syslog(LOG_ERR, "%s: socket(): %m", "upnp_event_create_notify"); goto error; } - if((flags = fcntl(obj->s, F_GETFL, 0)) < 0) { - syslog(LOG_ERR, "%s: fcntl(..F_GETFL..): %m", - "upnp_event_create_notify"); - goto error; - } - if(fcntl(obj->s, F_SETFL, flags | O_NONBLOCK) < 0) { - syslog(LOG_ERR, "%s: fcntl(..F_SETFL..): %m", + if(!set_non_blocking(obj->s)) { + syslog(LOG_ERR, "%s: set_non_blocking(): %m", "upnp_event_create_notify"); goto error; } diff --git a/miniupnpd/upnputils.c b/miniupnpd/upnputils.c index a407ff6..8e253d7 100644 --- a/miniupnpd/upnputils.c +++ b/miniupnpd/upnputils.c @@ -1,13 +1,15 @@ -/* $Id: upnputils.c,v 1.3 2011/05/20 09:42:23 nanard Exp $ */ +/* $Id: upnputils.c,v 1.4 2012/02/06 16:21:24 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 */ #include "config.h" #include +#include +#include #include #include #include @@ -65,4 +67,14 @@ sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size) } +int +set_non_blocking(int fd) +{ + int flags = fcntl(fd, F_GETFL); + if(flags < 0) + return 0; + if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) + return 0; + return 1; +} diff --git a/miniupnpd/upnputils.h b/miniupnpd/upnputils.h index 29fa2a3..363debf 100644 --- a/miniupnpd/upnputils.h +++ b/miniupnpd/upnputils.h @@ -1,7 +1,7 @@ -/* $Id: upnputils.h,v 1.1 2011/05/15 08:58:41 nanard Exp $ */ +/* $Id: upnputils.h,v 1.2 2012/02/06 16:21:24 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2011 Thomas Bernard + * (c) 2011-2012 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -16,5 +16,12 @@ int sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size); +/** + * set the file description as non blocking + * return 0 in case of failure, 1 in case of success + */ +int +set_non_blocking(int fd); + #endif