2006-02-02 16:25:55 +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-02-02 16:24:06 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2006-02-02 16:27:19 +01:00
|
|
|
|
|
|
|
#define __USE_GNU
|
2006-02-02 16:24:06 +01:00
|
|
|
#include <string.h>
|
2006-02-02 16:27:19 +01:00
|
|
|
|
2006-02-02 16:34:11 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
#include "hlswmaster.h"
|
2006-02-02 16:24:06 +01:00
|
|
|
#include "netpkt.h"
|
2006-02-02 16:43:41 +01:00
|
|
|
#include "plugin.h"
|
|
|
|
#include "plugin_helper.h"
|
2006-02-02 16:25:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_queue_portarr()
|
2006-02-02 16:44:46 +01:00
|
|
|
* sendet an eine reihe von ports
|
2006-02-02 16:25:55 +01:00
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *dstip destination IP, wenn NULL wird broadcast angenommen
|
|
|
|
* @param *portarr ports an die gesendet wird
|
|
|
|
* @param *buf daten die gesendet werden
|
|
|
|
* @param size groesse der daten
|
2006-02-02 16:25:55 +01:00
|
|
|
* @return false bei fehler
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int pkt_send_portarr(struct in_addr *dstip, struct scan_ports *portarr, char *buf, unsigned int size)
|
|
|
|
{
|
2006-02-02 16:25:55 +01:00
|
|
|
unsigned short port;
|
2006-02-02 16:44:46 +01:00
|
|
|
int ret = 1;
|
2006-02-02 16:47:20 +01:00
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
while (portarr && portarr->portlo) {
|
|
|
|
for (port = portarr->portlo; port <= portarr->porthi; port++)
|
2006-02-02 16:44:46 +01:00
|
|
|
if (!pkt_send(dstip, port, buf, size))
|
|
|
|
ret = 0;
|
2006-02-02 16:27:19 +01:00
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
portarr++;
|
|
|
|
}
|
2006-02-02 16:44:46 +01:00
|
|
|
return ret;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* pkt_check_portarr()
|
|
|
|
* prueft ob der src-port des pakets in der portliste vorhanden ist
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt paket vom gameserver
|
|
|
|
* @param *portarr ports die angenommen werden
|
2006-02-02 16:25:55 +01:00
|
|
|
* @return die gameid der portrange oder 0 wenn nicht vorhanden
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int pkt_check_portarr(struct net_pkt *pkt, struct scan_ports *portarr)
|
|
|
|
{
|
2006-02-02 16:25:55 +01:00
|
|
|
unsigned short port;
|
|
|
|
while (portarr && portarr->portlo) {
|
|
|
|
for (port = portarr->portlo; port <= portarr->porthi; port++)
|
|
|
|
if (port == ntohs(pkt->addr.sin_port))
|
|
|
|
return portarr->gameid;
|
2006-02-02 16:27:19 +01:00
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
portarr++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_memcmp()
|
|
|
|
* vergleicht das Paket mit einem Speicherbereich
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt paket vom gameserver
|
|
|
|
* @param offset offset ab dem verglichen wird
|
|
|
|
* @param *search daten nach denen gesucht wird
|
|
|
|
* @param size laenge der daten
|
2006-02-02 16:47:20 +01:00
|
|
|
* @return false wenn gleich
|
2006-02-02 16:44:46 +01:00
|
|
|
*
|
|
|
|
* @todo return false wenn offset + size >= pkt->size ?
|
2006-02-02 16:25:55 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int pkt_memcmp(struct net_pkt *pkt, unsigned int offset, char *search, unsigned int size)
|
|
|
|
{
|
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
if (offset >= pkt->size)
|
|
|
|
return 1;
|
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/* nicht ueber das paket hinaus vergleichen */
|
2006-02-02 16:24:06 +01:00
|
|
|
if (offset + size >= pkt->size)
|
|
|
|
size = pkt->size - offset;
|
|
|
|
|
2006-02-02 16:27:19 +01:00
|
|
|
return memcmp(pkt->buf + offset, search, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_memmem()
|
|
|
|
* sucht einen Speicherbereich in dem Paket
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt paket vom gameserver
|
|
|
|
* @param offset offset ab dem gesucht wird
|
|
|
|
* @param *search daten nach denen gesucht wird
|
|
|
|
* @param size laenge der daten
|
2006-02-02 16:47:20 +01:00
|
|
|
* @return offset auf den string im Paket, oder -1 wenn nicht gefunden
|
2006-02-02 16:27:19 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int pkt_memmem(struct net_pkt *pkt, unsigned int offset, char *search, unsigned int size)
|
|
|
|
{
|
|
|
|
void *found;
|
|
|
|
|
2006-02-02 16:27:19 +01:00
|
|
|
if (offset >= pkt->size)
|
2006-02-02 16:47:20 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
found = memmem(pkt->buf + offset, pkt->size, search, size);
|
|
|
|
|
|
|
|
return (found == NULL) ? -1 : (found - (void *)pkt->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_merge()
|
|
|
|
* fuegt zwei pakete zu einem zusammen
|
|
|
|
* die header werden vom ersten Paket uebernommen
|
|
|
|
* die pakete werden nicht gefreed(!)
|
|
|
|
*
|
|
|
|
* @param *pkt1 erstes Paket
|
|
|
|
* @param *pkt2 zweites Paket
|
|
|
|
* @return zusammengefasstes Paket
|
|
|
|
*/
|
|
|
|
struct net_pkt * pkt_merge(struct net_pkt *pkt1, struct net_pkt *pkt2)
|
|
|
|
{
|
|
|
|
struct net_pkt *ret;
|
|
|
|
ret = malloc(sizeof(struct net_pkt) + pkt1->size + pkt2->size);
|
2006-02-02 16:27:19 +01:00
|
|
|
|
2006-02-02 16:47:20 +01:00
|
|
|
memcpy(&ret->addr, &pkt1->addr, sizeof(ret->addr));
|
|
|
|
ret->size = pkt1->size + pkt2->size;
|
|
|
|
|
|
|
|
memcpy(ret->buf, pkt1->buf, pkt1->size);
|
|
|
|
memcpy(ret->buf + pkt1->size, pkt2->buf, pkt2->size);
|
|
|
|
|
|
|
|
return ret;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* server_add_pkt()
|
|
|
|
* fuegt der serverliste einen server hinzu
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param gameid gameid des gameservers
|
|
|
|
* @param *pkt daten vom gameserver (fuer ip/port)
|
2006-02-02 16:25:55 +01:00
|
|
|
* @return false bei fehler
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int server_add_pkt(unsigned int gameid, struct net_pkt *pkt)
|
|
|
|
{
|
2006-02-02 16:27:19 +01:00
|
|
|
return server_add(gameid, pkt->addr.sin_addr.s_addr, ntohs(pkt->addr.sin_port), 0);
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
2006-02-02 16:25:55 +01:00
|
|
|
|
2006-02-02 16:27:19 +01:00
|
|
|
/**
|
|
|
|
* pkt_ntoa()
|
|
|
|
* gibt die IP des Pakets als String zurueck
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt daten vom gameserver
|
2006-02-02 16:27:19 +01:00
|
|
|
* @return pointer auf String
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
char * pkt_ntoa(struct net_pkt *pkt)
|
|
|
|
{
|
2006-02-02 16:27:19 +01:00
|
|
|
return inet_ntoa(pkt->addr.sin_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_getport()
|
|
|
|
* gibt den Port des Pakets als short zurueck
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt daten vom gameserver
|
|
|
|
* @return portnummer
|
2006-02-02 16:27:19 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
unsigned short pkt_getport(struct net_pkt *pkt)
|
|
|
|
{
|
2006-02-02 16:27:19 +01:00
|
|
|
return ntohs(pkt->addr.sin_port);
|
|
|
|
}
|
2006-02-02 16:31:52 +01:00
|
|
|
|
|
|
|
/**
|
2006-02-02 16:47:20 +01:00
|
|
|
* pkt_sameaddr()
|
|
|
|
* vergleicht die Adressen zweier Pakete
|
|
|
|
*
|
|
|
|
* @param *pkt1 Paket 1
|
|
|
|
* @param *pkt2 Paket 2
|
|
|
|
* @return true wenn Adressen gleich
|
|
|
|
*/
|
|
|
|
int pkt_sameaddr(struct net_pkt *pkt1, struct net_pkt *pkt2)
|
|
|
|
{
|
|
|
|
return (pkt1->addr.sin_addr.s_addr == pkt2->addr.sin_addr.s_addr) &&
|
|
|
|
(pkt1->addr.sin_port == pkt2->addr.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_parse_int()
|
2006-02-02 16:44:46 +01:00
|
|
|
* gibt die dezimalzahl in dem paket ab einer position zurueck
|
2006-02-02 16:31:52 +01:00
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt daten vom gameserver
|
2006-02-02 16:47:20 +01:00
|
|
|
* @param offset offset auf den begin des integers
|
|
|
|
* @param *val pointer auf das ergebnis
|
|
|
|
* @return Anzahl der gelesenen Zeichen oder 0 bei fehler
|
2006-02-02 16:31:52 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int pkt_parse_int(struct net_pkt *pkt, unsigned int offset, int *val)
|
|
|
|
{
|
|
|
|
unsigned char *max = pkt->buf + pkt->size;
|
|
|
|
unsigned char *c = pkt->buf + offset;
|
2006-02-02 16:34:11 +01:00
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/* untere grenze abtesten */
|
2006-02-02 16:47:20 +01:00
|
|
|
if (pkt->buf > c || c > max)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*val = 0;
|
2006-02-02 16:31:52 +01:00
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/* ziffern einlesen */
|
2006-02-02 16:47:20 +01:00
|
|
|
while (isdigit(*c) && c < max)
|
|
|
|
*val = (*val * 10) + (*c++ - 0x30);
|
2006-02-02 16:34:11 +01:00
|
|
|
|
2006-02-02 16:47:20 +01:00
|
|
|
return (c - (pkt->buf + offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_parse_ip()
|
|
|
|
* gibt die IP in *.*.*.* ab p als integer zurueck
|
|
|
|
*
|
|
|
|
* @param *pkt daten vom gameserver
|
|
|
|
* @param offset offset auf den begin der IP
|
|
|
|
* @param *ip pointer auf eine in_addr struct
|
|
|
|
* @return Anzahl der gelesenen Zeichen oder 0 bei Fehler
|
|
|
|
*/
|
|
|
|
int pkt_parse_ip(struct net_pkt *pkt, int offset, struct in_addr *ip)
|
|
|
|
{
|
|
|
|
int i, tmp, count, pos = offset;
|
|
|
|
ip->s_addr = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
count = pkt_parse_int(pkt, pos, &tmp);
|
|
|
|
pos += count;
|
|
|
|
if (count == 0 || tmp < 0 || tmp > 255)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ip->s_addr = ip->s_addr>>8 | tmp<<24;
|
|
|
|
|
|
|
|
if (i != 3 && pkt->buf[pos++] != '.')
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return pos - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_print()
|
|
|
|
* gibt ein paket als hex-dump aus
|
|
|
|
*
|
|
|
|
* @param *pkt daten vom gameserver
|
|
|
|
* @return char pointer auf hex-dump
|
|
|
|
*/
|
|
|
|
char * pkt_print(struct net_pkt *pkt)
|
|
|
|
{
|
|
|
|
int pos = 0, i = 0, j;
|
|
|
|
char *buf = malloc(pkt->size * 4 + 64);
|
|
|
|
|
|
|
|
while (pos < pkt->size) {
|
|
|
|
i += sprintf(buf + i, "%04X: ", pos);
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
if (pos + j < pkt->size)
|
|
|
|
i += sprintf(buf + i, "%02X", pkt->buf[pos + j]);
|
|
|
|
else
|
|
|
|
i += sprintf(buf + i, " ");
|
|
|
|
|
|
|
|
if (j % 2)
|
|
|
|
buf[i++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
if (pos + j < pkt->size) {
|
|
|
|
unsigned char val = pkt->buf[pos + j];
|
|
|
|
if (val >= 0x20 && val < 0x80)
|
|
|
|
buf[i++] = val;
|
|
|
|
else
|
|
|
|
buf[i++] = '.';
|
|
|
|
} else {
|
|
|
|
buf[i++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += 16;
|
|
|
|
buf[i++] = '\r';
|
|
|
|
buf[i++] = '\n';
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
|
|
|
return buf;
|
2006-02-02 16:31:52 +01:00
|
|
|
}
|
2006-02-02 16:34:11 +01:00
|
|
|
|