File: suffix.c

package info (click to toggle)
links2 2.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,852 kB
  • sloc: ansic: 181,859; sh: 2,585; cpp: 1,450; makefile: 84; awk: 49; perl: 34
file content (66 lines) | stat: -rw-r--r-- 2,007 bytes parent folder | download | duplicates (4)
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
#include "links.h"

#include "suffix.inc"

#include "suffix_x.inc"

static int search_list(const_char_ptr const *list, int len, unsigned char *name)
{
	int result;
#define T_EQUAL(n, k)		!casestrcmp(cast_uchar list[n], k)
#define T_ABOVE(n, k)		casestrcmp(cast_uchar list[n], k) > 0
	BIN_SEARCH(len, T_EQUAL, T_ABOVE, name, result);
	return result != -1;
}

static int search_list_and_wildcards(const_char_ptr const *list, int len, unsigned char *name)
{
	unsigned char *dot, *x;
	int sl;

	if (search_list(list, len, name)) return 1;

	x = stracpy(cast_uchar "*.");
	add_to_strn(&x, name);
	sl = search_list(list, len, x);
	mem_free(x);
	if (sl) return 1;

	dot = cast_uchar strchr(cast_const_char name, '.');
	if (!dot) return 0;
	x = stracpy(cast_uchar "*");
	add_to_strn(&x, dot);
	sl = search_list(list, len, x);
	mem_free(x);
	return sl;
}

int is_tld(unsigned char *name)
{
	char *end;
	unsigned long l;
	if (strlen(cast_const_char name) == 2 && upcase(name[0]) >= 'A' && upcase(name[0]) <= 'Z' && upcase(name[1]) >= 'A' && upcase(name[1]) <= 'Z' && casestrcmp(name, cast_uchar "gz") && casestrcmp(name, cast_uchar "xz"))
		return 1;
	l = strtoul(cast_const_char name, &end, 10);
	if (!*end && l <= 255)
		return 1;
	return search_list(domain_suffix, array_elements(domain_suffix), name);
}

int allow_cookie_domain(unsigned char *server, unsigned char *domain)
{
	int sl = (int)strlen(cast_const_char server);
	int dl = (int)strlen(cast_const_char domain);
	if (dl > sl) return 0;
	if (casestrcmp(domain, server + sl - dl)) return 0;
	if (dl == sl) return 1;
	if (!numeric_ip_address(server, NULL)) return 0;
#ifdef SUPPORT_IPV6
	if (!numeric_ipv6_address(server, NULL, NULL)) return 0;
#endif
	if (server[sl - dl - 1] != '.') return 0;
	if (search_list_and_wildcards(domain_suffix_x, array_elements(domain_suffix_x), domain)) return 1;
	if (!strchr(cast_const_char domain, '.')) return 0;
	if (search_list_and_wildcards(domain_suffix, array_elements(domain_suffix), domain)) return 0;
	return 1;
}