Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Jan 2003 15:41:26 +0300
From:      Alex <alex@dynaweb.ru>
To:        freebsd-questions <freebsd-questions@FreeBSD.org>
Subject:   problems configuring ipfw via raw sockets
Message-ID:  <3E2FE2F6.7000806@dynaweb.ru>

next in thread | raw e-mail | index | archive | help
Hi ppl!

I need to use direct access to ipfw rules via raw sockets instead of 
some scripts using ipfw utility.

I  looked into ipfw sources and made a simple program to test if I could 
add a simple rule this way.
Just rewrote pieces of original code into my program w/out any serious 
change.

But setsockopt() always return EINVAL and string msg "Invalid argument".
And no details. Hence I'm in a fix. Icannot get what's wrong indeed.
It's my first experience with configuring ipfw this way and maybe I 
missed something obvious for a guru :0)

Here I place my code (short anough).
Any advice would be appritiated.
Maybe some links to some docs - I failed to find anything but a very 
short info in manpages.

#include    <stdlib.h>
#include    <sys/types.h>
#include    <sys/socket.h>
#include    <sys/queue.h>
#include    <netinet/in.h>
#include    <netinet/ip_fw.h>
#include    <arpa/inet.h>
#include    <netdb.h>
#include    <errno.h>

int        test(void)

{
int sock,res,sz;
struct ip_fw  rule;

sock = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
if (sock==-1) {printf("\n\nsoket() failed with 
\"%s\"\n\n",strerror(errno)); return -1;};


memset(&rule,0,sizeof(struct ip_fw));

rule.fw_number = 700;
rule.fw_flg = IP_FW_F_DENY;
rule.fw_src.s_addr = inet_addr("195.48.121.34");
rule.fw_smsk.s_addr = inet_addr("255.255.255.255");
rule.fw_dst.s_addr = inet_addr("127.0.0.0");
rule.fw_dmsk.s_addr = inet_addr("255.0.0.0");

rule.fw_prot = IPPROTO_IP;

sz = sizeof(struct ip_fw);
res = setsockopt(sock,IPPROTO_IP,IP_FW_ADD,&rule,&sz);
if (res==-1) {printf("\n\nsetsockopt() failed with 
\"%s\"\n\n",strerror(errno)); return -1;};

return 0;
};


int    main(void)

{
test();
return 0;
};


Alexander Komratov


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




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