Missing functions in h2ph converted headers

Discussion in 'Perl Misc' started by Chris Snell, Oct 24, 2004.

  1. Chris Snell

    Chris Snell Guest

    Hi,

    I need to access the getifaddrs() function, as defined in ifaddrs.h on
    FreeBSD/OpenBSD/Mac OS X. I'm trying to read the kernel's traffic
    counters for the machine's network interfaces. I ran h2ph on
    /usr/include/ifaddrs.h but it did not make the getifaddrs() function
    available to me. How can I make this system call from Perl?

    Also, getifaddrs provides its data in a struct which contains other
    structs. It's a pretty gnarly data structure. Supposing that I can
    access this from Perl's syscall, how can I quickly determine the total
    size of the idaddrs struct that getifaddrs returns so that I can
    allocate space in my scalar for this data?

    thanks,

    Chris

    PS- If anyone knows of a better way to access network interface
    statistics (but doesn't involve calling netstat(1)), I'd love to hear
    about it.
     
    Chris Snell, Oct 24, 2004
    #1
    1. Advertising

  2. Chris Snell

    Ben Morrow Guest

    Quoth (Chris Snell):
    >
    > I need to access the getifaddrs() function, as defined in ifaddrs.h on
    > FreeBSD/OpenBSD/Mac OS X. I'm trying to read the kernel's traffic
    > counters for the machine's network interfaces.


    It's possible you may be able to get the info you need with Net::pcap: I
    don't know what getifaddrs(2) does, so I can't tell.

    > I ran h2ph on /usr/include/ifaddrs.h but it did not make the
    > getifaddrs() function available to me. How can I make this system
    > call from Perl?


    Don't use h2ph or syscall: they're a perl4 mechanism. Instead, write (or
    check CPAN to see if someone else has written) an XS module that calls
    the C function and returns the values you need in a perlish form. If you
    are not happy with learning XS, you could try Inline::C instead, which
    hides most of the XS for you and lets you get away with only knowing
    Perl and C.

    > Also, getifaddrs provides its data in a struct which contains other
    > structs. It's a pretty gnarly data structure. Supposing that I can
    > access this from Perl's syscall, how can I quickly determine the total
    > size of the idaddrs struct that getifaddrs returns so that I can
    > allocate space in my scalar for this data?


    Errr.... you can't. :)

    Using XS or Inline::C, you can allocate a buffer with
    NEWSV(1, sizeof(struct ifaddr)) (or whatever). To get at its members,
    you use unpack: either unpack it yourself into a sensible Perl data
    structure to return, or use Inline::Struct to wrap an object around it
    (in which case you will probably need to copy the appropriate struct
    defns from the header, and possibly provide typemaps for the members).

    Good luck!

    Ben

    --
    And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
    * *
     
    Ben Morrow, Oct 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dont bother
    Replies:
    0
    Views:
    812
    dont bother
    Mar 3, 2004
  2. Gary Jefferson

    distutils, extensions, and missing headers

    Gary Jefferson, Sep 20, 2007, in forum: Python
    Replies:
    4
    Views:
    291
    Gary Jefferson
    Sep 21, 2007
  3. Afshar
    Replies:
    0
    Views:
    342
    Afshar
    Oct 8, 2008
  4. Phil
    Replies:
    4
    Views:
    687
    Gabriel Genellina
    Jan 17, 2010
  5. Ian
    Replies:
    2
    Views:
    1,964
Loading...

Share This Page