-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc.c
1689 lines (1574 loc) · 51.4 KB
/
svc.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
/*
* Pound - the reverse-proxy load-balancer
* Copyright (C) 2002-2010 Apsis GmbH
*
* This file is part of Pound.
*
* Pound 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 3 of the License, or
* (at your option) any later version.
*
* Pound 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/>.
*
* Contact information:
* Apsis GmbH
* P.O.Box
* 8707 Uetikon am See
* Switzerland
* EMail: [email protected]
*/
#include "pound.h"
#ifndef LHASH_OF
#define LHASH_OF(x) LHASH
#define CHECKED_LHASH_OF(type, h) h
#endif
/*
* Add a new key/content pair to a hash table
* the table should be already locked
*/
static void
t_add(LHASH_OF(TABNODE) *const tab, const char *key, const void *content, const size_t cont_len)
{
TABNODE *t, *old;
if((t = (TABNODE *)malloc(sizeof(TABNODE))) == NULL) {
logmsg(LOG_WARNING, "t_add() content malloc");
return;
}
if((t->key = strdup(key)) == NULL) {
free(t);
logmsg(LOG_WARNING, "t_add() strdup");
return;
}
if((t->content = malloc(cont_len)) == NULL) {
free(t->key);
free(t);
logmsg(LOG_WARNING, "t_add() content malloc");
return;
}
memcpy(t->content, content, cont_len);
t->last_acc = time(NULL);
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if((old = LHM_lh_insert(TABNODE, tab, t)) != NULL) {
#else
if((old = (TABNODE *)lh_insert(tab, t)) != NULL) {
#endif
free(old->key);
free(old->content);
free(old);
logmsg(LOG_WARNING, "t_add() DUP");
}
return;
}
/*
* Find a key
* returns the content in the parameter
* side-effect: update the time of last access
*/
static void *
t_find(LHASH_OF(TABNODE) *const tab, char *const key)
{
TABNODE t, *res;
t.key = key;
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if((res = (TABNODE *)LHM_lh_retrieve(TABNODE, tab, &t)) != NULL) {
#else
if((res = (TABNODE *)lh_retrieve(tab, &t)) != NULL) {
#endif
res->last_acc = time(NULL);
return res->content;
}
return NULL;
}
/*
* Delete a key
*/
static void
t_remove(LHASH_OF(TABNODE) *const tab, char *const key)
{
TABNODE t, *res;
t.key = key;
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if((res = LHM_lh_delete(TABNODE, tab, &t)) != NULL) {
#else
if((res = (TABNODE *)lh_delete(tab, &t)) != NULL) {
#endif
free(res->key);
free(res->content);
free(res);
}
return;
}
typedef struct {
LHASH_OF(TABNODE) *tab;
time_t lim;
void *content;
int cont_len;
} ALL_ARG;
static void
t_old_doall_arg(TABNODE *t, ALL_ARG *a)
{
TABNODE *res;
if(t->last_acc < a->lim)
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if((res = LHM_lh_delete(TABNODE, a->tab, t)) != NULL) {
#else
if((res = lh_delete(a->tab, t)) != NULL) {
#endif
free(res->key);
free(res->content);
free(res);
}
return;
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
IMPLEMENT_LHASH_DOALL_ARG_FN(t_old, TABNODE, ALL_ARG)
#else
#define t_old t_old_doall_arg
IMPLEMENT_LHASH_DOALL_ARG_FN(t_old, TABNODE *, ALL_ARG *)
#endif
/*
* Expire all old nodes
*/
static void
t_expire(LHASH_OF(TABNODE) *const tab, const time_t lim)
{
ALL_ARG a;
int down_load;
a.tab = tab;
a.lim = lim;
down_load = CHECKED_LHASH_OF(TABNODE, tab)->down_load;
CHECKED_LHASH_OF(TABNODE, tab)->down_load = 0;
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
LHM_lh_doall_arg(TABNODE, tab, LHASH_DOALL_ARG_FN(t_old), ALL_ARG, &a);
#else
lh_doall_arg(tab, LHASH_DOALL_ARG_FN(t_old), &a);
#endif
CHECKED_LHASH_OF(TABNODE, tab)->down_load = down_load;
return;
}
static void
t_cont_doall_arg(TABNODE *t, ALL_ARG *arg)
{
TABNODE *res;
if(memcmp(t->content, arg->content, arg->cont_len) == 0)
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if((res = LHM_lh_delete(TABNODE, arg->tab, t)) != NULL) {
#else
if((res = lh_delete(arg->tab, t)) != NULL) {
#endif
free(res->key);
free(res->content);
free(res);
}
return;
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
IMPLEMENT_LHASH_DOALL_ARG_FN(t_cont, TABNODE, ALL_ARG)
#else
#define t_cont t_cont_doall_arg
IMPLEMENT_LHASH_DOALL_ARG_FN(t_cont, TABNODE *, ALL_ARG *)
#endif
/*
* Remove all nodes with the given content
*/
static void
t_clean(LHASH_OF(TABNODE) *const tab, void *const content, const size_t cont_len)
{
ALL_ARG a;
int down_load;
a.tab = tab;
a.content = content;
a.cont_len = cont_len;
down_load = CHECKED_LHASH_OF(TABNODE, tab)->down_load;
CHECKED_LHASH_OF(TABNODE, tab)->down_load = 0;
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
LHM_lh_doall_arg(TABNODE, tab, LHASH_DOALL_ARG_FN(t_cont), ALL_ARG, &a);
#else
lh_doall_arg(tab, LHASH_DOALL_ARG_FN(t_cont), &a);
#endif
CHECKED_LHASH_OF(TABNODE, tab)->down_load = down_load;
return;
}
/*
* Log an error to the syslog or to stderr
*/
#ifdef HAVE_STDARG_H
void
logmsg(const int priority, const char *fmt, ...)
{
char buf[MAXBUF + 1];
va_list ap;
struct tm *t_now, t_res;
buf[MAXBUF] = '\0';
va_start(ap, fmt);
vsnprintf(buf, MAXBUF, fmt, ap);
va_end(ap);
if(log_facility == -1) {
fprintf((priority == LOG_INFO || priority == LOG_DEBUG)? stdout: stderr, "%s\n", buf);
} else {
if(print_log)
printf("%s\n", buf);
else
syslog(log_facility | priority, "%s", buf);
}
return;
}
#else
void
logmsg(const int priority, const char *fmt, va_alist)
va_dcl
{
char buf[MAXBUF + 1];
va_list ap;
struct tm *t_now, t_res;
buf[MAXBUF] = '\0';
va_start(ap);
vsnprintf(buf, MAXBUF, fmt, ap);
va_end(ap);
if(log_facility == -1) {
fprintf((priority == LOG_INFO || priority == LOG_DEBUG)? stdout: stderr, "%s\n", buf);
} else {
if(print_log)
printf("%s\n", buf);
else
syslog(log_facility | priority, "%s", buf);
}
return;
}
#endif
/*
* Translate inet/inet6 address/port into a string
*/
void
addr2str(char *const res, const int res_len, const struct addrinfo *addr, const int no_port)
{
char buf[MAXBUF];
int port;
void *src;
memset(res, 0, res_len);
#ifdef HAVE_INET_NTOP
switch(addr->ai_family) {
case AF_INET:
src = (void *)&((struct sockaddr_in *)addr->ai_addr)->sin_addr.s_addr;
port = ntohs(((struct sockaddr_in *)addr->ai_addr)->sin_port);
if(inet_ntop(AF_INET, src, buf, MAXBUF - 1) == NULL)
strncpy(buf, "(UNKNOWN)", MAXBUF - 1);
break;
case AF_INET6:
src = (void *)&((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr.s6_addr;
port = ntohs(((struct sockaddr_in6 *)addr->ai_addr)->sin6_port);
if(inet_ntop(AF_INET6, src, buf, MAXBUF - 1) == NULL)
strncpy(buf, "(UNKNOWN)", MAXBUF - 1);
break;
case AF_UNIX:
strncpy(buf, (char *)addr->ai_addr, MAXBUF - 1);
port = 0;
break;
default:
strncpy(buf, "(UNKNOWN)", MAXBUF - 1);
port = 0;
break;
}
if(no_port)
snprintf(res, res_len, "%s", buf);
else
snprintf(res, res_len, "%s:%d", buf, port);
#else
#error "Pound needs inet_ntop()"
#endif
return;
}
/*
* Parse a URL, possibly decoding hexadecimal-encoded characters
*/
int
cpURL(char *res, char *src, int len)
{
int state;
char *kp_res;
for(kp_res = res, state = 0; len > 0; len--)
switch(state) {
case 1:
if(*src >= '0' && *src <= '9') {
*res = *src++ - '0';
state = 2;
} else if(*src >= 'A' && *src <= 'F') {
*res = *src++ - 'A' + 10;
state = 2;
} else if(*src >= 'a' && *src <= 'f') {
*res = *src++ - 'a' + 10;
state = 2;
} else {
*res++ = '%';
*res++ = *src++;
state = 0;
}
break;
case 2:
if(*src >= '0' && *src <= '9') {
*res = *res * 16 + *src++ - '0';
res++;
state = 0;
} else if(*src >= 'A' && *src <= 'F') {
*res = *res * 16 + *src++ - 'A' + 10;
res++;
state = 0;
} else if(*src >= 'a' && *src <= 'f') {
*res = *res * 16 + *src++ - 'a' + 10;
res++;
state = 0;
} else {
*res++ = '%';
*res++ = *(src - 1);
*res++ = *src++;
state = 0;
}
break;
default:
if(*src != '%')
*res++ = *src++;
else {
src++;
state = 1;
}
break;
}
if(state > 0)
*res++ = '%';
if(state > 1)
*res++ = *(src - 1);
*res = '\0';
return res - kp_res;
}
/*
* Parse a header
* return a code and possibly content in the arg
*/
int
check_header(const char *header, char *const content)
{
regmatch_t matches[4];
static struct {
char header[32];
int len;
int val;
} hd_types[] = {
{ "Transfer-encoding", 17, HEADER_TRANSFER_ENCODING },
{ "Content-length", 14, HEADER_CONTENT_LENGTH },
{ "Connection", 10, HEADER_CONNECTION },
{ "Location", 8, HEADER_LOCATION },
{ "Content-location", 16, HEADER_CONTLOCATION },
{ "Host", 4, HEADER_HOST },
{ "Referer", 7, HEADER_REFERER },
{ "User-agent", 10, HEADER_USER_AGENT },
{ "Destination", 11, HEADER_DESTINATION },
{ "Expect", 6, HEADER_EXPECT },
{ "", 0, HEADER_OTHER },
};
int i;
if(!regexec(&HEADER, header, 4, matches, 0)) {
for(i = 0; hd_types[i].len > 0; i++)
if((matches[1].rm_eo - matches[1].rm_so) == hd_types[i].len
&& strncasecmp(header + matches[1].rm_so, hd_types[i].header, hd_types[i].len) == 0) {
/* we know that the original header was read into a buffer of size MAXBUF, so no overflow */
strncpy(content, header + matches[2].rm_so, matches[2].rm_eo - matches[2].rm_so);
content[matches[2].rm_eo - matches[2].rm_so] = '\0';
return hd_types[i].val;
}
return HEADER_OTHER;
} else if(header[0] == ' ' || header[0] == '\t') {
*content = '\0';
return HEADER_OTHER;
} else
return HEADER_ILLEGAL;
}
static int
match_service(const SERVICE *svc, const char *request, char **const headers)
{
MATCHER *m;
int i, found;
/* check for request */
for(m = svc->url; m; m = m->next)
if(regexec(&m->pat, request, 0, NULL, 0))
return 0;
/* check for required headers */
for(m = svc->req_head; m; m = m->next) {
for(found = i = 0; i < (MAXHEADERS - 1) && !found; i++)
if(headers[i] && !regexec(&m->pat, headers[i], 0, NULL, 0))
found = 1;
if(!found)
return 0;
}
/* check for forbidden headers */
for(m = svc->deny_head; m; m = m->next) {
for(found = i = 0; i < (MAXHEADERS - 1) && !found; i++)
if(headers[i] && !regexec(&m->pat, headers[i], 0, NULL, 0))
found = 1;
if(found)
return 0;
}
return 1;
}
/*
* Find the right service for a request
*/
SERVICE *
get_service(const LISTENER *lstn, const char *request, char **const headers)
{
SERVICE *svc;
for(svc = lstn->services; svc; svc = svc->next) {
if(svc->disabled)
continue;
if(match_service(svc, request, headers))
return svc;
}
/* try global services */
for(svc = services; svc; svc = svc->next) {
if(svc->disabled)
continue;
if(match_service(svc, request, headers))
return svc;
}
/* nothing matched */
return NULL;
}
/*
* extract the session key for a given request
*/
static int
get_REQUEST(char *res, const SERVICE *svc, const char *request)
{
int n, s;
regmatch_t matches[4];
if(regexec(&svc->sess_start, request, 4, matches, 0)) {
res[0] = '\0';
return 0;
}
s = matches[0].rm_eo;
if(regexec(&svc->sess_pat, request + s, 4, matches, 0)) {
res[0] = '\0';
return 0;
}
if((n = matches[1].rm_eo - matches[1].rm_so) > KEY_SIZE)
n = KEY_SIZE;
strncpy(res, request + s + matches[1].rm_so, n);
res[n] = '\0';
return 1;
}
static int
get_HEADERS(char *res, const SERVICE *svc, char **const headers)
{
int i, n, s;
regmatch_t matches[4];
/* this will match SESS_COOKIE, SESS_HEADER and SESS_BASIC */
res[0] = '\0';
for(i = 0; i < (MAXHEADERS - 1); i++) {
if(headers[i] == NULL)
continue;
if(regexec(&svc->sess_start, headers[i], 4, matches, 0))
continue;
s = matches[0].rm_eo;
if(regexec(&svc->sess_pat, headers[i] + s, 4, matches, 0))
continue;
if((n = matches[1].rm_eo - matches[1].rm_so) > KEY_SIZE)
n = KEY_SIZE;
strncpy(res, headers[i] + s + matches[1].rm_so, n);
res[n] = '\0';
}
return res[0] != '\0';
}
/*
* Pick a random back-end from a candidate list
*/
static BACKEND *
rand_backend(BACKEND *be, int pri)
{
while(be) {
if(!be->alive || be->disabled) {
be = be->next;
continue;
}
if((pri -= be->priority) < 0)
break;
be = be->next;
}
return be;
}
/*
* return a back-end based on a fixed hash value
* this is used for session_ttl < 0
*
* WARNING: the function may return different back-ends
* if the target back-end is disabled or not alive
*/
static BACKEND *
hash_backend(BACKEND *be, int abs_pri, char *key)
{
unsigned long hv;
BACKEND *res, *tb;
int pri;
hv = 2166136261;
while(*key)
hv = ((hv ^ *key++) * 16777619) & 0xFFFFFFFF;
pri = hv % abs_pri;
for(tb = be; tb; tb = tb->next)
if((pri -= tb->priority) < 0)
break;
if(!tb)
/* should NEVER happen */
return NULL;
for(res = tb; !res->alive || res->disabled; ) {
res = res->next;
if(res == NULL)
res = be;
if(res == tb)
/* NO back-end available */
return NULL;
}
return res;
}
/*
* Find the right back-end for a request
*/
BACKEND *
get_backend(SERVICE *const svc, const struct addrinfo *from_host, const char *request, char **const headers)
{
BACKEND *res;
char key[KEY_SIZE + 1];
int ret_val, no_be;
void *vp;
if(ret_val = pthread_mutex_lock(&svc->mut))
logmsg(LOG_WARNING, "get_backend() lock: %s", strerror(ret_val));
no_be = (svc->tot_pri <= 0);
switch(svc->sess_type) {
case SESS_NONE:
/* choose one back-end randomly */
res = no_be? svc->emergency: rand_backend(svc->backends, random() % svc->tot_pri);
break;
case SESS_IP:
addr2str(key, KEY_SIZE, from_host, 1);
if(svc->sess_ttl < 0)
res = no_be? svc->emergency: hash_backend(svc->backends, svc->abs_pri, key);
else if((vp = t_find(svc->sessions, key)) == NULL) {
if(no_be)
res = svc->emergency;
else {
/* no session yet - create one */
res = rand_backend(svc->backends, random() % svc->tot_pri);
t_add(svc->sessions, key, &res, sizeof(res));
}
} else
memcpy(&res, vp, sizeof(res));
break;
case SESS_URL:
case SESS_PARM:
if(get_REQUEST(key, svc, request)) {
if(svc->sess_ttl < 0)
res = no_be? svc->emergency: hash_backend(svc->backends, svc->abs_pri, key);
else if((vp = t_find(svc->sessions, key)) == NULL) {
if(no_be)
res = svc->emergency;
else {
/* no session yet - create one */
res = rand_backend(svc->backends, random() % svc->tot_pri);
t_add(svc->sessions, key, &res, sizeof(res));
}
} else
memcpy(&res, vp, sizeof(res));
} else {
res = no_be? svc->emergency: rand_backend(svc->backends, random() % svc->tot_pri);
}
break;
default:
/* this works for SESS_BASIC, SESS_HEADER and SESS_COOKIE */
if(get_HEADERS(key, svc, headers)) {
if(svc->sess_ttl < 0)
res = no_be? svc->emergency: hash_backend(svc->backends, svc->abs_pri, key);
else if((vp = t_find(svc->sessions, key)) == NULL) {
if(no_be)
res = svc->emergency;
else {
/* no session yet - create one */
res = rand_backend(svc->backends, random() % svc->tot_pri);
t_add(svc->sessions, key, &res, sizeof(res));
}
} else
memcpy(&res, vp, sizeof(res));
} else {
res = no_be? svc->emergency: rand_backend(svc->backends, random() % svc->tot_pri);
}
break;
}
if(ret_val = pthread_mutex_unlock(&svc->mut))
logmsg(LOG_WARNING, "get_backend() unlock: %s", strerror(ret_val));
return res;
}
/*
* (for cookies/header only) possibly create session based on response headers
*/
void
upd_session(SERVICE *const svc, char **const headers, BACKEND *const be)
{
char key[KEY_SIZE + 1];
int ret_val;
if(svc->sess_type != SESS_HEADER && svc->sess_type != SESS_COOKIE)
return;
if(ret_val = pthread_mutex_lock(&svc->mut))
logmsg(LOG_WARNING, "upd_session() lock: %s", strerror(ret_val));
if(get_HEADERS(key, svc, headers))
if(t_find(svc->sessions, key) == NULL)
t_add(svc->sessions, key, &be, sizeof(be));
if(ret_val = pthread_mutex_unlock(&svc->mut))
logmsg(LOG_WARNING, "upd_session() unlock: %s", strerror(ret_val));
return;
}
/*
* mark a backend host as dead/disabled; remove its sessions if necessary
* disable_only == 1: mark as disabled
* disable_only == 0: mark as dead, remove sessions
* disable_only == -1: mark as enabled
*/
void
kill_be(SERVICE *const svc, const BACKEND *be, const int disable_mode)
{
BACKEND *b;
int ret_val;
char buf[MAXBUF];
if(ret_val = pthread_mutex_lock(&svc->mut))
logmsg(LOG_WARNING, "kill_be() lock: %s", strerror(ret_val));
svc->tot_pri = 0;
for(b = svc->backends; b; b = b->next) {
if(b == be)
switch(disable_mode) {
case BE_DISABLE:
b->disabled = 1;
str_be(buf, MAXBUF - 1, b);
logmsg(LOG_NOTICE, "(%lx) BackEnd %s disabled", pthread_self(), buf);
break;
case BE_KILL:
b->alive = 0;
str_be(buf, MAXBUF - 1, b);
logmsg(LOG_NOTICE, "(%lx) BackEnd %s dead (killed)", pthread_self(), buf);
t_clean(svc->sessions, &be, sizeof(be));
break;
case BE_ENABLE:
str_be(buf, MAXBUF - 1, b);
logmsg(LOG_NOTICE, "(%lx) BackEnd %s enabled", pthread_self(), buf);
b->disabled = 0;
break;
default:
logmsg(LOG_WARNING, "kill_be(): unknown mode %d", disable_mode);
break;
}
if(b->alive && !b->disabled)
svc->tot_pri += b->priority;
}
if(ret_val = pthread_mutex_unlock(&svc->mut))
logmsg(LOG_WARNING, "kill_be() unlock: %s", strerror(ret_val));
return;
}
/*
* Search for a host name, return the addrinfo for it
*/
int
get_host(char *const name, struct addrinfo *res, int ai_family)
{
struct addrinfo *chain, *ap;
struct addrinfo hints;
int ret_val;
#ifdef HAVE_INET_NTOP
memset (&hints, 0, sizeof(hints));
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
if((ret_val = getaddrinfo(name, NULL, &hints, &chain)) == 0) {
#ifdef _AIX
ap = chain;
#else
for(ap = chain; ap != NULL; ap = ap->ai_next)
if(ap->ai_socktype == SOCK_STREAM)
break;
#endif
if(ap == NULL) {
freeaddrinfo(chain);
return EAI_NONAME;
}
*res = *ap;
if((res->ai_addr = (struct sockaddr *)malloc(ap->ai_addrlen)) == NULL) {
freeaddrinfo(chain);
return EAI_MEMORY;
}
memcpy(res->ai_addr, ap->ai_addr, ap->ai_addrlen);
freeaddrinfo(chain);
}
#else
#error "Pound requires getaddrinfo()"
#endif
return ret_val;
}
/*
* Find if a redirect needs rewriting
* In general we have two possibilities that require it:
* (1) if the redirect was done to the correct location with the wrong port
* (2) if the redirect was done to the back-end rather than the listener
*/
int
need_rewrite(const int rewr_loc, char *const location, char *const path, const char *v_host, const LISTENER *lstn, const BACKEND *be)
{
struct addrinfo addr;
struct sockaddr_in in_addr, be_addr;
struct sockaddr_in6 in6_addr, be6_addr;
regmatch_t matches[4];
char *proto, *host, *port, *cp, buf[MAXBUF];
/* check if rewriting is required at all */
if(rewr_loc == 0)
return 0;
/* applies only to INET/INET6 back-ends */
if(be->addr.ai_family != AF_INET && be->addr.ai_family != AF_INET6)
return 0;
/* split the location into its fields */
if(regexec(&LOCATION, location, 4, matches, 0))
return 0;
proto = location + matches[1].rm_so;
host = location + matches[2].rm_so;
if(location[matches[3].rm_so] == '/')
matches[3].rm_so++;
/* path is guaranteed to be large enough */
strcpy(path, location + matches[3].rm_so);
location[matches[1].rm_eo] = location[matches[2].rm_eo] = '\0';
if((port = strchr(host, ':')) != NULL)
*port++ = '\0';
/*
* Check if the location has the same address as the listener or the back-end
*/
memset(&addr, 0, sizeof(addr));
if(get_host(host, &addr, be->addr.ai_family))
return 0;
/*
* compare the back-end
*/
if(addr.ai_family != be->addr.ai_family) {
free(addr.ai_addr);
return 0;
}
if(addr.ai_family == AF_INET) {
memcpy(&in_addr, addr.ai_addr, sizeof(in_addr));
memcpy(&be_addr, be->addr.ai_addr, sizeof(be_addr));
if(port)
in_addr.sin_port = (in_port_t)htons(atoi(port));
else if(!strcasecmp(proto, "https"))
in_addr.sin_port = (in_port_t)htons(443);
else
in_addr.sin_port = (in_port_t)htons(80);
/*
* check if the Location points to the back-end
*/
if(memcmp(&be_addr.sin_addr.s_addr, &in_addr.sin_addr.s_addr, sizeof(in_addr.sin_addr.s_addr)) == 0
&& memcmp(&be_addr.sin_port, &in_addr.sin_port, sizeof(in_addr.sin_port)) == 0) {
free(addr.ai_addr);
return 1;
}
} else /* AF_INET6 */ {
memcpy(&in6_addr, addr.ai_addr, sizeof(in6_addr));
memcpy(&be6_addr, be->addr.ai_addr, sizeof(be6_addr));
if(port)
in6_addr.sin6_port = (in_port_t)htons(atoi(port));
else if(!strcasecmp(proto, "https"))
in6_addr.sin6_port = (in_port_t)htons(443);
else
in6_addr.sin6_port = (in_port_t)htons(80);
/*
* check if the Location points to the back-end
*/
if(memcmp(&be6_addr.sin6_addr.s6_addr, &in6_addr.sin6_addr.s6_addr, sizeof(in6_addr.sin6_addr.s6_addr)) == 0
&& memcmp(&be6_addr.sin6_port, &in6_addr.sin6_port, sizeof(in6_addr.sin6_port)) == 0) {
free(addr.ai_addr);
return 1;
}
}
/*
* compare the listener
*/
if(rewr_loc != 1 || addr.ai_family != lstn->addr.ai_family) {
free(addr.ai_addr);
return 0;
}
memset(buf, '\0', MAXBUF);
strncpy(buf, v_host, MAXBUF - 1);
if((cp = strchr(buf, ':')) != NULL)
*cp = '\0';
if(addr.ai_family == AF_INET) {
memcpy(&be_addr, lstn->addr.ai_addr, sizeof(be_addr));
/*
* check if the Location points to the Listener but with the wrong port or protocol
*/
if((memcmp(&be_addr.sin_addr.s_addr, &in_addr.sin_addr.s_addr, sizeof(in_addr.sin_addr.s_addr)) == 0
|| strcasecmp(host, buf) == 0)
&& (memcmp(&be_addr.sin_port, &in_addr.sin_port, sizeof(in_addr.sin_port)) != 0
|| strcasecmp(proto, lstn->ctx? "http": "https"))) {
free(addr.ai_addr);
return 1;
}
} else {
memcpy(&be6_addr, lstn->addr.ai_addr, sizeof(be6_addr));
/*
* check if the Location points to the Listener but with the wrong port or protocol
*/
if((memcmp(&be6_addr.sin6_addr.s6_addr, &in6_addr.sin6_addr.s6_addr, sizeof(in6_addr.sin6_addr.s6_addr)) == 0
|| strcasecmp(host, buf) == 0)
&& (memcmp(&be6_addr.sin6_port, &in6_addr.sin6_port, sizeof(in6_addr.sin6_port)) != 0
|| strcasecmp(proto, lstn->ctx? "http": "https"))) {
free(addr.ai_addr);
return 1;
}
}
free(addr.ai_addr);
return 0;
}
/*
* Non-blocking connect(). Does the same as connect(2) but ensures
* it will time-out after a much shorter time period SERVER_TO
*/
int
connect_nb(const int sockfd, const struct addrinfo *serv_addr, const int to)
{
int flags, res, error;
socklen_t len;
struct pollfd p;
if((flags = fcntl(sockfd, F_GETFL, 0)) < 0) {
logmsg(LOG_WARNING, "(%lx) connect_nb: fcntl GETFL failed: %s", pthread_self(), strerror(errno));
return -1;
}
if(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
logmsg(LOG_WARNING, "(%lx) connect_nb: fcntl SETFL failed: %s", pthread_self(), strerror(errno));
return -1;
}
error = 0;
if((res = connect(sockfd, serv_addr->ai_addr, serv_addr->ai_addrlen)) < 0)
if(errno != EINPROGRESS) {
logmsg(LOG_WARNING, "(%lx) connect_nb: connect failed: %s", pthread_self(), strerror(errno));
return (-1);
}
if(res == 0) {
/* connect completed immediately (usually localhost) */
if(fcntl(sockfd, F_SETFL, flags) < 0) {
logmsg(LOG_WARNING, "(%lx) connect_nb: fcntl reSETFL failed: %s", pthread_self(), strerror(errno));
return -1;
}
return 0;
}
memset(&p, 0, sizeof(p));
p.fd = sockfd;
p.events = POLLOUT;
if((res = poll(&p, 1, to * 1000)) != 1) {
if(res == 0) {
/* timeout */
logmsg(LOG_WARNING, "(%lx) connect_nb: poll timed out", pthread_self());
errno = ETIMEDOUT;
} else
logmsg(LOG_WARNING, "(%lx) connect_nb: poll failed: %s", pthread_self(), strerror(errno));
return -1;
}
/* socket is writeable == operation completed */
len = sizeof(error);
if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
logmsg(LOG_WARNING, "(%lx) connect_nb: getsockopt failed: %s", pthread_self(), strerror(errno));
return -1;
}
/* restore file status flags */
if(fcntl(sockfd, F_SETFL, flags) < 0) {
logmsg(LOG_WARNING, "(%lx) connect_nb: fcntl reSETFL failed: %s", pthread_self(), strerror(errno));
return -1;
}
if(error) {
/* getsockopt() shows an error */
errno = error;
logmsg(LOG_WARNING, "(%lx) connect_nb: error after getsockopt: %s", pthread_self(), strerror(errno));
return -1;
}
/* really connected */
return 0;
}
/*
* Check if dead hosts returned to life;
* runs every alive seconds
*/
static void
do_resurect(void)
{
LISTENER *lstn;
SERVICE *svc;
BACKEND *be;
struct addrinfo z_addr, *addr;
int sock, modified;
char buf[MAXBUF];
int ret_val;
/* check hosts still alive - HAport */
memset(&z_addr, 0, sizeof(z_addr));
for(lstn = listeners; lstn; lstn = lstn->next)
for(svc = lstn->services; svc; svc = svc->next)
for(be = svc->backends; be; be = be->next) {
if(be->be_type)
continue;
if(!be->alive)
/* already dead */
continue;
if(memcmp(&(be->ha_addr), &z_addr, sizeof(z_addr)) == 0)
/* no HA port */
continue;
/* try connecting */
switch(be->ha_addr.ai_family) {
case AF_INET: