~ubuntu-branches/ubuntu/precise/4g8/precise

« back to all changes in this revision

Viewing changes to src/init.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2007-03-26 16:19:06 UTC
  • Revision ID: james.westby@ubuntu.com-20070326161906-00xhpx7yaef36u63
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * author: Darren Bounds <dbounds@intrusense.com>
 
3
 * copyright: Copyright (C) 2002 by Darren Bounds
 
4
 * license: This software is under GPL version 2 of license
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 *
 
20
 * 4g8 official page at http://.net
 
21
 */
 
22
 
 
23
#include "init.h"
 
24
 
 
25
void
 
26
capture_loop(u_int8_t *user, struct pcap_pkthdr *pkthdr, u_int8_t *packet)
 
27
{
 
28
    if(display)
 
29
        process_packets(pkthdr, packet);
 
30
 
 
31
    if(!inject_packet())
 
32
        fatal_error("Unable to inject packet");
 
33
 
 
34
    return;
 
35
}
 
36
 
 
37
void
 
38
init()
 
39
{
 
40
    u_int8_t filter[255+1];
 
41
    u_int32_t data_link, localnet, netmask;
 
42
    pcap_dumper_t *p_dumper = NULL;
 
43
 
 
44
    struct bpf_program bpf;
 
45
    struct itimerval oval;
 
46
    struct libnet_ether_addr *hw_addr;
 
47
 
 
48
#ifdef DEBUG
 
49
    fprintf(stdout, "DEBUG: init()\n");
 
50
#endif
 
51
 
 
52
    memset(&oval, 0, sizeof(struct itimerval));
 
53
    memset(&bpf, 0, sizeof(struct bpf_program));
 
54
 
 
55
    signal(SIGTERM, clean_exit);
 
56
    signal(SIGINT, clean_exit);
 
57
    signal(SIGQUIT, clean_exit);
 
58
    signal(SIGHUP, clean_exit);
 
59
    signal(SIGALRM, arp_cache_alrm);
 
60
 
 
61
    if((pkt_d = libnet_init(0, device, error_buf)) == NULL)
 
62
        fatal_error("Unable to initialize packet injection");
 
63
 
 
64
    if(device == NULL)
 
65
        if((device = pcap_lookupdev(error_buf)) == NULL)
 
66
            fatal_error("%s: Check device permissions", error_buf);
 
67
 
 
68
    if((pkt = pcap_open_live(device, 1500, 1, 500, error_buf)) == NULL)
 
69
        fatal_error("Unable to open device: %s", error_buf);
 
70
 
 
71
    if(strlen(w_file) > 0)
 
72
    {
 
73
#ifdef DEBUG
 
74
        fprintf(stdout, "DEBUG: Writing to capture file: %s\n", w_file);
 
75
#endif
 
76
 
 
77
        if((p_dumper = pcap_dump_open(pkt, w_file)) == NULL)
 
78
            fatal_error("Unable to initialize packet capture: %s", pcap_geterr(pkt));
 
79
 
 
80
        display--;
 
81
    }
 
82
 
 
83
    if(pcap_lookupnet(device, &localnet, &netmask, error_buf) < 0)
 
84
        fprintf(stderr, "\nWarning: Unable to lookup network: %s\n", error_buf);
 
85
 
 
86
    hw_addr = libnet_get_hwaddr(pkt_d);
 
87
    snprintf(filter, 255, "(ip src host %s and ether dst %0x:%0x:%0x:%0x:%0x:%0x) or (ip dst host %s and ether dst %0x:%0x:%0x:%0x:%0x:%0x)", 
 
88
        host_ip, 
 
89
        hw_addr->ether_addr_octet[0], 
 
90
        hw_addr->ether_addr_octet[1], 
 
91
        hw_addr->ether_addr_octet[2], 
 
92
        hw_addr->ether_addr_octet[3], 
 
93
        hw_addr->ether_addr_octet[4], 
 
94
        hw_addr->ether_addr_octet[5], 
 
95
        host_ip,
 
96
        hw_addr->ether_addr_octet[0],
 
97
        hw_addr->ether_addr_octet[1],
 
98
        hw_addr->ether_addr_octet[2],
 
99
        hw_addr->ether_addr_octet[3],
 
100
        hw_addr->ether_addr_octet[4],
 
101
        hw_addr->ether_addr_octet[5]);
 
102
 
 
103
    fprintf(stdout, "GW: %s\n", filter);
 
104
 
 
105
    if(pcap_compile(pkt, &bpf, filter, 0, netmask) < 0)
 
106
        fatal_error("Unable to compile packet filters: %s\n", pcap_geterr(pkt));
 
107
 
 
108
    if(pcap_setfilter(pkt, &bpf) < 0)
 
109
        fatal_error("Unable to set packet filters: %s", pcap_geterr(pkt));
 
110
 
 
111
#ifdef HAVE_FREECODE
 
112
    pcap_freecode(&bpf); 
 
113
#endif /* HAVE_FREECODE */
 
114
 
 
115
    if((data_link = pcap_datalink(pkt)) < 0)
 
116
        fatal_error("Unable to determine datalink type: %s", pcap_geterr(pkt));
 
117
 
 
118
    hdr_len = retrieve_datalink_hdr_len(data_link);
 
119
 
 
120
    fprintf(stdout, "4g8: Lets see what %s is up to. (Device: %s)\n", host_ip, device);
 
121
 
 
122
    oval.it_interval.tv_sec = 2;
 
123
    oval.it_value.tv_sec = 1;
 
124
    setitimer(ITIMER_REAL, &oval, 0);
 
125
 
 
126
    if(pcap_loop(pkt, 0, 
 
127
        (display == 1) ? (pcap_handler)capture_loop : (pcap_handler)pcap_dump, 
 
128
        (display == 1) ? NULL : (u_int8_t *)p_dumper) < 0)
 
129
    {
 
130
        fatal_error("Unable to initialize pcap_loop: %s", pcap_geterr(pkt));
 
131
    }
 
132
 
 
133
    clean_exit(SUCCESS);
 
134
 
 
135
    return;
 
136
}