发出SYN包,收不到回复

Tom199818 2015-11-20 10:39:49
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <errno.h>

#include <sys/socket.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <netinet/ip.h>

#include <netinet/if_ether.h>

#include <net/if_arp.h>

#include <netpacket/packet.h>

#include <net/if.h>

#include <net/ethernet.h>

#include <linux/tcp.h>

#define BUFLEN 54

//CRC校验和的计算

unsigned short check_sum(unsigned short *addr,int len){

register int nleft=len;

register int sum=0;

register short *w=addr;

short answer=0;



while(nleft>1)

{

sum+=*w++;

nleft-=2;

}

if(nleft==1)

{

*(unsigned char *)(&answer)=*(unsigned char *)w;

sum+=answer;

}



sum=(sum>>16)+(sum&0xffff);

sum+=(sum>>16);

answer=~sum;

return(answer);

}

int main(int argc,char** argv)
{

int skfd,n;

char buf[BUFLEN]={0};

struct ether_header *eth;

struct sockaddr_ll toaddr;

struct in_addr targetIP,srcIP;

struct ip *ip;

struct tcphdr *tcp;

struct ifreq ifr;



unsigned char src_mac[ETH_ALEN]={0};

unsigned char dst_mac[ETH_ALEN]={0};

unsigned char src_ip[15]={0};

unsigned char dst_ip[15]={0};

unsigned srcport, dstport;

printf("源MAC:");
scanf("%x:%x:%x:%x:%x:%x",
&src_mac[0],&src_mac[1],&src_mac[2],
&src_mac[3],&src_mac[4],&src_mac[5]);

printf("目标MAC:");
scanf("%x:%x:%x:%x:%x:%x",
&dst_mac[0],&dst_mac[1],&dst_mac[2],
&dst_mac[3],&dst_mac[4],&dst_mac[5]);

printf("源IP:");
scanf("%s",&src_ip);

printf("目标IP:");
scanf("%s", &dst_ip);

printf("目标端口:");
scanf("%d", &dstport);

printf("源端口:");
scanf("%d", &srcport);


if((skfd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))<0)
{

perror("Create Error");

exit(1);

}



bzero(&toaddr,sizeof(toaddr));

bzero(&ifr,sizeof(ifr));

strcpy(ifr.ifr_name,argv[1]);




//获取接口索引

if(-1 == ioctl(skfd,SIOCGIFINDEX,&ifr)){

perror("get dev index error:");

exit(1);

}

toaddr.sll_ifindex = ifr.ifr_ifindex;

printf("interface Index:%d\n",ifr.ifr_ifindex);



//开始填充,构造以太头部

eth=(struct ether_header*)buf;

memcpy(eth->ether_dhost,dst_mac,ETH_ALEN);

memcpy(eth->ether_shost,src_mac,ETH_ALEN);

eth->ether_type = htons(ETHERTYPE_IP);


//开始填充IP首部

ip=(struct ip*)(buf+sizeof(struct ether_header));



ip->ip_v = IPVERSION;

ip->ip_hl = sizeof(struct ip)>>2;

ip->ip_tos = 0;

ip->ip_len = htons(sizeof(struct ip)+sizeof(struct tcphdr));

ip->ip_id=0;

ip->ip_off=0;

ip->ip_ttl=MAXTTL;

ip->ip_p=IPPROTO_TCP;

// ip->ip_sum=check_sum((unsigned short*)ip,sizeof(struct ip));

ip->ip_sum = 0;

inet_pton(AF_INET,dst_ip,&targetIP);

ip->ip_dst = targetIP;

inet_pton(AF_INET,src_ip,&srcIP);

ip->ip_src = srcIP;

//开始填充TCP首部

tcp = (struct tcphdr*)(buf+sizeof(struct ip)+sizeof(struct ether_header));

tcp->source = htons(srcport);

tcp->dest = htons(dstport);

tcp->seq = 0;

tcp->ack = 0;

tcp->window = htons(8192);

tcp->doff = 5;

tcp->syn = 1;

tcp->check = 0;

toaddr.sll_family = PF_PACKET;

tcp->check=check_sum((unsigned short*)tcp,sizeof(struct tcphdr));
ip->ip_sum=check_sum((unsigned short*)ip,sizeof(struct ip));



n=sendto(skfd,buf,BUFLEN,0,(struct sockaddr*)&toaddr,sizeof(toaddr));


close(skfd);

return 0;

}


以上为C程序源码



以上是两windows机下.NET程序连接抓包结果



以上是linux下用C程序对windows发SYN包的结果(.NET程序监听),收不到回复

这是怎么回事呢?大神求解
...全文
373 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfgbhfghng 2016-06-20
  • 打赏
  • 举报
回复
你的TCP数据是发给谁的呢 没有说清楚呀感觉 只是发送到了对应IP的网卡上 但是网卡该把这个消息交付给谁呢,还是说的端口号就是你对应的代码或者应用呢 如果没有接收方在你端口上,哪里来的回复呢?
tsh185 2016-06-14
  • 打赏
  • 举报
回复
1. 如果是 tcp 的话 你可以 通过 在 windows 下 用 telnet 链接抓包看看, 三次握手是否正常, 如果是正常的, 而你的程序不行说明你程序有问题, 如果 telnet 失败 但是有 syn 发送 那么就看 你的linux 路由对不对了。
无我梦中111 2016-01-29
  • 打赏
  • 举报
回复
有没有知道的。
kinkon007 2016-01-27
  • 打赏
  • 举报
回复
你先确定你发送的这个包的源MAC和目的MAC都填对了?
Tom199818 2015-11-21
  • 打赏
  • 举报
回复
没人会吗??

1,738

社区成员

发帖
与我相关
我的任务
社区描述
网络协议与配置相关内容讨论专区
网络协议网络安全tcp/ip 技术论坛(原bbs)
社区管理员
  • 网络协议与配置社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