Class: Netfilter::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/nfqueue.rb

Overview

This class represents a packet filtered by a Netfilter::Queue.

Defined Under Namespace

Classes: HardwareAddress, Header, Timeval

Constant Summary collapse

DROP =
0
ACCEPT =
1
STOLEN =
2
QUEUE =
3
REPEAT =
4
STOP =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nfad) ⇒ Packet

:nodoc:



67
68
69
70
71
72
73
74
# File 'lib/nfqueue.rb', line 67

def initialize(nfad) #:nodoc:
    @nfad = nfad

    phdr = Queue.nfq_get_msg_packet_hdr(nfad)
    hdr = Header.new(phdr)

    @id = [ hdr[:packet_id] ].pack("N").unpack("V")[0]
end

Instance Attribute Details

#dataObject

The packet contents.



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/nfqueue.rb', line 164

def data
    if @data.nil?
        pdata = FFI::MemoryPointer.new(:pointer, 1)
        size = Queue.nfq_get_payload(@nfad, pdata)
        if size < 0
            raise QueueError, "nfq_get_payload has failed"
        end
    
        @data = pdata.read_pointer.read_bytes(size)
    else
        @data
    end
end

#idObject (readonly)

Returns the value of attribute id.



64
65
66
# File 'lib/nfqueue.rb', line 64

def id
  @id
end

Instance Method Details

#hw_addrObject

The source hardware address.



152
153
154
155
156
157
158
159
# File 'lib/nfqueue.rb', line 152

def hw_addr
    phw = Queue.nfq_get_packet_hw(@nfad)
    return nil if phw.null?

    hw = HardwareAddress.new(phw)
    hw_addrlen = [ hw[:hw_addrlen] ].pack('v').unpack('n')[0]
    hw[:hw_addr].to_ptr.read_bytes(hw_addrlen)
end

#indevObject

The index of the device the queued packet was received via. If the return index is 0, the packet was locally generated or the input interface is not known (ie. POSTROUTING?).



93
94
95
# File 'lib/nfqueue.rb', line 93

def indev
    Queue.nfq_get_indev(@nfad)
end

#indev_nameObject

The name of the interface this packet was received through.



100
101
102
# File 'lib/nfqueue.rb', line 100

def indev_name
    get_interface_name(self.indev)
end

#outdevObject

The index of the device the queued packet will be sent out. It the returned index is 0, the packet is destined for localhost or the output interface is not yet known (ie. PREROUTING?).



123
124
125
# File 'lib/nfqueue.rb', line 123

def outdev
    Queue.nfq_get_outdev(@nfad)
end

#outdev_nameObject

The name of the interface this packet will be routed to.



130
131
132
# File 'lib/nfqueue.rb', line 130

def outdev_name
    get_interface_name(self.outdev)
end

#phys_indevObject

The index of the physical device the queued packet was received via. If the returned index is 0, the packet was locally generated or the physical input interface is no longer known (ie. POSTROUTING).



108
109
110
# File 'lib/nfqueue.rb', line 108

def phys_indev
    Queue.nfq_get_physindev(@nfad)
end

#phys_indev_nameObject

The name of the physical interface this packet was received through.



115
116
117
# File 'lib/nfqueue.rb', line 115

def phys_indev_name
    get_interface_name(self.phys_indev)
end

#phys_outdevObject

The index of the physical device the queued packet will be sent out. If the returned index is 0, the packet is destined for localhost or the physical output interface is not yet known (ie. PREROUTING).



138
139
140
# File 'lib/nfqueue.rb', line 138

def phys_outdev
    Queue.nfq_get_physoutdev(@nfad)
end

#phys_outdev_nameObject

The name of the physical interface this packet will be routed to.



145
146
147
# File 'lib/nfqueue.rb', line 145

def phys_outdev_name
    get_interface_name(self.phys_outdev)
end

#timestampObject

The packet timestamp.



79
80
81
82
83
84
85
86
87
# File 'lib/nfqueue.rb', line 79

def timestamp
    ptv = FFI::MemoryPointer.new :pointer
    tv = Timeval.new(ptv)
    if Queue.nfq_get_timestamp(@nfad, ptv) < 0
        0
    else
        Time.at(tv[:tv_sec])
    end
end