File: init.c

package info (click to toggle)
4g8 1.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 632 kB
  • ctags: 145
  • sloc: sh: 3,736; ansic: 1,397; makefile: 75
file content (136 lines) | stat: -rw-r--r-- 4,281 bytes parent folder | download | duplicates (3)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * author: Darren Bounds <dbounds@intrusense.com>
 * copyright: Copyright (C) 2002 by Darren Bounds
 * license: This software is under GPL version 2 of license
 *
 * 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
 *
 * 4g8 official page at http://.net
 */

#include "init.h"

void
capture_loop(u_int8_t *user, struct pcap_pkthdr *pkthdr, u_int8_t *packet)
{
    if(display)
        process_packets(pkthdr, packet);

    if(!inject_packet())
        fatal_error("Unable to inject packet");

    return;
}

void
init()
{
    u_int8_t filter[255+1];
    u_int32_t data_link, localnet, netmask;
    pcap_dumper_t *p_dumper = NULL;

    struct bpf_program bpf;
    struct itimerval oval;
    struct libnet_ether_addr *hw_addr;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: init()\n");
#endif

    memset(&oval, 0, sizeof(struct itimerval));
    memset(&bpf, 0, sizeof(struct bpf_program));

    signal(SIGTERM, clean_exit);
    signal(SIGINT, clean_exit);
    signal(SIGQUIT, clean_exit);
    signal(SIGHUP, clean_exit);
    signal(SIGALRM, arp_cache_alrm);

    if((pkt_d = libnet_init(0, device, error_buf)) == NULL)
        fatal_error("Unable to initialize packet injection");

    if(device == NULL)
        if((device = pcap_lookupdev(error_buf)) == NULL)
            fatal_error("%s: Check device permissions", error_buf);

    if((pkt = pcap_open_live(device, 1500, 1, 500, error_buf)) == NULL)
        fatal_error("Unable to open device: %s", error_buf);

    if(strlen(w_file) > 0)
    {
#ifdef DEBUG
        fprintf(stdout, "DEBUG: Writing to capture file: %s\n", w_file);
#endif

        if((p_dumper = pcap_dump_open(pkt, w_file)) == NULL)
            fatal_error("Unable to initialize packet capture: %s", pcap_geterr(pkt));

        display--;
    }

    if(pcap_lookupnet(device, &localnet, &netmask, error_buf) < 0)
	fprintf(stderr, "\nWarning: Unable to lookup network: %s\n", error_buf);

    hw_addr = libnet_get_hwaddr(pkt_d);
    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)", 
        host_ip, 
        hw_addr->ether_addr_octet[0], 
        hw_addr->ether_addr_octet[1], 
        hw_addr->ether_addr_octet[2], 
        hw_addr->ether_addr_octet[3], 
        hw_addr->ether_addr_octet[4], 
        hw_addr->ether_addr_octet[5], 
        host_ip,
        hw_addr->ether_addr_octet[0],
        hw_addr->ether_addr_octet[1],
        hw_addr->ether_addr_octet[2],
        hw_addr->ether_addr_octet[3],
        hw_addr->ether_addr_octet[4],
        hw_addr->ether_addr_octet[5]);

    fprintf(stdout, "GW: %s\n", filter);

    if(pcap_compile(pkt, &bpf, filter, 0, netmask) < 0)
        fatal_error("Unable to compile packet filters: %s\n", pcap_geterr(pkt));

    if(pcap_setfilter(pkt, &bpf) < 0)
        fatal_error("Unable to set packet filters: %s", pcap_geterr(pkt));

#ifdef HAVE_FREECODE
    pcap_freecode(&bpf); 
#endif /* HAVE_FREECODE */

    if((data_link = pcap_datalink(pkt)) < 0)
        fatal_error("Unable to determine datalink type: %s", pcap_geterr(pkt));

    hdr_len = retrieve_datalink_hdr_len(data_link);

    fprintf(stdout, "4g8: Lets see what %s is up to. (Device: %s)\n", host_ip, device);

    oval.it_interval.tv_sec = 2;
    oval.it_value.tv_sec = 1;
    setitimer(ITIMER_REAL, &oval, 0);

    if(pcap_loop(pkt, 0, 
        (display == 1) ? (pcap_handler)capture_loop : (pcap_handler)pcap_dump, 
        (display == 1) ? NULL : (u_int8_t *)p_dumper) < 0)
    {
        fatal_error("Unable to initialize pcap_loop: %s", pcap_geterr(pkt));
    }

    clean_exit(SUCCESS);

    return;
}