src/network.c

changeset 34
c4d4b8a8f902
parent 22
41bbfd4d17a3
child 45
e14a1d9aa91d
--- a/src/network.c	Wed Apr 09 11:12:04 2014 +0200
+++ b/src/network.c	Wed Apr 09 12:07:47 2014 +0200
@@ -29,6 +29,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 #include "network.h"
 
 #define new_socket() socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -133,3 +134,21 @@
 void net_recieve_data(int socket, void *data, size_t len) {
     recv(socket, data, len, 0);
 }
+
+int net_setnonblocking(int socket, _Bool nonblocking) {
+    int opts = fcntl(socket, F_GETFL);
+	if (opts < 0) {
+		return 1;
+	}
+    
+    if (nonblocking) {
+        opts |= O_NONBLOCK;
+    } else {
+        opts &= ~O_NONBLOCK;
+    }
+	if (fcntl(socket, F_SETFL, opts) < 0) {
+		return 1;
+	}
+    
+    return 0;
+}

mercurial