File: example.c

package info (click to toggle)
arpalert 2.0.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,332 kB
  • ctags: 415
  • sloc: ansic: 4,185; sh: 447; makefile: 153; perl: 28
file content (103 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (c) 2005-2010 Thierry FOURNIER
 * $Id: example.c 210 2006-10-05 12:25:42Z  $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>

#ifdef __FreeBSD__
#   define ETHER_ADDR_OCTET octet
#else
#   define ETHER_ADDR_OCTET ether_addr_octet
#endif

#include "../api/arpalert.h"

// context
int numb;
char *prefix;

// init module and context
void mod_load(char *config){
	char *p, *conf;
	int context = 0;
	char *args[50];
	int count = 0;
	int i = 0;
	
	// default conf
	prefix = "";
	numb = 0;
	
	// parse config
	conf = strdup(config);
	p = conf;
	while(*p != 0){
		if(context == 0 && *p != ' ' && *p != '\t'){
			context = 1;
			args[count] = p;
			count++;
			if(count == 50) break;
		}
		
		else if(context == 1 && ( *p == ' ' || *p == '\t')){
			context = 0;
			*p = 0;
		}
		
		p++;
	}
	
	// apply config
	i = 0;
	while(i < count){
		if(strcmp("prefix", args[i]) == 0){
			i ++;
			if(i < count) prefix = strdup(args[i]);
		}
		else if(strcmp("init", args[i]) == 0){
			i++;
			if(i < count) numb = atoi(args[i]);
		}
		i++;
	}

	// free temp
	free(conf);
}

// alert launched
void mod_alert(int type, int nargs, void **data){
	struct ether_addr *mac; 
	struct in_addr ip;

	mac = (struct ether_addr *)data[1];
	ip.s_addr = (*(struct in_addr *)data[2]).s_addr;
	numb++;

	printf("%s[%d]: type=%d nargs=%d port=%s mac="
	       "%02x:%02x:%02x:%02x:%02x:%02x ip=%s\n",
			 prefix, numb, type, nargs, (char *)data[0], 
			 mac->ETHER_ADDR_OCTET[0],
			 mac->ETHER_ADDR_OCTET[1],
			 mac->ETHER_ADDR_OCTET[2],
			 mac->ETHER_ADDR_OCTET[3],
			 mac->ETHER_ADDR_OCTET[4],
			 mac->ETHER_ADDR_OCTET[5],
	       inet_ntoa(ip));
}

// init module and context
void mod_unload(void){
	printf("MODULE CLOSED AFTER %d ALERTS\n", numb);
	free(prefix);
}