2015-04-21 20:09:12 +02:00
|
|
|
/*
|
|
|
|
|
This file is part of udpxd.
|
|
|
|
|
|
2016-09-22 21:37:30 +02:00
|
|
|
Copyright (C) 2015-2016 T.v.Dein.
|
2015-04-21 20:09:12 +02:00
|
|
|
|
|
|
|
|
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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
You can contact me by mail: <tom AT vondein DOT org>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "client.h"
|
2015-04-26 13:26:37 +02:00
|
|
|
#include "log.h"
|
2015-04-21 20:09:12 +02:00
|
|
|
|
|
|
|
|
void client_del(client_t *client) {
|
|
|
|
|
HASH_DEL(clients, client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void client_add(client_t *client) {
|
|
|
|
|
HASH_ADD_INT(clients, socket, client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client_t *client_find_fd(int fd) {
|
|
|
|
|
client_t *client = NULL;
|
|
|
|
|
HASH_FIND_INT(clients, &fd, client);
|
|
|
|
|
return client; /* maybe NULL! */
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 22:03:50 +02:00
|
|
|
client_t *client_find_src(host_t *src) {
|
2015-04-21 20:09:12 +02:00
|
|
|
client_t *current = NULL;
|
|
|
|
|
client_iter(clients, current) {
|
2015-04-24 22:03:50 +02:00
|
|
|
if(strcmp(current->src->ip, src->ip) == 0 && current->src->port == src->port)
|
2015-04-21 20:09:12 +02:00
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void client_seen(client_t *client) {
|
|
|
|
|
client->lastseen = (long)time(0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 22:03:50 +02:00
|
|
|
client_t *client_new(int fd, host_t *src, host_t *dst) {
|
2015-04-21 20:09:12 +02:00
|
|
|
client_t *client = malloc(sizeof(client_t));
|
|
|
|
|
client->socket = fd;
|
|
|
|
|
client->src = src;
|
|
|
|
|
client->dst = dst;
|
|
|
|
|
client_seen(client);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void client_close(client_t *client) {
|
|
|
|
|
client_del(client);
|
|
|
|
|
close(client->socket);
|
2015-04-25 20:14:24 +02:00
|
|
|
host_clean(client->src);
|
|
|
|
|
host_clean(client->dst);
|
2015-04-21 20:09:12 +02:00
|
|
|
free(client);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 20:14:24 +02:00
|
|
|
void client_clean(int asap) {
|
2015-04-21 20:09:12 +02:00
|
|
|
uint32_t now = (long)time(0);
|
|
|
|
|
uint32_t diff;
|
|
|
|
|
client_t *current;
|
|
|
|
|
client_iter(clients, current) {
|
|
|
|
|
diff = now - current->lastseen;
|
2015-04-25 20:14:24 +02:00
|
|
|
if(diff >= MAXAGE || asap) {
|
2015-04-26 13:26:37 +02:00
|
|
|
verbose("closing socket %s:%d for client %s:%d (aged out after %d seconds)\n",
|
2016-09-22 21:37:30 +02:00
|
|
|
current->src->ip, current->src->port, current->dst->ip, current->dst->port, MAXAGE);
|
2015-04-21 20:09:12 +02:00
|
|
|
client_close(current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|