I've spent some time lately writing modules that just present a perl
level mapping into some kernel-level concept. Initially I wrote

  http://search.cpan.org/~pevans/Socket-Packet-0.03/lib/Socket/Packet.pm

This really just contains exports of a bunch of constants, and some
pack/unpack functions for structure types. It's somewhat tedious to have
to go to all the effort of writing an XS module just for that.

So I've come up with a new approach, a way to generate a pureperl module
from pureperl source, without the module developer having to go anywhere
near C.

A little example rewriting most of the Socket::Packet module using this:

-----

$ cat Packet_const.pm.PL 
#!/usr/bin/perl

use strict;
use warnings;
use ExtUtils::H2PM;

module "Socket::Packet";

include "sys/socket.h";
include "netpacket/packet.h";
include "net/ethernet.h";

constant "PF_PACKET";
constant "AF_PACKET";

constant "PACKET_$_" for qw( HOST BROADCAST MULTICAST OTHERHOST OUTGOING );

constant "ETH_P_ALL";

constant "SIOCGSTAMP";
constant "SIOCGSTAMPNS";

structure "struct timeval",
   members => [
      tv_sec  => numeric,
      tv_usec => numeric,
   ];

structure "struct timespec",
   members => [
      tv_sec  => numeric,
      tv_nsec => numeric,
   ];

$ perl Packet_const.pm.PL
package Socket::Packet;
# This module was generated automatically by ExtUtils::H2PM from 
Packet_const.pm.PL

push @EXPORT_OK, 'PF_PACKET', 'AF_PACKET', 'PACKET_HOST', 'PACKET_BROADCAST', 
'PACKET_MULTICAST', 'PACKET_OTHERHOST', 'PACKET_OUTGOING', 'ETH_P_ALL', 
'SIOCGSTAMP', 'SIOCGSTAMPNS', 'pack_timeval', 'unpack_timeval', 
'pack_timespec', 'unpack_timespec';
use constant PF_PACKET => 17;
use constant AF_PACKET => 17;
use constant PACKET_HOST => 0;
use constant PACKET_BROADCAST => 1;
use constant PACKET_MULTICAST => 2;
use constant PACKET_OTHERHOST => 3;
use constant PACKET_OUTGOING => 4;
use constant ETH_P_ALL => 3;
use constant SIOCGSTAMP => 35078;
use constant SIOCGSTAMPNS => 35079;
use Carp;

sub pack_timeval
{ 
   @_ == 2 or croak "usage: pack_struct timeval(tv_sec, tv_usec)";
   pack "l l ", @_;
}

sub unpack_timeval
{ 
   length $_[0] == 8 or croak "unpack_struct timeval: expected 8 bytes";
   unpack "l l ", $_[0];
}


sub pack_timespec
{ 
   @_ == 2 or croak "usage: pack_struct timespec(tv_sec, tv_nsec)";
   pack "l l ", @_;
}

sub unpack_timespec
{ 
   length $_[0] == 8 or croak "unpack_struct timespec: expected 8 bytes";
   unpack "l l ", $_[0];
}


1;

-----

This has now generated me a platform-specific Packet_const.pm which I
can "use Socket::Packet_const;" in my actual code.

It is platform-specific, but it was created at the time I installed the
module. It's no less portable than the compiled Packet.so which would
have been created by going the traditional XS route.

Finally, find attached the full text of the module's documentation.


I'd be interested to hear any thoughts, etc...

-- 
Paul "LeoNerd" Evans

[email protected]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/
NAME
    "ExtUtils::H2PM" - automatically generate perl modules to wrap C header
    files

DESCRIPTION
    This module assists in generating wrappers around system functionallity,
    such as "socket()" types or "ioctl()" calls, where the only interesting
    features required are the values of some constants or layouts of
    structures normally only known to the C header files. Rather than
    writing an entire XS module just to contain some constants and
    pack/unpack functions, this module allows the author to generate, at
    module build time, a pure perl module containing constant declarations
    and structure utility functions. The module then requires no XS module
    to be loaded at run time.

    In comparison to h2ph, "C::Scan::Constants", and so on, this module
    works by generating a small C program containing "printf()" lines to
    output the values of the constants, compiling it, and running it. This
    allows it to operate without needing tricky syntax parsing or guessing
    of the contents of C header files.

    It can also automatically build pack/unpack functions for simple
    structure layouts, whose members are all simple integer fields. It is
    not intended as a full replacement of arbitrary code written in XS
    modules. If structures should contain pointers, or require special
    custom handling, then likely an XS module will need to be written.

