]> Nishi Git Mirror - tewi.git/commitdiff
module system kinda works
authorNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 17:41:07 +0000 (17:41 +0000)
committerNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 17:41:07 +0000 (17:41 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@17 8739d7e6-ffea-ec47-b151-bdff447c6205

14 files changed:
Makefile
Module/Makefile [new file with mode: 0644]
Module/mod_example.c [new file with mode: 0644]
Platform/generic.mk
Platform/win64.mk
Server/Makefile
Server/config.c
Server/http.c
Server/module.c [new file with mode: 0644]
Server/server.c
Server/tw_config.h
Server/tw_http.h
Server/tw_module.h [new file with mode: 0644]
example.conf

index 34e6226e349651381efee960b2e54712671d8ef9..490eddbe334a3de22a1196df2c94a84d1f8062f7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,19 +6,23 @@ PREFIX = /usr/local
 
 FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)
 
-.PHONY: all format clean ./Server ./Common
+.PHONY: all format clean ./Server ./Common ./Module
 
-all: ./Server
+all: ./Server ./Module
 
 ./Server:: ./Common
        $(MAKE) -C $@ $(FLAGS)
 
+./Module:: ./Common
+       $(MAKE) -C $@ $(FLAGS)
+
 ./Common::
        $(MAKE) -C $@ $(FLAGS)
 
 format:
-       clang-format --verbose -i `find ./Server ./Common -name "*.c" -or -name "*.h"`
+       clang-format --verbose -i `find ./Server ./Common ./Module -name "*.c" -or -name "*.h"`
 
 clean:
        $(MAKE) -C ./Server $(FLAGS) clean
+       $(MAKE) -C ./Module $(FLAGS) clean
        $(MAKE) -C ./Common $(FLAGS) clean
diff --git a/Module/Makefile b/Module/Makefile
new file mode 100644 (file)
index 0000000..acc6102
--- /dev/null
@@ -0,0 +1,17 @@
+# $Id$
+
+include $(PWD)/Platform/$(PLATFORM).mk
+
+.PHONY: all clean
+.SUFFIXES: .c .o .so
+
+all: mod_example.so
+
+.o.so:
+       $(CC) $(LDFLAGS) -shared -o $@ $< $(LIBS)
+
+.c.o:
+       $(CC) $(CFLAGS) -fPIC -c -o $@ $<
+
+clean:
+       rm -f *.o *.so *.a
diff --git a/Module/mod_example.c b/Module/mod_example.c
new file mode 100644 (file)
index 0000000..d170fd0
--- /dev/null
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#include "../Server/tw_module.h"
+
+int mod_init(struct tw_config* config, struct tw_tool* tools) {
+       tools->log("Example", "This is an example module");
+       return 0;
+}
index aa2147469b160b7f5163f4c8828c888074da1fd7..5e45edb384b1c7be50dd63a2dc276a5df08fe62f 100644 (file)
@@ -6,3 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common
 LDFLAGS =
 LIBS =
 EXEC =
+LIB = .so
index a374bf76c2d795cfef9188aa528784a42db6509a..0871d401ee9928fbec7d6b27f3a98313255c13f1 100644 (file)
@@ -6,3 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I $(PWD)/openssl/i
 LDFLAGS = -L $(PWD)/openssl/lib
 LIBS = -lws2_32
 EXEC = .exe
+LIB = .dll
index 0053402eee8e70430a87f9ba4b75757558778157..077da443e0910e3b584f80ef2ababe78e392206f 100644 (file)
@@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
 .PHONY: all clean
 .SUFFIXES: .c .o
 
-OBJS = version.o main.o config.o server.o ssl.o http.o
+OBJS = version.o main.o config.o server.o ssl.o http.o module.o
 
 all: tewi$(EXEC)
 
index 1920627ac9e7143e9accc2b344f3c118a9da24f2..df91aaa999bb97038c6b14879a3852786dc625d1 100644 (file)
@@ -3,6 +3,7 @@
 #define SOURCE
 
 #include "tw_config.h"
+#include "tw_module.h"
 
 #include <stdio.h>
 #include <stdint.h>
@@ -37,6 +38,7 @@ void tw_config_init(void) {
        config.root.sslkey = NULL;
        config.root.sslcert = NULL;
        config.vhost_count = 0;
+       config.server_root = cm_strdup(PREFIX);
        gethostname(config.hostname, 1024);
 }
 
@@ -125,6 +127,27 @@ int tw_config_read(const char* path) {
                                                        if(current->sslcert != NULL) free(current->sslcert);
                                                        current->sslcert = cm_strdup(r[1]);
                                                }
