0

Some days ago i wrote a post about an error returned from sendto() . It returned me 0 bytes written tough i pass a non-null value as length.

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);
    }
}

This is my code. the ip_hdr and tcp_hdr structs are my structs to make a simple ip and tcp header. It's so weird.It always worked in other softwares . Please help me.

4

2 回答 2

4

你忘了括号:

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

由于赋值运算符 ( =) 的优先级低于小于运算符 ( <),因此表达式

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

等于`

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

并且由于发送了多个字节,因此它等于

if (bcount = 0 )
于 2012-10-09T20:11:49.867 回答
2

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

问题是由运算符优先级引起的,您需要括号。

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

于 2012-10-09T20:12:23.447 回答