Hi people,
Can someone provide me with some (working!) code that spits out a UDP-packet over a raw ipv6 socket?
I'm working on a traceroute-like tool for a university project.
This is what I got so far, but it gives me errors when trying to send the packets.
Code:
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAXHOP 50
#define NUMPROBES 5
#define TIMEOUT 5
#define PORT 33333
//client address
struct sockaddr_in6 client_addr;
int sin_size = sizeof(struct sockaddr_in6);
struct payload {
int seqnum;
struct timeval tv;
};
/*
* format of a (udp) probe packet.
*/
struct opacket {
struct ip6_hdr ip;
struct udphdr udp;
struct payload payl;
};
int main(int argc, char *argv[])
{
char *hostname;
struct addrinfo hints;
struct sockaddr_in6 udp_addr;
struct addrinfo *aiudp;
struct opacket outpacket;
int icmpsock4,icmpsock6, sndsock;
int i,error,numbytes;
int on=1;
hostname=argv[1];
// Adressauflvsung
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_DGRAM;
error = getaddrinfo(hostname, NULL, &hints, &aiudp);
if(error){
fprintf(stderr, "%s", gai_strerror(error));
exit(1);
}
((struct sockaddr_in *)aiudp->ai_addr)->sin_port=0;
memset(&outpacket,0,sizeof(struct opacket));
outpacket.ip.ip6_ctlun.ip6_un1.ip6_un1_flow=6<<28;
outpacket.ip.ip6_ctlun.ip6_un1.ip6_un1_plen=htons(sizeof(struct payload)+sizeof(struct udphdr));
outpacket.ip.ip6_ctlun.ip6_un1.ip6_un1_nxt=IPPROTO_UDP; // udp
outpacket.ip.ip6_ctlun.ip6_un1.ip6_un1_hlim=30;
memcpy(&outpacket.ip.ip6_dst, aiudp->ai_addr, aiudp->ai_addrlen);
outpacket.udp.source = htons(PORT);
outpacket.udp.dest = htons(PORT);
outpacket.udp.len = htons(sizeof(struct payload)+sizeof(struct udphdr));
outpacket.udp.check = 0;
memset(&(outpacket.payl),0,sizeof(struct payload));
outpacket.payl.seqnum=htons(10);
//Ein Raw-Socket zum Testpakete versenden
if ((sndsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)) < 0) {
perror("latenzmessung: raw socket");
exit(1);
}
for(i=0;i<10;i++) {
if ( (numbytes=sendto(sndsock, (char *)&outpacket, sizeof(struct opacket), 0, (struct sockaddr *) aiudp.sin_addr, aiudp.addr_len)) == -1 ) {
perror("sendto");
}
}
}
Any help is greatly appreciated!