-
Notifications
You must be signed in to change notification settings - Fork 37
/
mod_mirrorbrain.c
3900 lines (3384 loc) · 154 KB
/
mod_mirrorbrain.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 2007,2008,2009 Peter Poeml / Novell Inc.
* Copyright 2007,2008,2009,2010,2011,2012,2013,2014 Peter Poeml
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* mod_mirrorbrain is the heart of MirrorBrain, which
* - redirects clients to mirror servers, based on an SQL database
* - generates metalinks in real-time
* - generates per-file HTML mirror lists
* - acts as a server for verification hashes
* See http://mirrorbrain.org/ for more information.
*
* Credits:
*
* This module was inspired by mod_offload, written by
* Ryan C. Gordon <icculus@icculus.org>.
*
* It uses code from mod_authn_dbd, mod_authnz_ldap, mod_status,
* apr_memcache, ssl_scache_memcache.c */
/* Copyright notice for the hex_to_bin() function (hex_decode())
*
* Copyright (c) 2001-2009, PostgreSQL Global Development Group
*
* PostgreSQL Database Management System
* (formerly known as Postgres, then as Postgres95)
*
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
*
* Portions Copyright (c) 1994, The Regents of the University of California
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without a written agreement
* is hereby granted, provided that the above copyright notice and this
* paragraph and the following two paragraphs appear in all copies.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */
#include "ap_config.h"
#include "httpd.h"
#include "http_request.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "util_md5.h"
#include "apr_version.h"
#include "apu_version.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_fnmatch.h"
#include "apr_hash.h"
#include "apr_base64.h"
#include "apr_dbd.h"
#include "mod_dbd.h"
#include <unistd.h> /* for getpid */
#include <math.h> /* for sqrt() and pow() */
#include <arpa/inet.h>
#ifdef WITH_MEMCACHE
#include "mod_memcache.h"
#include "apr_memcache.h"
#endif
#include "ap_mpm.h" /* for ap_mpm_query */
#include "mod_status.h"
#include "mod_form.h"
#define wild_match(p,s) (apr_fnmatch(p,s,APR_FNM_CASE_BLIND) == APR_SUCCESS)
/* from ssl/ssl_engine_config.c */
#define cfgMerge(el,unset) mrg->el = (add->el == (unset)) ? base->el : add->el
#define cfgMergeArray(el) mrg->el = apr_array_append(p, add->el, base->el)
#define cfgMergeString(el) cfgMerge(el, NULL)
#define cfgMergeBool(el) cfgMerge(el, UNSET)
#define cfgMergeInt(el) cfgMerge(el, UNSET)
#ifndef UNSET
#define UNSET (-1)
#endif
/* available since APR 1.3 */
#ifndef APR_ARRAY_IDX
#define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
#endif
#ifndef APR_ARRAY_PUSH
#define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary)))
#endif
#define MOD_MIRRORBRAIN_VER "2.19.0"
#define VERSION_COMPONENT "mod_mirrorbrain/"MOD_MIRRORBRAIN_VER
/* no space for time zones is provided here */
#define RFC3339_DATE_LEN (21)
#define MD5_DIGESTSIZE 16
#define SHA1_DIGESTSIZE 20
#define SHA256_DIGESTSIZE 32
#ifdef WITH_MEMCACHE
#define DEFAULT_MEMCACHED_LIFETIME 600
#endif
#define DEFAULT_MIN_MIRROR_SIZE 4096
#if (APR_MAJOR_VERSION == 1 && APR_MINOR_VERSION == 2)
#define DBD_LLD_FMT "d"
#else
#define DBD_LLD_FMT "lld"
#endif
#define DEFAULT_QUERY "SELECT id, identifier, region, country, " \
"lat, lng, " \
"asn, prefix, score, baseurl, " \
"region_only, country_only, " \
"as_only, prefix_only, " \
"other_countries, file_maxsize " \
"FROM server " \
"WHERE id::smallint = any(" \
"(SELECT mirrors " \
"FROM filearr " \
"WHERE path = %s)::smallint[]) " \
"AND enabled AND status_baseurl AND score > 0"
#define DEFAULT_QUERY_HASH "SELECT file_id, md5hex, sha1hex, sha256hex, " \
"sha1piecesize, sha1pieceshex, btihhex, pgp, " \
"zblocksize, zhashlens, zsumshex " \
"FROM hexhash " \
"WHERE file_id = (SELECT id " \
"FROM filearr " \
"WHERE path = %s) " \
"AND size = %" DBD_LLD_FMT " " \
"AND mtime = %" DBD_LLD_FMT " " \
"LIMIT 1"
/* the smaller, the smaller the effect of a raised prio in comparison to distance */
/* 5000000 -> mirror in 200km distance is preferred already when it has prio 100
* 1000000 -> mirror in 200km distance is preferred not before it has prio 300
* (compared to 100 as normal priority for other mirrors, and tested in
* Germany, which is a small country with many mirrors) */
#define DISTANCE_PRIO 2000000
module AP_MODULE_DECLARE_DATA mirrorbrain_module;
/* (meta) representations of a requested file */
enum { REDIRECT, META4, METALINK, MIRRORLIST, TORRENT,
ZSYNC, MAGNET, MD5, SHA1, SHA256, BTIH, YUMLIST, UNKNOWN };
static struct {
int id;
char *ext;
} reps [] = {
{ REDIRECT, "" },
{ META4, "meta4" },
{ METALINK, "metalink" },
{ MIRRORLIST, "mirrorlist" },
{ TORRENT, "torrent" },
{ ZSYNC, "zsync" },
{ MAGNET, "magnet" },
{ MD5, "md5" },
{ SHA1, "sha1" },
{ SHA256, "sha256" },
{ BTIH, "btih" },
{ YUMLIST, "yumlist" },
{ UNKNOWN, NULL }
};
/* A structure that represents a mirror */
typedef struct mirror_entry mirror_entry_t;
/* a mirror */
struct mirror_entry {
int id;
const char *identifier;
const char *region; /* 2-letter-string */
const char *country_code; /* 2-letter-string */
float lat; /* geographical latitude */
float lng; /* geographical longitude */
int dist; /* geographical distance to client */
const char *as; /* autonomous system number as string */
const char *prefix; /* network prefix xxx.xxx.xxx.xxx/yy */
apr_ipsubnet_t *ipsub; /* ip-subnet representation of the network prefix */
unsigned char region_only;
unsigned char country_only;
unsigned char as_only;
unsigned char prefix_only;
int score;
const char *baseurl;
apr_off_t file_maxsize;
char *other_countries; /* comma-separated 2-letter strings */
int rank;
int *nsame; /* to be able to access the number of elements in
the array from qsort() */
};
/* verification hashes of a file */
typedef struct hashbag hashbag_t;
struct hashbag {
apr_off_t id;
const char *md5hex;
const char *sha1hex;
const char *sha256hex;
int sha1piecesize;
apr_array_header_t *sha1pieceshex;
const char *btihhex;
const char *pgp;
int zblocksize;
const char *zhashlens;
const char *zsumshex;
};
/* per-dir configuration */
typedef struct
{
int engine_on;
int debug;
apr_off_t min_size;
int handle_headrequest_locally;
const char *mirror_base;
apr_array_header_t *fallbacks;
apr_array_header_t *exclude_mime;
apr_array_header_t *exclude_agents;
apr_array_header_t *exclude_networks;
apr_array_header_t *exclude_ips;
ap_regex_t *exclude_filemask;
ap_regex_t *metalink_torrentadd_mask;
const char *stampkey;
} mb_dir_conf;
/* per-server configuration */
typedef struct
{
#ifdef WITH_MEMCACHE
const char *instance;
int memcached_on;
int memcached_lifetime;
#endif
const char *metalink_publisher_name;
const char *metalink_publisher_url;
apr_array_header_t *tracker_urls;
apr_array_header_t *dhtnodes;
const char *metalink_broken_test_mirrors;
int metalink_magnets;
apr_array_header_t *yumdirs;
const char *mirrorlist_stylesheet;
const char *mirrorlist_header;
const char *mirrorlist_footer;
int only_hash;
const char *query;
const char *query_label;
const char *query_hash;
const char *query_hash_label;
} mb_server_conf;
typedef struct dhtnode dhtnode_t;
struct dhtnode {
char *name;
int port;
};
typedef struct yumdir yumdir_t;
struct yumdir {
char *dir; /* base dir */
char *file; /* marker file within base dir */
apr_array_header_t *args; /* query arguments */
};
typedef struct yumarg yumarg_t;
struct yumarg {
char *key;
ap_regex_t *regexp;
};
static ap_dbd_t *(*mb_dbd_acquire_fn)(request_rec*) = NULL;
static void (*mb_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
static apr_version_t vsn;
static int dbd_first_row;
static void debugLog(const request_rec *r, const mb_dir_conf *cfg,
const char *fmt, ...)
{
if (cfg->debug == 1) {
char buf[512];
va_list ap;
va_start(ap, fmt);
apr_vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
/* we use warn loglevel to be able to debug without
* setting the entire server into debug logging mode */
ap_log_rerror(APLOG_MARK,
APLOG_WARNING,
APR_SUCCESS,
r, "[mod_mirrorbrain] %s", buf);
}
}
static apr_status_t mb_cleanup()
{
return APR_SUCCESS;
}
static void mb_child_init(apr_pool_t *p, server_rec *s)
{
apr_pool_cleanup_register(p, NULL, mb_cleanup, mb_cleanup);
srand((unsigned int)getpid());
}
static int mb_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
apr_version(&vsn);
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
"[mod_mirrorbrain] compiled with APR/APR-Util %s/%s",
APR_VERSION_STRING, APU_VERSION_STRING);
if ((vsn.major == 1) && (vsn.minor == 2)) {
/* database access semantics were changed between 1.2 and 1.3 (strictly
* speaking, breaking the binary compatibility */
dbd_first_row = 0;
} else {
dbd_first_row = 1;
}
/* be visible in the server signature */
ap_add_version_component(pconf, VERSION_COMPONENT);
/* make sure that mod_form is loaded */
if (ap_find_linked_module("mod_form.c") == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"[mod_mirrorbrain] Module mod_form missing. It must be "
"loaded in order for mod_mirrorbrain to function properly");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* make sure that mod_dbd is loaded */
if (mb_dbd_prepare_fn == NULL) {
mb_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
if (mb_dbd_prepare_fn == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
"[mod_mirrorbrain] You must load mod_dbd to enable MirrorBrain functions");
return HTTP_INTERNAL_SERVER_ERROR;
}
mb_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
}
/* prepare DBD SQL statements */
static unsigned int label_num = 0;
server_rec *sp;
for (sp = s; sp; sp = sp->next) {
mb_server_conf *cfg = ap_get_module_config(sp->module_config,
&mirrorbrain_module);
/* make a label */
cfg->query_label = apr_psprintf(pconf, "mirrorbrain_dbd_%d", ++label_num);
cfg->query_hash_label = apr_psprintf(pconf, "mirrorbrain_dbd_hash_%d", ++label_num);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
"[mod_mirrorbrain] preparing stmt for server %s, label_num %d, label %s",
s->server_hostname, label_num, cfg->query_label);
mb_dbd_prepare_fn(sp, cfg->query, cfg->query_label);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
"[mod_mirrorbrain] preparing stmt for server %s, label_num %d, label %s",
s->server_hostname, label_num, cfg->query_hash_label);
mb_dbd_prepare_fn(sp, cfg->query_hash, cfg->query_hash_label);
}
return OK;
}
static void *create_mb_dir_config(apr_pool_t *p, char *dirspec)
{
mb_dir_conf *new =
(mb_dir_conf *) apr_pcalloc(p, sizeof(mb_dir_conf));
new->engine_on = UNSET;
new->debug = UNSET;
new->min_size = DEFAULT_MIN_MIRROR_SIZE;
new->handle_headrequest_locally = 0;
new->mirror_base = NULL;
new->fallbacks = apr_array_make(p, 10, sizeof (mirror_entry_t));
new->exclude_mime = apr_array_make(p, 0, sizeof (char *));
new->exclude_agents = apr_array_make(p, 0, sizeof (char *));
new->exclude_networks = apr_array_make(p, 4, sizeof (char *));
new->exclude_ips = apr_array_make(p, 4, sizeof (char *));
new->exclude_filemask = NULL;
new->metalink_torrentadd_mask = NULL;
new->stampkey = NULL;
return (void *) new;
}
static void *merge_mb_dir_config(apr_pool_t *p, void *basev, void *addv)
{
mb_dir_conf *mrg = (mb_dir_conf *) apr_pcalloc(p, sizeof(mb_dir_conf));
mb_dir_conf *base = (mb_dir_conf *) basev;
mb_dir_conf *add = (mb_dir_conf *) addv;
/* debugLog("merge_mb_dir_config: new=%08lx base=%08lx overrides=%08lx",
* (long)mrg, (long)base, (long)add); */
cfgMergeInt(engine_on);
cfgMergeInt(debug);
mrg->min_size = (add->min_size != DEFAULT_MIN_MIRROR_SIZE) ? add->min_size : base->min_size;
cfgMergeInt(handle_headrequest_locally);
cfgMergeString(mirror_base);
/* inheriting makes sense. But does inheriting also make sense if an
* inheriting directory has its own fallback mirror directives? */
/* mrg->fallbacks = apr_is_empty_array(add->fallbacks) ? base->fallbacks : add->fallbacks; */
/* it's a merge for now */
mrg->fallbacks = apr_array_append(p, base->fallbacks, add->fallbacks);
mrg->exclude_mime = apr_array_append(p, base->exclude_mime, add->exclude_mime);
mrg->exclude_agents = apr_array_append(p, base->exclude_agents, add->exclude_agents);
mrg->exclude_networks = apr_array_append(p, base->exclude_networks, add->exclude_networks);
mrg->exclude_ips = apr_array_append(p, base->exclude_ips, add->exclude_ips);
mrg->exclude_filemask = (add->exclude_filemask == NULL) ? base->exclude_filemask : add->exclude_filemask;
mrg->metalink_torrentadd_mask = (add->metalink_torrentadd_mask == NULL) ? base->metalink_torrentadd_mask : add->metalink_torrentadd_mask;
cfgMergeString(stampkey);
return (void *) mrg;
}
static void *create_mb_server_config(apr_pool_t *p, server_rec *s)
{
mb_server_conf *new =
(mb_server_conf *) apr_pcalloc(p, sizeof(mb_server_conf));
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
"[mod_mirrorbrain] creating server config");
#ifdef WITH_MEMCACHE
new->instance = "default";
new->memcached_on = UNSET;
new->memcached_lifetime = UNSET;
#endif
new->metalink_publisher_name = NULL;
new->metalink_publisher_url = NULL;
new->tracker_urls = apr_array_make(p, 5, sizeof (char *));
new->dhtnodes = apr_array_make(p, 5, sizeof (dhtnode_t));
new->metalink_broken_test_mirrors = NULL;
new->metalink_magnets = UNSET;
new->yumdirs = apr_array_make(p, 10, sizeof (yumdir_t));
new->mirrorlist_stylesheet = NULL;
new->mirrorlist_header = NULL;
new->mirrorlist_footer = NULL;
new->only_hash = UNSET;
new->query = DEFAULT_QUERY;
new->query_label = NULL;
new->query_hash = DEFAULT_QUERY_HASH;
new->query_hash_label = NULL;
return (void *) new;
}
static void *merge_mb_server_config(apr_pool_t *p, void *basev, void *addv)
{
mb_server_conf *base = (mb_server_conf *) basev;
mb_server_conf *add = (mb_server_conf *) addv;
mb_server_conf *mrg = apr_pcalloc(p, sizeof(mb_server_conf));
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
"[mod_mirrorbrain] merging server config");
#ifdef WITH_MEMCACHE
cfgMergeString(instance);
cfgMergeBool(memcached_on);
cfgMergeInt(memcached_lifetime);
#endif
cfgMergeString(metalink_publisher_name);
cfgMergeString(metalink_publisher_url);
mrg->tracker_urls = apr_array_append(p, base->tracker_urls, add->tracker_urls);
mrg->dhtnodes = apr_array_append(p, base->dhtnodes, add->dhtnodes);
cfgMergeString(metalink_broken_test_mirrors);
cfgMergeBool(metalink_magnets);
mrg->yumdirs = apr_array_append(p, base->yumdirs, add->yumdirs);
cfgMergeString(mirrorlist_stylesheet);
cfgMergeString(mirrorlist_header);
cfgMergeString(mirrorlist_footer);
cfgMergeBool(only_hash);
mrg->query = (add->query != (char *) DEFAULT_QUERY) ? add->query : base->query;
cfgMergeString(query_label);
mrg->query_hash = (add->query_hash != (char *) DEFAULT_QUERY_HASH)
? add->query_hash : base->query_hash;
cfgMergeString(query_hash_label);
return (void *) mrg;
}
static const char *mb_cmd_engine(cmd_parms *cmd, void *config, int flag)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->engine_on = flag;
cfg->mirror_base = apr_pstrdup(cmd->pool, cmd->path);
return NULL;
}
static const char *mb_cmd_debug(cmd_parms *cmd, void *config, int flag)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->debug = flag;
return NULL;
}
static const char *mb_cmd_minsize(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->min_size = apr_atoi64(arg1);
if (cfg->min_size < 0)
return "MirrorBrainMinSize requires a non-negative integer.";
return NULL;
}
static const char *mb_cmd_fallback(cmd_parms *cmd, void *config,
const char *arg1, const char *arg2,
const char *arg3)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
mirror_entry_t *new;
apr_uri_t uri;
if (APR_SUCCESS != apr_uri_parse(cmd->pool, arg3, &uri)) {
return "MirrorBrainFallback URI cannot be parsed";
}
new = apr_array_push(cfg->fallbacks);
new->nsame = &cfg->fallbacks->nelts;
new->id = 0;
new->identifier = uri.hostname;
new->region = apr_pstrdup(cmd->pool, arg1);
new->country_code = apr_pstrdup(cmd->pool, arg2);
new->lat = 0;
new->lng = 0;
new->dist = 0;
new->other_countries = NULL;
new->as = NULL;
new->prefix = NULL;
new->ipsub = NULL;
new->region_only = 0;
new->country_only = 0;
new->as_only = 0;
new->prefix_only = 0;
new->score = 1; /* give it a minimal score (but with 0, it wouldn't be considered) */
new->file_maxsize = 0;
if (arg3[strlen(arg3) - 1] == '/') {
new->baseurl = apr_pstrdup(cmd->pool, arg3);
} else {
new->baseurl = apr_pstrcat(cmd->pool, arg3, "/", NULL);
}
ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL,
"[mod_mirrorbrain] configured fallback mirror (%s:%s): %s",
new->region, new->country_code, new->baseurl);
return NULL;
}
static const char *mb_cmd_excludemime(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
char **mimepattern = (char **) apr_array_push(cfg->exclude_mime);
*mimepattern = apr_pstrdup(cmd->pool, arg1);
return NULL;
}
static const char *mb_cmd_excludeagent(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
char **agentpattern = (char **) apr_array_push(cfg->exclude_agents);
*agentpattern = apr_pstrdup(cmd->pool, arg1);
return NULL;
}
static const char *mb_cmd_excludenetwork(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
char **network = (char **) apr_array_push(cfg->exclude_networks);
*network = apr_pstrdup(cmd->pool, arg1);
return NULL;
}
static const char *mb_cmd_excludeip(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
char **ip = (char **) apr_array_push(cfg->exclude_ips);
*ip = apr_pstrdup(cmd->pool, arg1);
return NULL;
}
static const char *mb_cmd_exclude_filemask(cmd_parms *cmd, void *config,
const char *arg)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->exclude_filemask = ap_pregcomp(cmd->pool, arg, AP_REG_EXTENDED);
if (cfg->exclude_filemask == NULL) {
return "MirrorBrainExcludeFileMask regex could not be compiled";
}
return NULL;
}
static const char *mb_cmd_handle_headrequest_locally(cmd_parms *cmd,
void *config, int flag)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->handle_headrequest_locally = flag;
return NULL;
}
#ifdef WITH_MEMCACHE
static const char *mb_cmd_instance(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->instance = arg1;
return NULL;
}
#endif
static const char *mb_cmd_dbd_query(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->query = arg1;
return NULL;
}
static const char *mb_cmd_dbd_query_hash(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->query_hash = arg1;
return NULL;
}
static const char *mb_cmd_metalink_hashes_prefix(cmd_parms *cmd,
void *config,
const char *arg1)
{
return "mod_mirrorbrain: the MirrorBrainMetalinkHashesPathPrefix "
"directive is obsolete. It has no effect. Simply remove it.";
}
static const char *mb_cmd_metalink_publisher(cmd_parms *cmd, void *config,
const char *arg1,
const char *arg2)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->metalink_publisher_name = arg1;
cfg->metalink_publisher_url = arg2;
return NULL;
}
static const char *mb_cmd_mirrorlist_header(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->mirrorlist_header = arg1;
return NULL;
}
static const char *mb_cmd_mirrorlist_footer(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->mirrorlist_footer = arg1;
return NULL;
}
static const char *mb_cmd_tracker_url(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
char **url = (char **) apr_array_push(cfg->tracker_urls);
*url = apr_pstrdup(cmd->pool, arg1);
return NULL;
}
static const char *mb_cmd_dht_node(cmd_parms *cmd, void *config,
const char *arg1, const char *arg2)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
dhtnode_t *new = apr_array_push(cfg->dhtnodes);
new->name = apr_pstrdup(cmd->pool, arg1);
new->port = atoi(apr_pstrdup(cmd->pool, arg2));
if (new->port <= 0)
return "MirrorBrainDHTNode requires a positive integer "
"as second argument (server port).";
return NULL;
}
static const char *mb_cmd_hashes_suppress_filenames(cmd_parms *cmd, void *config,
int flag)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->only_hash = flag;
return NULL;
}
static const char *mb_cmd_metalink_broken_test_mirrors(cmd_parms *cmd,
void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->metalink_broken_test_mirrors = arg1;
return NULL;
}
static const char *mb_cmd_metalink_magnet_links(cmd_parms *cmd,
void *config,
int flag)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->metalink_magnets = flag;
return NULL;
}
static const char *mb_cmd_mirrorlist_stylesheet(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->mirrorlist_stylesheet = arg1;
return NULL;
}
static const char *mb_cmd_metalink_torrentadd_mask(cmd_parms *cmd, void *config,
const char *arg)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->metalink_torrentadd_mask = ap_pregcomp(cmd->pool, arg, AP_REG_EXTENDED);
if (cfg->metalink_torrentadd_mask == NULL) {
return "MirrorBrainMetalinkTorrentAddMask regex could not be compiled";
}
return NULL;
}
static const char *mb_cmd_redirect_stamp_key(cmd_parms *cmd, void *config,
const char *arg1)
{
mb_dir_conf *cfg = (mb_dir_conf *) config;
cfg->stampkey = arg1;
return NULL;
}
static const char *mb_cmd_add_yumdir(cmd_parms *cmd, void *dummy,
const char *arg)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
char *d = NULL; /* base dir */
char *f = NULL; /* marker file within base dir */
char *word;
apr_array_header_t *yumargs = apr_array_make(cmd->pool, 3, sizeof (yumarg_t));
while (*arg) {
word = ap_getword_conf(cmd->pool, &arg);
char *val = ap_strchr(word, '=');
if (!val) {
if (!d) {
d = word;
continue;
} else if (!f) {
f = word;
continue;
} else {
return "Invalid MirrorBrainYumDir parameter. Parameter must be "
"in the form 'key=value'.";
}
}
*val++ = '\0';
yumarg_t *a = apr_array_push(yumargs);
a->key = apr_pstrdup(cmd->pool, word);
/* we better anchor the regexp to the start and end, because when user
* data matches the regexp, we will substitute parts of the URL with it */
a->regexp = ap_pregcomp(cmd->pool,
apr_pstrcat(cmd->pool, "^", val, "$", NULL),
AP_REG_EXTENDED);
if (!a->regexp)
return "Regular expression for ProxyRemoteMatch could not be compiled.";
};
if (d == NULL)
return "MirrorBrainYumDir needs a (relative) base path";
if (f == NULL)
return "MirrorBrainYumDir needs a file name relative to the base path";
if (apr_is_empty_array(yumargs))
return "MirrorBrainYumDir needs at least one query argument";
yumdir_t *new = apr_array_push(cfg->yumdirs);
new->dir = apr_pstrdup(cmd->pool, d);
new->file = apr_pstrdup(cmd->pool, f);
new->args = yumargs;
return NULL;
}
#ifdef WITH_MEMCACHE
static const char *mb_cmd_memcached_on(cmd_parms *cmd, void *config,
int flag)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->memcached_on = flag;
return NULL;
}
static const char *mb_cmd_memcached_lifetime(cmd_parms *cmd, void *config,
const char *arg1)
{
server_rec *s = cmd->server;
mb_server_conf *cfg =
ap_get_module_config(s->module_config, &mirrorbrain_module);
cfg->memcached_lifetime = atoi(arg1);
if (cfg->memcached_lifetime <= 0)
return "MirrorBrainMemcachedLifeTime requires an integer > 0.";
return NULL;
}
#endif
static int find_lowest_rank(apr_array_header_t *arr)
{
if (arr->nelts == 1) {
return 0; /* the first and only element */
}
int i;
int lowest_id = 0;
int lowest = INT_MAX;
mirror_entry_t *mirror;
mirror_entry_t **mirrorp;
mirrorp = (mirror_entry_t **)arr->elts;
for (i = 0; i < arr->nelts; i++) {
mirror = mirrorp[i];
if (mirror->rank < lowest) {
lowest = mirror->rank;
lowest_id = i;
}
}
return lowest_id;
}
static int find_closest_dist(apr_array_header_t *arr)
{
if (arr->nelts == 1) {
return 0; /* the first and only element */
}
int i, d;
int closest_id = 0;
int closest = INT_MAX;
int closest_rank = INT_MAX;
mirror_entry_t *mirror;
mirror_entry_t **mirrorp;
int distprio = DISTANCE_PRIO / arr->nelts;
mirrorp = (mirror_entry_t **)arr->elts;
for (i = 0; i < arr->nelts; i++) {
mirror = mirrorp[i];
d = mirror->dist + distprio / mirror->score;
/* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "[find_closest_dist] "
"i: %d, %-30s - dist: %d, score: %d, %d/score: %d, d: %d",
i, mirror->identifier, mirror->dist, mirror->score, distprio,
distprio/mirror->score, d); */
if ( d < closest) {
/* this mirror is closer */
closest = d;
closest_id = i;
closest_rank = mirror->rank;
} else if (d == closest) {
/* this mirror is as close as the closest known mirror. So we pick
* one of them randomly. */
if (mirror->rank < closest_rank) {
closest = d;
closest_id = i;
closest_rank = mirror->rank;
}
}
}
return closest_id;
}
static int cmp_mirror_rank(const void *v1, const void *v2)
{
mirror_entry_t *m1 = *(mirror_entry_t **)v1;
mirror_entry_t *m2 = *(mirror_entry_t **)v2;
return m1->rank - m2->rank;
}
static int cmp_mirror_dist(const void *v1, const void *v2)
{
mirror_entry_t *m1 = *(mirror_entry_t **)v1;
mirror_entry_t *m2 = *(mirror_entry_t **)v2;
int distprio = DISTANCE_PRIO / *m1->nsame;
/* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
* "[cmp_mirror_dist] nsame: %d, distprio: %d", *m1->nsame, distprio); */
return (m1->dist + distprio / m1->score) - (m2->dist + distprio / m2->score);
}
static apr_array_header_t *get_n_best_mirrors(request_rec *r, int n,
apr_array_header_t *a1, apr_array_header_t *a2,
apr_array_header_t *a3, apr_array_header_t *a4,
apr_array_header_t *a5)
{
int i;
int found = 0;
apr_array_header_t *mirrors_n_best;
mirror_entry_t **mirrorp;
mirrors_n_best = apr_array_make(r->pool, n, sizeof (mirror_entry_t *));
mirrorp = (mirror_entry_t **)a1->elts;
for (i = 0; found < n && i < a1->nelts; i++, found++) {
*(void **)apr_array_push(mirrors_n_best) = mirrorp[i];
}
mirrorp = (mirror_entry_t **)a2->elts;
for (i = 0; found < n && i < a2->nelts; i++, found++) {
*(void **)apr_array_push(mirrors_n_best) = mirrorp[i];
}
mirrorp = (mirror_entry_t **)a3->elts;
for (i = 0; found < n && i < a3->nelts; i++, found++) {
*(void **)apr_array_push(mirrors_n_best) = mirrorp[i];
}
mirrorp = (mirror_entry_t **)a4->elts;