免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: force_eagle
打印 上一主题 下一主题

[网络子系统] 在内核中创建HTTP Response的疑惑 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-04-24 12:12 |只看该作者
LZ讲一下你构造回应包的过程,以及怎么构造的数据部分。HTTP的回应包应该有多种类型吧。
Godbach 发表于 2010-04-24 11:03



    主要是创造一个301 的回应包 等下就把代码贴出来 呵呵

论坛徽章:
0
12 [报告]
发表于 2010-04-24 12:13 |只看该作者
创建HTTP redirect 301 回应包代码
  1. const char *http_redirect_header =
  2.         "HTTP/1.1 200 OK\r\n"
  3.         "Date: Fri, 23 Apr 2010 12:54:40 GMT\r\n"
  4.         "Server: Apache/2.2.15 (Win32)\r\n"
  5.         //"Refresh: 0; url=http://%s\r\n"
  6.         "Content-length: %d\r\n"
  7.         "Content-Type: text/html; charset=iso-8859-1\r\n"
  8.         "\r\n";
  9. const char *http_redirect_body =         
  10.         //"Please follow <a href=\"http://%s\">link</a>!\r\n";
  11.         "<html><head>\r\n"
  12.           "<meta http-equiv=\"Refresh\" content=\"0; url=http://%s\">\r\n"
  13.         "</head><body>\r\n"
  14.           "<p>Please follow <a href=\"http://%s\">link</a>!</p>\r\n"
  15.         "</body></html>\r\n";



  16. static
  17. int http_build_redirect_url( const char *url, gbuffer_t *p )
  18. {
  19.         char *header = NULL;
  20.         char *body  = NULL;
  21.         char *buf   = NULL;
  22.         int header_len, body_len;
  23.         int rc = -1;       
  24.        
  25.         if ( NULL == p )
  26.                 goto _out;
  27.                
  28.         header = kmalloc( PATH_MAX, GFP_KERNEL );
  29.         if ( NULL == header ) {
  30.                 goto _out;
  31.         }
  32.         body = kmalloc( PATH_MAX, GFP_KERNEL );
  33.         if ( NULL == body ){
  34.                 goto _out;
  35.         }
  36.                
  37.         body_len = snprintf( body, PATH_MAX,
  38.                                         http_redirect_body,
  39.                                         url, url );
  40.                        
  41.         body_len = strlen( body );       
  42.         //DBG_INFO( verbose, "Length=%d\nBody:%s\n", body_len, body );                                       
  43.         header_len = snprintf( header, PATH_MAX,
  44.                                         http_redirect_header,                                       
  45.                                         body_len );
  46.         //DBG_INFO( verbose, "Header:%s\n", header );       
  47.                                                
  48.         buf = kmalloc( header_len + body_len, GFP_KERNEL );
  49.         if ( NULL == buf ){
  50.                 goto _out;
  51.         }
  52.        
  53.         p->buf = buf;
  54.         p->len = header_len + body_len;
  55.        
  56.         memcpy( buf, header, header_len );
  57.         memcpy( buf + header_len, body, body_len );
  58.        
  59. #if 0
  60.         {
  61.                 int i = 0;
  62.                 for( ; i < p->len; i ++ ){
  63.                         printk( "%c", buf[i] );
  64.                 }
  65.                 printk( "\n" );
  66.         }
  67. #endif       
  68.         rc = 0;
  69. _out:
  70.         if ( header ){
  71.                 kfree( header );
  72.         }       
  73.         if ( body ) {
  74.                 kfree( body );
  75.         }
  76.         return rc;
  77. }
复制代码

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
13 [报告]
发表于 2010-04-24 12:15 |只看该作者
godbach兄.. 每次到这个板块,你依旧你这里的常青树啊!!我最近搞起ndis,windows内核了
ubuntuer 发表于 2010-04-24 11:23

呵呵,过奖了。
最近工作多,周末也来公司拉磨了。

我最近也在分析一下IDS,不过是Linux下用的。
你搞的NIDS是要自己开发吗。

