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 trafficQuote:}
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.