top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible that we could open or block port using C program?

+1 vote
598 views
Is it possible that we could open or block port using C program?
posted Aug 20, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

I hope i got your problem right, suppose you want to open a TCP post (assuming your machine is a Linux machine) then you need the following command -

iptables -I INPUT -p tcp --dport 12345 --syn -j ACCEPT

Above command will open the TCP post with the port-id as 12345 on local machine, now what uyou need is to fire the same command from your C code may be system would be handy something like

system("iptables -I INPUT -p tcp --dport 12345 --syn -j ACCEPT") 

You may need to provide full path of iptables based on your local setting.

answer Aug 21, 2014 by Salil Agrawal
Sir i know that thing and currently using the same via shell script. but is there any system call(in built library) exists so that i will call it in my binary.
You need something like these header file
#include <linux/ip.h>
#include <linux/tcp.h> Assuming TCP
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netdevice.h>

You receive a packet and check if it is from blocked packet list or accepted port list.

Check the following link would be useful
http://forums.devshed.com/programming-42/firewall-linux-102118.html
...