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: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 "plugin.h"
|
|
|
|
#include "netpkt.h"
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* pkt_queue()
|
|
|
|
* erzeugt ein net_pkt aus den parametern und legt es in die send queue
|
|
|
|
*
|
|
|
|
* @param struct in_addr *dstip - pointer auf eine IP, wenn NULL wird broadcast angenommen
|
|
|
|
* @param unsigned int dstport - destination port
|
|
|
|
* @param char *buf - pointer auf den zu sendenen speicherbereich
|
|
|
|
* @param unsigned int size - groesse des speicherbereichs
|
|
|
|
* @return false bei fehler
|
|
|
|
*/
|
|
|
|
int pkt_send(struct in_addr *dstip, unsigned int dstport, char *buf, unsigned int size) {
|
2006-02-02 16:24:06 +01:00
|
|
|
struct net_pkt *pkt;
|
|
|
|
|
|
|
|
if (!(pkt = malloc(sizeof(struct net_pkt) + size)))
|
2006-02-02 16:25:55 +01:00
|
|
|
return 0;
|
2006-02-02 16:24:06 +01:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&pkt->list);
|
|
|
|
|
|
|
|
pkt->addr.sin_family = AF_INET;
|
|
|
|
pkt->addr.sin_port = htons(dstport);
|
2006-02-02 16:25:55 +01:00
|
|
|
pkt->addr.sin_addr.s_addr = (dstip ? dstip->s_addr : 0xFFFFFFFF);
|
2006-02-02 16:24:06 +01:00
|
|
|
|
|
|
|
pkt->size = size;
|
|
|
|
memcpy(pkt->buf, buf, size);
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
pkt_queue(pkt);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_queue_portarr()
|
|
|
|
* erzeugt ein net_pkt aus den parametern und legt es in die send queue
|
|
|
|
*
|
|
|
|
* @param char *dstip - pointer auf eine IP, wenn NULL wird broadcast angenommen
|
|
|
|
* @param struct scan_ports *port_arr
|
|
|
|
* @param char *buf - pointer auf den zu sendenen speicherbereich
|
|
|
|
* @param unsigned int size - groesse des speicherbereichs
|
|
|
|
* @return false bei fehler
|
|
|
|
*/
|
|
|
|
int pkt_send_portarr(struct in_addr *dstip, struct scan_ports *portarr, char *buf, unsigned int size) {
|
|
|
|
unsigned short port;
|
|
|
|
while (portarr && portarr->portlo) {
|
|
|
|
for (port = portarr->portlo; port <= portarr->porthi; port++)
|
|
|
|
pkt_send(dstip, port, buf, size);
|
2006-02-02 16:27:19 +01:00
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
portarr++;
|
|
|
|
}
|
|
|
|
return 1;
|
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
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @param struct scan_ports *port_arr
|
|
|
|
* @return die gameid der portrange oder 0 wenn nicht vorhanden
|
|
|
|
*/
|
|
|
|
int pkt_check_portarr(struct net_pkt *pkt, struct scan_ports *portarr) {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @param unsigned int offset
|
|
|
|
* @param char *search
|
|
|
|
* @param unsigned int size
|
|
|
|
* @return true wenn gleich
|
|
|
|
*/
|
2006-02-02 16:24:06 +01:00
|
|
|
int pkt_memcmp(struct net_pkt *pkt, unsigned int offset, char *search, unsigned int size) {
|
|
|
|
|
|
|
|
if (offset >= pkt->size)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @param unsigned int offset
|
|
|
|
* @param char *search
|
|
|
|
* @param unsigned int size
|
|
|
|
* @return pointer auf den string im Paket
|
|
|
|
*/
|
|
|
|
void * pkt_memmem(struct net_pkt *pkt, unsigned int offset, char *search, unsigned int size) {
|
|
|
|
if (offset >= pkt->size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return memmem(pkt->buf + offset, pkt->size, search, size);
|
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
|
|
|
|
*
|
|
|
|
* @param unsigned int gameid
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @return false bei fehler
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @return pointer auf String
|
|
|
|
*/
|
|
|
|
char * pkt_ntoa(struct net_pkt *pkt) {
|
|
|
|
return inet_ntoa(pkt->addr.sin_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_getport()
|
|
|
|
* gibt den Port des Pakets als short zurueck
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @return portnr
|
|
|
|
*/
|
|
|
|
unsigned short pkt_getport(struct net_pkt *pkt) {
|
|
|
|
return ntohs(pkt->addr.sin_port);
|
|
|
|
}
|
2006-02-02 16:31:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* pkt_atoi()
|
|
|
|
* gibt den integer in dem paket ab der position zurueck
|
|
|
|
*
|
|
|
|
* @param struct net_pkt *pkt
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
int pkt_atoi(struct net_pkt *pkt, void *p) {
|
|
|
|
/* check buffer bounds */
|
|
|
|
if ((void *)pkt->buf > p || ((void *)pkt->buf + pkt->size) < p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// TODO: kann ueber paketlaenge lesen
|
|
|
|
return atoi(p);
|
|
|
|
}
|