Re: отпрвка syn пакета
От: alexora  
Дата: 16.01.04 16:43
Оценка:
Народ, это вообще глюк, я с Linux перенес код под Free и все равно не катит.
sendto мне пишет, что отправлено 40 байт, но не в tcpdump, не в netstat ничего не видно.

Ниже укзаный код, просто напросто должен отправлять syn запрос :


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>

#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif
#ifndef __USE_BSD
#define __USE_BSD
#endif
#ifndef __BSD_SOURCE
#define __BSD_SOURCE
#endif

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#define P 25 

unsigned short csum(unsigned short *ptr,int nbytes)
{
    register long           sum;            /* assumes long == 32 bits */
        u_short                 oddbyte;
        register u_short        answer;         /* assumes u_short == 16 bits */
 
        sum = 0;
        while (nbytes > 1)  {
                sum += *ptr++;
                nbytes -= 2;
        }
                                 /* mop up an odd byte, if necessary */
        if (nbytes == 1) {
                oddbyte = 0;            /* make sure top half is zero */
                *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
                sum += oddbyte;
        }
 
        sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
        sum += (sum >> 16);                     /* add carry */
        answer = ~sum;          /* ones-complement, then truncate to 16 bits */
        return(answer);
}

int main (void) 
{ 
    int s = socket (PF_INET, SOCK_RAW, 255); 

    char datagram[4096]; 
    struct ip *iph = (struct ip *) datagram; 
    struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct ip); 
    struct sockaddr_in sin; 
    
    int sendbytes;
    int  on = 1;
    int  index;

    sin.sin_family=AF_INET; 
    sin.sin_port = htons (80); 
    sin.sin_addr.s_addr = inet_addr ("212.23.65.112"); 

     memset (datagram, 0, 4096); 

    iph->ip_hl=5; 
    iph->ip_v=4; 
    iph->ip_tos=0; 
    iph->ip_len=sizeof (struct ip) + sizeof (struct tcphdr); 

    iph->ip_id=htonl (index + 2345); 
    iph->ip_off=0; 
    iph->ip_ttl=255; 
    iph->ip_p=6; 
    iph->ip_sum=0; 
    iph->ip_src.s_addr=inet_addr ("181.182.283.284"); 
    iph->ip_dst.s_addr=sin.sin_addr.s_addr; 

    tcph->th_sport=htons (1234); 
    tcph->th_dport=htons (P); 
    tcph->th_seq = 23432 + index; 
    tcph->th_ack=index + 54321; 
    tcph->th_x2=0; 
    tcph->th_off=0; 
    tcph->th_flags=TH_SYN | TH_URG; 
    tcph->th_win=htonl (65535); 
    tcph->th_sum=0; 
    tcph->th_urp=0; 

        tcph->th_sum = csum ((unsigned short *) datagram, (sizeof (struct ip) + sizeof (struct tcphdr) + 1) & ~1);
        iph->ip_sum   = csum ((unsigned short *) datagram, iph->ip_len >> 1);
    
        if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0)
        {
         fprintf(stderr, "error setsockopt\n");
         exit(1);
    }
    
    sendbytes = sendto(s, datagram, 40 , 0 , (struct sockaddr *)&sin, sizeof(sin));
    printf("Send %i bytes\n",sendbytes);  
}



Может кто то знает в чем бага ?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.