FUNCTIONS
  module $name
    Sets the name of the perl module to generate. This will apply a
    "package" header.

  include $file
    Adds a file to the list of headers which will be included by the C
    program, to obtain the constants or structures from

  constant $name
    Adds a numerical constant.

  structure $name, %args
    Adds a structure definition. This requires a named argument, "members".
    This should be an ARRAY ref containing an even number of name-definition
    pairs. The first of each pair should be a member name. The second should
    be one of the following structure member definitions.

    *       numeric

            The field contains a single signed or unsigned number. Its size
            and signedness will be automatically detected.

    The structure definition results in two new functions being created,
    "pack_$name" and "unpack_$name", where $name is the name of the
    structure (with the leading "struct" prefix stripped). These behave
    similarly to the familiar functions such as "pack_sockaddr_in"; the
    "pack_" function will take a list of fields and return a packed string,
    the "unpack_" function will take a string and return a list of fields.

  no_export, use_export, use_export_ok
    Controls the export behaviour of the generated symbols. "no_export"
    creates symbols that are not exported by their package, they must be
    used fully- qualified. "use_export" creates symbols that are exported by
    default. "use_export_ok" creates symbols that are exported if they are
    specifically requested at "use" time.

    The mode can be changed at any time to affect only the symbols that
    follow it. It defaults to "use_export_ok".

EXAMPLES
    Normally this module would be used by another module at build time, to
    construct the relevant constants and structure functions from system
    headers.

    For example, suppose your operating system defines a new type of socket,
    which has its own packet and address families, and perhaps some new
    socket options which are valid on this socket. We can build a module to
    contain the relevant constants and structure functions by writing, for
    example:

     #!/usr/bin/perl

     use ExtUtils::H2PM;
 
     module "Socket::Moonlazer";

     include "moon/lazer.h";

     constant "AF_MOONLAZER";
     constant "PF_MOONLAZER";

     constant "SOL_MOONLAZER";

     constant "MOONLAZER_POWER";
     constant "MOONLAZER_WAVELENGTH";

     structure "struct lazerwl",
        members => [
           lwl_nm_coarse => numeric,
           lwl_nm_fine   => numeric,
        ];

    If we save this script as, say, lib/Socket/Moonlazer.pm.PL, then when
    "ExtUtils::MakeMaker" or "Module::Build" come to build the module, they
    will execute the script, and capture its output to store as
    lib/Socket/Moonlazer.pm. Once installed, any other code can simply

     use Socket::Moonlazer qw( AF_MOONLAZER );

    to import a constant.

    The method described above doesn't allow us any room to actually include
    other code in the module. Perhaps, as well as these simple constants,
    we'd like to include functions, documentation, etc... To allow this,
    name the script instead something like lib/Socket/Moonlazer_const.pm.PL,
    so that this is the name used for the generated output. The code can
    then be included in the actual lib/Socket/Moonlazer.pm (which will just
    be a normal perl module) by

     package Socket::Moonlazer;

     use Socket::Moonlazer_const;

     sub get_power
     {
        getsockopt( $_[0], SOL_MOONLAZER, MOONLAZER_POWER );
     }

     sub set_power
     {
        setsockopt( $_[0], SOL_MOONLAZER, MOONLAZER_POWER, $_[1] );
     }

     sub get_wavelength
     {
        my $wl = getsockopt( $_[0], SOL_MOONLAZER, MOONLAZER_WAVELENGTH );
        defined $wl or return;
        unpack_lazerwl( $wl );
     }

     sub set_wavelength
     {
        my $wl = pack_lazerwl( $_[1], $_[2] );
        setsockopt( $_[0], SOL_MOONLAZER, MOONLAZER_WAVELENGTH, $wl );
     }

     1;

TODO
    *   Consider more flexible structure members. Perhaps string-like
        members that wrap fixed-size "char" arrays. With strings comes the
        requirement to have members that store a size. This requires
        cross-referential members. And while we're at it it might be nice to
        have constant members; fill in constants without consuming arguments
        when packing, assert the right value on unpacking.

AUTHOR
    Paul Evans <[email protected]>

Attachment: signature.asc
Description: Digital signature

Reply via email to