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.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
REmaxer
  • 1
  • 3

2 Answers2

4

You forgot the parentheses:

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

since the assignment operator (=) has lower precedence than the smaller-than operator (<), the expression

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

is equal to `

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

and since more than zero bytes were sent, it is equal to

if (bcount = 0 )
MByD
  • 135,866
  • 28
  • 264
  • 277
2

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

the problem is caused by operator precedence, you need parentheses.

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

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176