hlswmaster/scanner.c

402 lines
9.6 KiB
C
Raw Normal View History

2006-02-02 16:24:06 +01:00
/***************************************************************************
* Copyright (C) 03/2005 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* 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 2 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <inttypes.h>
2006-02-02 16:24:06 +01:00
#include "list.h"
#include "netpkt.h"
#include "configfile.h"
2006-02-02 16:24:06 +01:00
#include "hlswmaster.h"
#include "plugin.h"
#include "plugin_helper.h"
2006-02-02 16:24:06 +01:00
#define DEBUG 1
2006-02-02 16:24:06 +01:00
struct rx_entry {
struct list_head list;
struct net_pkt *pkt;
};
/** rx packet liste */
static LIST_HEAD(rxlist);
/** sichert die rx liste */
static pthread_mutex_t rxlist_lock = PTHREAD_MUTEX_INITIALIZER;
/** anzahl der pakete in der rxlist */
static sem_t rxlist_sem;
/** interne serverliste */
static LIST_HEAD(serverlist);
/** sichert die interne serverliste */
static pthread_mutex_t serverlist_lock = PTHREAD_MUTEX_INITIALIZER;
/** scan socket */
2006-02-02 16:24:06 +01:00
static int scan_sock;
2006-02-02 16:25:55 +01:00
/**
* server_cmp()
* LIST_FIND helper
*
* @param *a gameserver 1
* @param *b gameserver 2
* @return true wenn es sich um den selben server handelt
2006-02-02 16:25:55 +01:00
*/
static inline int server_cmp(const struct game_server *a, struct game_server *b)
{
return (a->gameid == b->gameid && a->ip == b->ip &&
a->port1 == b->port1 && a->port2 == b->port2);
2006-02-02 16:24:06 +01:00
}
2006-02-02 16:25:55 +01:00
/**
* server_add()
* wenn der server noch nicht in der internen liste vorhanden ist
* wird er hinzugefuegt, ansonsten nur die modtime angepasst
*
* @param gameid gameid des servers
* @param ip ip des servers
* @param port1 erster port
* @param port2 zweiter port
* @return false bei fehler
*/
int server_add(uint16_t gameid, uint32_t ip, uint16_t port1, uint16_t port2)
{
struct game_server server, *nserver;
server.gameid = gameid;
server.ip = ip;
server.port1 = port1;
server.port2 = port2;
pthread_mutex_lock(&serverlist_lock);
/* diesen server in der liste suchen */
nserver = LIST_FIND(&serverlist, server_cmp, struct game_server *, &server);
if (!nserver) {
/* neuen eintrag anlegen */
if (!(nserver = malloc(sizeof(struct game_server)))) {
pthread_mutex_unlock(&serverlist_lock);
log_print("server_add(): malloc()");
return 0;
}
#ifdef DEBUG
{
struct in_addr tmp;
tmp.s_addr = server.ip;
printf("server_add_new: gameid=%2d ip=%15s port1=%5d port2=%5d\n",
server.gameid, inet_ntoa(tmp), server.port1, server.port2);
}
#endif
memcpy(nserver, &server, sizeof(struct game_server));
list_add_tail(&nserver->list, &serverlist);
}
/* modtime anpassen */
nserver->modtime = time(NULL);
pthread_mutex_unlock(&serverlist_lock);
return 1;
}
/**
* serverlist_refresh()
* loescht alte server aus der liste
* baut aus den verbleibenden die client_liste auf
*
* @param timeout timeout in sekunden
*/
static void serverlist_refresh(long timeout)
{
struct game_server *server, *tmp;
long now = time(NULL);
pthread_mutex_lock(&serverlist_lock);
list_for_each_entry_safe(server, tmp, &serverlist, list) {
if ((server->modtime + timeout) < now) {
#ifdef DEBUG
{
struct in_addr tmp2;
tmp2.s_addr = server->ip;
printf("server timeout: gameid=%2d ip=%15s port1=%5d port2=%5d\n",
server->gameid, inet_ntoa(tmp2), server->port1, server->port2);
}
#endif
list_del(&server->list);
free(server);
} else {
client_pkt_add(server);
}
}
pthread_mutex_unlock(&serverlist_lock);
client_pkt_commit();
}
/**
* pkt_send()
* schickt ein paket ab
2006-02-02 16:25:55 +01:00
*
* @param *dstip destination IP, wenn NULL wird broadcast angenommen
* @param dstport destination port
* @param *buf datenbereich der gesendet wird
* @param size groesse der daten
* @return false bei fehler
*/
int pkt_send(struct in_addr *dstip, unsigned int dstport, char *buf, unsigned int size)
{
struct sockaddr_in addr;
int ret = 1;
addr.sin_family = AF_INET;
addr.sin_port = htons(dstport);
addr.sin_addr.s_addr = (dstip ? dstip->s_addr : 0xFFFFFFFF);
if (sendto(scan_sock, buf, size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
log_print("scan_send(): sendto()");
ret = 0;
}
usleep(10000);
return ret;
}
/**
* scan_receive_real()
* arbeitet die receive queue ab
*/
void scan_receive_real(void)
{
struct rx_entry *rx;
struct net_pkt *pkt;
int cnt, retval;
do {
sem_wait(&rxlist_sem);
pthread_mutex_lock(&rxlist_lock);
rx = list_entry(rxlist.next, struct rx_entry, list);
list_del(&rx->list);
pthread_mutex_unlock(&rxlist_lock);
pkt = rx->pkt;
free(rx);
retval = plugins_parse(pkt);
switch (retval) {
case PARSE_REJECT:
log_print("scan_receive(): unknown packet: %s:%d size:%d",
inet_ntoa(pkt->addr.sin_addr),
ntohs(pkt->addr.sin_port),
pkt->size);
#if 0
{
char *p = pkt_print(pkt);
log_print("%s", p);
free(p);
}
#endif
case PARSE_ACCEPT:
free(pkt);
case PARSE_ACCEPT_FREED:
break;
}
sem_getvalue(&rxlist_sem, &cnt);
} while (cnt > 0);
}
/**
* scan_control()
* triggert den gc der plugins
* triggert den serverlist_refresh
* triggert den scan der plugins
* arbeitet die rx queue ab
2006-02-02 16:25:55 +01:00
*/
void scan_control(void)
{
long last_plugin_gc = 0, last_list_refresh = 0, last_scan = 0;
int plugin_timeout, list_timeout, list_refresh, scan_interval;
int cnt;
long now;
plugin_timeout = config_get_int("global", "plugin_timeout", 30);
list_timeout = config_get_int("global", "serverlist_timeout", 30);
list_refresh = config_get_int("global", "serverlist_refresh", 5);
scan_interval = config_get_int("global", "scan_interval", 30);
2006-02-02 16:24:06 +01:00
log_print("thread_start: scan_control");
log_print(" scan_interval: %ds", scan_interval);
log_print(" serverlist_refresh: %ds", list_refresh);
log_print(" serverlist_timeout: %ds", list_timeout);
log_print(" plugin_timeout: %d", plugin_timeout);
2006-02-02 16:24:06 +01:00
while (1) {
now = time(NULL);
/* interne plugin daten aufraeumen */
if (last_plugin_gc + plugin_timeout < now) {
last_plugin_gc = now;
plugins_gc(plugin_timeout);
}
2006-02-02 16:24:06 +01:00
/* server liste aufraeumen, und neue client pkts erzeugen */
if (last_list_refresh + list_refresh < now) {
last_list_refresh = now;
serverlist_refresh(list_timeout);
}
/* neuen scan ausloesen */
if (last_scan + scan_interval < now) {
last_scan = now;
plugins_scan();
2006-02-02 16:24:06 +01:00
}
/* empfangene daten parsen */
sem_getvalue(&rxlist_sem, &cnt);
if (cnt > 0)
scan_receive_real();
usleep(500000);
2006-02-02 16:24:06 +01:00
}
}
2006-02-02 16:25:55 +01:00
/**
* scan_receive()
* wartet auf serverantworten und uebergibt die pakete der
* receive queue
2006-02-02 16:25:55 +01:00
*/
void scan_receive(void)
{
2006-02-02 16:24:06 +01:00
struct net_pkt *pkt;
struct rx_entry *rx;
2006-02-02 16:24:06 +01:00
fd_set fdsel, fdcpy;
int recvsize, i;
log_print("thread_start: scan_receiver");
2006-02-02 16:24:06 +01:00
FD_ZERO(&fdsel);
FD_SET(scan_sock, &fdsel);
while (1) {
memcpy(&fdcpy, &fdsel, sizeof(fdsel));
select(FD_SETSIZE, &fdcpy, NULL, NULL, NULL);
if (ioctl(scan_sock, FIONREAD, &recvsize) == -1) {
log_print("scan_receive(): ioctl()");
2006-02-02 16:24:06 +01:00
continue;
2006-02-02 16:31:52 +01:00
}
2006-02-02 16:31:52 +01:00
if (!(pkt = malloc(sizeof(struct net_pkt) + recvsize))) {
log_print("scan_receive(): malloc()");
2006-02-02 16:24:06 +01:00
continue;
2006-02-02 16:31:52 +01:00
}
2006-02-02 16:24:06 +01:00
i = sizeof(struct sockaddr_in);
pkt->size = recvfrom(scan_sock, pkt->buf, recvsize, 0, (struct sockaddr *)&pkt->addr, &i);
if (pkt->size == 0) {
free(pkt);
continue;
2006-02-02 16:34:11 +01:00
}
rx = malloc(sizeof(struct rx_entry));
rx->pkt = pkt;
2006-02-02 16:34:11 +01:00
pthread_mutex_lock(&rxlist_lock);
list_add_tail(&rx->list, &rxlist);
pthread_mutex_unlock(&rxlist_lock);
sem_post(&rxlist_sem);
2006-02-02 16:24:06 +01:00
}
}
/**
* schliesst den server scan socket
*/
static void scan_close()
{
close(scan_sock);
sem_destroy(&rxlist_sem);
}
2006-02-02 16:25:55 +01:00
/**
* scan_init()
* initialisiert den socket fuer den server scan
* initialisiert die rxlist semaphore
*
2006-02-02 16:25:55 +01:00
* @return false on error
*/
int scan_init()
{
2006-02-02 16:24:06 +01:00
struct sockaddr_in dst;
int i = 1, port;
char *ip;
2006-02-02 16:24:06 +01:00
if ((scan_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
log_print("scan_init(): socket()");
return 0;
}
ip = config_get_string("global", "scan_ip", "0.0.0.0");
port = config_get_int("global", "scan_port", 7130);
2006-02-02 16:24:06 +01:00
dst.sin_family = AF_INET;
dst.sin_port = htons(port);
inet_aton(ip, &dst.sin_addr);
2006-02-02 16:24:06 +01:00
if (bind(scan_sock, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
log_print("scan_init(): bind()");
return 0;
}
if (setsockopt(scan_sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i))) {
log_print("scan_init(): setsockopt()");
return 0;
}
if (atexit(scan_close) != 0) {
log_print("scan_init(): atexit()");
return 0;
}
2006-02-02 16:24:06 +01:00
log_print("scan socket initialized (%s:%d)", ip, port);
sem_init(&rxlist_sem, 0, 0);
2006-02-02 16:24:06 +01:00
return 1;
}