Ponizszy kod ma za zadanie budowac pakiet eth+ip+tcp lecz niestety naglowek eth nie jest dodawany.
Czekam na wasze propozycje co do poprawy tego zrodla.
#define __USE_BSD /* use bsd'ish ip header */
#include <sys/socket.h> /* these headers are for a Linux system, but */
#include <netinet/in.h> /* the names on other systems are easy to guess.. */
#include <netinet/ip.h>
#include <net/ethernet.h>
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <netinet/tcp.h>
#include <unistd.h>
#define P 25 /* lets flood the sendmail port */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main (void)
{
int s = socket (PF_INET, SOCK_RAW, htons(ETH_P_ALL));
int packetsize = sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct tcphdr);
char datagram[packetsize];
struct ether_header *eth = (struct ether_header *) datagram;
struct ip *iph = (struct ip*) (datagram + sizeof(struct ether_header));
struct tcphdr *tcph = (struct tcphdr*)
(datagram + sizeof(struct ether_header) +sizeof(struct ip));
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (P);
sin.sin_addr.s_addr = inet_addr ("2.2.2.2");
memset (datagram, 0, packetsize);
char smac[]="11:11:11:11:11:11";
char dmac[]="22:22:22:22:22:22";
memcpy(eth->ether_dhost,(u_char *)ether_aton(dmac),ETH_ALEN); // Destination MAC
memcpy(eth->ether_shost,(u_char *)ether_aton(smac),ETH_ALEN); // Source MAC
eth->ether_type = htons(ETHERTYPE_IP); // IP Protokoll
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof(struct ether_header)+sizeof (struct ip) + sizeof (struct tcphdr);
iph->ip_id = htonl (54321);
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0;
iph->ip_src.s_addr = inet_addr ("1.1.1.1");
iph->ip_dst.s_addr = sin.sin_addr.s_addr;
tcph->th_sport = htons (1234);
tcph->th_dport = htons (P);
tcph->th_seq = random ();
tcph->th_ack = 0;
tcph->th_x2 = 0;
tcph->th_off = 0;
tcph->th_flags = TH_SYN;
tcph->th_win = htonl (65535);
tcph->th_sum = 0;
tcph->th_urp = 0;
iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
{
int one = 1;
const int *val = &one;
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
printf ("Warning: Cannot set HDRINCL!\n");
}
if (sendto (s,
datagram,
iph->ip_len,
0,
(struct sockaddr *) &sin,
sizeof (sin)) < 0)
printf ("error\n");
else
printf ("WYSLALEM \n");
return 0;
}