summaryrefslogtreecommitdiffstats
path: root/include/server.h
blob: 3435b1f1859898681864f6988ff175a62bcb9b53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef GMNISRV_SERVER
#define GMNISRV_SERVER
#include <assert.h>
#include <openssl/ssl.h>
#include <poll.h>
#include <time.h>
#include <stdbool.h>
#include "gemini.h"
#include "url.h"

struct gmnisrv_server;

enum response_state {
	RESPOND_HEADER,
	RESPOND_BODY,
};

struct gmnisrv_client {
	struct gmnisrv_server *server;
	struct timespec ctime;
	struct sockaddr addr;
	socklen_t addrlen;
	int sockfd;
	struct pollfd *pollfd;

	SSL *ssl;
	BIO *bio, *sbio;

	char buf[BUFSIZ];
	static_assert(BUFSIZ >= GEMINI_MAX_URL + 3);
	size_t bufix, bufln;

	enum response_state state;
	enum gemini_status status;
	char *meta;
	int bodyfd;
	size_t bbytes;
 
	struct gmnisrv_host *host;
	char *path;
};

struct gmisrv_config;

struct gmnisrv_server {
	struct gmnisrv_config *conf;
	struct pollfd *fds;
	nfds_t nfds, fdsz;

	// nlisten is initialized once and does not change. The fds list starts
	// with this many listening sockets, then has sockets for each active
	// client, up to nfds.
	size_t nlisten;

	struct gmnisrv_client *clients;
	size_t nclients, clientsz;

	bool run;
};

int server_init(struct gmnisrv_server *server, struct gmnisrv_config *conf);
void server_run(struct gmnisrv_server *server);
void server_finish(struct gmnisrv_server *server);

#endif