tags:

views:

168

answers:

3

I am developing a firewall for Linux as my project. I am able to capture packets and to block them. I am using IPTABLES.

How can I use variables with sprintf instead of hardcoded values?

sprintf(comm, "iptables -A INPUT -s $str -j DROP")
// inplace of:
sprintf(comm, "iptables -A INPUT -s 192.168.0.43 -j DROP")
+2  A: 
sprintf(comm, "iptables -A INPUT -s %s -j DROP", "192.168.0.43");
// also:
char ipaddress[] = "192.168.0.43";
sprintf(comm, "iptables -A INPUT -s %s -j DROP", ipaddress);

Read more in man sprintf.

Roger Pate
+1  A: 

It works just like regular printf(). Same format strings are accepted. However, be careful to avoid overflowing the string buffer. In that sense, snprintf() will be MUCH more acceptable than sprintf().

Roman D
thanks but it gives an error of incompatible typesnw wht to do??? actually i want to use iptables commands in my c programits working for static ipaddress but i want to do that for dynamically
astha goyal
What exactly does the error say? Are you following the `snprintf()` documentation?
Roman D
@astha: It sounds like you need to learn about general string manipulation in C, which is a stumbling block for many new programmers.
Roger Pate
A: 

how to get a substring from string "192.168.23.33" upto dot sign. Which function is used???

astha
ask your own question :)
shodanex