hlswmaster/p_hlswproxy.c

111 lines
3.3 KiB
C
Raw Normal View History

2006-02-02 16:27:19 +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. *
***************************************************************************/
2006-11-24 21:53:25 +01:00
#include <stdlib.h>
2006-02-02 16:27:19 +01:00
#include <string.h>
2006-11-24 21:53:25 +01:00
#include <netinet/in.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
2006-02-02 16:27:19 +01:00
2006-11-24 21:53:25 +01:00
#include "netpkt.h"
#include "plugin.h"
#include "plugin_helper.h"
2006-11-24 21:53:25 +01:00
#include "logging.h"
#include "scanner.h"
2006-02-02 16:27:19 +01:00
2006-11-24 21:53:25 +01:00
struct game_entry {
uint16_t gameid;
uint32_t ip;
uint16_t port1;
uint16_t port2;
2006-02-02 16:27:19 +01:00
} __attribute__ ((packed));
2006-11-24 21:53:25 +01:00
struct master_entry {
struct list_head list;
struct sockaddr_in addr;
};
static LIST_HEAD(master_list);
static char scanmsg[] = "\xff\xff\xff\xffHLSWLANSEARCH";
static int scan(void)
{
2006-11-24 21:53:25 +01:00
struct master_entry *entry;
list_for_each_entry(entry, &master_list, list)
pkt_send(&entry->addr.sin_addr, ntohs(entry->addr.sin_port), scanmsg, sizeof(scanmsg));
2006-02-02 16:27:19 +01:00
return 1;
}
static int parse(struct net_pkt *pkt)
{
2006-11-24 21:53:25 +01:00
// TODO: check against master_list
2006-02-02 16:27:19 +01:00
if (pkt_getport(pkt) != 7140)
return PARSE_REJECT;
2006-02-02 16:27:19 +01:00
2006-11-24 21:53:25 +01:00
struct game_entry *game = (void *)pkt->buf + sizeof(scanmsg);
2006-02-02 16:27:19 +01:00
2006-11-24 21:53:25 +01:00
while ((void *)game < ((void *)pkt->buf + pkt->size)) {
server_add(game->gameid, game->ip, game->port1, game->port2);
game++;
2006-02-02 16:27:19 +01:00
}
2006-11-24 21:53:25 +01:00
return PARSE_ACCEPT;
2006-02-02 16:27:19 +01:00
}
2006-11-24 21:53:25 +01:00
static int init_callback(const char *value, void *privdata)
{
2006-11-24 21:53:25 +01:00
struct master_entry *entry = malloc(sizeof(struct master_entry));
if (entry == NULL) {
log_print(LOG_ERROR, "hlswproxy_init(): out of memory");
return -1;
}
2006-11-24 21:53:25 +01:00
if (parse_saddr(value, &entry->addr) != 0) {
log_print(LOG_WARN, " invalid dst: %s", value);
free(entry);
return -1;
}
log_print(LOG_INFO, " adding remote master %s:%d",
inet_ntoa(entry->addr.sin_addr), ntohs(entry->addr.sin_port));
list_add(&entry->list, &master_list);
return 0;
}
2006-11-24 21:53:25 +01:00
static int init(void)
{
2006-11-24 21:53:25 +01:00
if (config_get_strings("hlswproxy", "scan", init_callback, NULL) <= 0)
return -1;
return 0;
}
struct hlswmaster_plugin plugin = {
2006-02-02 16:27:19 +01:00
.name = "hlswproxy",
.scan = &scan,
.parse = &parse,
.init = &init,
2006-02-02 16:27:19 +01:00
};