]> Nishi Git Mirror - mandshurica.git/commitdiff
listens now
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 09:22:03 +0000 (09:22 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 09:22:03 +0000 (09:22 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@18 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Makefile
Module/http.c

index 8d89a4a048a4a7c2182ae12e9333003ce499fe4c..0d91d2e79abe5f1041b36a845e5dce515ae7e8c0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ MODULE_PREFIX := $(PREFIX)/lib/devforge
 WEBROOT_PREFIX := $(PREFIX)/lib/devforge/webroot
 
 CC := gcc
-CFLAGS := -g -std=c99 -DPREFIX=\\\"$(PREFIX)\\\" -DWEBROOT_PREFIX=\\\"$(WEBROOT_PREFIX)\\\" -DMODULE_PREFIX=\\\"$(MODULE_PREFIX)\\\"
+CFLAGS := -g -std=c99 -DPREFIX=\\\"$(PREFIX)\\\" -DWEBROOT_PREFIX=\\\"$(WEBROOT_PREFIX)\\\" -DMODULE_PREFIX=\\\"$(MODULE_PREFIX)\\\" -D_DEFAULT_SOURCE
 LDFLAGS :=
 LIBS :=
 
index 9a5228fc1b4221b32b0e4ba337c1fdb392063d5a..60203c526418a5462727f51488f69375ddb1d843 100644 (file)
 
 #include <stddef.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/types.h>
+
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
 
 const char mod_type[] = DF_MOD_SRV;
 
@@ -55,5 +63,69 @@ void* mod_server_thread(void* ptr) {
        *ret = 0;
        struct devforge_config* sconfig = (struct devforge_config*)ptr;
        sconfig->devforge_log(DF_INFO, "HTTP server preparing");
+
+       int server_socket;
+       struct sockaddr_in6 server_address;
+       
+       int yes = 1;
+       int no = 0;
+
+       if((server_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0){
+               *ret = 1;
+               return (void*)ret;
+       }
+       if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0){
+               close(server_socket);
+               sconfig->devforge_log(DF_ERROR, "setsockopt(SOL_SOCKET, SO_REUSEADDR) failed");
+               *ret = 1;
+               return (void*)ret;
+       }
+       if(setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0){
+               close(server_socket);
+               sconfig->devforge_log(DF_ERROR, "setsockopt(IPPROTO_TCP, TCP_NODELAY) failed");
+               *ret = 1;
+               return (void*)ret;
+       }
+       if(setsockopt(server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)) < 0){
+               close(server_socket);
+               sconfig->devforge_log(DF_ERROR, "setsockopt(IPPROTO_IPV6, IPV6_V6ONLY) failed");
+               *ret = 1;
+               return (void*)ret;
+       }
+
+       char* port = config->devforge_get_param("HTTPPort");
+       if(port == NULL) port = "1024";
+
+       memset(&server_address, 0, sizeof(server_address));
+       server_address.sin6_family = AF_INET6;
+       server_address.sin6_addr = in6addr_any;
+       server_address.sin6_port = htons(atoi(port));
+
+       if(bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0){
+               close(server_socket);
+               sconfig->devforge_log(DF_ERROR, "bind() failed");
+               *ret = 1;
+               return (void*)ret;
+       }
+       if(listen(server_socket, 128) < 0){
+               close(server_socket);
+               sconfig->devforge_log(DF_ERROR, "listen() failed");
+               *ret = 1;
+               return (void*)ret;
+       }
+       signal(SIGCHLD, SIG_IGN);
+
+       while(1){
+               struct sockaddr_storage claddr;
+               int clen = sizeof(claddr);
+               int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen);
+               pid_t pid = fork();
+               if(pid == 0){
+                       _exit(0);
+               }else{
+                       close(sock);
+               }
+       }
+
        return (void*)ret;
 }