- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netinet/ip.h>
- #include <string.h>
- #include <netdb.h>
- #include <netinet/tcp.h>
- #include <netinet/udp.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <net/if.h>
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <linux/if_ether.h>
- #include <net/ethernet.h>
- 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[40960]={0};
- int main()
- {
- struct sockaddr_in addr;
- struct ether_header *peth;
- struct iphdr *pip;
- struct tcphdr *ptcp;
- struct udphdr *pudp;
- char mac[16];
- int i,sock, r, len;
- char *data;
- char *ptemp;
- char ss[32],dd[32];
- if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
- {
- die("socket", 1);
- }
- do_promisc("eth0", sock);
- for (;;)
- {
- len = sizeof(addr);
- r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,(socklen_t *)&len);
-
- if(r <=0)
- {
- continue;
- }
- buf[r] = 0;
- ptemp = buf;
- peth = (struct ether_header *)ptemp;
- ptemp += sizeof(struct ether_header);
- pip = (struct iphdr *)ptemp;
- ptemp += sizeof(struct ip);
- switch (pip->protocol)
- {
- case IPPROTO_TCP:
- ptcp = (struct tcphdr *)ptemp;
- printf("TCP pkt :FORM:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&(pip->saddr)),ntohs(ptcp->source));
- printf("TCP pkt :TO:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&(pip->daddr)),ntohs(ptcp->dest));
- break;
- case IPPROTO_UDP:
- pudp = (struct udphdr *)ptemp;
- printf("UDP pkt:\n len:%d payload len:%d from %s:%d to %s:%d\n",
- r,
- ntohs(pudp->len),
- inet_ntoa(*(struct in_addr*)&(pip->saddr)),
- ntohs(pudp->source),
- inet_ntoa(*(struct in_addr*)&(pip->daddr)),
- ntohs(pudp->dest)
- );
- break;
- case IPPROTO_ICMP:
- printf("ICMP pkt:%s\n",inet_ntoa(*(struct in_addr*)&(pip->saddr)));
- break;
- case IPPROTO_IGMP:
- printf("IGMP pkt:\n");
- break;
- default:
- printf("Unkown pkt, protocl:%d\n", pip->protocol);
- break;
- }
- perror("dump");
- }
- return 0;
- }
发表于 @
2008年12月24日 16:36:00 | | 编辑|
举报| 收藏