hlswmaster/src/scanner.c

155 lines
4.1 KiB
C
Raw Normal View History

2006-02-02 16:24:06 +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. *
***************************************************************************/
#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>
#include <sys/ioctl.h>
#include <pthread.h>
#include "list.h"
#include "netpkt.h"
#include "hlswmaster.h"
LIST_HEAD(send_queue);
static int scan_sock;
2006-02-02 16:25:55 +01:00
/**
* _pkt_queue()
* packt ein paket in die sender queue
2006-02-02 16:27:19 +01:00
* @param struct net_pkt *pkt
2006-02-02 16:25:55 +01:00
*/
2006-02-02 16:24:06 +01:00
void pkt_queue(struct net_pkt *pkt) {
list_add_tail(&pkt->list, &send_queue);
}
2006-02-02 16:25:55 +01:00
/**
* scan_transmit()
* triggert den scan der plugins
* arbeitet die sender queue ab
*
* TODO: ratelimiting
* TODO: interval als parameter (config file?)
*/
2006-02-02 16:24:06 +01:00
void scan_transmit(void) {
struct net_pkt *pkt, *tmp;
while (1) {
plugins_scan();
list_for_each_entry_safe(pkt, tmp, &send_queue, list) {
if (sendto(scan_sock, pkt->buf, pkt->size, 0, (struct sockaddr *)&pkt->addr, sizeof(pkt->addr)) < 0)
log_print("scan_transmit(): sendto()");
list_del(&pkt->list);
free(pkt);
2006-02-02 16:31:52 +01:00
usleep(10000);
2006-02-02 16:24:06 +01:00
}
sleep(30);
}
}
2006-02-02 16:25:55 +01:00
/**
* scan_receive()
* wartet auf serverantworten und uebergibt die pakete den
* plugins zur auswertung
*/
2006-02-02 16:24:06 +01:00
void scan_receive(void) {
struct net_pkt *pkt;
fd_set fdsel, fdcpy;
int recvsize, i;
FD_ZERO(&fdsel);
FD_SET(scan_sock, &fdsel);
while (1) {
memcpy(&fdcpy, &fdsel, sizeof(fdsel));
select(FD_SETSIZE, &fdcpy, NULL, NULL, NULL);
ioctl(scan_sock, FIONREAD, &recvsize);
2006-02-02 16:31:52 +01:00
if (recvsize <= 0) {
log_print("scan_receive(): drop short packet");
2006-02-02 16:24:06 +01:00
continue;
2006-02-02 16:31:52 +01:00
}
if (!(pkt = malloc(sizeof(struct net_pkt) + recvsize))) {
log_print("scan_receive(): malloc()");
2006-02-02 16:24:06 +01:00
continue;
2006-02-02 16:31:52 +01:00
}
2006-02-02 16:24:06 +01:00
i = sizeof(struct sockaddr_in);
pkt->size = recvfrom(scan_sock, pkt->buf, recvsize, 0, (struct sockaddr *)&pkt->addr, &i);
2006-02-02 16:34:11 +01:00
if (!plugins_parse(pkt)) {
log_print("scan_receive(): unknown packet: %s:%d size:%d",
inet_ntoa(pkt->addr.sin_addr), ntohs(pkt->addr.sin_port), pkt->size);
}
2006-02-02 16:24:06 +01:00
free(pkt);
}
}
2006-02-02 16:25:55 +01:00
/**
* scan_init()
* initialisiert den socket fuer den server scan
* @return false on error
*
2006-02-02 16:27:19 +01:00
* TODO: src ip als parameter (config-file?)
2006-02-02 16:25:55 +01:00
*/
2006-02-02 16:24:06 +01:00
int scan_init() {
struct sockaddr_in dst;
2006-02-02 16:29:30 +01:00
int i = 1;
2006-02-02 16:24:06 +01:00
if ((scan_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
log_print("scan_init(): socket()");
return 0;
}
dst.sin_family = AF_INET;
dst.sin_port = htons(7130);
inet_aton("0.0.0.0", &dst.sin_addr);
if (bind(scan_sock, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
log_print("scan_init(): bind()");
return 0;
}
if (setsockopt(scan_sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i))) {
log_print("scan_init(): setsockopt()");
return 0;
}
return 1;
}
2006-02-02 16:25:55 +01:00
/**
* schliesst den server scan socket
*/
2006-02-02 16:24:06 +01:00
void scan_exit() {
close(scan_sock);
}