list_delete(&outgoing_list, outgoing);
}
-/*
- accept a new tcp connect and create a
- new connection
-*/
-void handle_new_meta_connection(void *data, int flags) {
- (void)flags;
- listen_socket_t *l = data;
- connection_t *c;
- sockaddr_t sa;
- int fd;
- socklen_t len = sizeof(sa);
-
- fd = accept(l->tcp.fd, &sa.sa, &len);
-
- if(fd < 0) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
- return;
- }
-
- sockaddrunmap(&sa);
-
+static bool check_tarpit(const sockaddr_t *sa, int fd) {
// Check if we get many connections from the same host
static sockaddr_t prev_sa;
- if(!sockaddrcmp_noport(&sa, &prev_sa)) {
+ if(!sockaddrcmp_noport(sa, &prev_sa)) {
static time_t samehost_burst;
static time_t samehost_burst_time;
if(samehost_burst > max_connection_burst) {
tarpit(fd);
- return;
+ return true;
}
}
if(connection_burst >= max_connection_burst) {
connection_burst = max_connection_burst;
tarpit(fd);
+ return true;
+ }
+
+ return false;
+}
+
+/*
+ accept a new tcp connect and create a
+ new connection
+*/
+void handle_new_meta_connection(void *data, int flags) {
+ (void)flags;
+ listen_socket_t *l = data;
+ connection_t *c;
+ sockaddr_t sa;
+ int fd;
+ socklen_t len = sizeof(sa);
+
+ fd = accept(l->tcp.fd, &sa.sa, &len);
+
+ if(fd < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
+ return;
+ }
+
+ sockaddrunmap(&sa);
+
+ if(!is_local_connection(&sa) && check_tarpit(&sa, fd)) {
return;
}
assert_int_equal(1234, service_to_port("1234"));
}
+static void test_is_local_connection_ipv4(void **state) {
+ (void)state;
+
+ sockaddr_t sa;
+
+ assert_true(inet_pton(AF_INET, "127.0.0.0", &sa.in.sin_addr));
+ sa.sa.sa_family = AF_INET;
+ assert_true(is_local_connection(&sa));
+
+ assert_true(inet_pton(AF_INET, "127.42.13.5", &sa.in.sin_addr));
+ sa.sa.sa_family = AF_INET;
+ assert_true(is_local_connection(&sa));
+
+ assert_true(inet_pton(AF_INET, "127.255.255.255", &sa.in.sin_addr));
+ sa.sa.sa_family = AF_INET;
+ assert_true(is_local_connection(&sa));
+
+ assert_true(inet_pton(AF_INET, "128.0.0.1", &sa.in.sin_addr));
+ sa.sa.sa_family = AF_INET;
+ assert_false(is_local_connection(&sa));
+}
+
+static void test_is_local_connection_ipv6(void **state) {
+ (void)state;
+
+ sockaddr_t sa;
+
+ assert_true(inet_pton(AF_INET6, "::1", &sa.in6.sin6_addr));
+ sa.sa.sa_family = AF_INET6;
+ assert_true(is_local_connection(&sa));
+
+ assert_true(inet_pton(AF_INET6, "::1:1", &sa.in6.sin6_addr));
+ sa.sa.sa_family = AF_INET6;
+ assert_false(is_local_connection(&sa));
+
+ assert_true(inet_pton(AF_INET6, "fe80::", &sa.in6.sin6_addr));
+ sa.sa.sa_family = AF_INET6;
+ assert_false(is_local_connection(&sa));
+}
+
+static void test_is_local_connection_unix(void **state) {
+ (void)state;
+
+ sockaddr_t sa = {.sa.sa_family = AF_UNIX};
+ assert_true(is_local_connection(&sa));
+}
+
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_service_to_port_invalid),
cmocka_unit_test(test_service_to_port_valid),
+ cmocka_unit_test(test_is_local_connection_ipv4),
+ cmocka_unit_test(test_is_local_connection_ipv6),
+ cmocka_unit_test(test_is_local_connection_unix),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}