LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2008, 12:03 PM   #1
darell
LQ Newbie
 
Registered: Oct 2008
Posts: 7

Rep: Reputation: 0
[raw sockets, C] tcp header cut


hi @all,

I want to send a tcp syn paket(and later answer to acks) via Raw Sockets in C.
My Problem is, that the tcp-header in my outgoing paket is cut.

The Sniffer detect this:
Code:
bogus tcp header length(0, must be at least 20)
Here is the code:
Code:
int send_tcp_syn()
{
    int sock, one = 1;

    if( (sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) {
        perror("building socket");
        return -1;
    }
    paket_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
    paket = (unsigned char*) malloc(paket_size);
        
    struct iphdr *ip = (struct iphdr  *) paket;                        
    struct tcphdr *tcp = (struct tcphdr *) (paket + sizeof(struct iphdr));
    memset(paket, 0, paket_size);

/* set ip attributes */
    ip -> version    = 4;
    ip -> ihl    = 5;
    ip -> ttl    = 255;
    ip -> id    = htonl( random() );
    ip -> daddr       = inet_addr(HOST_IP);
    ip -> saddr    = inet_addr( get_random_ip() );
    ip -> protocol    = IPPROTO_TCP;
    ip -> tot_len     = paket_size;

/* tcp attributes */
    tcp -> source     = htons(HTTP_PORT);
    tcp -> dest         = htons(HTTP_PORT);
    tcp -> seq          = htonl(1000000000);
    tcp -> ack_seq  = htonl(1000000000);
    tcp -> syn          = 1;
    tcp -> window  = htons(1024);
    tcp -> check      = 0;

    host.sin_family     = AF_INET;
    host.sin_port        = tcp -> dest;
    host.sin_addr.s_addr     = ip  -> daddr;
    
    if( setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &one, 1) == -1) {    
        perror("setsockopt");
        return -2;
    }
    if( sendto(sock, paket, paket_size, 0, (struct sockaddr *)&host,  
                                           sizeof(host))     < 0 ) {
        perror("sendto");
        return -3;
    }
    return 0;
}
Thank you for helping me
 
Old 10-09-2008, 08:55 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You need to set the data offset (tcp->doff) to the 4 byte rounded start of your data (basically tcp header size). It needs to be set even if its an empty packet.
 
Old 10-09-2008, 11:04 AM   #3
darell
LQ Newbie
 
Registered: Oct 2008
Posts: 7

Original Poster
Rep: Reputation: 0
hi estabroo,

It work's;
Thank you very much, I posted the problem in a lot of boards and a long time no one was able to resolve it.

Next time I will post it here at first.

Darell
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
raw sockets in c wee-face Programming 1 02-01-2007 09:35 AM
Raw Sockets Srikanth0210 Programming 2 12-05-2005 03:22 AM
raw sockets and C wrongman Programming 3 05-04-2004 02:17 PM
raw sockets BashTin Programming 1 06-07-2003 06:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration