Search code examples
csocketsfreebsdipfw

FreeBSD IPFW add an IP to table


I want to add an IP to a table in IPFW from within a C program. I don't want to call system() as this code has to be as efficient as possible. So basically my question is, can somebody explain how does this work? As there is no API that can be used, I'm reviewing the source (to see how the adding is being done), but this is taking longer than expected and if possible, it would be great if someone explains this.

So what I'm trying to do is when I receive a connection from a client, I want to put the IP address of that client in a certain table.

Thanks in advance!


Solution

  • OK. So I've found the solution.

    There's a struct in ip_fw.h for that - ipfw_table_entry

    The code looks something like this:

    ...
    ipfw_table_entry entry;
    
    bzero(&entry, sizeof entry);
    
    entry.addr = inet_addr("192.168.1.0");
    entry.tbl = 100;
    entry.masklen = 32;
    
    setsockopt(ipfw_socket, IPPROTO_IP, IP_FW_TABLE_ADD, &entry, sizeof entry);