PDA

View Full Version : Variable in functions


jmathon
November 17th, 2008, 09:53
Hi everyone,

I'm curently working on a FreeBSD module.
I have some experience in C programming, but not in kernel mode.

I have a strange behaviour, I need some explanation :).

The module I am writing uses mbufs to log connections thanks to ip_input hooking.

When I use :

printf("%s->%s",ip->ip_src, ip->ip_dst);

The output is the same for both variables.

But, If I make two printf, one for ip_src and one for ip_dst both are different.

Why ?
This behaviour appears on every function, not only printf.

Thanks
Jeremie

kamikaze
November 17th, 2008, 10:25
The printf available in kernel modules is not the same as that in libc. Have a look at printf(9). Maybe there's a bug, because it's supposed to work as expected.

lulf@
November 17th, 2008, 20:39
What data types are ip_src and ip_dst? If they are of struct in_addr, they are basically an uint32_t, for which you cannot use "%s" as a format specifier. If you look at printf(3) or printf(9), you can see what format specifier you should use.

It's really hard to see another source for the problem.

cracauer@
November 17th, 2008, 21:40
Yeah, these aren't strings in the first place.

jmathon
November 17th, 2008, 21:40
Here is my function :

struct ip *ip;
ip = mtod(m, struct ip *);
printf("%s:%s",inet_ntoa(ip->ip_src),inet_ntoa(ip->ip_dst));

Thx.

AverageJoe
November 17th, 2008, 23:44
inet_ntoa() returns the dots-and-numbers string in a static buffer that is overwritten with each call to the function.
So, depending on the order of evaluation of the arguments to printf, it will either print ip_src or ip_dst in both places.

aragon
November 18th, 2008, 01:25
Don't mean to break your thunder, but do you know about divert(4)? It might be easier and safer.

jmathon
November 18th, 2008, 08:49
Don't mean to break your thunder, but do you know about divert(4)? It might be easier and safer.

It is too dependent on ipfw(8), need to load ipdivert etc.