This is an ip address used by the pcap library. How can I get an ip address stored thusly to print?
Code:printf ("%d\n", my_ip_int_var);
This is an ip address used by the pcap library. How can I get an ip address stored thusly to print?
Code:printf ("%d\n", my_ip_int_var);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Firstly it's unsigned so what you're doing won't work.
And secondly, if you want each octal (ie, a.b.c.d) just do a bitwise shift (and maybe other things ) and print. That is ofcourse if pcap provides no support for unpacking it, who's to say the type won't change from unsigned int to unsigned int64 in the future?
Apparently it's a 32-bit unsigned int. In C99, you can use PRIu32 to print that.
Can you give me an example of how to do this? I just googled "bitshift ip address" and surprisingly there is nothing invloving C.
I did try inet_ntoa:
Which compiles but segfaults, which seems odd even if the byte order is wrong.Code:bpf_u_int32 ip; // presume a functional address in this struct in_addr tmp; tmp.s_addr=ip; printf ("%s\n", inet_ntoa(tmp));
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Some questions are too trivial even for Google. (I mean, once you realize that the 32-bits are set up as four eight-bit octets, then you're done.)
You need to not presume a functional address, but actually make it so -- changing to
worked for me. (You should be able to get away with your other data type too.)Code:unsigned int ip = 0x0f000001;
Edit: I should be more specific -- I didn't actually try your version because I didn't want to bother. So my point is really that there's nothing wrong with what you've posted, so you'll have to be more specific. If it makes you feel better, I changed it to bfp_whatever and it still works.
Last edited by tabstop; 01-02-2009 at 08:03 AM.
Won't that overwrite the ip address I'm trying to decipher?
Anyway, it's in little endian because this actually worked, much to my excitement:
Which I guess is a sort of bit shift.Code:struct ip_addr { unsigned char one; unsigned char two; unsigned char three; unsigned char four; }; int main() { bpf_u_int32 ip; struct ip_addr *ptr=(struct ip_addr*)&ip; printf ("%d.%d.%d.%d\n", ptr->one,ptr->two,ptr->three,ptr->four); }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Last edited by MK27; 01-02-2009 at 08:39 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge