|
发表于 2004-10-5 20:21:36
|
显示全部楼层
我改过的[quote:7dd034c398="daixi"][code:1]#include <stdio.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <pcap.h>
#define MY_ETH_LEN 14
#define MY_DEBUG
void proc_ip_tcp(struct iphdr *pip, const unsigned char *packet, unsigned int plen) {
}
void proc_ip_udp(struct iphdr *pip, const unsigned char *packet, unsigned int plen) {
struct udphdr *pudp = NULL;
char *pdata = NULL;
int data_len = 0;
struct in_addr ins;
char *sa;
struct in_addr ind;
char *da;
//下面改
pudp = (struct udphdr *)(packet + sizeof(struct ether_header)+sizeof(struct iphdr));
pdata = (char *)(pudp + pudp->len);
//改完
data_len = plen - MY_ETH_LEN - pip->ihl - pudp->len;
ins.s_addr = pip->saddr;
sa = (char *)inet_ntoa(ins);
ind.s_addr = pip->daddr;
da = (char *)inet_ntoa(ind);
printf("IP - UDP:\n");
printf("ttl=%u\n", pip->ttl);
//改
printf("%s:%u ==> %s:%u\n", sa, ntohs(pudp->source), da, ntohs(pudp->dest));
//改完
printf("======> Length: %u\n", plen);
}
void proc_ip(const struct pcap_pkthdr *pkthdr, const unsigned char *packet) {
struct iphdr *pip = (struct iphdr *)(packet + MY_ETH_LEN);
switch(pip->protocol) {
case IPPROTO_TCP:
proc_ip_tcp(pip, packet, pkthdr->caplen);
break;
case IPPROTO_UDP:
proc_ip_udp(pip, packet, pkthdr->caplen);
break;
default:
break;
}
}
void proc_arp(const struct pcap_pkthdr *pkthdr, const unsigned char *packet) {
printf("===> ARP\n");
}
void proc_rarp(const struct pcap_pkthdr *pkthdr, const unsigned char *packet) {
printf("===> RARP\n");
}
void my_callback(unsigned char *unless, const struct pcap_pkthdr *pkthdr, const unsigned char *packet) {
struct ether_header *peth = (struct ether_header *)packet;
u_int16_t eth_type = ntohs(peth->ether_type);
#ifdef MY_DEBUG
printf("Capture one packet! Eth_Type: 0x%x\n", eth_type);
#endif
switch(eth_type) {
case ETHERTYPE_IP:
proc_ip(pkthdr, packet);
break;
case ETHERTYPE_ARP:
proc_arp(pkthdr, packet);
break;
case ETHERTYPE_REVARP:
proc_rarp(pkthdr, packet);
break;
default:
break;
}
return;
}
void proc_cap(char *device, char *filter) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *descr;
descr = pcap_open_live(device, BUFSIZ, 0, -1, errbuf);
if(descr == NULL) {
printf("Cannot open device live!\n");
return;
}
pcap_loop(descr, 100, my_callback, NULL);
}
void show_help() {
printf("Usage: capdump -d device_name -f filter_string\n");
printf(" -d assign a device name(needed)\n");
printf(" -f assign a filter string(optional)\n");
}
void my_main(int argc, char *argv[]) {
int c = 0;
char *device = NULL;
char *filter = NULL;
int h = 0;
while((c = getopt(argc, argv, "d:f:h")) != -1) {
switch(c) {
case 'd':
device = optarg;
break;
case 'f':
filter = optarg;
break;
case 'h':
show_help();
h = 1;
break;
default:
break;
}
}
#ifdef MY_DEBUG
printf("Device: %s, Filter: %s\n", device, filter);
#endif
if(h == 1) return;
if(device==NULL) {
printf("Cannot get device!\n");
return;
}
proc_cap(device, filter);
}
int main(int argc, char *argv[]) {
my_main(argc, argv);
return 0;
}
[/code:1][/quote] |
|