Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Jan 2004 21:20:47 +0700 (GMT-7)
From:      "lattera" <lattera@nosleep.info>
To:        freebsd-ipfw@freebsd.org
Subject:   divert sockets code
Message-ID:  <49344.216.190.11.115.1074608447.squirrel@vulcan.g3host.net>

next in thread | raw e-mail | index | archive | help
------=_20040120212047_33052
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

I can't seem to get pointers right in my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define USE_BSD
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netinet/tcp.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>

#define PORT 6137

int main(int argc, char *argv[])
{
	int sockfd, n, clisize, ipsize, tcpsize, i;
	struct sockaddr_in server, client;
	char buf[65536], *payload;
	struct tcphdr *tcp;
	struct ip *iphdr;

	if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT)) < 0)
	{
		perror("socket");
		exit(1);
	}
	server.sin_family = PF_INET;
	server.sin_port = htons(PORT);
	server.sin_addr.s_addr = INADDR_ANY;
	if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
	{
		perror("bind");
		exit(1);
	}	exit(1);

	while (1)
	{
		clisize = sizeof(client);
		if ((n=recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&client,
&clisize))<0)
		{
			perror("recv");
			exit(1);
		}
		iphdr = (struct ip *)buf;
		if (iphdr->ip_p != IPPROTO_TCP)
		{
			if (sendto(sockfd, buf, n, 0, (struct sockaddr *)&client, clisize) != n)
			{
				perror("send");
				exit(1);
			}
		}
		tcp = (struct tcphdr *)(buf + (4*(iphdr->ip_hl)));
		if (!(tcp->th_flags & TH_PUSH))
		{
			if (sendto(sockfd, buf, n, 0, (struct sockaddr *)&client, clisize) != n)
			{
				perror("send");
				exit(1);
			}
			continue;
		}

		payload = (char *)(tcp + ((tcp->th_off)*4));
		if (strstr(payload, "GET /etc/passwd"))
			continue;

		if (sendto(sockfd, buf, n, 0, (struct sockaddr *)&client, clisize) != n)
		{
			perror("send");
			exit(1);
		}
	}
}

Can someone tell me what I need to do?

Attached is the C source file of the above code (for readability)


-- 
"So crucify the go before it's far too late to leave behind this place so
negative and blind and cynical and you will come to find that we are all
one mind capable of all that's imagined and all conceivable." -- Tool -
Reflection
http://lattera.nosleep.info
http://www.sf.net/projects/hidprox
------=_20040120212047_33052--



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