Protocol not available writing to eth?
Sanders, Maarten (M.J.L.)
MSA at VanOord.com
Thu Feb 2 14:52:23 UTC 2006
Hi,
We are trying to convince a MOXA UC-7110 to act as a very simple
firewall. We want it to listen to UDP on eth0 and to forward the data to
eth1. At a later stage we apply our own filtering.
The code below compiles cleanly but returns:
"Protocol not available"
as soon as eth1 is opened.
Has anyone done a similar thing before or has some pointers to an
example? Most examples we find are not so 'raw' on the devices as we
intend.
Thanks,
Maarten Sanders
<<<<<tcpc1.c>>>>>>>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet_ntoa() */
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <string.h>
#include <unistd.h>
#define BUFSIZE 2048
int main(void)
{
int i = 20;
int sock_r, sock_s;
ssize_t n;
unsigned char buffer[BUFSIZE];
char on = 1;
struct ifreq req, req_s;
if ((sock_r = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
{
perror("socket() error");
return -1;
}
/* bind device to the socket */
strncpy(req.ifr_name, "eth0", sizeof(req.ifr_name));
if (ioctl(sock_r, SIOCGIFFLAGS, &req) < 0)
{
perror("ioctl() error");
return -1;
}
/* set the interface promiscous mode */
req.ifr_flags |= IFF_PROMISC;
if (ioctl(sock_r, SIOCSIFFLAGS, &req) < 0)
{
perror("ioctl() error");
return -1;
}
// en zend
if ((sock_s = socket(PF_PACKET, SOCK_RAW, htons(IPPROTO_RAW))) < 0)
{
perror("socket()s error");
return -1;
}
/* bind device to the socket */
strncpy(req_s.ifr_name, "eth1", sizeof(req_s.ifr_name));
if ( ioctl(sock_s, SIOCGIFFLAGS, &req_s) < 0)
{
perror("ioctl()s error");
return -1;
}
if (setsockopt(sock_s,IPPROTO_RAW,IP_HDRINCL,&on,sizeof(on)) < 0 )
//ioctl(sock_s, SIOCSIFFLAGS, &req_s) < 0)
{
perror("ioctl()s error");
return -1;
}
printf("Initialized ! %d %d\n",sock_r ,sock_s);
sleep(3);
while (1)
{
if ( (n = read(sock_r, buffer, BUFSIZE)) <= 0 )
{
printf("read() <= 0\n");
}
else if ( buffer[0x17] == 0x11 ) // UDP
{
if ( send(sock_s, buffer ,n ,0)<= 0 )
printf("write() <= 0 %s\n",strerror(errno));
}
fprintf(stdout, "%d %d %d %x %d\n",
n,ntohs(*(short*)(buffer+0x12)),(ntohs(*(short*)(buffer+0x14)) & 0xfff)
<< 3,buffer[0x17],ntohs(*(short*)(buffer+0x24))); fflush(stdout);
}
return 0;
}
More information about the uClibc
mailing list