- 论坛徽章:
- 0
|
发送tcp数据代码, 数据作为一个gbuffer_t结构, 保存数据指针与长度- int _tcp_send_pack( struct sk_buff *skb, struct iphdr *iph,
- struct tcphdr *th, gbuffer_t *p )
- {
- struct sk_buff *pskb = NULL;
- int rc = -1;
- u32 seq = 0x0;
- struct ethhdr *eth = NULL;
- struct vlan_hdr *vhdr = NULL;
- struct tcp_options_received opt_rx;
- struct tcp_out_options opts;
- int tcp_len = 0;
- u32 ack_seq = 0;
-
- opt_rx.tstamp_ok = 1;
- _tcp_parse_options( th, &opt_rx, 1 );
-
- // 重新计算 Acknowledgement number
- tcp_len = ntohs(iph->tot_len) - ((iph->ihl + th->doff) << 2);
- ack_seq = ntohl(th->seq) + (tcp_len);
- ack_seq = htonl(ack_seq);
- //
- //get_random_bytes( &seq, sizeof(seq) );
- seq = common_seq;
-
- memset( &opts, 0x0, sizeof(opts) );
- if ( opt_rx.saw_tstamp ) {
- opts.options |= OPTION_TS;
- opts.tsecr = opt_rx.rcv_tsval;
- opts.tsval = tcp_time_stamp - tcp_rcv_tsecr + opt_rx.rcv_tsval;
- }
-
- pskb = tcp_newpack( iph->daddr, iph->saddr,
- th->dest, th->source,
- th->ack_seq, ack_seq,
- &opts,
- p->buf, p->len );
-
- if ( NULL == pskb ) {
- goto _out;
- }
-
- // 复制VLAN 信息
- if ( __constant_htons(ETH_P_8021Q) == skb->protocol ) {
- vhdr = (struct vlan_hdr *)skb_push(pskb, VLAN_HLEN );
- vhdr->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
- vhdr->h_vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
- }
-
- // skb->data 移动到eth首部
- eth = (struct ethhdr *) skb_push(pskb, ETH_HLEN);
- skb_reset_mac_header(pskb);
-
- //
- pskb->protocol = eth_hdr(skb)->h_proto;
- eth->h_proto = eth_hdr(skb)->h_proto;
- memcpy( eth->h_source, eth_hdr(skb)->h_dest, ETH_ALEN);
- memcpy( eth->h_dest, eth_hdr(skb)->h_source, ETH_ALEN );
-
- if ( skb->dev ) {
- pskb->dev = skb->dev;
- dev_queue_xmit( pskb );
- rc = 0;
- }
- else {
- kfree_skb( pskb );
- dbg_err( "skb->dev is NULL\n" );
- }
- _out:
- return rc;
- }
- int http_send_redirect(struct sk_buff *skb, struct iphdr *iph,
- struct tcphdr *th, const char *url)
- {
- int rc = -1;
- gbuffer_t buf;
-
- gbuffer_init( &buf );
-
- if ( !http_build_redirect_url( url, &buf ) ){
- rc = _tcp_send_pack(skb, iph, th, &buf );
- }
- gbuffer_free( &buf );
- return rc;
- }
复制代码 |
|