upnputils.c/.h: add set_non_blocking() function

This commit is contained in:
Thomas Bernard 2012-02-07 00:18:05 +01:00
parent 994bd1391b
commit 0d0a50a084
3 changed files with 28 additions and 14 deletions

View File

@ -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 /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2008-2012 Thomas Bernard * (c) 2008-2012 Thomas Bernard
@ -16,13 +16,13 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "config.h" #include "config.h"
#include "upnpevents.h" #include "upnpevents.h"
#include "miniupnpdpath.h" #include "miniupnpdpath.h"
#include "upnpglobalvars.h" #include "upnpglobalvars.h"
#include "upnpdescgen.h" #include "upnpdescgen.h"
#include "upnputils.h"
#ifdef ENABLE_EVENTS #ifdef ENABLE_EVENTS
/*enum subscriber_service_enum { /*enum subscriber_service_enum {
@ -184,7 +184,7 @@ static void
upnp_event_create_notify(struct subscriber * sub) upnp_event_create_notify(struct subscriber * sub)
{ {
struct upnp_event_notify * obj; struct upnp_event_notify * obj;
int flags;
obj = calloc(1, sizeof(struct upnp_event_notify)); obj = calloc(1, sizeof(struct upnp_event_notify));
if(!obj) { if(!obj) {
syslog(LOG_ERR, "%s: calloc(): %m", "upnp_event_create_notify"); 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"); syslog(LOG_ERR, "%s: socket(): %m", "upnp_event_create_notify");
goto error; goto error;
} }
if((flags = fcntl(obj->s, F_GETFL, 0)) < 0) { if(!set_non_blocking(obj->s)) {
syslog(LOG_ERR, "%s: fcntl(..F_GETFL..): %m", syslog(LOG_ERR, "%s: set_non_blocking(): %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",
"upnp_event_create_notify"); "upnp_event_create_notify");
goto error; goto error;
} }

View File

@ -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 /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * 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 * This software is subject to the conditions detailed
* in the LICENCE file provided within the distribution */ * in the LICENCE file provided within the distribution */
#include "config.h" #include "config.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -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;
}

View File

@ -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 /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * 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 * This software is subject to the conditions detailed
* in the LICENCE file provided within the distribution */ * in the LICENCE file provided within the distribution */
@ -16,5 +16,12 @@
int int
sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size); 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 #endif