summaryrefslogtreecommitdiff
path: root/main.c
blob: 52d10bd0ce95f9c1b174a750c7435ccb8fc61680 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.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


int num_threads=0;


static void signal_handler(int sig){
	if(sig==SIGCHLD){
		int saved_errno=errno;
		while(true){
			int status;
			pid_t pid=waitpid(-1,&status,WNOHANG);
			if(pid<=0){
				break;
			}
			if(WIFEXITED(status)){
				num_threads--;
			}
		}
		errno=saved_errno;
	}
}


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

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 connection_handler(int sock){
	Headers *headers=http_get_headers(sock);
	if(headers==NULL){
		return;
	}

	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;
	}
}


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;
	}

	signal(SIGCHLD,signal_handler);

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

	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));

		pid_t pid=fork();
		if(pid==0){
			close(sock);
			connection_handler(clientsock);
			close(clientsock);
			exit(0);
		}
		if(pid<0){
			perror("fork");
			return 1;
		}
		num_threads++;
		printf("Accept from %s; child pid %d, %d thread%s\n",str,pid,num_threads,
		       num_threads==1?"":"s");
		close(clientsock);
	}
}