000
09.11.2008, 13:01 Uhr
~fasikon
Gast
|
hi, hab hier diesen code um ein udp raw-socket paket zu erstellen und zu verschicken. wenn ich zum sendto() aufruf komme krieg ich immer eine "Invalid Arguments" fehlermeldung.
ich blicke auch nicht durch was ich falsch machen, kann mir da jemand helfen ?
C++: |
#define __USE_BSD /* use bsd'ish ip header */ #include <sys/socket.h> /* these headers are for a Linux system, but */ #include <netinet/ip.h> #include <netinet/udp.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
unsigned short in_cksum(unsigned short *ptr, int nbytes) { register long sum =0; u_short oddbyte = 0; while(nbytes > 1) { sum += *ptr++; nbytes -=2; } if(nbytes == 1) { *((u_char *) &oddbyte) = *(u_char*) ptr; sum += oddbyte; } sum = (sum>>16) + (sum & 0xffff); sum += (sum>>16); return (u_short)~sum; }
int main(int argc, char **argv) {
int uid = geteuid(); if ( uid != 0) { printf("You're not root ! %i", uid); exit(1); }
unsigned int headersize = sizeof(struct ip) + sizeof(struct udphdr); unsigned int payload = 512; unsigned int packetsize = headersize + payload; unsigned char packet[packetsize]; memset(packet,0,packetsize); int rawsock=0; if ((rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) { perror("socket() - failed"); exit(1); }
/* Eigenen IP-Header verwenden, Kernel-Header unterdrücken */ int on=1; if (setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (const char* )&on, sizeof(on)) < 0 ) { perror("IP_HDRINCL failed!\n"); exit(1); } struct sockaddr_in sin; memset(&sin, 0, sizeof(struct sockaddr_in)); sin.sin_addr.s_addr = inet_addr("127.0.0.1"); sin.sin_port = htons(1235); sin.sin_family = AF_INET; /* IP Header*/ struct ip *iph = (struct ip*) packet; iph->ip_v = 4; //IPv4 iph->ip_hl = 5; iph->ip_tos = 0x0; iph->ip_len = htons(packetsize); iph->ip_len = 0; iph->ip_ttl = 100; iph->ip_sum = 0; iph->ip_p = IPPROTO_UDP; iph->ip_src.s_addr = inet_addr("127.0.0.1"); iph->ip_dst.s_addr = inet_addr("192.168.2.103"); iph->ip_id = htons(random()); iph->ip_id = 0;
/* UDP Header */ struct udphdr *udp = (struct udphdr *) (packet + sizeof(struct ip)); udp->uh_sport = htons(1234); udp->uh_dport = htons(1235); udp->uh_ulen = htons(packetsize - sizeof(struct ip)); udp->uh_sum = 0; printf("Checksumme, Headersize \n"); iph->ip_sum = in_cksum((unsigned short*) &iph, sizeof(struct ip)); printf("Calculated Checksumme: %i\n", iph->ip_sum);
if (sendto(rawsock, packet, packetsize, 0, (struct sockaddr*) &sin, sizeof(sin)) < 0 ) perror("sendto() failed"); close(rawsock); return 0; }
|
|