hlswmaster-ng/hlswserver.cpp

79 lines
1.7 KiB
C++

#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "config.h"
#include "logging.h"
#include "hlswserver.h"
#define HLSW_HEADER "\xFF\xFF\xFF\xFFHLSWLANSEARCH\x00"
#define HLSW_HEADER_LEN 0x12
#define HLSW_MASTER_PORT 7140
HlswServer::HlswServer(Config& conf, GameList& slist)
{
struct sockaddr_in dst;
char *ip;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
LogSystem::log(LOG_CRIT, "HlswServer(): socket()");
return;
}
ip = conf.getString("global", "master_ip", "0.0.0.0");
LogSystem::log(LOG_NOTICE, "HlswServer listen on %s:%d", ip, HLSW_MASTER_PORT);
dst.sin_family = AF_INET;
dst.sin_port = htons(HLSW_MASTER_PORT);
inet_aton(ip, &dst.sin_addr);
if (bind(sock, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
LogSystem::log(LOG_WARNING, "HlswServer(): bind()");
return;
}
out = new HlswPacket();
}
HlswServer::~HlswServer()
{
close(sock);
}
int HlswServer::execute(void* arg)
{
struct sockaddr_in src;
unsigned char buf[32];
socklen_t len;
int ret;
while (1) {
/* auf clientanfrage warten */
len = sizeof(src);
ret = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&src, &len);
if (ret != HLSW_HEADER_LEN) {
LogSystem::log(LOG_WARNING, "HlswServer: invalid packet");
continue;
}
/* testen ob es sich um ein HLSW anforderung handelt */
if (memcmp(buf, HLSW_HEADER, HLSW_HEADER_LEN)) {
LogSystem::log(LOG_WARNING, "HlswServer: not a hlsw packet");
continue;
}
if (out != NULL && out->count > 0) {
HlswPacket* pkt;
for (pkt = out; pkt != NULL; pkt = pkt->next) {
sendto(sock, pkt->data, pkt->getSize(), 0,
(struct sockaddr *)&src, sizeof(src));
}
}
}
return 0;
}