From 3d47a4eee2d0548e354e514df9e100093ab10f1a Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 13 Apr 2024 09:22:03 +0000 Subject: [PATCH] listens now git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@18 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Makefile | 2 +- Module/http.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8d89a4a..0d91d2e 100644 --- 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 := diff --git a/Module/http.c b/Module/http.c index 9a5228f..60203c5 100644 --- a/Module/http.c +++ b/Module/http.c @@ -34,6 +34,14 @@ #include #include +#include +#include +#include +#include + +#include +#include +#include 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; } -- 2.43.0