当前位置: 技术问答>linux和unix
帮忙给看下程序!
来源: 互联网 发布时间:2016-07-03
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void die(char *why, int n) { perror(why); exit(n); } int do_promisc(char *nif, int...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void die(char *why, int n)
{
perror(why);
exit(n);
}
int do_promisc(char *nif, int sock )
{
struct ifreq ifr;
strncpy(ifr.ifr_name, nif,strlen(nif)+1);
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {
die("ioctl", 2);
}
ifr.ifr_flags |= IFF_PROMISC;
if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) {
die("ioctl", 3);
}
}
char buf[2*32767];
main()
{
struct sockaddr_in addr;
struct iphdr *ip;
struct udphdr *udp;
int sock, r, len;
char *data;
char ss[32], dd[32];
if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP)) == -1)
die("socket", 1);
do_promisc("eth0", sock);
for(;;) {
len = sizeof(addr);
r = recvfrom(sock,(char *)buf,sizeof(buf),0,(struct sockaddr *)&addr,&len);
buf[r] = 0;
ip = (struct iphdr *)buf;
udp = (struct udphdr *)(buf + sizeof(struct iphdr));
// if(ntohs(udp->source) == 23){
printf("PktSize: %d IPLEN %d PROT %d %s:%d-->;%s:%d n",
r, ip->tot_len,
ip->protocol,
strcpy(ss, inet_ntoa(*(struct in_addr*)&(ip->saddr))),
ntohs(udp->source),
strcpy(dd, inet_ntoa(*(struct in_addr*)&(ip->daddr))),
ntohs(udp->dest)
);
// }
}
}
程序的功能就是提取流经本机的UDP数据包,然后是按如下格式打印出结果:
PktSize: 191 IPLEN 48896 PROT 17 211.68.71.4:53-->;192.168.1.100:50699
PktSize: 152 IPLEN 38912 PROT 17 211.68.71.4:53-->;192.168.1.100:58637
PktSize: 209 IPLEN 53504 PROT 17 211.68.71.4:53-->;192.168.1.100:44420
PktSize: 192 IPLEN 49152 PROT 17 211.68.71.4:53-->;192.168.1.100:53474
PktSize: 201 IPLEN 51456 PROT 17 211.68.71.4:53-->;192.168.1.100:58119
PktSize: 189 IPLEN 48384 PROT 17 211.68.71.4:53-->;192.168.1.100:48201
现在我只想截取端口为53的数据包,加上注释的那个if语句后,程序能正常编译运行,但是没有输出结果!
请大牛指点下我!多谢了!!!
|
53 和 23 搞得自己混乱了吧
|
|
不错
|
新手进来,学习一下,支持