summaryrefslogtreecommitdiff
path: root/main.c
blob: d38b141549325b66600f67c1ce143bf52c7d2a9f (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include "http.h"
#include "memory.h"
#include "plugin.h"
#include "util.h"


#define PORT 8080


static int num_threads=0;
static pthread_mutex_t num_threads_mutex;


typedef struct Plugin{
	char *name;
	Handler handler;
	struct Plugin *next;
} Plugin;

// Will only be read once threading starts, so no mutex needed
static Plugin *pluginlist=NULL,*pluginlist_last=NULL;

static void plugin_register_callback(const char *name,Handler handler){
	if(pluginlist==NULL){
		pluginlist=pluginlist_last=malloc(1,Plugin);
		pluginlist->name=copy_str(name);
		pluginlist->handler=handler;
		pluginlist->next=NULL;
	} else {
		Plugin *newplugin=malloc(1,Plugin);
		newplugin->name=copy_str(name);
		newplugin->handler=handler;
		newplugin->next=NULL;
		pluginlist_last->next=newplugin;
		pluginlist_last=newplugin;
	}
	printf("Registered plugin '%s' (handler %p)\n",name,handler);
}

typedef void (*register_function)(register_callback_t callback);

static void load_plugin(const char *name){
	void *lib=dlopen(name,RTLD_NOW|RTLD_LOCAL);
	if(lib==NULL){
		fprintf(stderr,"dlopen(%s): %s\n",name,dlerror());
		exit(1);
	}
	register_function regfunc=(register_function)dlsym(lib,"plugin_register_yourself");
	if(regfunc==NULL){
		fprintf(stderr,"Cannot find plugin_register_yourself symbol in library '%s'\n",name);
		exit(1);
	}
	regfunc(&plugin_register_callback);
}


static void* thread_entry_connection_handler(void *sock_){
	int sock=(int)(uintptr_t)sock_;

	Headers *headers=http_get_headers(sock);
	if(headers==NULL){
		goto cleanup_exit;
	}

	for(Plugin *pl=pluginlist;pl!=NULL;pl=pl->next){
		switch(pl->handler(sock,headers)){
			case HR_HANDLED:
				printf("Request handled by %s\n",pl->name);
				break;

			case HR_NEXT:
				continue;

			case HR_ERROR:
				printf("Request handler '%s' threw error\n",pl->name);
				break;
		}
		break;
	}

cleanup_exit:
	close(sock);
	PTHREAD_CHECK(pthread_mutex_lock,&num_threads_mutex);
	num_threads--;
	PTHREAD_CHECK(pthread_mutex_unlock,&num_threads_mutex);
	return NULL;
}


int main(int argc,char **argv){
	if(argc==1){
		printf("Loading no plugin files\n");
	} else {
		for(int i=1;i<argc;i++){
			load_plugin(argv[i]);
		}
	}

	int sock=socket(PF_INET,SOCK_STREAM,0);
	if(sock<0){
		perror("socket");
		return 1;
	}

	int one=1;
	if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(int))<0){
		perror("setsockopt");
		return 1;
	}

	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_addr.s_addr=htonl(INADDR_ANY);
	sin.sin_port=htons(PORT);

	if(bind(sock,(struct sockaddr*)&sin,sizeof(sin))<0){
		close(sock);
		perror("bind");
		return 1;
	}

	if(listen(sock,8)<0){
		perror("listen");
		return 1;
	}

	printf("Listening on port %d\n",PORT);

	PTHREAD_CHECK(pthread_mutex_init,&num_threads_mutex,NULL);

	while(true){
		struct sockaddr_storage client_addr;
		socklen_t client_addr_sz=sizeof(client_addr);
		int clientsock=accept(sock,(struct sockaddr*)&client_addr,&client_addr_sz);
		if(clientsock<0){
			perror("accept");
			continue;
		}

		char str[INET_ADDRSTRLEN];
		inet_ntop(client_addr.ss_family,&((struct sockaddr_in*)&client_addr)->sin_addr,
		          str,sizeof(str));

		printf("Accept from %s; %d thread%s still left\n",str,num_threads,num_threads==1?"":"s");

		PTHREAD_CHECK(pthread_mutex_lock,&num_threads_mutex);
		num_threads++;
		PTHREAD_CHECK(pthread_mutex_unlock,&num_threads_mutex);

		pthread_t th;
		PTHREAD_CHECK(pthread_create,&th,NULL,thread_entry_connection_handler,(void*)(uintptr_t)clientsock);
		PTHREAD_CHECK(pthread_detach,th);
	}
}