hlswmaster-ng/mod_halflife.cpp

74 lines
1.8 KiB
C++

#include <string.h>
#include "mod_halflife.h"
#include "modhelper.h"
static struct game_ports port_arr[] = {
{ 27015, 27024, ID_HL },
{ 0,0,0 }
};
static const char scanmsg1[] = "\xff\xff\xff\xff""details";
static const char scanmsg2[] = "\xff\xff\xff\xff\x54";
static const char scanmsg3[] = "\xff\xff\xff\xff""TSource Engine Query";
static const char reply1[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x49, 0x07 }; // I.
static const char reply2[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x6D, 0x00 }; // m
void ModHalfLife::scan(MultiSock* msock)
{
ModHelper::send(msock, port_arr, scanmsg1, strlen(scanmsg1));
ModHelper::send(msock, port_arr, scanmsg2, strlen(scanmsg2));
ModHelper::send(msock, port_arr, scanmsg3, strlen(scanmsg3));
}
int ModHalfLife::parse(NetPkt* pkt, GameList* glist)
{
struct in_addr tmp;
int port, count, pos;
if (ModHelper::checkPorts(pkt, port_arr) == 0)
return PARSE_REJECT;
// eat weird connection attempts
if (pkt->getSize() <= 4)
return PARSE_ACCEPT_QUIRK;
/* Halflife2 answer */
if (pkt->compare(0, reply1, sizeof(reply1))) {
glist->addGame(ID_HL2, pkt);
return PARSE_ACCEPT;
}
/* Halflife1 short answer */
if (pkt->compare(0, reply2, sizeof(reply2))) {
glist->addGame(ID_HL, pkt);
return PARSE_ACCEPT;
}
/* Halflife1 answer (with IP:port) */
if (!pkt->compare(0, reply2, sizeof(reply2) -1))
return PARSE_REJECT;
/* try to parse server IP */
pos = 5;
if ((count = pkt->parse_ip(pos, &tmp)) < 7)
return PARSE_REJECT;
/* parse server port */
pos += count +1;
if ((count = pkt->parse_int(pos, &port)) == 0)
return PARSE_REJECT;
// IP is 0.0.0.0 or 127.0.0.1 -> do not use IP info
if (tmp.s_addr == 0x00000000 || tmp.s_addr == 0x0100007F) {
glist->addGame(ID_HL, pkt);
} else {
// TODO: implement
glist->addGame(ID_HL, pkt);
//glist->add(ID_HL, &tmp, port, false);
}
return PARSE_ACCEPT;
}