-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.c
222 lines (170 loc) · 4.42 KB
/
tracker.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <Config.h>
#include <ConnectionManager.h>
#include <log.h>
#include <tracker.h>
#include <util.h>
#include <xmalloc.h>
#define DEFAULT_HOTLINE_PORT 5500
#define DEFAULT_TRACKER_PORT 5499
#define DEFAULT_UPDATE_INTERVAL 150
typedef struct _TListNode {
int fd;
struct sockaddr_in sin;
struct _TListNode *l;
} *TListNode;
static char *server_name = NULL, *server_desc = NULL;
static int server_name_l, server_desc_l;
static int server_port = DEFAULT_HOTLINE_PORT;
static int update_interval = DEFAULT_UPDATE_INTERVAL;
static int tlist_count = 0;
static TListNode tlist = NULL;
static int tthread_active = 0;
static pthread_t ttid;
static pthread_mutex_t tlock = PTHREAD_MUTEX_INITIALIZER;
static void tlist_add(char *str)
{
char *hostname;
int port;
struct hostent *host;
TListNode n = NEW(TListNode);
hostname = str;
if((str = strchr(hostname, ':')) != NULL) {
*str++ = '\0';
port = atoi(str);
} else
port = DEFAULT_TRACKER_PORT;
memset(&(n->sin), '\0', sizeof(struct sockaddr_in));
if((n->sin.sin_addr.s_addr = inet_addr(hostname)) < 0) {
if((host = xgethostbyname(hostname)) == NULL)
goto err;
memcpy(&n->sin.sin_addr, host->h_addr, host->h_length);
xfree(host);
}
n->sin.sin_family = AF_INET;
n->sin.sin_port = htons(port);
if((n->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
goto err;
n->l = tlist;
tlist = n;
tlist_count++;
return;
err:
log("!!! Couldn't resolve tracker: %s", hostname);
xfree(n);
}
static void tlist_clear()
{
TListNode n;
while(tlist != NULL) {
n = tlist;
tlist = tlist->l;
close(n->fd);
xfree(n);
}
tlist_count = 0;
}
static void tracker_broadcast()
{
TListNode c;
uint8_t *dgram;
uint16_t users;
dgram = (uint8_t *)xcalloc(1, 14 + server_name_l + server_desc_l);
users = cm_user_count();
/* Version code */
*(uint16_t *)dgram = htons(1);
/* Server port */
*(uint16_t *)(dgram + 2) = htons(server_port);
/* Number of users on server */
*(uint16_t *)(dgram + 4) = htons(users);
/* Server name */
dgram[12] = server_name_l;
memcpy(dgram + 13, server_name, server_name_l);
/* Server description */
dgram[13 + server_name_l] = server_desc_l;
memcpy(dgram + 14 + server_name_l, server_desc, server_desc_l);
/* Broadcast datagram to trackers */
for(c = tlist; c != NULL; c = c->l)
sendto(c->fd, dgram, 14 + server_name_l + server_desc_l, 0, (struct sockaddr *)&(c->sin), sizeof(struct sockaddr_in));
xfree(dgram);
}
static void *tracker_main(void *ptr)
{
int sleep_period;
pthread_mutex_lock(&tlock);
log("*** Tracker thread started. Updating %d trackers every %d seconds", tlist_count, update_interval);
while(tlist != NULL) {
tracker_broadcast();
sleep_period = update_interval;
pthread_mutex_unlock(&tlock);
sleep(sleep_period);
pthread_mutex_lock(&tlock);
}
pthread_mutex_unlock(&tlock);
return NULL;
}
static void tracker_thread_spawn()
{
if(tthread_active)
return;
if(pthread_create(&ttid, NULL, tracker_main, NULL) < 0) {
log("!!! Warning: Couldn't spawn tracker thread");
return;
}
tthread_active = 1;
}
void tracker_init()
{
int i, v;
char *t;
pthread_mutex_lock(&tlock);
tlist_clear();
for(i = 0; (t = config_vlist_value("trackers", "host_list", i)) != NULL; i++) {
tlist_add(t);
xfree(t);
}
if(i == 0)
goto done;
t = config_value("global", "server_name");
if(server_name == NULL && t == NULL) {
log("*** Set global::server_name if you wish to broadcast to trackers");
log("*** No broadcasting to trackers will be done with the current configuration");
goto done;
}
if(t != NULL) {
if(server_name != NULL)
xfree(server_name);
server_name = t;
server_name_l = strlen(server_name);
}
t = config_value("global", "server_description");
if(server_desc != NULL) {
if(t != NULL) {
xfree(server_desc);
server_desc = NULL;
}
} else if(t == NULL)
t = xstrdup("");
if(server_name == NULL) {
server_desc = t;
server_desc_l = strlen(server_desc);
}
if((v = config_int_value("trackers", "update_interval")) < 0) {
log("*** trackers::update_interval invalid/unspecified");
} else
update_interval = v;
if((server_port = config_int_value("global", "port")) < 0)
server_port = DEFAULT_HOTLINE_PORT;
tracker_thread_spawn();
done:
pthread_mutex_unlock(&tlock);
}