summaryrefslogtreecommitdiff
path: root/accel-pppd/extra/connlimit.c
blob: fd7c952b619e842c0bbe3eee46cbbefb08672c71 (plain)
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
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include "ppp.h"
#include "events.h"
#include "triton.h"
#include "log.h"

#include "memdebug.h"

struct item
{
	struct list_head entry;
	uint64_t key;
	struct timespec ts;
	int count;
};

static int conf_burst = 3;
static int conf_burst_timeout = 60 * 1000;
static int conf_limit_timeout = 5000;

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static LIST_HEAD(items);

int __export connlimit_check(uint64_t key)
{
	struct item *it;
	struct timespec ts;
	unsigned int d;
	struct list_head *pos, *n;
	LIST_HEAD(tmp_list);
	int r = 1;

	
	clock_gettime(CLOCK_MONOTONIC, &ts);

	pthread_mutex_lock(&lock);
	log_debug("connlimit: check entry %" PRIu64 "\n", key);
	list_for_each_safe(pos, n, &items) {
		it = list_entry(pos, typeof(*it), entry);

		d = (ts.tv_sec - it->ts.tv_sec) * 1000 + (ts.tv_nsec - it->ts.tv_nsec) / 1000000;

		if (it->key == key) {
			if (d >= conf_burst_timeout) {
				it->ts = ts;
				list_move(&it->entry, &items);
				it->count = 0;
				r = 0;
				break;
			}
			it->count++;
			if (it->count >= conf_burst) {
				if (d >= conf_limit_timeout) {
					it->ts = ts;
					list_move(&it->entry, &items);
					r = 0;
				} else
					r = -1;
			} else
				r = 0;
			break;
		}

		if (d > conf_burst_timeout) {
			log_debug("connlimit: remove %" PRIu64 "\n", it->key);
			list_move(&it->entry, &tmp_list);
		}
	}
	pthread_mutex_unlock(&lock);

	if (r == 1) {
		it = _malloc(sizeof(*it));
		memset(it, 0, sizeof(*it));
		it->ts = ts;
		it->key = key;

		log_debug("connlimit: add entry %" PRIu64 "\n", key);

		pthread_mutex_lock(&lock);
		list_add(&it->entry, &items);
		pthread_mutex_unlock(&lock);

		r = 0;
	}
	
	if (r == 0)
		log_debug("connlimit: accept %" PRIu64 "\n", key);
	else
		log_debug("connlimit: drop %" PRIu64 "\n", key);

	
	while (!list_empty(&tmp_list)) {
		it = list_entry(tmp_list.next, typeof(*it), entry);
		list_del(&it->entry);
		_free(it);
	}

	return r;
}

static int parse_limit(const char *opt, int *limit, int *time)
{
	char *endptr;

	*limit = strtol(opt, &endptr, 10);

	if (!*endptr) {
		*time = 1;
		return 0;
	}
	
	if (*endptr != '/')
		goto out_err;
	
	opt = endptr + 1;
	*time = strtol(opt, &endptr, 10);

	if (endptr == opt)
		*time = 1;

	if (*endptr == 's')
		return 0;
	
	if (*endptr == 'm') {
		*time *= 60;
		return 0;
	}
	
	if (*endptr == 'h') {
		*time *= 3600;
		return 0;
	}
		
out_err:	
	log_error("connlimit: failed to parse '%s'\n", opt);
	return -1;
}

static void load_config()
{
	const char *opt;
	int n,t;

	opt = conf_get_opt("connlimit", "limit");
	if (opt) {
		if (parse_limit(opt, &n, &t))
			return;
		conf_limit_timeout = t * 1000 / n;
	}

	opt = conf_get_opt("connlimit", "burst");
	if (opt)
		conf_burst = atoi(opt);
	
	opt = conf_get_opt("connlimit", "timeout");
	if (opt)
		conf_burst_timeout = atoi(opt) * 1000;
}

static void init()
{
	load_config();

	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
}

DEFINE_INIT(200, init);