Set O_NONBLOCK

This commit is contained in:
angt
2016-01-31 08:45:39 +01:00
parent 8634b27969
commit edb8f861bd

15
mud.c
View File

@@ -5,6 +5,7 @@
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/socket.h>
struct path {
@@ -25,6 +26,17 @@ struct mud {
struct path *path;
};
static
int mud_set_nonblock (int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags==-1)
flags = 0;
return fcntl(fd, F_SETFL, flags|O_NONBLOCK);
}
static
struct addrinfo *mud_addrinfo (const char *host, const char *port, int flags)
{
@@ -186,6 +198,9 @@ int mud_bind (struct mud *mud, const char *host, const char *port)
if (fd==-1)
continue;
if (mud_set_nonblock(fd))
continue;
if (!bind(fd, (struct sockaddr *)p->ai_addr, p->ai_addrlen)) {
mud_new_sock(mud, fd, p->ai_family);
break;