- Timestamp:
- Sep 13, 2024, 10:36:03 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/log.c
r6 r12 38 38 if(log[i] == 's') { 39 39 char* tmp = result; 40 result = cm_strcat(tmp, va_arg(args, char*)); 40 char* c = va_arg(args, char*); 41 result = cm_strcat(tmp, c == NULL ? "(null)" : c); 41 42 free(tmp); 42 43 } else if(log[i] == 'd') { -
trunk/Server/config.c
r7 r12 7 7 #include <stdlib.h> 8 8 #include <string.h> 9 #include <unistd.h> 9 10 10 11 #include <cm_string.h> … … 13 14 struct tw_config config; 14 15 16 struct tw_config_entry* tw_vhost_match(const char* name, int port) { 17 int i; 18 for(i = 0; i < config.vhost_count; i++) { 19 if(strcmp(config.vhosts[i].name, name) == 0 && config.vhosts[i].port == port) { 20 return &config.vhosts[i]; 21 } 22 } 23 return &config.root; 24 } 25 15 26 void tw_config_init(void) { 16 27 int i; … … 18 29 config.ports[i] = -1; 19 30 } 31 for(i = 0; i < MAX_VHOSTS; i++) { 32 config.vhosts[i].sslkey = NULL; 33 config.vhosts[i].sslcert = NULL; 34 } 35 config.root.sslkey = NULL; 36 config.root.sslcert = NULL; 37 config.vhost_count = 0; 38 gethostname(config.hostname, 1024); 20 39 } 21 40 … … 30 49 line[0] = 0; 31 50 int stop = 0; 51 struct tw_config_entry* current = &config.root; 32 52 char* vhost = NULL; 33 53 while(stop == 0) { … … 48 68 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) { 49 69 if(vhost != NULL) { 50 cm_log("Config", "Already in virtual host section ");70 cm_log("Config", "Already in virtual host section at line %d", ln); 51 71 stop = 1; 52 72 } else { 53 73 if(r[1] == NULL) { 54 cm_log("Config", "Missing virtual host ");74 cm_log("Config", "Missing virtual host at line %d", ln); 55 75 stop = 1; 56 76 } else { 57 77 vhost = cm_strdup(r[1]); 78 current = &config.vhosts[config.vhost_count++]; 79 int i; 80 current->name = cm_strdup(vhost); 81 current->port = 80; 82 for(i = 0; vhost[i] != 0; i++) { 83 if(vhost[i] == ':') { 84 current->name[i] = 0; 85 current->port = atoi(current->name + i + 1); 86 break; 87 } 88 } 58 89 } 59 90 } 60 91 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) { 61 92 if(vhost == NULL) { 62 cm_log("Config", "Not in virtual host section ");93 cm_log("Config", "Not in virtual host section at line %d", ln); 63 94 stop = 1; 64 95 } else { 65 96 free(vhost); 66 97 vhost = NULL; 98 current = &config.root; 67 99 } 68 100 } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) { … … 75 107 ; 76 108 config.ports[j] = port; 109 } 110 } else if(cm_strcaseequ(r[0], "SSLKey")) { 111 if(r[1] == NULL) { 112 cm_log("Config", "Missing path at line %d", ln); 113 stop = 1; 114 } else { 115 if(current->sslkey != NULL) free(current->sslkey); 116 current->sslkey = cm_strdup(r[1]); 117 } 118 } else if(cm_strcaseequ(r[0], "SSLCertificate")) { 119 if(r[1] == NULL) { 120 cm_log("Config", "Missing path at line %d", ln); 121 stop = 1; 122 } else { 123 if(current->sslcert != NULL) free(current->sslcert); 124 current->sslcert = cm_strdup(r[1]); 77 125 } 78 126 } else { -
trunk/Server/server.c
r11 r12 3 3 #include "tw_server.h" 4 4 5 #include "tw_ssl.h" 5 6 #include "tw_config.h" 6 7 … … 115 116 struct pass_entry { 116 117 int sock; 118 int port; 117 119 bool ssl; 118 120 }; … … 121 123 int sock = ((struct pass_entry*)ptr)->sock; 122 124 bool ssl = ((struct pass_entry*)ptr)->ssl; 125 int port = ((struct pass_entry*)ptR)->port; 126 free(ptr); 123 127 #else 124 void tw_server_pass(int sock, bool ssl ) {128 void tw_server_pass(int sock, bool ssl, int port) { 125 129 #endif 130 SSL_CTX* ctx = NULL; 131 SSL* s = NULL; 132 if(ssl) { 133 ctx = tw_create_ssl_ctx(port); 134 s = SSL_new(ctx); 135 SSL_set_fd(s, sock); 136 if(SSL_accept(s) <= 0) goto cleanup; 137 } 138 cleanup: 126 139 close_socket(sock); 127 140 #ifdef __MINGW32__ … … 151 164 int clen = sizeof(claddr); 152 165 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen); 166 cm_log("Server", "New connection accepted"); 153 167 #ifdef __MINGW32__ 154 168 HANDLE thread; … … 156 170 e->sock = sock; 157 171 e->ssl = config.ports[i] & (1ULL << 32); 172 e->port = config.ports[i]; 158 173 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL); 159 174 #else 160 175 pid_t pid = fork(); 161 176 if(pid == 0) { 162 tw_server_pass(sock, config.ports[i] & (1ULL << 32) );177 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i]); 163 178 _exit(0); 164 179 } else { -
trunk/Server/ssl.c
r11 r12 2 2 3 3 #include "tw_ssl.h" 4 5 #include "tw_config.h" 6 7 #include <stdio.h> 8 9 #include <cm_log.h> 10 11 extern struct tw_config config; 12 13 int tw_ssl_cert_cb(SSL* ssl, void* arg) { 14 const char* s = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); 15 if(s != NULL) { 16 cm_log("SSL", "Certificate request for %s", s); 17 } else { 18 s = config.hostname; 19 cm_log("SSL", "Could not get the servername, defaulting to the hostname: %s", s); 20 } 21 struct tw_config_entry* e = tw_vhost_match(s, (uint64_t)arg); 22 if(e != NULL && e->sslkey != NULL && e->sslcert != NULL) { 23 SSL_use_PrivateKey_file(ssl, e->sslkey, SSL_FILETYPE_PEM); 24 SSL_use_certificate_file(ssl, e->sslcert, SSL_FILETYPE_PEM); 25 return 1; 26 } else { 27 return 0; 28 } 29 } 30 31 SSL_CTX* tw_create_ssl_ctx(uint64_t port) { 32 SSL_CTX* ctx = SSL_CTX_new(TLS_server_method()); 33 SSL_CTX_set_cert_cb(ctx, tw_ssl_cert_cb, (void*)port); 34 return ctx; 35 } -
trunk/Server/tw_config.h
r7 r12 6 6 #include <stdint.h> 7 7 8 /* I don't think you would listen to 1024 ports */9 8 #define MAX_PORTS 1024 10 9 11 struct tw_config_entry {}; 10 #define MAX_VHOSTS 1024 11 12 struct tw_config_entry { 13 char* name; 14 int port; 15 char* sslkey; 16 char* sslcert; 17 }; 12 18 13 19 struct tw_config { 14 20 uint64_t ports[MAX_PORTS + 1]; /* If port & (1 << 32) is non-zero, it is SSL */ 21 char hostname[1025]; 15 22 struct tw_config_entry root; 23 struct tw_config_entry vhosts[MAX_VHOSTS]; 24 int vhost_count; 16 25 }; 17 26 18 27 void tw_config_init(void); 19 28 int tw_config_read(const char* path); 29 struct tw_config_entry* tw_vhost_match(const char* name, int port); 20 30 21 31 #endif -
trunk/Server/tw_ssl.h
r11 r12 4 4 #define __TW_SSL_H__ 5 5 6 #include <openssl/ssl.h> 7 8 SSL_CTX* tw_create_ssl_ctx(uint64_t port); 9 6 10 #endif
Note:
See TracChangeset
for help on using the changeset viewer.