LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2003, 03:55 PM   #1
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
raw ipv6 socket example


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!

Last edited by Steave; 06-27-2003 at 04:01 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw socket with perl Whiteghost Linux - General 1 10-14-2005 10:29 AM
Help with raw socket programming tuxfood Programming 2 07-25-2005 01:17 PM
Socket Raw linuxanswer Programming 1 04-01-2004 09:43 PM
ping without raw socket linuxnw Linux - Networking 3 12-09-2003 11:57 PM
raw socket permition moonloader Linux - Networking 2 12-08-2003 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration