-
Notifications
You must be signed in to change notification settings - Fork 0
/
hp_redirect.c
782 lines (646 loc) · 23.3 KB
/
hp_redirect.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
/* houseportal - A simple web portal for home servers
*
* Copyright 2019, Pascal Martin
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* hp_redirect.c - The houseportal's HTTP request redirector.
*
* SYNOPSYS:
*
* void hp_redirect_start (int argc, const char **argv);
*
* Initialize the HTTP request redirector.
*
* void hp_redirect_background (void);
*
* This function should be called periodically. It checks for
* and applies configuration changes, prune obsolete items, etc.
*
* void hp_redirect_list_json (int services, char *buffer, int size);
*
* This function populates the buffer with a JSON string that represents
* the current redirect database. If services is not 0, only entries
* mapping to a service are listed.
*
* void hp_redirect_peers_json (char *buffer, int size);
*
* This function populates the buffer with a JSON string that represents
* the active peers.
*
* void hp_redirect_service_json (const char *service, char *buffer, int size);
*
* This function populates the buffer with a JSON string that represents
* the active targets for the specified service. If service is null, all
* services are reported.
*/
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include "houseportal.h"
#include "houselog.h"
#include "houseportalhmac.h"
static const char *ConfigurationPath = "/etc/house/portal.config";
static time_t ConfigurationTime = 0;
static int RestrictUdp2Local = 0;
static const char *PortalPort = "70";
#define MAX_UDP_POINTS 4
static int PortalUdpPoints[MAX_UDP_POINTS];
static int PortalUdpPointsCount = 0;
typedef struct {
char *path;
char *service;
char *target;
int length;
int hide;
time_t expiration;
} HttpRedirection;
#define REDIRECT_MAX 128
#define REDIRECT_LIFETIME 600
static int RedirectionCount = 0;
static HttpRedirection Redirections[REDIRECT_MAX];
typedef struct {
char *name;
time_t expiration;
} PortalPeers;
static int PeerCount = 0;
static PortalPeers Peers[REDIRECT_MAX];
// Cryptographic keys.
//
typedef struct {
char *method;
char *value;
} HttpRequest;
static HttpRequest IntermediateDecode[128]; // Don't make the name obvious.
static int IntermediateDecodeLength = 0;
static const char *HostName = 0;
static const HttpRedirection *SearchBestRedirect (const char *path) {
int i;
int quality = 0;
time_t now = time(0);
HttpRedirection *found = 0;
if (path[0] == 0 || path[1] == 0) return 0;
for (i = 0; i < RedirectionCount; ++i) {
int length = Redirections[i].length;
time_t expiration = Redirections[i].expiration;
if (length <= quality) continue;
if (expiration < now && expiration > 0) continue;
if (strncmp (Redirections[i].path, path, length)) continue;
if (path[length] != 0 && path[length] != '/') continue;
found = Redirections + i;
quality = length;
}
return found;
}
static void DeprecatePermanentConfiguration (void) {
int i;
for (i = 0; i < RedirectionCount; ++i) {
if (Redirections[i].expiration == 0) Redirections[i].expiration = 1;
}
for (i = 0; i < IntermediateDecodeLength; ++i) {
if (IntermediateDecode[i].method) {
free(IntermediateDecode[i].method);
IntermediateDecode[i].method = 0;
}
if (IntermediateDecode[i].value) {
free(IntermediateDecode[i].value);
IntermediateDecode[i].value = 0;
}
}
IntermediateDecodeLength = 0;
RestrictUdp2Local = 0;
}
static void PruneRedirect (time_t now) {
int i;
int pruned = 0;
for (i = RedirectionCount-1; i >= 0; --i) {
time_t expiration = Redirections[i].expiration;
if (expiration == 0 || now < expiration) continue; // Not expired.
houselog_event ("ROUTE", Redirections[i].path, "REMOVED",
"%s", Redirections[i].target);
// Do not free path: the echhtp route still uses it.
free (Redirections[i].target);
if (Redirections[i].service) free (Redirections[i].service);
if (i < RedirectionCount-1) {
Redirections[i] = Redirections[RedirectionCount-1];
RedirectionCount -= 1;
} else {
RedirectionCount = i;
}
pruned = 1;
}
if (!pruned) return;
DEBUG {
printf ("After pruning:\n");
for (i = 0; i < RedirectionCount; ++i) {
printf ("REDIRECT %ld%s %s -> %s\n",
Redirections[i].expiration,
Redirections[i].hide?" HIDE":"",
Redirections[i].path,
Redirections[i].target);
}
}
}
static const char *RedirectRoute (const char *method, const char *uri,
const char *data, int length) {
const HttpRedirection *r = SearchBestRedirect (uri);
if (r) {
static char url[2048]; // Accessed once after return.
char parameters[1024];
if (r->hide) {
uri += r->length;
if (uri[0] == 0) uri = "/";
}
echttp_parameter_join (parameters, sizeof(parameters));
if (parameters[0])
snprintf (url, sizeof(url), "http://%s%s?%s",
r->target, uri, parameters);
else
snprintf (url, sizeof(url), "http://%s%s", r->target, uri);
if (r->expiration) {
echttp_redirect (url);
} else {
echttp_permanent_redirect (url);
}
} else {
echttp_error (500, "Unresolvable redirection.");
}
return "";
}
static void AddSingleRedirect (int live, int hide,
const char *target,
const char *service, const char *path) {
int i;
char buffer[1024];
time_t expiration = (live)?time(0)+REDIRECT_LIFETIME:0;
if (!strchr(target, ':')) {
snprintf (buffer, sizeof(buffer), "%s:%s", HostName, target);
target = buffer;
}
// First search if this is just a renewal.
// It is OK to renew an obsolete entry. A renewal may change everything
// but the path: target, permanent/live, hide option, etc..
//
for (i = 0; i < RedirectionCount; ++i) {
if (strcmp (Redirections[i].path, path) == 0) {
if (live && Redirections[i].expiration == 0) return; // Permanent..
if (strcmp (Redirections[i].target, target)) {
houselog_event ("ROUTE", path, "REPLACED", "%s WITH %s",
Redirections[i].target, target);
free (Redirections[i].target);
Redirections[i].target = strdup(target);
}
if (service) {
if (!Redirections[i].service) {
houselog_event ("ROUTE", path, "UPDATED",
"NOW SERVICE %s", service);
Redirections[i].service = strdup(service);
} else if (strcmp (Redirections[i].service, service)) {
houselog_event ("ROUTE", path, "UPDATED",
"SERVICE CHANGED FROM %s TO %s",
Redirections[i].service, service);
free (Redirections[i].service);
Redirections[i].service = strdup(service);
}
} else if (Redirections[i].service) {
houselog_event ("ROUTE", path, "UPDATED", "NOT A SERVICE");
free (Redirections[i].service);
Redirections[i].service = 0;
}
Redirections[i].hide = hide;
Redirections[i].expiration = expiration;
return;
}
}
// Not a renewal: use a new slot.
// Do not reuse an expired slot: echttp does not handle dynamic changes
// to HTTP routes at this time.
//
if (RedirectionCount < REDIRECT_MAX) {
char *p = strdup(path);
houselog_trace (HOUSE_INFO, p,
"add %s route %s to %s%s",
live?"live":"permanent",p,target,hide?" (hide)":"");
houselog_event ("ROUTE", p, "ADD",
"%s (%s)", target, live?"live":"permanent");
echttp_route_match (p, RedirectRoute);
Redirections[RedirectionCount].path = p;
Redirections[RedirectionCount].target = strdup(target);
if (service)
Redirections[RedirectionCount].service = strdup(service);
else
Redirections[RedirectionCount].service = 0;
Redirections[RedirectionCount].length = strlen(path);
Redirections[RedirectionCount].hide = hide;
Redirections[RedirectionCount].expiration = expiration;
RedirectionCount += 1;
}
}
static void AddRedirect (int live, char **token, int count) {
int i = 1;
int hide = 0;
const char *target = token[0];
if (strcmp ("HIDE", token[1]) == 0) {
hide = 1;
i = 2;
}
for (; i < count; ++i) {
char *service = 0;
char *path = token[i];
char *s = strchr (path, ':');
if (s) {
*s = 0;
service = path;
path = s+1;
}
AddSingleRedirect (live, hide, target, service, path);
}
}
static void AddOnePeer (const char *name, time_t expiration) {
int i;
for (i = 0; i < PeerCount; ++i) {
if (!strcmp(Peers[i].name, name)) {
if (Peers[i].expiration > 0 &&
Peers[i].expiration < expiration) { // No downgrade.
Peers[i].expiration = expiration;
DEBUG printf ("Peer %s updated to %ld\n", name, expiration);
}
return;
}
}
// This is a new peer: add to the list.
if (PeerCount < REDIRECT_MAX) {
Peers[PeerCount].name = strdup(name);
Peers[PeerCount].expiration = expiration;
PeerCount += 1;
}
}
static void AddPeers (int live, char **token, int count) {
int i;
time_t default_expiration = (live)?time(0)+REDIRECT_LIFETIME:0;
if (!strcmp(HostName, token[0])) return; // Got our own packet.
for (i = 0; i < count; ++i) {
time_t expiration = default_expiration;
if (live) {
// Extract the sender's expiration time.
char *s = strchr (token[i], '=');
if (s) {
expiration = atol(s+1);
*s = 0;
}
}
AddOnePeer (token[i], expiration);
}
}
static void DecodeMessage (char *buffer, int live) {
int i, start, count;
char *token[16];
// Split the line
for (i = start = count = 0; buffer[i] >= ' '; ++i) {
if (buffer[i] == ' ') {
if (count >= 14) {
houselog_trace (HOUSE_WARNING, "HousePortal",
"Too many tokens at %s", buffer+i);
if (!live) exit(1);
return;
}
token[count++] = buffer + start;
do {
buffer[i] = 0;
} while (buffer[++i] == ' ');
start = i;
}
}
buffer[i] = 0;
token[count++] = buffer + start;
if (strcmp("REDIRECT", token[0]) == 0) {
if (live) count -= 1; // Do not count the timestamp.
if (count < 3) {
houselog_trace (HOUSE_WARNING, "HousePortal",
"Incomplete redirect (%d arguments)", count);
if (!live) exit(1);
return;
}
AddRedirect (live, token+live+1, count-1); // Remove the keyword.
} else if (strcmp("PEER", token[0]) == 0) {
if (live) count -= 1; // Do not count the timestamp.
if (count < 2) {
houselog_trace (HOUSE_WARNING, "HousePortal",
"Incomplete peer (%d argument)", count);
if (!live) exit(1);
return;
}
AddPeers (live, token+live+1, count-1); // remove the keyword
} else if (live) {
return; // Ignore other messages below.
} else if (strcmp("LOCAL", token[0]) == 0) {
houselog_trace (HOUSE_INFO, "HousePortal", "LOCAL mode");
houselog_event ("SYSTEM", "HousePortal", "SET", "LOCAL MODE");
RestrictUdp2Local = 1;
} else if (strcmp("SIGN", token[0]) == 0) {
if (count == 3 && IntermediateDecodeLength < 128) {
int index = IntermediateDecodeLength++;
IntermediateDecode[index].method = strdup(token[1]);
IntermediateDecode[index].value = strdup(token[2]);
DEBUG printf ("%s signature key\n", token[1]);
houselog_event ("SYSTEM", "HousePortal", "SET", "SIGNATURE");
}
} else {
houselog_trace (HOUSE_WARNING, "HousePortal",
"Invalid keyword %s", token[0]);
if (!live) exit(1);
}
}
static void LoadConfig (const char *name) {
char buffer[1024];
char *token[16];
struct stat fileinfo;
if (stat (name, &fileinfo) == 0) {
ConfigurationTime = fileinfo.st_mtim.tv_sec;
}
FILE *f = fopen (name, "r");
if (f == 0) {
houselog_trace (HOUSE_FAILURE, "HousePortal",
"Cannot access configuration file %s", name);
exit(0);
}
while (!feof(f)) {
buffer[0] = 0;
fgets (buffer, sizeof(buffer), f);
if (buffer[0] != '#' && buffer[0] > ' ') {
DecodeMessage (buffer, 0);
}
}
fclose(f);
if (IntermediateDecodeLength)
houselog_trace (HOUSE_INFO,
"HousePortal", "Registrations must be signed");
}
static int hp_redirect_inspect2 (const char *data,
const char *method, const char *value) {
int i;
const char *signature;
for (i = 0; i < IntermediateDecodeLength; ++i) {
if (strcmp(IntermediateDecode[i].method, method)) continue;
signature = houseportalhmac (IntermediateDecode[i].method,
IntermediateDecode[i].value, data);
if (strcmp(signature, value) == 0) return 1; // Passed.
DEBUG printf ("Signature %s did not match client signature %s\n",
signature, value);
}
houselog_trace (HOUSE_WARNING,
"HousePortal", "No signature match for %s", data);
return 0; // Failed all available keys.
}
static int hp_redirect_inspect (char *data, int length) {
char *cypher = 0;
char *crypto = 0;
const char *sha256mark = " SHA-256 ";
data[length] = 0;
cypher = strstr (data, sha256mark);
if (cypher) {
crypto = cypher + strlen(sha256mark);
*(cypher++) = 0; // remove signature from registration data.
}
// All future cyphers names checked here..
if (IntermediateDecodeLength <= 0) return 1; // No key. Accept all.
if (crypto) {
crypto[-1] = 0; // Split cypher from signature.
return hp_redirect_inspect2 (data, cypher, crypto);
}
return 0; // Not signed, but signature was required.
}
static void hp_redirect_udp (int fd, int mode) {
int length;
char buffer[1024];
length = hp_udp_receive (fd, buffer, sizeof(buffer));
if (length > 0) {
buffer[length] = 0;
DEBUG printf ("Received: %s\n", buffer);
if (hp_redirect_inspect (buffer, length)) {
DecodeMessage (buffer, 1);
}
}
}
static void hp_redirect_open(void) {
int count;
while (PortalUdpPointsCount >= 0) {
echttp_forget (PortalUdpPoints[--PortalUdpPointsCount]);
}
count = hp_udp_server (PortalPort, RestrictUdp2Local,
PortalUdpPoints, MAX_UDP_POINTS);
if (count <= 0) {
houselog_trace (HOUSE_FAILURE, "HousePortal",
"Cannot open UDP sockets for port %s", PortalPort);
PortalUdpPointsCount = 0;
return;
}
PortalUdpPointsCount = count;
while (count > 0) {
echttp_listen (PortalUdpPoints[--count], 1, hp_redirect_udp, 0);
}
}
static void hp_redirect_publish (time_t now) {
int i;
int length;
char buffer[1400];
snprintf (buffer, sizeof(buffer), "PEER %ld", now);
length = strlen(buffer);
for (i = 0; i < PeerCount; ++i) {
time_t expiration = Peers[i].expiration;
if (expiration >= now)
snprintf (buffer+length, sizeof(buffer)-length,
" %s=%ld", Peers[i].name, (long)expiration);
else if (!expiration)
snprintf (buffer+length, sizeof(buffer)-length,
" %s", Peers[i].name);
length += strlen(buffer+length);
}
if (IntermediateDecodeLength) {
const char *signature =
houseportalhmac (IntermediateDecode[0].method,
IntermediateDecode[0].value,
buffer);
if (!signature) return;
snprintf (buffer+length, sizeof(buffer)-length,
" %s %s", IntermediateDecode[0].method, signature);
length += strlen(buffer+length);
}
// There are two ways of publishing:
// * Use broadcast to talk to the discovered peers.
// * Use explicit unicast for each statically defined peer.
// We do this because the static peer feature is meant
// for peers that cannot be reached through broadcast.
//
DEBUG printf ("Publish: %s\n", buffer);
hp_udp_broadcast (buffer, length);
for (i = 1; i < PeerCount; ++i) { // Do not send to ourself.
if (Peers[i].expiration == 0)
hp_udp_unicast (Peers[i].name, buffer, length);
}
}
void hp_redirect_background (void) {
static time_t LastCheck = 0;
time_t now = time(0);
struct stat fileinfo;
if (now > LastCheck + 30) {
int pruned = 0;
if (!RestrictUdp2Local && !hp_udp_has_broadcast()) {
hp_redirect_open();
return;
}
if (stat (ConfigurationPath, &fileinfo) == 0) {
if (ConfigurationTime != fileinfo.st_mtim.tv_sec) {
houselog_trace (HOUSE_INFO, "HousePortal",
"Configuration file %s changed",
ConfigurationPath);
DeprecatePermanentConfiguration();
LoadConfig (ConfigurationPath);
PruneRedirect (now+3000); // Force immediate expiration.
pruned = 1;
}
} else {
houselog_trace (HOUSE_FAILURE, "HousePortal",
"Cannot stat %s", ConfigurationPath);
}
if (!pruned) PruneRedirect (now);
if (!RestrictUdp2Local) hp_redirect_publish (now);
LastCheck = now;
}
}
static int hp_redirect_preamble (time_t now, char *buffer, int size) {
snprintf (buffer, size,
"{\"host\":\"%s\",\"timestamp\":%d,\"portal\":{", HostName, now);
return strlen(buffer);
}
void hp_redirect_list_json (int services, char *buffer, int size) {
int i;
int length;
int reclen;
char *cursor;
const char *prefix = "";
time_t now = time(0);
char service[256];
length = hp_redirect_preamble (now, buffer, size);
cursor = buffer + length;
snprintf (cursor, size-length, "\"redirect\":[");
length += strlen(cursor);
cursor = buffer + length;
for (i = 0; i < RedirectionCount; ++i) {
time_t expiration = Redirections[i].expiration;
if (Redirections[i].service)
snprintf (service, sizeof(service),
"\"service\":\"%s\",",Redirections[i].service);
else if (services)
continue; // Skip if no service was declared.
else
service[0] = 0;
snprintf (cursor, size-length,
"%s{\"path\":\"%*.*s\",%s\"expire\":%d,\"target\":\"%s\",\"hide\":%s,\"active\":%s}",
prefix,
Redirections[i].length, Redirections[i].length,
Redirections[i].path,
service,
expiration,
Redirections[i].target,
Redirections[i].hide?"true":"false",
(expiration == 0 || expiration > now)?"true":"false");
reclen = strlen(cursor);
prefix = ",";
if (length + reclen >= size) break;
length += reclen;
cursor += reclen;
}
snprintf (cursor, size-length, "]}}");
buffer[size-1] = 0;
}
void hp_redirect_peers_json (char *buffer, int size) {
int i;
int length;
char *cursor;
const char *prefix = "";
time_t now = time(0);
length = hp_redirect_preamble (now, buffer, size);
cursor = buffer + length;
snprintf (cursor, size-length, "\"peers\":[");
length += strlen(cursor);
cursor = buffer + length;
for (i = 0; i < PeerCount; ++i) {
time_t expiration = Peers[i].expiration;
if (expiration && expiration <= now) continue;
snprintf (cursor, size-length, "%s\"%s\"", prefix, Peers[i].name);
length += strlen(cursor);
prefix = ",";
cursor = buffer + length;
}
snprintf (cursor, size-length, "]}}");
buffer[size-1] = 0;
}
void hp_redirect_service_json (const char *name, char *buffer, int size) {
int i;
int length;
int port = echttp_port(4);
char hostaddress[1024];
char *cursor;
const char *prefix = "";
time_t now = time(0);
length = hp_redirect_preamble (now, buffer, size);
cursor = buffer + length;
snprintf (cursor, size-length,
"\"service\":{\"name\":\"%s\",\"url\":[", name);
length += strlen(cursor);
cursor = buffer + length;
if (port == 80) {
strncpy (hostaddress, HostName, sizeof(hostaddress));
hostaddress[sizeof(hostaddress)-1] = 0;
} else {
snprintf (hostaddress, sizeof(hostaddress), "%s:%d", HostName, port);
}
for (i = 0; i < RedirectionCount; ++i) {
time_t expiration = Redirections[i].expiration;
if (expiration && expiration <= now) continue;
if (!Redirections[i].service) continue;
if (strcmp(Redirections[i].service, name)) continue;
snprintf (cursor, size-length, "%s\"http://%s%s\"",
prefix, hostaddress, Redirections[i].path);
length += strlen(cursor);
prefix = ",";
cursor = buffer + length;
}
snprintf (cursor, size-length, "]}}}");
buffer[size-1] = 0;
}
void hp_redirect_start (int argc, const char **argv) {
int i;
char hostname[1000];
gethostname (hostname, sizeof(hostname));
HostName = strdup(hostname);
for (i = 1; i < argc; ++i) {
echttp_option_match ("-config=", argv[i], &ConfigurationPath);
echttp_option_match ("-portal-port=", argv[i], &PortalPort);
}
AddOnePeer (HostName, 0); // List ourself first.
LoadConfig (ConfigurationPath);
hp_redirect_open();
}