Commit b533f71728904eb17e9a0a171ce9d5feea816372

Baptiste 2024-07-19T16:23:41

socket_init_listen

diff --git a/kc3_http/http.c b/kc3_http/http.c
index dbacf54..5324fed 100644
--- a/kc3_http/http.c
+++ b/kc3_http/http.c
@@ -12,5 +12,8 @@
  */
 #include <libkc3/kc3.h>
 #include "http.h"
+#include "socket.h"
 
-
+buf *socket_to_buf(int socket) {
+    //listen on the socket first
+}
diff --git a/kc3_http/socket.c b/kc3_http/socket.c
new file mode 100644
index 0000000..c5cd92e
--- /dev/null
+++ b/kc3_http/socket.c
@@ -0,0 +1,35 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <libkc3/kc3.h>
+#include <sys/socket.h>
+#include "socket.h"
+
+int * socket_init_listen(int *s, const struct sockaddr *addr, socklen_t addr_len)
+{
+    assert(s);
+    assert(addr);
+    assert(addr_len);
+    socket = socket(addr->sa_family, SOCK_STREAM, 0);
+    if (socket < 0) {
+        err_puts("socket_init_listen: socket: %s", strerror(errno));
+        assert(! "socket_init_listen: socket");
+        return NULL;
+    }
+    if (bind(socket, addr, addr_len) < 0) {
+        err_puts("socket_init_listen: bind: %s", strerror(errno));
+        assert(! "socket_init_listen: bind");
+        return NULL;
+    }
+}
+
+buf * socket_to_buf(int *s, s_buf *dest);
\ No newline at end of file
diff --git a/kc3_http/socket.h b/kc3_http/socket.h
new file mode 100644
index 0000000..eedda23
--- /dev/null
+++ b/kc3_http/socket.h
@@ -0,0 +1,21 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#ifndef SOCKET_H
+#define SOCKET_H
+
+#include "types.h"
+
+int * socket_init_listen(int *s, const struct sockaddr *addr, socklen_t addr_len);
+buf * socket_to_buf(int *s, s_buf *dest);
+
+#endif /* SOCKET_H */