Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Sep 2001 09:57:04 +0300
From:      Ruslan Ermilov <ru@FreeBSD.ORG>
To:        David Preece <davep@afterswish.com>
Cc:        davidc@acns.ab.ca, sridharv@ufl.edu, bright@mu.org, hackers@FreeBSD.ORG
Subject:   Re: Raw sockets: Stevens shall provide.
Message-ID:  <20010921095704.F27714@sunbay.com>
In-Reply-To: <01092112052007.36513@development.wgtn.csg.co.nz>; from davep@afterswish.com on Fri, Sep 21, 2001 at 12:05:20PM %2B1200
References:  <01092112052007.36513@development.wgtn.csg.co.nz>

next in thread | previous in thread | raw e-mail | index | archive | help

--NzB8fVQJ5HfG6fxh
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Fri, Sep 21, 2001 at 12:05:20PM +1200, David Preece wrote:
> Thanks for your help, as suggested the combination of IP_HDRINCL and reading 
> Stevens seems to have me back on the right path. Part of the confusion was 
> the Linux implementation of IP_HDRINCL appears to need the hardware addresses 
> writing as well, consequently there are lots of examples of this being done 
> in order to forge arp replies etc - not what I needed. Or rather, it was 
> actually.
> 
> Anyway, the code is now refusing to send packets entirely, which is actually 
> good because it implies a checksum is broken somewhere and that's a bug, not 
> a misunderstanding about the API.
> 
I didn't follow the thread, but the error may be caused by supplying some
header fields in incorrect byte order.  Some fields are expected in host
byte order, and some are in net byte order, e.g. the ip_id (if non-zero).

I wrote the demo program that uses IP_HDRINCL option, attached.


Cheers,
-- 
Ruslan Ermilov		Oracle Developer/DBA,
ru@sunbay.com		Sunbay Software AG,
ru@FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age

--NzB8fVQJ5HfG6fxh
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="rawip.c"

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

#include <err.h>
#include <string.h>
#include <stddef.h>

struct udppacket {
	struct ip ip;
	struct udphdr uh;
	char data[IP_MAXPACKET - sizeof(struct ip) - sizeof(struct udphdr)];
};
/*
struct ip {
};
struct udphdr {
        u_short uh_sport;               # source port 
        u_short uh_dport;               # destination port 
        u_short uh_ulen;                # udp length 
        u_short uh_sum;                 # udp checksum 
};
*/

int
main(void)
{
	int s;
	int hincl = 1;
	struct udppacket packet;
	struct sockaddr_in to;

	bzero(&to, sizeof to);
	to.sin_len = sizeof to;
	to.sin_family = AF_INET;
	to.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	to.sin_port = 0;
	
	bzero(&packet, offsetof(struct udppacket, data));
	packet.ip.ip_v = IPVERSION;
	packet.ip.ip_hl = sizeof(packet.ip) >> 2;
	packet.ip.ip_len = sizeof(packet.ip) + sizeof(packet.uh);
	packet.ip.ip_id = 0;
	packet.ip.ip_off = 0;
	packet.ip.ip_ttl = MAXTTL;
	packet.ip.ip_p = IPPROTO_UDP;
	packet.ip.ip_src.s_addr =
	packet.ip.ip_dst.s_addr = htonl(INADDR_LOOPBACK);

	if ((s = socket(AF_INET, SOCK_RAW, 0)) == -1)
		err(1, "socket");

	if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)) == -1)
		err(1, "setsockopt(IP_HDRINCL)");

	if (sendto(s, &packet, 28, 0, (struct sockaddr *)&to, sizeof to) == -1)
		err(1, "sendto");

	return (0);
}

--NzB8fVQJ5HfG6fxh--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010921095704.F27714>