论坛徽章:
0
14 [报告]
发表于 2010-04-24 12:16 |只看该作者
发送tcp数据代码, 数据作为一个gbuffer_t结构, 保存数据指针与长度
  1. int _tcp_send_pack( struct sk_buff *skb, struct iphdr *iph,
  2.                 struct tcphdr *th, gbuffer_t *p )
  3. {
  4.         struct sk_buff *pskb = NULL;
  5.         int rc = -1;
  6.         u32 seq = 0x0;
  7.         struct ethhdr *eth = NULL;
  8.         struct vlan_hdr *vhdr = NULL;
  9.         struct tcp_options_received opt_rx;
  10.         struct tcp_out_options opts;
  11.         int tcp_len = 0;
  12.         u32 ack_seq = 0;
  13.        
  14.         opt_rx.tstamp_ok = 1;
  15.         _tcp_parse_options( th, &opt_rx, 1 );
  16.                
  17.         // 重新计算 Acknowledgement number
  18.         tcp_len = ntohs(iph->tot_len) - ((iph->ihl + th->doff) << 2);
  19.         ack_seq = ntohl(th->seq) + (tcp_len);
  20.         ack_seq = htonl(ack_seq);
  21.         //
  22.         //get_random_bytes( &seq, sizeof(seq) );
  23.         seq = common_seq;
  24.        
  25.         memset( &opts, 0x0, sizeof(opts) );
  26.         if ( opt_rx.saw_tstamp ) {
  27.                 opts.options |= OPTION_TS;               
  28.                 opts.tsecr = opt_rx.rcv_tsval;
  29.                 opts.tsval = tcp_time_stamp - tcp_rcv_tsecr + opt_rx.rcv_tsval;
  30.         }
  31.        
  32.         pskb = tcp_newpack( iph->daddr, iph->saddr,
  33.                                 th->dest, th->source,
  34.                                 th->ack_seq, ack_seq,
  35.                                 &opts,
  36.                                 p->buf, p->len );
  37.                                
  38.         if ( NULL == pskb ) {
  39.                 goto _out;
  40.         }
  41.        
  42.         // 复制VLAN 信息
  43.         if ( __constant_htons(ETH_P_8021Q) == skb->protocol ) {
  44.                 vhdr = (struct vlan_hdr *)skb_push(pskb, VLAN_HLEN );
  45.                 vhdr->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
  46.                 vhdr->h_vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
  47.         }
  48.        
  49.         // skb->data 移动到eth首部
  50.         eth = (struct ethhdr *) skb_push(pskb, ETH_HLEN);
  51.         skb_reset_mac_header(pskb);
  52.        
  53.         //
  54.         pskb->protocol  = eth_hdr(skb)->h_proto;
  55.         eth->h_proto    = eth_hdr(skb)->h_proto;
  56.         memcpy( eth->h_source, eth_hdr(skb)->h_dest, ETH_ALEN);   
  57.         memcpy( eth->h_dest, eth_hdr(skb)->h_source, ETH_ALEN );
  58.        
  59.         if ( skb->dev ) {
  60.                 pskb->dev = skb->dev;               
  61.                 dev_queue_xmit( pskb );
  62.                 rc = 0;
  63.         }
  64.         else {
  65.                 kfree_skb( pskb );
  66.                 dbg_err( "skb->dev is NULL\n" );
  67.         }
  68. _out:       
  69.         return rc;       
  70. }

  71. int http_send_redirect(struct sk_buff *skb, struct iphdr *iph,
  72.                 struct tcphdr *th, const char *url)
  73. {       
  74.         int rc = -1;
  75.         gbuffer_t buf;
  76.        
  77.         gbuffer_init( &buf );
  78.        
  79.         if ( !http_build_redirect_url( url, &buf ) ){
  80.                 rc = _tcp_send_pack(skb, iph, th, &buf );
  81.         }
  82.         gbuffer_free( &buf );       
  83.         return rc;
  84. }
