source: Main/trunk/Server/server.c@ 17

Last change on this file since 17 was 17, checked in by Nishi, on Sep 14, 2024 at 2:41:07 AM

module system kinda works

  • Property svn:keywords set to Id
File size: 4.5 KB
RevLine 
[8]1/* $Id: server.c 17 2024-09-13 17:41:07Z nishi $ */
2
[16]3#define SOURCE
4
[8]5#include "tw_server.h"
6
[12]7#include "tw_ssl.h"
[8]8#include "tw_config.h"
[16]9#include "tw_http.h"
[8]10
11#include <unistd.h>
12#include <string.h>
[11]13#include <stdbool.h>
[8]14
15#include <cm_log.h>
16
17#ifdef __MINGW32__
18#include <winsock2.h>
[11]19#include <process.h>
[8]20#define NO_IPV6
21#else
22#include <sys/select.h>
23#include <sys/socket.h>
24#include <arpa/inet.h>
25#include <netinet/in.h>
26#include <netinet/tcp.h>
27#endif
28
29extern struct tw_config config;
30
31fd_set fdset;
32int sockcount = 0;
33
34#ifdef NO_IPV6
[9]35#define SOCKADDR struct sockaddr_in
[8]36#else
[9]37#define SOCKADDR struct sockaddr_in6
[8]38#endif
[9]39SOCKADDR addresses[MAX_PORTS];
40int sockets[MAX_PORTS];
[8]41
42void close_socket(int sock) {
43#ifdef __MINGW32__
44 closesocket(sock);
45#else
46 close(sock);
47#endif
48}
49
50int tw_server_init(void) {
51 int i;
52#ifdef __MINGW32__
53 WSADATA wsa;
54 WSAStartup(MAKEWORD(2, 0), &wsa);
55#endif
56 for(i = 0; config.ports[i] != -1; i++)
57 ;
58 sockcount = i;
59 for(i = 0; config.ports[i] != -1; i++) {
60#ifdef NO_IPV6
61 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
62#else
63 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
64#endif
65#ifdef __MINGW32__
66 if(sock == INVALID_SOCKET)
67#else
68 if(sock < 0)
69#endif
70 {
71 cm_log("Server", "Socket creation failure");
72 return 1;
73 }
74 int yes = 1;
75 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
76 close_socket(sock);
77 cm_log("Server", "setsockopt failure (reuseaddr)");
78 return 1;
79 }
80 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
81 close_socket(sock);
82 cm_log("Server", "setsockopt failure (nodelay)");
83 return 1;
84 }
85#ifndef NO_IPV6
86 int no = 0;
87 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
88 close_socket(sock);
89 cm_log("Server", "setsockopt failure (IPv6)");
90 return 1;
91 }
92#endif
93 memset(&addresses[i], 0, sizeof(addresses[i]));
94#ifdef NO_IPV6
95 addresses[i].sin_family = AF_INET;
96 addresses[i].sin_addr.s_addr = INADDR_ANY;
97 addresses[i].sin_port = htons(config.ports[i]);
98#else
99 addresses[i].sin6_family = AF_INET6;
100 addresses[i].sin6_addr = in6addr_any;
101 addresses[i].sin6_port = htons(config.ports[i]);
102#endif
103 if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
104 close_socket(sock);
105 cm_log("Server", "Bind failure");
106 return 1;
107 }
108 if(listen(sock, 128) < 0) {
109 close_socket(sock);
110 cm_log("Server", "Listen failure");
111 return 1;
112 }
113 sockets[i] = sock;
114 }
115 return 0;
116}
[9]117
[16]118size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
119 if(ssl == NULL) {
120 return recv(s, data, len, 0);
121 } else {
122 return SSL_read(ssl, data, len);
123 }
124}
125
126size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
127 if(ssl == NULL) {
128 return send(s, data, len, 0);
129 } else {
130 return SSL_write(ssl, data, len);
131 }
132}
133
[11]134#ifdef __MINGW32__
135struct pass_entry {
136 int sock;
[12]137 int port;
[11]138 bool ssl;
139};
140
141unsigned int WINAPI tw_server_pass(void* ptr) {
142 int sock = ((struct pass_entry*)ptr)->sock;
143 bool ssl = ((struct pass_entry*)ptr)->ssl;
[14]144 int port = ((struct pass_entry*)ptr)->port;
[12]145 free(ptr);
[11]146#else
[12]147void tw_server_pass(int sock, bool ssl, int port) {
[11]148#endif
[13]149 char* name = config.hostname;
150
[12]151 SSL_CTX* ctx = NULL;
152 SSL* s = NULL;
[15]153 bool sslworks = false;
[12]154 if(ssl) {
155 ctx = tw_create_ssl_ctx(port);
156 s = SSL_new(ctx);
157 SSL_set_fd(s, sock);
158 if(SSL_accept(s) <= 0) goto cleanup;
[15]159 sslworks = true;
[12]160 }
[16]161 struct tw_http_request req;
162 int ret = tw_http_parse(s, sock, &req);
[17]163 if(ret == 0) {
164 }
[12]165cleanup:
[16]166 if(sslworks) {
[15]167 SSL_shutdown(s);
168 }
169 SSL_free(s);
[11]170 close_socket(sock);
171#ifdef __MINGW32__
172 _endthreadex(0);
173#endif
174}
175
176void tw_server_loop(void) {
[9]177 struct timeval tv;
[11]178 while(1) {
[10]179 FD_ZERO(&fdset);
180 int i;
[11]181 for(i = 0; i < sockcount; i++) {
[10]182 FD_SET(sockets[i], &fdset);
183 }
[9]184 tv.tv_sec = 1;
185 tv.tv_usec = 0;
[10]186 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
[11]187 if(ret == -1) {
[9]188 break;
[11]189 } else if(ret > 0) {
[9]190 /* connection */
191 int i;
[11]192 for(i = 0; i < sockcount; i++) {
193 if(FD_ISSET(sockets[i], &fdset)) {
[9]194 SOCKADDR claddr;
195 int clen = sizeof(claddr);
196 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
[12]197 cm_log("Server", "New connection accepted");
[11]198#ifdef __MINGW32__
199 HANDLE thread;
200 struct pass_entry* e = malloc(sizeof(*e));
201 e->sock = sock;
202 e->ssl = config.ports[i] & (1ULL << 32);
[12]203 e->port = config.ports[i];
[11]204 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
205#else
206 pid_t pid = fork();
207 if(pid == 0) {
[12]208 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i]);
[11]209 _exit(0);
210 } else {
211 close_socket(sock);
212 }
213#endif
[9]214 }
215 }
216 }
217 }
218}
Note: See TracBrowser for help on using the repository browser.