aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 145a614b9c99f8517879386e41ee50999a1786bb (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
179
180
181
182
183
184
185
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <assert.h>
#include <sodium.h>
#include "broadcast.h"
#include "command.h"
#include "conn_data.h"
#include "db.h"
#include "event.h"
#include "firebase.h"
#include "net.h"
#include "plugin.h"
#include "runloop.h"
#include "user_data.h"
#include "hashtable.h"

#define PORT (29536)  // python: int("msg",36)


static int create_server_socket(void){
	int sock=socket(AF_INET,SOCK_STREAM,0);
	if(sock<0)die_perror("socket");
	int one=1;
	setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof one);

	struct sockaddr_in name;
	name.sin_family=AF_INET;
	name.sin_addr.s_addr=htonl(INADDR_ANY);
	name.sin_port=htons(PORT);
	if(bind(sock,(struct sockaddr*)&name,sizeof name)<0)die_perror("bind");

	if(listen(sock,16)<0)die_perror("listen");
	return sock;
}

static struct hashtable *conn_hash = NULL;

static void conn_hash_init(void) {
	conn_hash = ht_alloc();
}

static void add_conn_data(int fd, struct conn_data *item) {
	ht_insert(conn_hash, fd, item);
}

static struct conn_data* find_conn_data(int fd){
	struct conn_data *item = (struct conn_data*)ht_find(conn_hash, fd);
	assert(item);
	return item;
}

static void delete_conn_data(int fd){
	debug("Deleting conn_data for fd=%d",fd);

	struct conn_data *item = find_conn_data(fd);

	if (item->userid != -1) {
		userdata_unregister(item->userid, fd);
		broadcast_online_change(item->userid);
	}
	conn_data_nullify(item);
	free(item);

	ht_delete(conn_hash, fd);
}

static bool client_socket_callback(int fd){
	struct conn_data *data=find_conn_data(fd);
	if(data->bufsz-data->buflen<256){
		data->bufsz*=2;
		data->buffer=realloc(data->buffer,data->bufsz,char);
	}

	ssize_t ret;
	do ret=read(fd,data->buffer+data->buflen,data->bufsz-data->buflen);
	while(ret<0&&errno==EINTR);
	if(ret==0||(ret<0&&(errno==ECONNRESET||errno==ETIMEDOUT))){
		delete_conn_data(fd);
		close(fd);
		return true;
	}
	if(ret<0)die_perror("read");
	data->buflen+=ret;

	while(true){
		char *lfp=memchr(data->buffer,'\n',data->buflen);
		if(lfp==NULL)break;
		size_t length=lfp-data->buffer;
		bool should_close=handle_input_line(data,data->buffer,length);
		memmove(data->buffer,lfp+1,data->buflen-length-1);
		data->buflen-=length+1;

		if(should_close){
			delete_conn_data(fd);
			close(fd);
			return true;
		}
	}
	return false;
}

static bool server_socket_callback(int fd){
	int sock;
	do sock=accept(fd,NULL,NULL);
	while(sock<0&&errno==EINTR);
	if(sock<0)die_perror("accept");
	runloop_add_fd(sock,client_socket_callback,true);

	struct conn_data *item = malloc(1, struct conn_data);
	conn_data_init(item, sock);
	add_conn_data(sock, item);

	debug("Added conn_data for fd=%d",sock);
	return false;
}

static bool timeout_callback(int fd){
	// Note: this does not check the protocol version. If the protocol changes
	// significantly regarding push messages, perhaps that should change.
	return net_send_raw_text(fd,"_push ping\n",11);
}

#ifndef __APPLE__
static void srandomdev(void){
	FILE *f=fopen("/dev/urandom","r");
	assert(f);
	unsigned int seed;
	size_t nread=fread(&seed,1,sizeof(seed),f);
	assert(nread==sizeof(seed));
	fclose(f);
	srandom(seed);
}
#endif

static void signal_handler(int sig){
	if(sig==SIGPIPE){
		// ignore
	}
}

static bool constructors_work = false;

__attribute__((constructor))
static void constructor(void) { constructors_work = true; }

int main(int argc,char **argv){
	if (!constructors_work) {
		fprintf(stderr, "This platform does not support application constructors, which tomsg requires\n");
		return 1;
	}

	srandomdev();
	if(sodium_init()<0){
		fprintf(stderr,"Could not initialise libsodium!\n");
		return 1;
	}

	signal(SIGPIPE,signal_handler);

	conn_hash_init();

	plugin_init();
	for(int i=1;i<argc;i++){
		plugin_register(argv[i]);
	}
	if(argc<=1)printf("Loaded no plugins\n");

	db_init();
	firebase_init();
	int sock=create_server_socket();
	printf("Listening on port %d\n",PORT);
	runloop_set_timeout(60*1000000,timeout_callback);
	runloop_add_fd(sock,server_socket_callback,false);
	runloop_run();
	printf("Shutting down because runloop stopped\n");
	firebase_stop();
	db_close();
}