Click here to Skip to main content
15,867,756 members
Articles / Desktop Programming / MFC

An Adventure: How to Implement a Firewall-Hook Driver?

Rate me:
Please Sign up or sign in to vote.
4.83/5 (65 votes)
28 Oct 20049 min read 668.9K   11K   194  
Firewall-Hook driver is a completely unknown method to develop simple packet filtering applications. With this article, I want to tell you how this driver works and what you need to do to use it in your applications.
#include "stdafx.h"
#include "sockutil.h"
#include <stdlib.h>
#include <string.h>

/*++

Descripci�n:

    Convierte una Ip de cadena a formato de red

Argumentos:

    ip - cadena que contiene la direccion ipstring that represent a ip address
    
    Si la cadena no tiene el formato correcto(x.x.x.x) devuelve -1. Si los octetos
    son menores de 0 o mayores de 255 devuelve direccion 0.0.0.0.

Valores devueltos:

	ip en formato de red

--*/
int inet_addr(const char *sIp, unsigned long *lIp)
{
	int octets[4];
	int i;
	const char * auxCad = sIp;
	*lIp = 0;
	
	// Extraigo cada uno de los octetos. Atoi extrae caracteres hasta encontrar
	// un caracter no numerico, en nuestro el '.'
	for(i = 0; i < 4; i++)
	{
		octets[i] = atoi(auxCad);

		if(octets[i] < 0 || octets[i] > 255)
			return -1;

		*lIp |= (octets[i] << (i*8));

		// Acualizo auxCad para que apunte al siguiente octeto
		auxCad = strchr(auxCad, '.');

		if(auxCad == NULL && i!=3)
			return -1;

		auxCad++;
	}


	return 0;
}



/*++

Descripci�n:

    Cambia el orden de los octetos. Mayor orden a menor orden y viceversa.

Argumentos:

    port - Numero a convertir


Valores devueltos:

	puerto convertido

--*/
unsigned short htons(unsigned short port)
{
	unsigned short portRet;

	portRet = ((port << 8) | (port >> 8));

	return portRet;
}

char *IpToString(char *ip, unsigned long lIp)
{
	char octeto[4];

	ip[0] = 0;

	itoa(lIp & 0xff, octeto, 10);

	strcat(ip, octeto);
	strcat(ip, ".");


	itoa((lIp >> 8) & 0xff, octeto, 10);

	strcat(ip, octeto);
	strcat(ip, ".");


	itoa((lIp >> 16) & 0xff, octeto, 10);

	strcat(ip, octeto);
	strcat(ip, ".");

	itoa((lIp >> 24) & 0xff, octeto, 10);

	strcat(ip, octeto);
	

	return ip;
}
	

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Chief Technology Officer
Spain Spain
To summarize: learn, learn, learn... and then try to remember something I.... I don't Know what i have to remember...

http://www.olivacorner.com

Comments and Discussions