#include #include #include #include #include #include #include unsigned short 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 (int argc, char** argv) { char datagram[4096]; struct sockaddr_in sin; int fd = socket (PF_INET, SOCK_RAW, IPPROTO_UDP); struct iphdr *iph = (struct iphdr *) datagram; struct udphdr *udph = (struct udphdr *) datagram + sizeof (struct iphdr); sin.sin_family = AF_INET; sin.sin_port = htons (5070); sin.sin_addr.s_addr = inet_addr ("213.248.63.122"); memset (datagram, 0, 4096); iph->ihl = 5; iph->version = 4; iph->tos = 0; iph->tot_len = sizeof (struct iphdr) + sizeof (struct udphdr); /* no payload */ iph->id = htonl (54321); /* the value doesn't matter here */ iph->frag_off = 0; iph->ttl = 255; iph->protocol = 6; iph->check = 0; /* set it to 0 before computing the actual checksum later */ iph->saddr = inet_addr ("192.168.0.1");/* SYN's can be blindly spoofed */ iph->daddr = sin.sin_addr.s_addr; udph->source = htonl (1234); udph->dest = htonl (5070); udph->len = sizeof (struct udphdr); /* no payload curently */ udph->check = 0; /* system IP stack will do the job */ iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1); int one = 1; const int *val = &one; if (setsockopt (fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) printf ("Warning: Cannot set HDRINCL!\n"); if (sendto (fd, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0) printf ("error\n"); else printf ("Success."); return 0; }