Missing functions in h2ph converted headers

C

Chris Snell

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

Ben Morrow

Quoth (e-mail address removed) (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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top