mirror of
https://codeberg.org/scip/udpxd.git
synced 2025-12-16 19:40:58 +01:00
initial commit
This commit is contained in:
85
client.c
Normal file
85
client.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
This file is part of udpxd.
|
||||
|
||||
Copyright (C) 2015 T.v.Dein.
|
||||
|
||||
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"
|
||||
|
||||
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! */
|
||||
}
|
||||
|
||||
client_t *client_find_src(struct sockaddr_in *src) {
|
||||
client_t *current = NULL;
|
||||
client_iter(clients, current) {
|
||||
if (current->src == src)
|
||||
return current;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void client_seen(client_t *client) {
|
||||
client->lastseen = (long)time(0);
|
||||
}
|
||||
|
||||
client_t *client_new(int fd, struct sockaddr_in *src, struct sockaddr_in *dst) {
|
||||
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) {
|
||||
if(VERBOSE) {
|
||||
char *srcip = inet_ntoa(client->src->sin_addr);
|
||||
char *bindip = inet_ntoa(client->dst->sin_addr);
|
||||
fprintf(stderr, "closing socket %s:%d for client %s:%d (aged out)\n",
|
||||
srcip, ntohs(client->src->sin_port), bindip, ntohs(client->dst->sin_port));
|
||||
}
|
||||
client_del(client);
|
||||
close(client->socket);
|
||||
free(client->src);
|
||||
free(client->dst);
|
||||
free(client);
|
||||
}
|
||||
|
||||
void client_clean() {
|
||||
uint32_t now = (long)time(0);
|
||||
uint32_t diff;
|
||||
client_t *current;
|
||||
client_iter(clients, current) {
|
||||
diff = now - current->lastseen;
|
||||
if(diff >= MAXAGE) {
|
||||
client_close(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user