Commit 3b2dd0ce623243ecb6eab38468d09e1db33aa05b

Baptiste 2024-07-19T16:59:33

socket handling work in progress

diff --git a/kc3_http/http.c b/kc3_http/http.c
index 5324fed..645a439 100644
--- a/kc3_http/http.c
+++ b/kc3_http/http.c
@@ -14,6 +14,3 @@
 #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
index c5cd92e..9cc480d 100644
--- a/kc3_http/socket.c
+++ b/kc3_http/socket.c
@@ -10,26 +10,46 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <libkc3/kc3.h>
+#include <errno.h>
+#include <string.h>
 #include <sys/socket.h>
+#include <netdb.h>
+#include <libkc3/kc3.h>
 #include "socket.h"
 
-int * socket_init_listen(int *s, const struct sockaddr *addr, socklen_t addr_len)
+int * socket_init_listen(int *s, const s_str *host, u16 port)
 {
+    struct sockaddr *addr;
+    socklen_t addr_len;
+    sw e;
+    struct hostent *hostent;
     assert(s);
-    assert(addr);
-    assert(addr_len);
+    assert(host);
+    hostent = gethostbyname2(host->ptr.pchar, AF_INET);
+    if (! hostent) {
+        e = errno;
+        err_write_1("socket_init_listen: gethostbyname2: ");
+        err_puts(strerror(e));
+        assert(!"socket_init_listen: gethostbyname2");
+        return NULL;
+    }
+    addr = (struct sockaddr *) hostent->h_addr_list[0];
+    addr_len = hostent->h_length;
     socket = socket(addr->sa_family, SOCK_STREAM, 0);
     if (socket < 0) {
-        err_puts("socket_init_listen: socket: %s", strerror(errno));
+        e = errno;
+        err_write_1("socket_init_listen: socket: ");
+        err_puts(strerror(e));
         assert(! "socket_init_listen: socket");
         return NULL;
     }
+    addr->port = htons(port);
     if (bind(socket, addr, addr_len) < 0) {
-        err_puts("socket_init_listen: bind: %s", strerror(errno));
+        e = errno;
+        err_write_1("socket_init_listen: bind: ");
+        err_puts(strerror(e));
         assert(! "socket_init_listen: bind");
         return NULL;
     }
-}
-
-buf * socket_to_buf(int *s, s_buf *dest);
\ No newline at end of file
+    return s;
+}
\ No newline at end of file
diff --git a/kc3_http/socket.h b/kc3_http/socket.h
index eedda23..fd5b606 100644
--- a/kc3_http/socket.h
+++ b/kc3_http/socket.h
@@ -15,7 +15,6 @@
 
 #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);
+int * socket_init_listen(int *s, const s_str *host, u16 port);
 
 #endif /* SOCKET_H */
diff --git a/kc3_http/sources.mk b/kc3_http/sources.mk
index 59f7be4..6d2e05e 100644
--- a/kc3_http/sources.mk
+++ b/kc3_http/sources.mk
@@ -1,8 +1,10 @@
 # sources.mk generated by update_sources
 HEADERS = \
 	"http.h" \
+	"socket.h" \
 	"types.h" \
 
 SOURCES = \
 	"http.c" \
+	"socket.c" \
 
diff --git a/kc3_http/sources.sh b/kc3_http/sources.sh
index 3c74236..ba1e699 100644
--- a/kc3_http/sources.sh
+++ b/kc3_http/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='http.h types.h '
-SOURCES='http.c '
+HEADERS='http.h socket.h types.h '
+SOURCES='http.c socket.c '