forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX_browser_sources.c
1148 lines (950 loc) · 26.8 KB
/
EX_browser_sources.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011 azazello and ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quakedef.h"
#include "fs.h"
#ifndef _WIN32
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#endif
#include <curl/curl.h>
#include "EX_browser.h"
#define SOURCES_LIST_FILENAME "sb/sources.txt"
// Used by curl to read server lists from the web
struct curl_buf
{
char *ptr;
size_t len;
};
// sources table
source_data *sources[MAX_SOURCES];
int sourcesn;
source_data * Create_Source(void)
{
source_data *s;
s = (source_data *) Q_malloc(sizeof(source_data));
s->serversn = 0;
s->servers_allocated = 0;
s->last_update.wYear = 0;
s->name[0] = 0;
s->checked = 0;
s->servers = NULL;
s->unique = source_unique++;
return s;
}
void Reset_Source(source_data *s)
{
int i;
if (s->servers != NULL)
{
for (i=0; i < s->serversn; i++)
Q_free(s->servers[i]);
Q_free(s->servers);
}
s->serversn = 0;
s->servers_allocated = 0;
s->last_update.wYear = 0;
//s->name[0] = 0;
}
// used only for list re-loading
void Delete_Source(source_data *s)
{
Reset_Source(s);
Q_free(s);
}
// returns true, if there were some problems (like domain-name addresses)
// which require the source to be dumped to file in corrected form
qbool Update_Source_From_File(source_data *s, char *fname, server_data **servers, int *pserversn)
{
vfsfile_t *f;
char line[2048];
qbool should_dump = false;
//length = COM_FileOpenRead (fname, &f);
f = FS_OpenVFS(fname, "rb", FS_ANY);
if (f) {
while (VFS_GETS(f, line, sizeof(line)))
{
netadr_t addr;
if (!strchr(line, ':'))
strlcat (line, ":27000", sizeof (line));
if (!NET_StringToAdr(line, &addr))
continue;
servers[(*pserversn)++] = Create_Server2(addr);
if (line[0] <= '0' || line[0] >= '9')
should_dump = true;
}
VFS_CLOSE(f);
} else {
//Com_Printf ("Updating %15.15s failed: file not found\n", s->name);
// ?????? should_dump = true;
}
return should_dump;
}
static size_t SB_URL_to_FileName(const char *str, char *dest, size_t size)
{
size_t written = 0;
char *hexa = "0123456789abcdef";
while (*str != '\0' && written + 1 < size) {
if (isalnum(*str) || *str == '.') {
*dest++ = *str++;
written++;
}
else {
if (written + 1 < size) {
int curchar = *str++;
written += 3;
*dest++ = '_';
*dest++ = hexa[curchar / 16];
*dest++ = hexa[curchar % 16];
}
else {
break;
}
}
}
*dest = '\0';
return written;
}
static size_t SB_URL_To_Filename_Length(const char *s)
{
return strlen(s)*3+1;
}
static void Precache_Source(source_data *s)
{
int i;
char name[1024];
server_data *servers[MAX_SERVERS];
int serversn = 0;
if (s->type == type_url) {
char *filename;
size_t filename_size = SB_URL_To_Filename_Length(s->address.url);
filename = Q_malloc(filename_size);
SB_URL_to_FileName(s->address.url, filename, filename_size);
snprintf(name, sizeof (name), "sb/cache/%s", filename);
Q_free(filename);
}
else if (s->type == type_master) {
snprintf(name, sizeof (name), "sb/cache/%d_%d_%d_%d_[%d].txt",
s->address.address.ip[0], s->address.address.ip[1],
s->address.address.ip[2], s->address.address.ip[3],
ntohs(s->address.address.port));
}
else {
return;
}
Update_Source_From_File(s, name, servers, &serversn);
if (serversn > 0)
{
SYSTEMTIME tm;
char tmp_path[MAX_OSPATH] = {0};
snprintf(&tmp_path[0], sizeof(tmp_path), "%s/ezquake/%s", com_basedir, name);
if (GetFileLocalTime(tmp_path, &tm))
{
Reset_Source(s);
s->servers = (server_data **) Q_malloc(serversn * sizeof(server_data *));
for (i=0; i < serversn; i++)
s->servers[i] = servers[i];
s->serversn = serversn;
s->servers_allocated = serversn;
if (s->checked)
rebuild_servers_list = 1;
memcpy(&s->last_update, &tm, sizeof(SYSTEMTIME));
}
}
}
static void SB_Process_URL_Buffer(const struct curl_buf *curl_buf, server_data *servers[], int *serversn)
{
netadr_t addr;
char *buf, *p0, *p1;
// Don't modify curl_buf as it might be used to create cache file
buf = Q_malloc(sizeof(char) * curl_buf->len);
memcpy(buf, curl_buf->ptr, sizeof(char) * curl_buf->len);
// Not using strtok as it's not thread safe
for (p0 = buf, p1 = buf; p1 < buf + curl_buf->len; p1++) {
if (*p1 == '\n') {
*p1 = '\0';
NET_StringToAdr(p0, &addr);
servers[(*serversn)++] = Create_Server2(addr);
p0 = p1 + 1;
}
}
Q_free(buf);
}
static struct curl_buf *curl_buf_init(void)
{
// Q_malloc handles errors and exits on failure
struct curl_buf *curl_buf = Q_malloc(sizeof(struct curl_buf));
curl_buf->len = 0;
curl_buf->ptr = Q_malloc(curl_buf->len + 1);
return curl_buf;
}
static void curl_buf_deinit(struct curl_buf *curl_buf)
{
Q_free(curl_buf->ptr);
Q_free(curl_buf);
}
static size_t curl_write_func( void *ptr, size_t size, size_t nmemb, void* buf_)
{
struct curl_buf * buf = (struct curl_buf *)buf_;
size_t new_len = buf->len + size * nmemb;
// not checking for realloc errors since Q_realloc will exit on failure
buf->ptr = Q_realloc(buf->ptr, new_len + 1);
memcpy(buf->ptr + buf->len, ptr, size * nmemb);
buf->ptr[new_len] = '\0';
buf->len = new_len;
return size * nmemb;
}
int SB_Cache_Source(const source_data *s, const struct curl_buf *curl_buf)
{
size_t filename_buf_len;
char *filename;
FILE *f;
filename_buf_len = SB_URL_To_Filename_Length(s->address.url);
filename = Q_malloc(filename_buf_len);
SB_URL_to_FileName(s->address.url, filename, filename_buf_len);
if (!FS_FCreateFile(filename, &f, "ezquake/sb/cache", "wb+")) {
Com_Printf_State(PRINT_FAIL, "SB_Cache_Source() Can't create cache file");
Q_free(filename);
return 0;
}
fwrite(curl_buf->ptr, sizeof(char), curl_buf->len, f);
fclose(f);
Q_free(filename);
return 1;
}
struct curl_buf *SB_Retrieve_Data(const source_data *s)
{
CURL *curl;
CURLcode res;
struct curl_buf *curl_buf;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, s->address.url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
}
else {
Com_Printf_State(PRINT_FAIL, "SB_Retrieve_Data() Can't init cURL\n");
return NULL;
}
curl_buf = curl_buf_init();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
Com_Printf("SB_Retrieve_Data(): Could not read URL %s\n", s->address.url);
curl_easy_cleanup(curl);
curl_buf_deinit(curl_buf);
return NULL;
}
curl_easy_cleanup(curl);
return curl_buf;
}
static void SB_Update_Source_From_URL(const source_data *s, server_data *servers[],
int *serversn)
{
struct curl_buf *curl_buf;
if (s->type != type_url) {
Com_Printf_State(PRINT_FAIL, "SB_Update_Source_From_URL() Invalid argument\n");
return;
}
// Retrieve servers
curl_buf = SB_Retrieve_Data(s);
if (curl_buf == NULL) {
// SB_Retrieve_Data will print meaningful error message
return;
}
// Update servers variable
SB_Process_URL_Buffer(curl_buf, servers, serversn);
// Cache servers (file)
// No need to check for errors since we cleanup anyways
SB_Cache_Source(s, curl_buf);
curl_buf_deinit(curl_buf);
}
void Update_Source(source_data *s)
{
int i;
qbool should_dump = false;
server_data *servers[MAX_SERVERS];
int serversn = 0;
if (s->type == type_dummy)
return;
if (s->type == type_file)
{
// read servers from file
char name[1024];
snprintf(name, sizeof (name), "sb/%s", s->address.filename);
should_dump = Update_Source_From_File(s, name, servers, &serversn);
GetLocalTime(&(s->last_update));
}
if (s->type == type_url)
{
SB_Update_Source_From_URL(s, servers, &serversn);
}
if (s->type == type_master)
{
// get servers from master server
char request[] = {'c', '\n', '\0'};
socket_t newsocket;
struct sockaddr_storage server;
int ret = 0, i;
unsigned char answer[10000];
fd_set fd;
struct timeval tv;
int trynum;
int timeout;
newsocket = UDP_OpenSocket(PORT_ANY);
// so we have a socket
// send status request
for (trynum=0; trynum < sb_masterretries.value; trynum++) {
NetadrToSockadr (&(s->address.address), &server);
ret = sendto (newsocket, request, sizeof(request), 0,
(struct sockaddr *)&server, sizeof(server) );
}
if (ret < 0)
return;
timeout = Sys_DoubleTime() + (sb_mastertimeout.value / 1000.0);
while (Sys_DoubleTime() < timeout) {
//fd.fd_count = 1;
//fd.fd_array[0] = newsocket;
FD_ZERO(&fd);
FD_SET(newsocket, &fd);
tv.tv_sec = 0;
tv.tv_usec = 1000 * 1.5 * sb_mastertimeout.value; // multiply timeout by 1.5
ret = select(newsocket+1, &fd, NULL, NULL, &tv);
// get answer
if (ret > 0)
ret = recvfrom (newsocket, (char *) answer, 10000, 0, NULL, NULL);
if (ret > 0 && ret < 10000)
{
answer[ret] = 0;
if (memcmp(answer, "\xff\xff\xff\xff\x64\x0a", 6))
{
closesocket(newsocket);
return;
}
// create servers avoiding duplicates
for (i=6; i+5 < ret; i+=6)
{
char buf[32];
server_data* server;
qbool exists = false;
int j;
snprintf(buf, sizeof (buf), "%u.%u.%u.%u:%u",
(int)answer[i+0], (int)answer[i+1],
(int)answer[i+2], (int)answer[i+3],
256 * (int)answer[i+4] + (int)answer[i+5]);
server = Create_Server(buf);
for (j=0; j<serversn; j++) {
if (NET_CompareAdr(servers[j]->address, server->address)) {
exists = true;
break;
}
}
if (!exists)
servers[serversn++] = server;
else
Delete_Server(server);
}
}
}
closesocket(newsocket);
}
SB_ServerList_Lock();
// copy all servers to source list
if (serversn > 0)
{
Reset_Source(s);
s->servers = Q_malloc((serversn + (s->type==type_file ? MAX_UNBOUND : 0)) * sizeof(server_data *));
for (i=0; i < serversn; i++)
s->servers[i] = servers[i];
s->serversn = serversn;
s->servers_allocated = serversn + (s->type == type_file ? MAX_UNBOUND : 0);
if (s->checked)
rebuild_servers_list = 1;
if (sb_mastercache.value)
{
DumpSource(s);
should_dump = false;
}
GetLocalTime(&(s->last_update));
}
else {
if (s->type == type_file)
{
Reset_Source(s);
s->servers = Q_malloc(MAX_UNBOUND * sizeof(server_data *));
}
}
SB_ServerList_Unlock();
if (should_dump)
DumpSource(s);
//Com_Printf ("Updating %15.15s: %d servers\n", s->name, serversn);
}
int updating_sources = 0;
int source_full_update = 0;
typedef struct infohost_s
{
double lastsenttime;
int phase;
} infohost;
source_data **psources;
int psourcesn;
int Update_Multiple_Sources_Proc(void * lpParameter)
{
// get servers from master server
SYSTEMTIME lt;
char request[] = {'c', '\n', '\0'};
socket_t newsocket;
struct sockaddr_storage server;
int ret = 0, i, sourcenum;
unsigned char answer[10000];
fd_set fd;
struct timeval tv;
int total_masters = 0;
int updated = 0;
int d1, d2;
GetLocalTime(<);
d1 = lt.wSecond + 60*(lt.wMinute + 60*(lt.wHour + 24*(lt.wDay)));
// update file sources - this should be a flash
for (sourcenum = 0; sourcenum < psourcesn; sourcenum++)
if (psources[sourcenum]->checked)
{
if (psources[sourcenum]->type == type_file)
Update_Source(psources[sourcenum]);
if (psources[sourcenum]->type == type_url)
Update_Source(psources[sourcenum]); // todo cache this too
else if (psources[sourcenum]->type == type_master)
{
source_data *s = psources[sourcenum];
if (s->last_update.wYear != 0 && !source_full_update)
{
d2 = s->last_update.wSecond + 60*(s->last_update.wMinute + 60*(s->last_update.wHour + 24*(s->last_update.wDay)));
if (d1 > d2 && d1 < d2 + sb_sourcevalidity.value*60)
continue;
}
total_masters++;
}
}
// update master sources
newsocket = UDP_OpenSocket(PORT_ANY);
for (sourcenum = 0; sourcenum < psourcesn && !abort_ping; sourcenum++)
{
server_data *servers[MAX_SERVERS];
int serversn = 0;
int trynum = 0;
source_data *s = psources[sourcenum];
double timeout;
if (psources[sourcenum]->type != type_master || !psources[sourcenum]->checked)
continue;
if (s->last_update.wYear != 0 && !source_full_update)
{
d2 = s->last_update.wSecond + 60*(s->last_update.wMinute + 60*(s->last_update.wHour + 24*(s->last_update.wDay)));
if (d1 > d2 && d1 < d2 + sb_sourcevalidity.value*60)
continue;
}
// send trynum queries to master server
for (trynum=0; trynum < sb_masterretries.value; trynum++)
{
NetadrToSockadr (&(s->address.address), &server);
ret = sendto (newsocket, request, sizeof(request), 0,
(struct sockaddr *)&server, sizeof(server) );
}
if (ret <= 0)
continue;
timeout = Sys_DoubleTime() + (sb_mastertimeout.value / 1000.0);
while (Sys_DoubleTime() < timeout) {
struct sockaddr_storage hostaddr;
netadr_t from;
//fd.fd_count = 1;
//fd.fd_array[0] = newsocket;
FD_ZERO(&fd);
FD_SET(newsocket, &fd);
tv.tv_sec = 0;
tv.tv_usec = 1000 * sb_mastertimeout.value;
ret = select(newsocket+1, &fd, NULL, NULL, &tv);
// get answer
i = sizeof(hostaddr);
if (ret > 0)
ret = recvfrom (newsocket, (char *) answer, 10000, 0,
(struct sockaddr *)&hostaddr, (socklen_t *)&i);
if (ret > 0 && ret < 10000)
{
SockadrToNetadr (&hostaddr, &from);
if (from.ip[0] == s->address.address.ip[0] &&
from.ip[1] == s->address.address.ip[1] &&
from.ip[2] == s->address.address.ip[2] &&
from.ip[3] == s->address.address.ip[3] &&
from.port == s->address.address.port)
{
answer[ret] = 0;
if (memcmp(answer, "\xff\xff\xff\xff\x64\x0a", 6))
{
continue;
}
// create servers avoiding duplicates
for (i=6; i+5 < ret; i+=6)
{
char buf[32];
server_data* server;
qbool exists = false;
int j;
snprintf(buf, sizeof (buf), "%u.%u.%u.%u:%u",
(int)answer[i+0], (int)answer[i+1],
(int)answer[i+2], (int)answer[i+3],
256 * (int)answer[i+4] + (int)answer[i+5]);
server = Create_Server(buf);
for (j=0; j<serversn; j++) {
if (NET_CompareAdr(servers[j]->address, server->address)) {
exists = true;
break;
}
}
if (!exists)
servers[serversn++] = server;
else
Delete_Server(server);
}
}
}
}
// copy all servers to source list
if (serversn > 0)
{
updated++;
SB_ServerList_Lock();
Reset_Source(s);
s->servers = (server_data **) Q_malloc(serversn * sizeof(server_data *));
for (i=0; i < serversn; i++)
s->servers[i] = servers[i];
s->serversn = serversn;
s->servers_allocated = serversn;
if (s->checked)
rebuild_servers_list = 1;
GetLocalTime(&(s->last_update));
SB_ServerList_Unlock();
if (sb_mastercache.value)
DumpSource(s);
}
ping_pos = updated / (double)total_masters;
}
closesocket(newsocket);
// Not having this here leads to crash almost always when some
// other action with servers list happens right after this function.
// Even 1 ms delay was enough during the tests, previously 500 ms was used.
//Sys_MSleep(100);
updating_sources = 0;
sb_queuedtriggers |= SB_TRIGGER_SOURCESUPDATED;
return 0;
}
void Update_Init(source_data *s[], int sn)
{
psources = s;
psourcesn = sn;
abort_ping = 0;
updating_sources = 1;
ping_pos = 0;
}
// starts asynchronous sources update
void Update_Multiple_Sources_Begin(source_data *s[], int sn)
{
Update_Init(s, sn);
if (Sys_CreateDetachedThread(Update_Multiple_Sources_Proc, NULL) < 0) {
Com_Printf("Failed to create Update_Multiple_Sources_Proc thread\n");
}
}
// starts synchronous sources update
void Update_Multiple_Sources(source_data *s[], int sn)
{
Update_Init(s, sn);
Update_Multiple_Sources_Proc(NULL);
}
void SB_Sources_Update(qbool full)
{
source_full_update = full;
Update_Multiple_Sources(sources, sourcesn);
if (rebuild_servers_list)
Rebuild_Servers_List();
}
void SB_Sources_Update_Begin(qbool full)
{
source_full_update = full;
Update_Multiple_Sources_Begin(sources, sourcesn);
}
unsigned int SB_Sources_Marked_Count(void)
{
int i;
unsigned int ret = 0;
for (i = 0; i < sourcesn; i++) {
if (sources[i]->checked == 1) {
ret++;
}
}
return ret;
}
void Toggle_Source(source_data *s)
{
s->checked = !(s->checked);
rebuild_servers_list = 1;
}
void Mark_Source(source_data *s)
{
if (!s->checked)
{
s->checked = 1;
rebuild_servers_list = 1;
}
}
void Unmark_Source(source_data *s)
{
if (s->checked)
{
s->checked = 0;
rebuild_servers_list = 1;
}
}
char * next_space(char *s)
{
char *ret = s;
while (*ret && !isspace2(*ret))
ret++;
return ret;
}
char * next_nonspace(char *s)
{
char *ret = s;
while (*ret && isspace2(*ret))
ret++;
return ret;
}
char * next_quote(char *s)
{
char *ret = s;
while (*ret && *ret != '\"')
ret++;
return ret;
}
qbool SB_Sources_Dump(void)
{
FILE *f;
int i;
if (!FS_FCreateFile(SOURCES_LIST_FILENAME, &f, "ezquake", "wt")) {
return false;
}
for (i = 0; i < sourcesn; i++) {
sb_source_type_t type = sources[i]->type;
if (type == type_master || type == type_file || type == type_url) {
const char *typestr;
const char *name = sources[i]->name;
const char *loc;
if (type == type_master) {
typestr = "master";
loc = NET_AdrToString(sources[i]->address.address);
}
else if (type == type_url) {
typestr = "url";
loc = sources[i]->address.url;
}
else {
typestr = "file";
loc = sources[i]->address.filename;
}
fprintf(f, "%s \"%s\" %s\n", typestr, name, loc);
}
}
fclose(f);
return true;
}
int SB_Source_Add(const char* name, const char* address, sb_source_type_t type)
{
source_data *s;
char addr[512];
int pos;
if (strlen(name) <= 0 || strlen(address) <= 0)
return -1;
// create new source
s = Create_Source();
s->type = type;
strlcpy (s->name, name, sizeof (s->name));
strlcpy (addr, address, sizeof (addr));
if (s->type == type_file) {
strlcpy (s->address.filename, address, sizeof (s->address.filename));
}
else if (s->type == type_url) {
strlcpy(s->address.url, address, sizeof(s->address.url));
}
else {
if (!strchr(addr, ':')) {
strlcat (addr, ":27000", sizeof (addr));
}
if (!NET_StringToAdr(addr, &(s->address.address))) {
return -1;
}
}
pos = sourcesn++;
sources[pos] = s;
Mark_Source(sources[pos]);
Update_Source(sources[pos]);
SB_Sources_Dump();
return pos;
}
void SB_Source_Remove(int i)
{
source_data *s;
if (i < 0 || i >= MAX_SOURCES) {
return;
}
s = sources[i];
if (s->type == type_dummy)
return;
Q_free(sources[i]);
// remove from SB
if (i < sourcesn - 1)
{
memmove(sources+i,
sources+i + 1,
(sourcesn-i-1)*sizeof(*sources));
}
sourcesn--;
SB_Sources_Dump();
}
void Reload_Sources(void)
{
int i;
vfsfile_t *f;
char ln[2048];
source_data *s;
SB_ServerList_Lock();
for (i=0; i < sourcesn; i++)
Delete_Source(sources[i]);
sourcesn = 0;
// create dummy unbound source
sources[0] = Create_Source();
sources[0]->type = type_dummy;
strlcpy (sources[0]->name, "Unbound", sizeof (sources[0]->name));
sources[0]->servers = (server_data **) Q_malloc(MAX_UNBOUND*sizeof(server_data *));
sources[0]->serversn = 0;
sources[0]->servers_allocated = MAX_UNBOUND;
sourcesn = 1;
f = FS_OpenVFS(SOURCES_LIST_FILENAME, "rb", FS_ANY);
if (!f)
{
//Com_Printf ("sources file not found: %s\n", SOURCES_PATH);
SB_ServerList_Unlock();
return;
}
s = Create_Source();
while (VFS_GETS(f, ln, sizeof(ln)))
{
char line[2048];
char *p, *q;
if (sscanf(ln, "%[ -~ ]s", line) != 1) {
continue;
}
p = next_nonspace(line);
if (*p == '/')
continue; // comment
q = next_space(p);
if (!strncmp(p, "master", q-p)) {
s->type = type_master;
}
else if (!strncmp(p, "file", q-p)) {
s->type = type_file;
}
else if (!strncmp(p, "url", q-p)) {
s->type = type_url;
}
else {
continue;
}
p = next_nonspace(q);
q = (*p == '\"') ? next_quote(++p) : next_space(p);
if (q-p <= 0)
continue;
strlcpy (s->name, p, min(q-p+1, MAX_SOURCE_NAME+1));
p = next_nonspace(q+1);
q = next_space(p);
*q = 0;
if (q-p <= 0)
continue;
if (s->type == type_file)
strlcpy (s->address.filename, p, sizeof (s->address.filename));
else if (s->type == type_url)
strlcpy (s->address.url, p, sizeof (s->address.url));
else
if (!NET_StringToAdr(p, &(s->address.address)))
continue;
sources[sourcesn] = Create_Source();
i = sources[sourcesn]->unique;
memcpy(sources[sourcesn], s, sizeof(source_data));
sources[sourcesn]->unique = i;
sourcesn++;
}
Delete_Source(s);
VFS_CLOSE(f);
//Com_Printf("Read %d sources for Server Browser\n", sourcesn);
// update all file sources
for (i=0; i < sourcesn; i++)
if (sources[i]->type == type_file)
Update_Source(sources[i]);
else if (sources[i]->type == type_master || sources[i]->type == type_url)
Precache_Source(sources[i]);
rebuild_servers_list = 1;
resort_sources = 1;
SB_ServerList_Unlock();
}
int rebuild_servers_list = 0;
void Rebuild_Servers_List(void)
{
int i;
int suppressed_servers = 0;
int server_limit = sizeof(servers) / sizeof(servers[0]);
serversn = 0;
rebuild_servers_list = 0;
SB_ServerList_Lock();
for (i=0; i < sourcesn; i++)
{
if (sources[i]->checked)
{