hlswmaster/src/plugin_helper.c

137 lines
4.4 KiB
C
Raw Normal View History

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>
#include <string.h>
#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);
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;
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;
return memcmp(pkt->buf, search, size);
}
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) {
return server_add(gameid, &pkt->addr.sin_addr, pkt->addr.sin_port, 0);
2006-02-02 16:24:06 +01:00
}
2006-02-02 16:25:55 +01:00