Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  NET::RAWIP (3)

.ds Aq ’


Net::RawIP - Perl extension to manipulate raw IP packets with interface to libpcap

This is the documentation of "Net::RawIP" version 0.25



  use Net::RawIP;

  $n = Net::RawIP->new({
                        ip  => {
                                saddr =>,
                                daddr =>,
                        tcp => {
                                source => 139,
                                dest   => 139,
                                psh    => 1,
                                syn    => 1,
  $n->ethset(source =>, dest =>;   
  $p = $n->pcapinit("eth0", "dst port 21", 1500, 30);
  $f = dump_open($p, "/my/home/log");
  loop($p, 10, \&dump, $f);


This package provides a class which can be used for creating, manipulating and sending raw IP packets with optional features for manipulating Ethernet headers.

<B>Note:B> Ethernet related methods are implemented on Linux and *BSD only.

As its name implies, this module is quite low-level, and currently duplicates some features with Net::Pcap. If you prefer a higher-level module (in terms of Perl support), please take a look at Net::Write, which provides a portable interface to construct and send raw packets on the network.

Exported constants


Exported functions

open_live open_offline dump_open lookupdev lookupnet dispatch loop dump compile setfilter next datalink snapshot is_swapped major_version minor_version stats file fileno perror geterr strerror close dump_close timem linkoffset ifaddrlist rdev

By default exported functions are the <B>loopB>, <B>dispatchB>, <B>dump_openB>, <B>dumpB>, <B>open_liveB>, <B>timemB>, <B>linkoffsetB>, <B>ifaddrlistB>, <B>rdevB>. You have to use the export tag <B>pcapB> for export all of the pcap functions. Please read the docs for the libpcap and look at Net::RawIP::libpcap(3pm).

Please look at the examples in the examples/ folder of the distribution.



              ARGPROTO => {PROTOKEY => PROTOVALUE,...}
              ip       => {IPKEY => IPVALUE,...},

<B>ARGPROTOB> is one of (<B>tcpB>, <B>udpB>, <B>icmpB>, <B>genericB>) defining the protcol of the current packet. Defaults to <B>tcpB>.

You can <B>NOTB> change protocol in the object after its creation. Unless you want your packet to be TCP, you must set the protocol type in the new() call.

The possible values of <B>PROTOKEYB> depend on the value of ARGPROTO

If ARGPROTO is <tcp> PROTOKEY can be one of (<B>sourceB>, <B>destB>, <B>seqB>, <B>ack_seqB>, <B>doffB>, <B>res1B>, <B>res2B>, <B>urgB>, <B>ackB>, <B>pshB>, <B>rstB>, <B>synB>, <B>finB>, <B>windowB>, <B>checkB>, <B>urg_ptrB>, <B>dataB>).

If ARGPROTO is <B>icmpB> PROTOKEY can be one of (<B>typeB>, <B>codeB>, <B>checkB>, <B>gatewayB>, <B>idB>, <B>sequenceB>, <B>unusedB>, <B>mtuB>, <B>dataB>).

If ARGPROTO is <B>udpB> PROTOKEY can be one of (<B>sourceB>, <B>destB>, <B>lenB>, <B>checkB>, <B>dataB>)

If ARGPROTO is <B>genericB> PROTOKEY can be <B>dataB> only.

The <B>dataB> entries are scalars containing packed network byte order data.

As the real icmp packet is a C union one can specify only one of the following set of values.
o <B>gatewayB> - (int)
o (<B>idB> and <B>sequenceB>) - (short and short)
o (<B>mtuB> and <B>unusedB>) - (short and short)

The default values are:
o (0,0,0,0,5,0,0,0,0,0,0,0,0,0xffff,0,0,’’) for tcp
o (0,0,0,0,0,0,0,0,’’) for icmp
o (0,0,0,0,’’) for udp
o (’’) for generic

The valid values for <B>urgB> <B>ackB> <B>pshB> <B>rstB> <B>synB> <B>finB> are 0 or 1. The value of <B>dataB> is a string. Length of the result packet will be calculated if you do not specify non-zero value for <B>tot_lenB>.

The value of <B>ipB> is a hash defining the parameters of the IP header (<B>iphdrB>) in the current IP packet.

<B>IPKEYB> is one of (<B>versionB>, <B>ihlB>, <B>tosB>, <B>tot_lenB>, <B>idB>, <B>frag_offB>, <B>ttlB>, <B>protocolB>, <B>checkB>, <B>saddrB>, <B>daddrB>). You can to specify any and all of the above parameters. If <B>checkB> is not given checksum will be calculated automatically.

The values of the <B>saddrB> and the <B>daddrB> can be hostname (e.g. ) or IP address (, and even the integer value if you happen to know what is as an unsigned int in the host format ;).


    my $rawip = Net::RawIP->new({udp =>{}});


    my $rawip = Net::RawIP->new({ip => { tos => 22 }, udp => { source => 22,dest =>23 } });

The default values of the <B>ipB> hash are
o (4,5,16,0,0,0x4000,64,6,0,0,0) for <B>tcpB>
o (4,5,16,0,0,0x4000,64,17,0,0,0) for <B>udpB>
o (4,5,16,0,0,0x4000,64,1,0,0,0) for <B>icmpB>
o (4,5,16,0,0,0x4000,64,0,0,0,0) for <B>genericB>

dump_open If <B>dump_openB> opens and returns a valid file descriptor, this descriptor can be used in the perl callback as a perl filehandle.
dispatch <B>loopB> and <B>dispatchB> can run a perl code refs as a callbacks for packet analyzing and printing. the fourth parameter for <B>loopB> and <B>dispatchB> can be an array or a hash reference and it can be dereferenced in a perl callback.
next <B>B>next()<B>B> returns a string (next packet).
timem <B>B>timem()<B>B> returns a string that looks like <B>secB>.<B>microsecB>, where the <B>secB> and the <B>microsecB> are the values returned by gettimeofday(3). If <B>microsecB> is less than 100000 then zeros will be added to the left side of <B>microsecB> for adjusting to six digits.

Similar to sprintf(%.6f, Time::HiRes::time());

linkoffset The function which called <B>linkoffsetB> returns a number of the bytes in the link protocol header e.g. 14 for a Ethernet or 4 for a Point-to-Point protocol. This function has one input parameter (pcap_t*) that is returned by open_live.
ifaddrlist <B>B>ifaddrlist()<B>B> returns a hash reference. In this hash keys are the running network devices, values are ip addresses of those devices in an internet address format.
rdev <B>B>rdev()<B>B> returns a name of the outgoing device for given destination address. It has one input parameter (destination address in an internet address or a domain name or a host byteorder int formats).
proto Returns the name of the subclass current object e.g. <B>tcpB>. No input parameters.
packet Returns a scalar which contain the packed ip packet of the current object. No input parameters.
set Method for setting the parameters of the current object. The given parameters must look like the parameters for the constructor.
bset($packet,$eth) Method for setting the parameters of the current object. <B>B>$packet<B>B> is a scalar which contain binary structure (an ip or an eth packet). This scalar must match with the subclass of the current object. If <B>B>$eth<B>B> is given and it have a non-zero value then assumed that packet is a ethernet packet,otherwise it is a ip packet.
get is a method for get the parameters from the current object. This method returns the array which will be filled with an asked parameters in order as they have ordered in packet if you’d call it with an array context. If this method is called with a scalar context then it returns a hash reference. In that hash will stored an asked parameters as values,the keys are their names.

The input parameter is a hash reference. In this hash can be three keys. They are a <B>ipB> and an one of the <B>ARGPROTOB>s. The value must be an array reference. This array contain asked parameters. E.g. you want to know current value of the tos from the iphdr and the flags of the tcphdr. Here is a code :

  ($tos,$urg,$ack,$psh,$rst,$syn,$fin) = $packet->get({
            ip => [qw(tos)],
        tcp => [qw(psh syn urg ack rst fin)]

The members in the array can be given in any order.

For get the ethernet parameters you have to use the key <B>ethB> and the values of the array (<B>destB>,<B>sourceB>,<B>protoB>). The values of the <B>destB> and the <B>sourceB> will look like the output of the ifconfig(8) e.g. 00:00:E8:43:0B:2A.

send($delay,$times) is a method which has used for send raw ip packet. The input parameters are the delay seconds and the times for repeating send. If you do not specify parameters for the <B>sendB>,then packet will be sent once without delay. If you do specify for the times a negative value then packet will be sent forever. E.g. you want to send the packet for ten times with delay equal to one second. Here is a code :


The delay could be specified not only as integer but and as 0.25 for sleep to 250 ms or 3.5 to sleep for 3 seconds and 500 ms.

pcapinit($device,$filter,$psize,$timeout) is a method for some a pcap init. The input parameters are a device,a string with a program for a filter,a packet size,a timeout. This method will call the function open_live,then compile the filter string by compile(), set the filter and returns the pointer (<B>pcap_t *B>).
pcapinit_offline($fname) is a method for an offline pcap init.The input parameter is a name of the file which contains raw output of the libpcap dump function. Returns the pointer (<B>pcap_t *B>).
<B>ethnewB>(<B>B>$device<B>B>,<B>destB> => <B>ARGOFDESTB>,<B>sourceB> => <B>ARGOFSOURCEB>) is a method for init the ethernet subclass in the current object, <B>B>$device<B>B> is a required parameter,<B>destB> and <B>sourceB> are an optional, <B>B>$device<B>B> is an ethernet device e.g. <B>eth0B>, an <B>ARGOFDESTB> and an <B>ARGOFSOURCEB> are a the ethernet addresses in the ethernet header of the current object.

The <B>ARGOFDESTB> and the <B>ARGOFSOURCEB> can be given as a string which contain just 6 bytes of the real ethernet address or like the output of the ifconfig(8) e.g. 00:00:E8:43:0B:2A or just an ip address or a hostname of a target, then a mac address will be discovered automatically.

The ethernet frame will be sent with given addresses. By default the <B>sourceB> and the <B>destB> will be filled with a hardware address of the <B>B>$device<B>B>.

<B>NOTE:B> For use methods which are related to the ethernet you have to before initialize ethernet subclass by <B>ethnewB>.

ethset is a method for set an ethernet parameters in the current object. The given parameters must look like parameters for the <B>ethnewB> without a <B>B>$device<B>B>.
ethsend is a method for send an ethernet frame. The given parameters must look like a parameters for the <B>sendB>.
send_eth_frame($frame,$times,$delay) is a method for send any ethernet frame which you may construct by hands.<B>B>$frame<B>B> is a packed ethernet frame exept destination and source fields(these fields can be setting by <B>ethsetB> or <B>ethnewB>). Another parameters must look like the parameters for the <B>sendB>.
optset(OPTPROTO => { type => [...],data => [...] },...) is a method for set an IP and a TCP options. The parameters for the optset must be given as a key-value pairs. The <B>OPTPROTOB>,s are the prototypes of the options(<B>ipB>,<B>tcpB>),values are the hashes references.The keys in this hashes are <B>typeB> and <B>dataB>. The value of the <B>typeB> is an array reference. This array must be filled with an integers.Refer to a RFC for a valid types.The value of the <B>dataB> also is an array reference. This array must be filled with strings which must contain all bytes from a option except bytes with type and length of an option.Of course indexes in those arrays must be equal for the one option.If type is equal to 0 or 1 then there is no bytes with a length and a data,but you have to specify zero data for compability.
<B>optgetB>(OPTPROTO => { type => [...] },...) is a method for get an IP and a TCP options. The parameters for the optget must be given as key-value pairs. The <B>OPTPROTOB> is the prototype of the options(<B>ipB>,<B>tcpB>),the values are the hashes references.The key is the <B>typeB>.The value of the <B>typeB> is an array reference. The return value is an array which will be filled with asked types,lengths,datas of the each type of the option in order as you have asked.If you do not specify type then all types,lengths,datas of an options will be returned. E.g. you want to know all the IP options from the current object. Here is a code:

    @opts = $n->optget(ip => {});

E.g. you want to know just the IP options with the type which equal to 131 and 137. Here is a code:

    ($t131,$l131,$d131,$t137,$l137,$d137) = $n->optget(
                                   ip =>{
                        type =>[(131,137)]
                        }        );

<B>optunsetB> is a method for unset a subclass of the IP or the TCP options from a current object.It can be used if you won’t use options in the current object later. This method must be used only after the <B>optsetB>. The parameters for this method are the <B>OPTPROTOB>’s. E.g. you want to unset an IP options. Here is a code:


E.g. you want to unset a TCP and an IP options. Here is a code:



pcap(3), tcpdump(1), RFC 791-793, RFC 768.

Net::Pcap, Net::Pcap::Easy, Net::Pcap::Reassemble, Net::Pcap::FindDevice

Net::Write for an alternative module to send raw packets on the network


Current maintainer is Se\k:'Aperghis-Tramoni <>

Previous authors & maintainers:
o Sergey Kolychev <>
o Gabor Szabo <>


Copyright (c) 1998-2006 Sergey Kolychev. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Steve Bonds <>
+ work on some endianness bugs and improving code comments


Hey! <B>The above document had some coding errors, which are explained below:B>
Around line 704: Unknown directive: =head
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 NET::RAWIP (3) 2008-10-29

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.