hlswmaster/src/plugin_helper.c

202 lines
5.7 KiB
C

/***************************************************************************
* 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. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define __USE_GNU
#include <string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "hlswmaster.h"
#include "plugin.h"
#include "netpkt.h"
/**
* 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) {
struct net_pkt *pkt;
if (!(pkt = malloc(sizeof(struct net_pkt) + size)))
return 0;
INIT_LIST_HEAD(&pkt->list);
pkt->addr.sin_family = AF_INET;
pkt->addr.sin_port = htons(dstport);
pkt->addr.sin_addr.s_addr = (dstip ? dstip->s_addr : 0xFFFFFFFF);
pkt->size = size;
memcpy(pkt->buf, buf, size);
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);
portarr++;
}
return 1;
}
/**
* 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;
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
*/
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;
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);
}
/**
* 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) {
return server_add(gameid, pkt->addr.sin_addr.s_addr, ntohs(pkt->addr.sin_port), 0);
}
/**
* 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);
}
/**
* 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) {
int val = 0;
void *max = ((void *)pkt->buf + pkt->size);
char *c = p;
/* check lower bounds */
if ((void *)pkt->buf > p || p > max)
return 0;
while (isdigit(*c) && (void *)c < max)
val = (val * 10) + (*c++ - 0x30);
return val;
}