Code:
static PyObject *
ipfw_table_add(PyObject *self, PyObject *args)
{
int table,mask=32,value=0;
char *ip_str;
int i,s=-1;
ipfw_table_entry ent;
if (!PyArg_ParseTuple(args,"is|ii",&table,&ip_str,&mask,&value))
return NULL;
// Table add entry
if(table<MIN_TABLE){
PyErr_SetString(IPFWError, "m..");
return NULL;
}
ent.value = value;
ent.tbl = table;
ent.masklen = mask;
if(!inet_aton(ip_str, (struct in_addr*)&ent.addr)){
PyErr_SetString(IPFWError, "ip address bad");
return NULL;
}
s=socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if(s<0){
PyErr_SetString(IPFWError, "socket fail");
return NULL;
}
i = setsockopt(s, IPPROTO_IP, IP_FW_TABLE_ADD, &ent, sizeof(ent));
if(i && errno != EEXIST){ // Allow add existent entry
PyErr_SetString(IPFWError, "setsocket fail");
close(s);
return NULL;
}
close(s);
Py_INCREF(Py_None);
return Py_None;
}
http://bitbucket.org/hizel/py-ipfw/changeset/434dd0bc3cb7/
my script:
Code:
#!/usr/local/bin/perl
use Socket;
use constant IPPROTO_RAW => 255;
socket( SOCKET,AF_INET, SOCK_RAW, IPPROTO_RAW ) or die "Can't open raw socket: $!\n";
setsockopt(SOCKET, IPPROTO_IP, IP_FW_TABLE_ADD, 1) or die "Can't send packet: $!\n";
Code:
./perl2.pl
Can't send packet: Protocol not available
How do? (ipfw_table_add(PyObject *self, PyObject *args) ??)
what is &ent, sizeof(ent)); ?
Code:
i = setsockopt(s, IPPROTO_IP, IP_FW_TABLE_ADD, &ent, sizeof(ent));