source: Main/trunk/Server/config.c@ 14

Last change on this file since 14 was 13, checked in by Nishi, on Sep 13, 2024 at 10:38:57 PM

better

  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/* $Id: config.c 13 2024-09-13 13:38:57Z nishi $ */
2
3#include "tw_config.h"
4
5#include <stdio.h>
6#include <stdint.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include <cm_string.h>
12#include <cm_log.h>
13
14struct tw_config config;
15
16struct 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 == -1 ? 1 : config.vhosts[i].port == port)) {
20 return &config.vhosts[i];
21 }
22 }
23 return &config.root;
24}
25
26void tw_config_init(void) {
27 int i;
28 for(i = 0; i < MAX_PORTS + 1; i++) {
29 config.ports[i] = -1;
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);
39}
40
41int tw_config_read(const char* path) {
42 cm_log("Config", "Reading %s", path);
43 char cbuf[2];
44 cbuf[1] = 0;
45 int ln = 0;
46 FILE* f = fopen(path, "r");
47 if(f != NULL) {
48 char* line = malloc(1);
49 line[0] = 0;
50 int stop = 0;
51 struct tw_config_entry* current = &config.root;
52 char* vhost = NULL;
53 while(stop == 0) {
54 int c = fread(cbuf, 1, 1, f);
55 if(cbuf[0] == '\n' || c <= 0) {
56 ln++;
57 char* l = cm_trim(line);
58 if(strlen(l) > 0 && l[0] != '#') {
59 char** r = cm_split(l, " \t");
60 int i;
61 if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
62 for(i = 1; r[i] != NULL; i++) {
63 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
64 stop = 1;
65 break;
66 }
67 }
68 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
69 if(vhost != NULL) {
70 cm_log("Config", "Already in virtual host section at line %d", ln);
71 stop = 1;
72 } else {
73 if(r[1] == NULL) {
74 cm_log("Config", "Missing virtual host at line %d", ln);
75 stop = 1;
76 } else {
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 = -1;
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 }
89 }
90 }
91 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
92 if(vhost == NULL) {
93 cm_log("Config", "Not in virtual host section at line %d", ln);
94 stop = 1;
95 } else {
96 free(vhost);
97 vhost = NULL;
98 current = &config.root;
99 }
100 } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
101 for(i = 1; r[i] != NULL; i++) {
102 uint64_t port = atoi(r[i]);
103 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
104 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
105 int j;
106 for(j = 0; config.ports[j] != -1; j++)
107 ;
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]);
125 }
126 } else {
127 if(r[0] != NULL) {
128 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
129 }
130 stop = 1;
131 }
132 for(i = 0; r[i] != NULL; i++) free(r[i]);
133 free(r);
134 }
135 free(l);
136 free(line);
137 line = malloc(1);
138 line[0] = 0;
139 if(c <= 0) break;
140 } else if(cbuf[0] != '\r') {
141 char* tmp = line;
142 line = cm_strcat(tmp, cbuf);
143 free(tmp);
144 }
145 }
146 free(line);
147 fclose(f);
148 return stop;
149 } else {
150 cm_log("Config", "Could not open the file");
151 return 1;
152 }
153}
Note: See TracBrowser for help on using the repository browser.