#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;
*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;
}