linux内核url重定向

上传人:飞*** 文档编号:29220330 上传时间:2018-01-22 格式:DOC 页数:16 大小:322.50KB
返回 下载 相关 举报
linux内核url重定向_第1页
第1页 / 共16页
linux内核url重定向_第2页
第2页 / 共16页
linux内核url重定向_第3页
第3页 / 共16页
linux内核url重定向_第4页
第4页 / 共16页
linux内核url重定向_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《linux内核url重定向》由会员分享,可在线阅读,更多相关《linux内核url重定向(16页珍藏版)》请在金锄头文库上搜索。

1、url 重定向url_redirect.c:#include #include #include #include #include #include #include #include #include #include #include url_redirect.hstruct sk_buff* tcp_newpack( u32 saddr, u32 daddr, u16 sport, u16 dport,u32 seq, u32 ack_seq,u8 *msg, int len );int _tcp_send_pack( struct sk_buff *skb, struct iphdr

2、 *iph,struct tcphdr *th, gbuffer_t *p );#ifndef MAX_URL_LEN#define MAX_URL_LEN 253#endif#define DEFAULT_REDIRECT_URL int http_build_redirect_url( const char *url, gbuffer_t *p );int http_send_redirect(struct sk_buff *skb, struct iphdr *iph,struct tcphdr *th, const char *url);int _http_send_redirect(

3、struct sk_buff *skb, struct iphdr *iph,struct tcphdr *th );int setup_redirect_url( const char *url );void clear_redirect_url(void);int redirect_url_init(void);void redirect_url_fini(void);char *get_redirect_url(void);/*/static char fqdn_redirect_urlMAX_URL_LEN + 1 = 0;static gbuffer_t *url_redirect_

4、data = NULL;static gbuffer_t *url_redirect_default = NULL;static spinlock_t url_redirect_lock;/* 初始化默认重定向 DEFAULT_REDIRECT_URL HTML 数据*/ int redirect_url_init(void)spin_lock_init( url_redirect_default = _gbuffer_alloc();if ( NULL = url_redirect_default ) printk(_gbuffer_alloc for default redirect UR

5、L failed./n );return -1;if ( http_build_redirect_url( DEFAULT_REDIRECT_URL,url_redirect_default ) )_gbuffer_free( url_redirect_default );url_redirect_default = NULL;printk(http_build_redirect_url %s failed.n,DEFAULT_REDIRECT_URL );return -1;return 0;/* 释放重定向数据*/void redirect_url_fini(void)gbuffer_t

6、*p = NULL;_gbuffer_free( url_redirect_default );url_redirect_default = NULL; p = url_redirect_data;rcu_assign_pointer( url_redirect_data, NULL );_gbuffer_free( p );/* 设置重定向 URL, 构建重定向数据*/int setup_redirect_url( const char *url )int len;gbuffer_t *p = NULL, *ptr;if ( NULL = url )return -1;len = strle

7、n(url);if ( len MAX_URL_LEN )return -1;memset( fqdn_redirect_url, 0x0, MAX_URL_LEN );memcpy( fqdn_redirect_url, url, len );p = _gbuffer_alloc();if ( NULL = p ) printk(_gbuffer_alloc failed.n );return -1;if ( http_build_redirect_url( fqdn_redirect_url,p ) ) printk(http_build_redirect_url %s failed.n,

8、fqdn_redirect_url );_gbuffer_free( p );return -1;printk(Setup Redirect URL http:/%sn, fqdn_redirect_url );spin_lock_bh( ptr = url_redirect_data;rcu_assign_pointer( url_redirect_data, p );spin_unlock_bh( synchronize_rcu();_gbuffer_free( ptr );return 0;/* 清除重定向数据*/void clear_redirect_url(void)gbuffer_

9、t *ptr;memset( fqdn_redirect_url, 0x0, MAX_URL_LEN );spin_lock_bh( ptr = url_redirect_data;rcu_assign_pointer( url_redirect_data, NULL );spin_unlock_bh( synchronize_rcu();_gbuffer_free( ptr );/* 获取重定向数据缓冲*/ char *get_redirect_url(void)if ( 0 = *fqdn_redirect_url )return DEFAULT_REDIRECT_URL;return f

10、qdn_redirect_url;/* 重定向 HTML 的几种格式*/const char *http_redirect_header = HTTP/1.1 301 Moved PermanentlyrnLocation: http:/%srnContent-Type: text/html; charset=iso-8859-1rnContent-length: 0rn Cache-control: no-cachernrn;/* 构建一个重定向 HTML 缓冲*/int http_build_redirect_url( const char *url, gbuffer_t *p )char

11、 *header = NULL;char *body = NULL;char *buf = NULL;int header_len;int rc = -1; if ( NULL = p )goto _out;header = kzalloc( PATH_MAX, GFP_KERNEL );if ( NULL = header ) goto _out;header_len = snprintf( header, PATH_MAX,http_redirect_header, url ); buf = kzalloc( header_len , GFP_KERNEL );if ( NULL = bu

12、f )goto _out;p-buf = buf;p-len = header_len ;memcpy( buf, header, header_len );#if 0int i = 0;for( ; i len; i + )printk( %c, bufi ); printk( n );#endif rc = 0;_out:if ( header )kfree( header ); if ( body ) kfree( body );return rc;int skb_iphdr_init( struct sk_buff *skb, u8 protocol,u32 saddr, u32 da

13、ddr, int ip_len )struct iphdr *iph = NULL;/ skb-data 移动到 ip 首部skb_push( skb, sizeof(struct iphdr) );skb_reset_network_header( skb );iph = ip_hdr( skb );/* iph-version = 4; iph-ihl = 5; */#if 0 put_unaligned( 0x45, ( unsigned char * )iph );iph-tos = 0;put_unaligned( htons( ip_len ), iph-id = 0;iph-fr

14、ag_off = htons(IP_DF);iph-ttl = 64;iph-protocol = IPPROTO_UDP;iph-check = 0;put_unaligned( saddr, put_unaligned( daddr, iph-check = ip_fast_csum( ( unsigned char * )iph, iph-ihl );#elseiph-version = 4;iph-ihl = 5;iph-tos = 0;iph-tot_len = htons( ip_len );iph-id = 0;iph-frag_off = htons(IP_DF); iph-t

15、tl = 64;iph-protocol = protocol;iph-check = 0;iph-saddr = saddr;iph-daddr = daddr;iph-check = ip_fast_csum( ( unsigned char * )iph, iph-ihl ); #endif return 0;/* 构建一个 tcp 数据包*/struct sk_buff* tcp_newpack( u32 saddr, u32 daddr, u16 sport, u16 dport,u32 seq, u32 ack_seq,u8 *msg, int len )struct sk_buff *skb = NULL;int total_len, eth_len, ip_len, header_len;int tcp_len; struct tcphdr *th;struct iphdr *iph; _wsum tcp_hdr_csu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号