aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-04-05 17:53:27 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-04-07 00:03:28 +0200
commitebabaa69c0a3ba992895c7a66729e81e0923d5f1 (patch)
tree39b4a5b90a4f51c98486e8a00898e983b8878a12 /src/socket.c
Initial commit.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c
new file mode 100644
index 0000000..99e4477
--- /dev/null
+++ b/src/socket.c
@@ -0,0 +1,30 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <assert.h>
+
+#include "socket.h"
+
+
+int socket_init_in(psocket *psocket, const char *listen_addr, unsigned int listen_port)
+{
+ struct in_addr addr = {0};
+
+ assert(psocket);
+ if (!inet_aton(listen_addr, &addr))
+ return 1;
+
+ psocket->sock.sin_family = AF_INET;
+ psocket->sock.sin_addr = addr;
+ psocket->sock.sin_port = htons(listen_port);
+ psocket->fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
+ return psocket->fd < 0;
+}
+
+int socket_bind_listen(psocket *psocket)
+{
+ assert(psocket);
+ if (bind(psocket->fd, &psocket->sock, sizeof(psocket->sock)) < 0)
+ return 1;
+ return listen(psocket->fd, POTD_BACKLOG) < 0;
+}