# Variable in functions



## jmathon (Nov 17, 2008)

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 (Nov 17, 2008)

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@ (Nov 17, 2008)

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@ (Nov 17, 2008)

Yeah, these aren't strings in the first place.


----------



## jmathon (Nov 17, 2008)

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 (Nov 17, 2008)

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 (Nov 18, 2008)

Don't mean to break your thunder, but do you know about divert(4)?  It might be easier and safer.


----------



## jmathon (Nov 18, 2008)

aragon said:
			
		

> 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.


----------

