Ive created some socket programs in c++ before and used beej's guide, and I have decided to advance towards raw sockets. Im trying to work through this tutorial: http://mixter.void.ru/rawip.html but so far, I have been unsuccessfull.
I understand the theory behind this fairly well, although I have some questions:
1. can some one please post working source code for a raw socket client (& server) or tell me what is wrong with the code at http://mixter.void.ru/rawip.html? my code so far (copied and pasted from the tutorial, except for the libraries):
- Code: Select all
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <iostream>
#define Port 25
using namespace std;
int main ( void )
{
cout << "hello world\n";
//create socket:
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP); /* open raw socket */
char datagram[4096]; /* this buffer will contain ip header, tcp header,
and payload. we'll point an ip header structure
at its beginning, and a tcp header structure after
that to write the header values into it */
struct ip *iph = (struct ip *) datagram;
struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct ip);
struct sockaddr_in sin;
/* the sockaddr_in containing the dest. address is used
in sendto() to determine the datagrams path */
return 0;
}
compile errors (g++):
- Code: Select all
sourcefile.cpp: In function ‘int main()’:
sourcefile.cpp:30: error: invalid application of ‘sizeof’ to incomplete type ‘ip’
sourcefile.cpp:30: error: invalid use of incomplete type ‘struct tcphdr’
sourcefile.cpp:30: error: forward declaration of ‘struct tcphdr’
2. With a raw socket server, how would one listen() and read the packet being recved (to verify which IP it came from etc)? also, how do you fill out the ip header and tcp header structures (from an incoming packet)?
3. How do you find the MAC address of the sender of a packet without using arp tables? because the Physical Layer is above the TCP/IP layer, which means when the kernel gives the packet to my program, it will not have the source and destination MAC addresses correct?
Also, if anyone has found a better tutorial for raw sockets, I would appreciate a link
Thanks for any help given!
PS. I run Fedora 10, not particularly familiar on the linux OS yet.