复制代码

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
15 [报告]
发表于 2010-04-24 12:16 |只看该作者
主要是创造一个301 的回应包 等下就把代码贴出来 呵呵

你1楼贴的图中,你构造的回应包是第5个吗。如果是,为什么是200 OK呢?

论坛徽章:
0
16 [报告]
发表于 2010-04-24 12:18 |只看该作者
用到的一些数据结构
gbuffer_t
  1. struct gbuffer{
  2.         u8  *buf;
  3.         u32 len;
  4. };

  5. typedef struct gbuffer gbuffer;
  6. typedef struct gbuffer gbuffer_t;
复制代码
以下两个结构是从内核copy过来的, 主要是接收/发送tcp时的options处理.

struct tcp_out_options
  1. #define OPTION_SACK_ADVERTISE        (1 << 0)
  2. #define OPTION_TS                (1 << 1)
  3. #define OPTION_MD5                (1 << 2)

  4. struct tcp_out_options {
  5.         u8 options;                /* bit field of OPTION_* */
  6.         u8 ws;                        /* window scale, 0 to disable */
  7.         u8 num_sack_blocks;        /* number of SACK blocks to include */
  8.         u16 mss;                /* 0 to disable */
  9.         __u32 tsval, tsecr;        /* need to include OPTION_TS */
  10. };
复制代码
struct tcp_options_received
  1. struct tcp_options_received {
  2. /*        PAWS/RTTM data        */
  3.         long        ts_recent_stamp;/* Time we stored ts_recent (for aging) */
  4.         u32        ts_recent;        /* Time stamp to echo next                */
  5.         u32        rcv_tsval;        /* Time stamp value                     */
  6.         u32        rcv_tsecr;        /* Time stamp echo reply                */
  7.         u16         saw_tstamp : 1,        /* Saw TIMESTAMP on last packet                */
  8.                 tstamp_ok : 1,        /* TIMESTAMP seen on SYN packet                */
  9.                 dsack : 1,        /* D-SACK is scheduled                        */
  10.                 wscale_ok : 1,        /* Wscale seen on SYN packet                */
  11.                 sack_ok : 4,        /* SACK seen on SYN packet                */
  12.                 snd_wscale : 4,        /* Window scaling received from sender        */
  13.                 rcv_wscale : 4;        /* Window scaling to send to receiver        */
  14. /*        SACKs data        */
  15.         u8        eff_sacks;        /* Size of SACK array to send with next packet */
  16.         u8        num_sacks;        /* Number of SACK blocks                */
  17.         u16        user_mss;          /* mss requested by user in ioctl */
  18.         u16        mss_clamp;        /* Maximal mss, negotiated at connection setup */
  19. };
复制代码

论坛徽章:
0
17 [报告]
发表于 2010-04-24 12:20 |只看该作者
你1楼贴的图中,你构造的回应包是第5个吗。如果是,为什么是200 OK呢?
Godbach 发表于 2010-04-24 12:16



    这个200 OK 是另外一个HTTP Redirect的方法, 用来测试的,
  1. HTTP/1.1 200 ok
  2. Refresh: 0; url=http://www.example.com/
  3. Content-type: text/html
  4. Content-length: 78

  5. Please follow <a href="http://www.example.com/">link</a>!
复制代码

论坛徽章:
0
18 [报告]
发表于 2010-04-24 12:27 |只看该作者
中间有些代码是后来 调试加上去的
就没有删除了!

论坛徽章:
0
19 [报告]
发表于 2010-04-24 12:38 |只看该作者
呵呵,过奖了。
最近工作多,周末也来公司拉磨了。

我最近也在分析一下IDS,不过是Linux下用的。
你 ...
Godbach 发表于 2010-04-24 12:15



    没,自己弄的玩玩!!在window内核上搞搞封堵 转发之类的...
   最近回学校了,下星期答辩就解放了!

论坛徽章:
0
20 [报告]
发表于 2012-04-28 13:58 |只看该作者
LZ

_tcp_parse_options
部分的代码可否贴出来看看?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP