Thread: printf a bpf_u_int32

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    printf a bpf_u_int32

    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

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Apparently it's a 32-bit unsigned int. In C99, you can use PRIu32 to print that.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    And secondly, if you want each octal (ie, a.b.c.d) just do a bitwise shift (and maybe other things ) and print.
    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:
    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));
    Which compiles but segfaults, which seems odd even if the byte order is wrong.
    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

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Can you give me an example of how to do this? I just googled "bitshift ip address" and surprisingly there is nothing invloving C.
    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.)
    Quote Originally Posted by MK27 View Post
    I did try inet_ntoa:
    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));
    Which compiles but segfaults, which seems odd even if the byte order is wrong.
    You need to not presume a functional address, but actually make it so -- changing to
    Code:
    unsigned int ip = 0x0f000001;
    worked for me. (You should be able to get away with your other data type too.)

    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Code:
    unsigned int ip = 0x0f000001;
    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:
    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);
    }
    Which I guess is a sort of bit shift.
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    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:
    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);
    }
    Which I guess is a sort of bit shift.
    Well my point (such as it was) is that the only possibility of error in your code was the presumed getting of data into your ip variable.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Well my point (such as it was) is that the only possibility of error in your code was the presumed getting of data into your ip variable.
    I forgot to #include <arpa/inet.h> (has in_addr_t in it). Now I get the same result both ways.

    Thanks again

    ps. I guess it actually was big endian then...
    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

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zacs7 View Post
    who's to say the type won't change from unsigned int to unsigned int64 in the future?
    The fact that an IP address is 32 bits long, and that would be pointless?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM