aboutsummaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
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;
+}