Networking Forums

Networking Forums > Computer Networking > Linux Networking > ipv4 program sends ipv6

Reply
Thread Tools Display Modes

ipv4 program sends ipv6

 
 
Paul
Guest
Posts: n/a

 
      03-25-2006, 06:58 PM
Hi !

Watch this code I've written.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>


int main( int argc , char** argv ) {

if( argc < 2 ) { printf("Error: too few arguments.IP address missing
\n"); exit(1); }

unsigned int packetsize = sizeof( struct iphdr ) + sizeof( struct
tcphdr );
unsigned char* packet = malloc( packetsize );
memset( packet , 0 , packetsize );

int uid = getuid();
if( uid != 0 ) { printf("Error: you must be root.\n"); exit(1); }

int socket_fd = socket( AF_INET , SOCK_RAW , IPPROTO_TCP );
if( socket_fd == -1 ) { printf("Error: allocating socket.\n");
exit(1); }

int one = 1;
if( setsockopt(socket_fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) ==
-1 ) { printf("Error: setsockopt.\n"); exit(1); }

struct iphdr* ip = (struct iphdr*) packet;
ip->version = 4;
ip->ihl = 5;
ip->id = htonl( random() );
ip->saddr = inet_addr("192.168.2.11");
ip->daddr = inet_addr( argv[1] );
ip->ttl = 255;
ip->protocol = IPPROTO_IP;
ip->tot_len = packetsize;
ip->check = 0; //kernel calculates checksum

struct tcphdr* tcp = (struct tcphdr*) ( packet + sizeof( struct
iphdr ) );
tcp->source = htons( 46723 ); //port is radom doesn't matter
tcp->dest = htons( 80 );
tcp->ack_seq = htonl(0);
tcp->seq = htonl(1);
tcp->syn = 1; //start a tcp com. , so syn is set
tcp->window = htons(1024);
tcp->check = 0; //kernel calculates checksum

struct sockaddr_in* addr = malloc( sizeof( struct sockaddr_in ) );
addr->sin_family = AF_INET;
addr->sin_port = tcp->source;
addr->sin_addr.s_addr = ip->saddr;

if( ( sendto( socket_fd , packet , packetsize , 0 , ( struct sockaddr* )
addr , sizeof( struct sockaddr_in ) ) ) == -1 ) { printf("Error: send");
exit(1); }

}

It sends a tcp package. But if I run ethereal to capture the network traffic
it displays this: http://wecdhcje.homepage.t-online.de/my_com.jpg

A normal tcp package ( here with opera ) looks like this:
http://wecdhcje.homepage.t-online.de/my_com_2.jpg

It has a TCP block which can be watched with ethereal. Although I've defined
a tcp header, a tcp block doesn't appear in ethereal ? What's missing in my
code? I do not understand.
 
Reply With Quote
 
 
 
 
Robert Harris
Guest
Posts: n/a

 
      03-25-2006, 08:17 PM
Paul wrote:
> Hi !
>
> Watch this code I've written.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
>
> #include <sys/types.h>
> #include <arpa/inet.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <netinet/ip.h>
> #include <netinet/tcp.h>
>
>
> int main( int argc , char** argv ) {
>
> if( argc < 2 ) { printf("Error: too few arguments.IP address missing
> \n"); exit(1); }
>
> unsigned int packetsize = sizeof( struct iphdr ) + sizeof( struct
> tcphdr );
> unsigned char* packet = malloc( packetsize );
> memset( packet , 0 , packetsize );
>
> int uid = getuid();
> if( uid != 0 ) { printf("Error: you must be root.\n"); exit(1); }
>
> int socket_fd = socket( AF_INET , SOCK_RAW , IPPROTO_TCP );
> if( socket_fd == -1 ) { printf("Error: allocating socket.\n");
> exit(1); }
>
> int one = 1;
> if( setsockopt(socket_fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) ==
> -1 ) { printf("Error: setsockopt.\n"); exit(1); }
>
> struct iphdr* ip = (struct iphdr*) packet;
> ip->version = 4;
> ip->ihl = 5;
> ip->id = htonl( random() );
> ip->saddr = inet_addr("192.168.2.11");
> ip->daddr = inet_addr( argv[1] );
> ip->ttl = 255;
> ip->protocol = IPPROTO_IP;


should be IPPROTO_TCP

[Robert]

> ip->tot_len = packetsize;
> ip->check = 0; //kernel calculates checksum
>
> struct tcphdr* tcp = (struct tcphdr*) ( packet + sizeof( struct
> iphdr ) );
> tcp->source = htons( 46723 ); //port is radom doesn't matter
> tcp->dest = htons( 80 );
> tcp->ack_seq = htonl(0);
> tcp->seq = htonl(1);
> tcp->syn = 1; //start a tcp com. , so syn is set
> tcp->window = htons(1024);
> tcp->check = 0; //kernel calculates checksum
>
> struct sockaddr_in* addr = malloc( sizeof( struct sockaddr_in ) );
> addr->sin_family = AF_INET;
> addr->sin_port = tcp->source;
> addr->sin_addr.s_addr = ip->saddr;
>
> if( ( sendto( socket_fd , packet , packetsize , 0 , ( struct sockaddr* )
> addr , sizeof( struct sockaddr_in ) ) ) == -1 ) { printf("Error: send");
> exit(1); }
>
> }
>
> It sends a tcp package. But if I run ethereal to capture the network traffic
> it displays this: http://wecdhcje.homepage.t-online.de/my_com.jpg
>
> A normal tcp package ( here with opera ) looks like this:
> http://wecdhcje.homepage.t-online.de/my_com_2.jpg
>
> It has a TCP block which can be watched with ethereal. Although I've defined
> a tcp header, a tcp block doesn't appear in ethereal ? What's missing in my
> code? I do not understand.

 
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
libnetfilter_conntrack with IPv4 & IPv6 Washington Ratso Linux Networking 0 07-07-2011 10:07 PM
IPv6 address not connecting but IPv4-mapped -IPv6 does. Please help. DanielJohnson Network Routers 0 01-07-2009 12:10 AM
Using IPv4 TCPMSS target with IPv6-in-IPv4 Mark T.B. Carroll Linux Networking 1 03-18-2007 10:30 AM
IPv6 to IPv4 tjm Windows Networking 0 03-23-2006 07:53 PM
IPv4 --> IPv6 Anthony T. Wilson Linux Networking 2 12-20-2003 08:23 PM



1 2 3 4 5 6 7 8 9 10 11