hello!
my packetgenerator, isnt working properly.
when i use tcpdump to look at the packets i am sending they dont equal the ones i was specifying in the source code. this isnt working.
Code:
#include <string.h>
#include <stdio.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/slab.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
char datagram[4096];
struct iphdr *iphead = (struct iphdr *)datagram;
struct tcphdr *tcphead = (struct tcphdr *)datagram + sizeof(iphead);
void help(void);
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 opt;
unsigned int count, i;
int sockfd;
struct sockaddr_in dest;
int sin_size;
char buffer[2048];
int destport, sourceport;
char *sourceip, *destip;
const int on = 1;
srand(time(NULL));
if(getuid() != 0){
fprintf(stderr, "you must be r00t to build packets\n");
exit(1);
}
while((opt = getopt(argc, argv, "S:D:P:p:c:")) != EOF){
switch(opt){
case 'S': { sourceip = optarg; break; }
case 'D': { destip = optarg; break; }
case 'P': { sourceport = atoi(optarg); break; }
case 'p': { destport = atoi(optarg); break; }
case 'c': { count = atoi(optarg); break; }
default: { help(); break; }
}
}
if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1){
perror("socket");
exit(1);
}
sin_size = sizeof(struct sockaddr);
dest.sin_family = AF_INET;
dest.sin_port = htons(destport);
dest.sin_addr.s_addr = inet_addr(destip);
memset(&(dest.sin_zero), '\0', 8);
memset(datagram, 0, 4096);
memset(iphead, 0, sizeof(iphead));
memset(tcphead, 0, sizeof(tcphead));
iphead->ihl = 5;
iphead->version = 4;
iphead->tos = 0;
iphead->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
iphead->id = htons(getpid());
iphead->frag_off = 0;
iphead->ttl = 255;
iphead->protocol = 6;
iphead->check = csum((unsigned short *)datagram, iphead->tot_len >> 1);
iphead->saddr = INADDR_ANY;
iphead->daddr = inet_addr(destip);
tcphead->source = htons(sourceport);
tcphead->dest = htons(destport);
tcphead->seq = htons(random());
tcphead->ack_seq = 0;
tcphead->fin = 0;
tcphead->syn = 1;
tcphead->rst = 0;
tcphead->psh = 0;
tcphead->ack = 0;
tcphead->urg = 0;
tcphead->window = htons(65535);
tcphead->check = 0;
tcphead->urg_ptr = 0;
if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0){
perror("setsockopt");
exit(1);
}
if( (sendto(sockfd, datagram, iphead->tot_len, 0, (struct sockaddr *)&dest, sizeof(struct sockaddr))) == -1){
perror("sendto");
exit(1);
}
if( (recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&dest, &sin_size)) == -1){
perror("recvfrom");
exit(1);
}
close(sockfd);
return 0;
}
void help()
{
printf("usage: ./userpacket -S <sourceip> -D <destip> -P <sourceport> -p <destport> -c <# of packets>\n\n");
}
i hope that someone of you can help me.
maybe you have a clue whats wrong with that code.
i compiled and executed that code on an x86 arch with slackware.
thanks