ipv4 program sends ipv6

ipv4 program sends ipv6

Post by Paul » Mon, 27 Mar 2006 04:58:21



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

Quote:}

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.

 
 
 

ipv4 program sends ipv6

Post by Robert Harri » Mon, 27 Mar 2006 06:17:20



> 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]

- Show quoted text -

Quote:>     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.


 
 
 

1. Using IPv4 TCPMSS target with IPv6-in-IPv4

I have a braindead ISP that filters some ICMP but doesn't seem to
realise it. (Yes, I know, but choices are few. ):) With IPv4 I have to
use,

iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

or large packets don't get through.

Now I'm playing with IPv6, again small packets get through and large
packets don't, so I'm guessing that exactly the same problem is
occurring. Unfortunately, the relevant gateway machines tend to run late
2.4 kernels, so ip6tables isn't offering a TCPMSS target.

But, that machine uses IPv6-in-IPv4 anyway (a route for 2000::/3 via
::192.88.99.1) so I am wondering: is there some magic that I can use to
make the IPv6 stuff use the IPv4 TCPMSS target, so I can get sensible
IPv6 connectivity without having to use a very recent kernel?

Mark.

--
Functional programming vacancy at http://www.aetion.com/

2. Red Hat 6.2 won't install, freezes on Adaptec 2940 controller.

3. IPv6: Allow Both IPv6 and IPv4 Sockets on the Same Port Number (IPV6_V6ONLY Support)

4. URGENT help needed: Sol 2.5.1. fails to configure network interface after patch

5. ipv4: move proc stuff from net/ipv4/af_inet.c to net/ipv4/proc.c

6. Help! PostgreSQL Won't Start

7. IPv4 vs IPv6

8. Help! mail and internetfaststart

9. what are the advantages of IPv6 over IPv4??

10. How to support IPv6 passthrough/bridge with a IPv4-only router?

11. IPv4 --> IPv6

12. (4/5) Brlock removal from ipv4/ipv6

13. IPv6 and IPv4 conversion with Linux Router.