LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 954|回复: 5

请大家解释一下这C程序?

[复制链接]
发表于 2003-12-8 22:01:20 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>



int main(int argc, char *argv[])
{
        int s, bytes,a=0,sy=0,f=0,r=0,u=0,p=0;
        int          ac=0,syc=0,fc=0,rc=0;
        struct tcphdr  *tcp;
        struct iphdr    *ip;
        struct in_addr  addr;
        char            buffer[4000];
        
        s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
        if (s == -1)
        {
                perror("socket() failed");
                return 1;
        }
        
        ip = (struct iphdr*) buffer;
        tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));

        
        while( (bytes = recv(s, buffer, sizeof(buffer), 0)) > 0)
        {
                addr.s_addr = ip->saddr;
                a=ntohs(tcp->ack);
                sy=ntohs(tcp->syn);
                r=ntohs(tcp->rst);
                f=ntohs(tcp->fin);       
                p=ntohs(tcp->psh);
                u=ntohs(tcp->urg);
        if (ip->saddr!=inet_addr("192.168.0.113"))
        {       
        printf("acket from source:%s\nwith flags ->",inet_ntoa(addr));
        if(a==256)
        {
        printf("Ack ");
        }
        if ( sy==256)
        {printf("Syn ");
        ;}
        if (f==256)
        {
        printf("Fin ");       
        }
        if (r==256)
        {
        printf("Rst ");
        }
        if (p==256)
        {
        printf("sh ");
        }
        if (u==256)
        {
        printf("Urg");
        }

        printf("\n");

        printf("With the sequence number of ->%i\n",ntohl(tcp->seq));
        printf("From port->%i\n\n",ntohs(tcp->source));
        }
        }
        
        if (bytes == -1)
        {
                perror("recv() failed");
                return 2;
        }
        return 0;
       
}
发表于 2003-12-9 12:08:47 | 显示全部楼层
我想这是个数据包信息过滤程序

---------------------------------------------------------
ip = (struct iphdr*) buffer;
tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));
---------------------------------------------------------
获取ip报头和tcp报头,只需移动指针即可.
详细内容看/usr/src/linux/include/linux/skbuff.h的struct sk_buff


---------------------------------------------------------
while( (bytes = recv(s, buffer, sizeof(buffer), 0)) > 0)
---------------------------------------------------------
读取数据包


---------------------------------------------------------
addr.s_addr = ip->saddr;
a=ntohs(tcp->ack);
sy=ntohs(tcp->syn);
r=ntohs(tcp->rst);
f=ntohs(tcp->fin);
p=ntohs(tcp->psh);
u=ntohs(tcp->urg);
---------------------------------------------------------
获取ip地址,而后获取tcp报头的状态
详细内容在/usr/src/linux/include/linux/tcp.h的struct tcphdr


最后判断各标志,over!
 楼主| 发表于 2003-12-9 13:36:37 | 显示全部楼层
强阿~~~~象sniffer..

看来还要努力学习。。。
发表于 2003-12-9 17:06:57 | 显示全部楼层
还不是sniffer,只是一个简单本机tcp协议还原程序
发表于 2004-4-7 17:58:52 | 显示全部楼层

回复: 请大家解释一下这C程序?

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. #include <netinet/ip.h>
  6. #include <netinet/tcp.h>
  7. int main(int argc, char *argv[])
  8. {
  9.         int s, bytes,a=0,sy=0,f=0,r=0,u=0,p=0;
  10.         int          ac=0,syc=0,fc=0,rc=0;
  11.         struct tcphdr  *tcp;
  12.         struct iphdr    *ip;
  13.         struct in_addr  addr;
  14.         char            buffer[4000];
  15.         
  16.         s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  17.         if (s == -1)
  18.         {
  19.                 perror("socket() failed");
  20.                 return 1;
  21.         }
  22.         
  23.         ip = (struct iphdr*) buffer;
  24.         tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));

  25.         
  26.         while( (bytes = recv(s, buffer, sizeof(buffer), 0)) > 0)
  27.         {
  28.                 addr.s_addr = ip->saddr;
  29.                 a=ntohs(tcp->ack);
  30.                 sy=ntohs(tcp->syn);
  31.                 r=ntohs(tcp->rst);
  32.                 f=ntohs(tcp->fin);       
  33.                 p=ntohs(tcp->psh);
  34.                 u=ntohs(tcp->urg);
  35.         if (ip->saddr!=inet_addr("192.168.0.113"))
  36.         {       
  37.         printf("Packet from source: %s\nwith flags ->",inet_ntoa(addr));
  38.         if(a==256)
  39.         {
  40.         printf("Ack ");
  41.         }
  42.         if ( sy==256)
  43.         {printf("Syn ");
  44.         ;}
  45.         if (f==256)
  46.         {
  47.         printf("Fin ");       
  48.         }
  49.         if (r==256)
  50.         {
  51.         printf("Rst ");
  52.         }
  53.         if (p==256)
  54.         {
  55.         printf("Psh ");
  56.         }
  57.         if (u==256)
  58.         {
  59.         printf("Urg");
  60.         }

  61.         printf("\n");

  62.         printf("With the sequence number of ->%i\n",ntohl(tcp->seq));
  63.         printf("From port->%i\n\n",ntohs(tcp->source));
  64.         }
  65.         }
  66.         
  67.         if (bytes == -1)
  68.         {
  69.                 perror("recv() failed");
  70.                 return 2;
  71.         }
  72.         return 0;
  73.        
  74. }
复制代码
这样看舒服点。。这是unix的程序。。
发表于 2004-4-8 10:33:54 | 显示全部楼层
6个标志位。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表