|
|

楼主 |
发表于 2005-5-16 16:50:50
|
显示全部楼层
- #include <stdio.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #define INTERFACE "eth0"
- struct ipp{
- unsigned int ip_length:4;
- unsigned int ip_version:4;
- unsigned short ip_total_length;
- unsigned short ip_id;
- unsigned short ip_flags;
- unsigned char ttl;
- unsigned char ip_protocol;
- unsigned short ip_cksun;
- unsigned int ip_sourse;
- unsigned int ip_dest;
- }
- struct tcp{
- unsigned short tcp_sourse_port;
- unsigned short tcp_dest_port;
- unsigned int tcp_seqno;
- unsigned int tcp_ackno;
- unsigned int tcp_resl:4,
- tcp_hlen:4,
- tcp_fin:1,
- tcp_syn:1,
- tcp_rst:1,
- tcp_psh:1,
- tcp_ack:1,
- tcp_urg:1,
- tcp_res2:2;
- unsigned short tcp_winsize;
- unsigned short tcp_cksum;
- unsigned short tcp_urgent;
- }
- int Moshi(char * interface,int sock);
- int main()
- {
- int sock,byte_recieved,fromlen;
- char buff[65535];
- struct sockaddr_in from;
- struct ipp *ip;
- struct tcp *tcp;
- sock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
- if sock<0 exit(0);
- Moshi(INTERFACE,sock);
- while(1)
- {
- fromlen=sizeof from;
- byte_recieved=recvfrom(sock,buff,sizeof buff,0,(struct sockaddr *)&from,&fromlen);
- printf("\nReceived bytes:::%5d\n",byte_recieved);
- printf("Message from:::%s\n",inet_ntoa(from.sin_addr));
- ip=(struct ipp *)buff;
- if (ip->ip_protocol==6){
- tcp=(struct tcp *)(buff+4*(ip->ip_length));
- printf("From port:::%u\n",tcp->tcp_sourse_port);
- printf("End port:::%d\n",tcp->tcp_dest_port);
- }
- }
- }
- int Moshi(char * interface,int sock)
- {
- struct ifreq ifr;
- strncpy(ifr.ifr_name,interface,(strlen(interface)+1));
- if(ioctl(sock,SIOCGIFFLAGS,&ifr)==-1){
- exit(0);
- }
- ifr.ifr_flags|=IFF_PROMISC;
- if(ioctl(sock,SIOCSIFFLAGS,&ifr)==-1){
- exit(0);
- }
- printf("\nZhunbei jiuxu!\n");
- return(0);
- }
复制代码 |
|