File: pcap_iface.c

package info (click to toggle)
xprobe 0.0.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 496 kB
  • ctags: 105
  • sloc: sh: 1,507; ansic: 1,442; makefile: 90
file content (213 lines) | stat: -rw-r--r-- 5,857 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* $Id: pcap_iface.c,v 1.16 2001/10/20 15:23:55 fygrave Exp $ */
/*
** Copyright (C) 2001 Fyodor Yarochkin <fygrave@tigerteam.net>,
**                    Ofir Arkin       <ofir@sys-security.com>
**
** 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.
**
** All material for nonprofit, educational use only.
**
** 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 "xprobe.h"

/* heavily based on R.Stevens examples */

pcap_t *pd;
int datalink;

inter_t interf;

volatile unsigned char lpcap_done;

int init_pcap(char *iname, char *hostname, hstnet_t *targ) {
    char cmd[BUFSIZ+1], errbuf[PCAP_ERRBUF_SIZE];
    struct bpf_program fcode;

    if (iname == NULL) {
        if ((iname = pcap_lookupdev(errbuf)) == NULL) {
            fprintf(stderr,"pcap_lookup: %s\n",errbuf);
            return -1;
        }
    }
    
    strncpy((void *)interf.iname, iname, sizeof(interf.iname));
    interf.addr = get_iface_addr(interf.iname);

    fprintf(stderr, "Interface: %s/%s\n\n",interf.iname, inet_ntoa(interf.addr));
    
    if ((pd = pcap_open_live(iname, LPCAP_SNAPLEN, 0, LPCAP_TIMEOUT,
        errbuf)) == NULL) {
        fprintf(stderr,"pcap_open_live: %s\n",errbuf);
        return -1;
    }

    if (pcap_lookupnet(iname, (bpf_u_int32 *)&(interf.net),
                                 (bpf_u_int32 *)&(interf.netmask), errbuf) < 0) {
        fprintf(stderr,"pcap_lookupnet: %s\n",errbuf);
        return -1;
    }

    snprintf(cmd, sizeof(cmd),"icmp");

    if (pcap_compile(pd, &fcode, cmd, 0,*((bpf_u_int32*)&interf.netmask)) < 0) {
        fprintf(stderr,"failed to initalize pcap filter! (check "
                        "netmask/hostname!)\n");
        return -1;
    }

    if (pcap_setfilter(pd, &fcode) < 0) {
        fprintf(stderr,"pcap_setfilter: %s\n",errbuf);
        return -1;
    }

    if ((datalink = pcap_datalink(pd)) < 0) {
        fprintf(stderr,"pcap_datalink: %s\n",errbuf);
        return -1;
    }

    
   return 1; 
}

#ifdef __linux__
/* timeout handling on linux version of libpcap is bloody broken */
/* so we gotta fix it in such a dogdy way */

unsigned char *next_pack(ssize_t *len) {
    unsigned char *ptr = NULL;
    struct pcap_pkthdr hdr;
    int sd;
    fd_set fds;
    struct timeval wait;

    wait.tv_sec = receive_timeout;
    wait.tv_usec = 0;

    sd = pcap_fileno(pd);

    while(!lpcap_done && ptr == NULL) {
        FD_ZERO(&fds);
        FD_SET(sd, &fds);
        if (select(sd+1, &fds, (fd_set *)0, (fd_set *)0, &wait) <= 0)
            return NULL;
        ptr = (char *) pcap_next(pd, &hdr);
    }

    if (ptr != NULL) *len = hdr.caplen;
    return(ptr);

}

#else /* everywhere esles timeout seems to be honored properly */

unsigned char *next_pack(ssize_t *len) {
    unsigned char *ptr;
    struct pcap_pkthdr hdr;

    while((ptr = (unsigned char *) pcap_next(pd, &hdr)) == NULL) if (lpcap_done) return NULL;

    *len = hdr.caplen;
    return(ptr);

}

#endif

unsigned char *read_icmp(ssize_t  *packlen, int type, int code) {
    unsigned char *ptr;
    struct ether_header *ehp;
    struct ip *ip;
    struct icmp *icmp;

    lpcap_done = 0;
    signal(SIGALRM, read_icmp_timeout);
    alarm(receive_timeout);

    while(!lpcap_done) {

        if((ptr = next_pack(packlen)) == NULL) return NULL;
#ifdef DEBUG
        hexdump(ptr, *packlen);
#endif                

        switch(datalink) {
            case DLT_RAW:
                break;

            case DLT_NULL:
#ifdef DLT_LOOP
            case DLT_LOOP:
#endif
                *packlen -= 4;
                ptr += 4;
                break;

            case DLT_EN10MB:
                ehp = (struct ether_header *)ptr;
                if (ntohs(ehp->ether_type) != ETHERTYPE_IP) {
                    fprintf(stderr,">>ERROR: datagram type %x is not IP\n",
                            ntohs(ehp->ether_type));
                    ptr = NULL;
                    break;
                }
                *packlen -= 14;
                ptr += 14;
                break;

            case DLT_PPP:
                *packlen -= 24;
                ptr += 24;
                break;

            case DLT_SLIP:
                *packlen -= 24;
                ptr += 24;
                break;
           case DLT_FDDI:
                *packlen -= 21;
                ptr += 21;
                break;
#ifdef DLT_LINUX_SLL
            case DLT_LINUX_SLL:
                *packlen -= 16;
                ptr += 16;
                break;
#endif                
           default:     
                fprintf(stderr,"Unknown datalink type: %d\nPlease report!\n",
                     datalink);
                exit(1);
        }
        if (ptr != NULL && *packlen > (sizeof(ip) + 8)) {
            ip   = (struct ip *) ptr;
            icmp =(struct icmp *)(ptr + (ip->ip_hl<<2));
#ifdef DEBUG
            fprintf(stderr,"Received: icmp type: %d code: %d\n",
                    icmp->icmp_type, icmp->icmp_code);
#endif            

            if (((code == -1) || (code == icmp->icmp_code)) &&
                ((type == -1) || (type == icmp->icmp_type))) 
                return ptr;
        }
    } /* while(.. */
                
    return NULL;
}


void read_icmp_timeout(int sig) {
    lpcap_done = 1;
}