added set_non_block()

This commit is contained in:
Nikos Mavrogiannopoulos
2014-10-05 22:00:53 +02:00
parent 5a32ad3f3f
commit db48e3db07
3 changed files with 12 additions and 10 deletions

View File

@@ -20,6 +20,7 @@
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <vpn.h>
#include <sys/socket.h>
@@ -160,6 +161,14 @@ fd_set set;
return len;
}
void set_non_block(int fd)
{
int val;
val = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, val | O_NONBLOCK);
}
ssize_t recv_timeout(int sockfd, void *buf, size_t len, unsigned sec)
{
int ret;

View File

@@ -33,6 +33,8 @@ void *_talloc_size2(void *ctx, size_t size);
#define DEFAULT_SOCKET_TIMEOUT 10
void set_non_block(int fd);
ssize_t force_write(int sockfd, const void *buf, size_t len);
ssize_t force_read(int sockfd, void *buf, size_t len);
ssize_t force_read_timeout(int sockfd, void *buf, size_t len, unsigned sec);

View File

@@ -122,16 +122,7 @@ int y;
static void set_common_socket_options(int fd)
{
int val;
val = fcntl(fd, F_GETFL, 0);
if ((val == -1)
|| (fcntl(fd, F_SETFL, val | O_NONBLOCK) < 0)) {
int e = errno;
fprintf(stderr, "fcntl() error: %s", strerror(e));
exit(1);
}
set_non_block(fd);
set_cloexec_flag (fd, 1);
}