Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Sendto()仍然返回已发送的0字节_C_Sockets_Gentoo_Sendto - Fatal编程技术网

C Sendto()仍然返回已发送的0字节

C Sendto()仍然返回已发送的0字节,c,sockets,gentoo,sendto,C,Sockets,Gentoo,Sendto,几天前,我写了一篇关于sendto()返回的错误的帖子。 它返回了0个字节,但我传递了一个非空值作为长度 void caught_packet(u_char *user_args,const struct pcap_pkthdr *cap_header,const u_char *packet){ int bcount,sockfd; //Bytes written and Socket Descriptor char new_packet[sizeof(struct ip_hd

几天前,我写了一篇关于sendto()返回的错误的帖子。 它返回了0个字节,但我传递了一个非空值作为长度

void caught_packet(u_char *user_args,const struct pcap_pkthdr *cap_header,const u_char *packet){
    int bcount,sockfd; //Bytes written and Socket Descriptor
    char new_packet[sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)]; //New Packet
    struct ip_hdr *iphdr; //IP Header
    struct tcp_hdr *tcphdr; //TCP Header
    struct ip_hdr *iphdr_new; //New IP Header
    struct tcp_hdr *tcphdr_new; //New TCP Header
    struct sockaddr_in sin; //sockaddr_in
    u_long src_ip,dest_ip; //source ip,dest ip
    int src_port,dest_port; //source port,dest port

    //Initialize New_Packer
    memset(new_packet,0,(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));

    //Assign to sockfd argument passed by callback function
    sockfd = *((int *)user_args);

    //Assign old header to iphdr and tcphdr
    iphdr = (struct ip_hdr *)(packet + sizeof(struct ether_header));
    tcphdr = (struct tcp_hdr *)(packet + sizeof(struct ether_header) + sizeof(struct ip_hdr));

    //Assign new header to buffer new_packet converting its address in structs
    iphdr_new = (struct ip_hdr *) new_packet;
    tcphdr_new = (struct tcp_hdr *) (new_packet + sizeof(struct ip_hdr));
    src_ip = ntohl(iphdr -> ip_src_addr);
    dest_ip = ntohl(iphdr -> ip_dest_addr);
    src_port = ntohs(tcphdr -> tcp_src_port);
    dest_port = ntohs(tcphdr -> tcp_dest_port);
    sin.sin_family = AF_INET;

    //pretends to be source port
    sin.sin_port = htons(src_port);

    //pretends to be source address
    sin.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)&src_ip)) );

    //Filling up new_iphdr
    //TOS
    iphdr_new->ip_tos = IPTOS_LOWDELAY;
    //TOT_LEN
    iphdr_new->ip_len = sizeof(struct ip_hdr)+sizeof(struct tcp_hdr);
    //ID
    iphdr_new->ip_id = htons((u_int16_t)random_number(3));
    //FRAG_OFF
    iphdr_new->ip_frag_offset = htons((u_int16_t)random_number(3));
    //TTL
    iphdr_new->ip_ttl = IPDEFTTL;
    //PROTOCOL
    iphdr_new->ip_type = IPPROTO_TCP;
    //Source address (Dest) <-->
    iphdr_new->ip_src_addr = htonl(dest_ip);
    //Destination address (Source) <-->
    iphdr_new->ip_dest_addr = htonl(src_ip);

    //Filling up new_tcphdr
    //Source port (Dest) <-->
    tcphdr_new->tcp_src_port = htons(dest_port);
    //Destination port (Source) <-->
    tcphdr_new->tcp_dest_port = htons(src_port);
    //SEQ (ACK) <-->
    tcphdr_new->tcp_seq = tcphdr->tcp_ack;
    //ACK  (SEQ) <-->
    tcphdr_new->tcp_ack = (tcphdr->tcp_seq + 1) ;
    //Flags  
    tcphdr_new->tcp_flags = TCP_SYN|TCP_ACK;
    //Window Size
    tcphdr_new->tcp_window = htons((u_int16_t)random_number(3));
    //Checksum (Done by Kernel)
    tcphdr_new->tcp_checksum = 0;
    //Urgent Pointer
    tcphdr_new->tcp_urgent = 0;
    //CheckSum Ip header
    iphdr_new->ip_checksum = csum((unsigned short *)new_packet,sizeof(struct ip_hdr) + sizeof(struct tcp_hdr));

    //Deliver packet to destination
    if(bcount = sendto (sockfd,new_packet,iphdr_new->ip_len,0,(struct sockaddr *)&sin,sizeof(sin))<0)
        printf("Couldn't send RESET Packet\n");
    else{
        printf("Bing! ");
        printf("(%d) bytes sent \n",bcount);
    }
}
void capture_数据包(u_char*user_参数,const struct pcap\u pkthdr*cap_头,const u_char*packet){
int bcount,sockfd;//写入的字节和套接字描述符
char new_packet[sizeof(struct ip_hdr)+sizeof(struct tcp_hdr)];//新数据包
结构ip_hdr*iphdr;//ip头
struct tcp_hdr*tcphdr;//tcp头
struct ip_hdr*iphdr_new;//新建ip头
struct tcp_hdr*tcphdr_new;//新建tcp头
sin中的struct sockaddr\u;//sin中的sockaddr\u
u_long src_ip,dest_ip;//源ip,dest ip
int src_port,dest_port;//源端口,dest端口
//初始化新的包装器
memset(新的_数据包,0,(sizeof(struct ip_hdr)+sizeof(struct tcp_hdr));
//分配给回调函数传递的sockfd参数
sockfd=*((int*)用户参数);
//将旧标头分配给iphdr和tcphdr
iphdr=(结构ip_hdr*)(数据包+sizeof(结构以太网头));
tcphdr=(结构tcp_hdr*)(数据包+sizeof(结构以太网头)+sizeof(结构ip_hdr));
//将新的头分配给缓冲区新的\u数据包,并在结构中转换其地址
iphdr_new=(结构ip_hdr*)新_数据包;
tcphdr_new=(struct tcp_hdr*)(new_packet+sizeof(struct ip_hdr));
src_ip=ntohl(iphdr->ip_src_addr);
目的地ip=NTHOL(iphdr->ip目的地地址);
src_port=ntohs(tcphdr->tcp_src_port);
dest\u port=ntohs(tcphdr->tcp\u dest\u port);
sin.sin_family=AF_INET;
//假装是源端口
sin.sin_端口=htons(src_端口);
//假装是源地址
sin.sin_addr.s_addr=inet_addr(inet_ntoa(*(struct in_addr*)和src_ip));
//填充新的\u iphdr
//托斯
iphdr\u新建->ip\u tos=IPTOS\u低延迟;
//托特伦
iphdr_new->ip_len=sizeof(struct ip_hdr)+sizeof(struct tcp_hdr);
//身份证
iphdr_new->ip_id=htons((u_int16_t)随机数(3));
//散架
iphdr_new->ip_frag_offset=htons((u_int16_t)随机数(3));
//TTL
iphdr\u新建->ip\u ttl=IPDEFTTL;
//协议
iphdr_新建->ip_类型=IPPROTO_TCP;
//源地址(Dest)
iphdr\u new->ip\u src\u addr=htonl(dest\u ip);
//目标地址(源)
iphdr\u new->ip\u dest\u addr=htonl(src\u ip);
//填充新的\u tcphdr
//源端口(Dest)
tcphdr\u new->tcp\u src\u port=htons(dest\u port);
//目标端口(源)
tcphdr\u new->tcp\u dest\u port=htons(src\u port);
//序号(确认)
tcphdr_new->tcp_seq=tcphdr->tcp_ack;
//确认(序号)
tcphdr_new->tcp_ack=(tcphdr->tcp_seq+1);
//旗帜
tcphdr_new->tcp_flags=tcp_SYN | tcp_ACK;
//窗口大小
tcphdr_new->tcp_window=htons((u_int16_t)随机数(3));
//校验和(由内核完成)
tcphdr_new->tcp_校验和=0;
//紧急指针
tcphdr_新建->tcp_紧急=0;
//校验和Ip报头
iphdr_new->ip_checksum=csum((无符号短*)新_数据包,sizeof(struct ip_hdr)+sizeof(struct tcp_hdr));
//将数据包传送到目的地

如果(bcount=sendto(sockfd,new_packet,iphdr_new->ip_len,0,(struct sockaddr*)&sin,sizeof(sin))您忘记了括号:

if( ( bcount = sendto (sockfd,new_packet,iphdr_new->ip_len,0,(struct sockaddr *)&sin,sizeof(sin))<0) )
等于`

if (bcount = (sendto( .. ) < 0) )

你忘了括号:

if( ( bcount = sendto (sockfd,new_packet,iphdr_new->ip_len,0,(struct sockaddr *)&sin,sizeof(sin))<0) )
等于`

if (bcount = (sendto( .. ) < 0) )

bcount=sendto(sockfd,new_packet,iphdr_新->ip_len,0,(struct sockaddr*)和sin,sizeof(sin))ip_len,0,(struct sockaddr*)和sin,sizeof(sin))<0
bcount=sendto(sockfd,new_packet,iphdr_新->ip_len,0,(struct sockaddr*)和sin,sizeof(sin))ip_len,0,(struct sockaddr*),sizeof(sin)))<0

打印的确切内容是什么?”(0)字节发送”?为什么您要制作自己的TCP数据包,而不仅仅是通过
connect(2)
建立连接,然后通过
send(2)
发送?打印的确切内容是什么?”(0)字节发送”?为什么您要制作自己的TCP数据包,而不仅仅是使用
connect(2)
建立连接并使用
send(2)
发送?