+                                       } else if(cm_strcaseequ(r[0], "ServerRoot")) {
+                                               if(r[1] == NULL) {
+                                                       cm_log("Config", "Missing path at line %d", ln);
+                                                       stop = 1;
+                                               } else {
+                                                       if(config.server_root != NULL) free(config.server_root);
+                                                       config.server_root = cm_strdup(r[1]);
+                                               }
+                                       } else if(cm_strcaseequ(r[0], "LoadModule")) {
+                                               for(i = 1; r[i] != NULL; i++) {
+                                                       void* mod = tw_module_load(r[i]);
+                                                       if(mod != NULL) {
+                                                               if(tw_module_init(mod) != 0) {
+                                                                       stop = 1;
+                                                                       break;
+                                                               }
+                                                       } else {
+                                                               stop = 1;
+                                                               break;
+                                                       }
+                                               }
                                        } else {
                                                if(r[0] != NULL) {
                                                        cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
index 0462d3d450e25f24894fea936ae5b27b285f4d81..b63059109ca6a8f05b1e0fd07504c5351c5b7a5c 100644 (file)
 
 #include <stdbool.h>
 #include <stdlib.h>
-#include <sys/socket.h>
+#include <string.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#else
+#include <sys/select.h>
+#endif
 
 void tw_free_request(struct tw_http_request* req) {
        if(req->method != NULL) free(req->method);
diff --git a/Server/module.c b/Server/module.c
new file mode 100644 (file)
index 0000000..ec793ed
--- /dev/null
@@ -0,0 +1,58 @@
+/* $Id$ */
+
+#include "tw_module.h"
+
+#include "tw_config.h"
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+
+#ifdef __MINGW32__
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+
+extern struct tw_config config;
+
+void* tw_module_load(const char* path) {
+       char* p = getcwd(NULL, 0);
+       chdir(config.server_root);
+       void* lib;
+#ifdef __MINGW32__
+       lib = LoadLibraryA(path);
+#else
+       lib = dlopen(path, DL_LAZY);
+#endif
+       if(lib == NULL) {
+               cm_log("Module", "Could not load %s", path);
+       }
+       chdir(p);
+       free(p);
+       return lib;
+}
+
+void* tw_module_symbol(void* mod, const char* sym) {
+#ifdef __MINGW32__
+       return GetProcAddress(mod, sym);
+#else
+       return dlsym(mod, sym);
+#endif
+}
+
+void tw_init_tools(struct tw_tool* tools) { tools->log = cm_log; }
+
+int tw_module_init(void* mod) {
+       tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
+       if(mod_init == NULL) {
+               cm_log("Module", "Could not init a module");
+               return 1;
+       } else {
+               struct tw_tool tools;
+               tw_init_tools(&tools);
+               return mod_init(&config, &tools);
+       }
+}
index 3f4d45d9875d41ec68919963de96691966d668dd..6f9cd016bbd7cafc0ec2b4903a3ce21a955fb42f 100644 (file)
@@ -160,6 +160,8 @@ void tw_server_pass(int sock, bool ssl, int port) {
        }
        struct tw_http_request req;
        int ret = tw_http_parse(s, sock, &req);
+       if(ret == 0) {
+       }
 cleanup:
        if(sslworks) {
                SSL_shutdown(s);
index 2aa1bb568257bd3e64e959207219f6ff74286f71..030d06ed5efa96e2178d954e33eb1059b8476a75 100644 (file)
@@ -22,6 +22,7 @@ struct tw_config {
        struct tw_config_entry root;
        struct tw_config_entry vhosts[MAX_VHOSTS];
        int vhost_count;
+       char* server_root;
 };
 
 void tw_config_init(void);
index 2818c65cb500743795d40f3213e2aab0bdf92efe..79c03779fffb1231a247f2f3fe867e6461175eba 100644 (file)
@@ -15,8 +15,6 @@ struct tw_http_response {
        char** headers;
 };
 
-struct tw_http_tool {};
-
 #ifdef SOURCE
 #include <openssl/ssl.h>
 int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req);
diff --git a/Server/tw_module.h b/Server/tw_module.h
new file mode 100644 (file)
index 0000000..fe2a4c4
--- /dev/null
@@ -0,0 +1,19 @@
+/* $Id$ */
+
+#ifndef __TW_MODULE_H__
+#define __TW_MODULE_H__
+
+#include "tw_config.h"
+
+struct tw_tool {
+       void (*log)(const char* name, const char* log, ...);
+};
+
+typedef int (*tw_mod_init_t)(struct tw_config* config, struct tw_tool* tools);
+
+void* tw_module_load(const char* path);
+void* tw_module_symbol(void* mod, const char* sym);
+void tw_init_tools(struct tw_tool* tools);
+int tw_module_init(void* mod);
+
+#endif
index 74c4248d26cc827a896dd6907040cf31ddd28100..351fcf92d44c70fed1ee286d06c3eb21682f7e36 100644 (file)
@@ -1,6 +1,8 @@
 # $Id$
 # This is an example config
 
+LoadModule /home/nishi/SVN/tewi/trunk/Module/mod_example.so
+
 Listen 8000 8001 8002 8003 8004
 ListenSSL 8443 8444 8445 8446 8447