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(queue, nfad) ⇒ Packet

:nodoc:



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

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

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

    @id = [ hdr[:packet_id] ].pack("N").unpack("V")[0]
    @protocol = [ hdr[:hw_protocol] ].pack('n').unpack("v")[0]
end

Instance Attribute Details

#dataObject

The packet contents.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nfqueue.rb', line 174

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

#protocolObject (readonly)

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

Instance Method Details

#hw_addrObject

The source hardware address.



162
163
164
165
166
167
168
169
# File 'lib/nfqueue.rb', line 162

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?).



103
104
105
# File 'lib/nfqueue.rb', line 103

def indev
    Queue.nfq_get_indev(@nfad)
end

#indev_nameObject

The name of the interface this packet was received through.



110
111
112
# File 'lib/nfqueue.rb', line 110

def indev_name
    get_interface_name(self.indev)
end

#nfmarkObject

The netfilter mark.



82
83
84
# File 'lib/nfqueue.rb', line 82

def nfmark
    Queue.nfq_get_nfmark(@nfad)
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?).



133
134
135
# File 'lib/nfqueue.rb', line 133

def outdev
    Queue.nfq_get_outdev(@nfad)
end

#outdev_nameObject

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



140
141
142
# File 'lib/nfqueue.rb', line 140

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



118
119
120
# File 'lib/nfqueue.rb', line 118

def phys_indev
    Queue.nfq_get_physindev(@nfad)
end

#phys_indev_nameObject

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



125
126
127
# File 'lib/nfqueue.rb', line 125

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



148
149
150
# File 'lib/nfqueue.rb', line 148

def phys_outdev
    Queue.nfq_get_physoutdev(@nfad)
end

#phys_outdev_nameObject

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



155
156
157
# File 'lib/nfqueue.rb', line 155

def phys_outdev_name
    get_interface_name(self.phys_outdev)
end

#timestampObject

The packet timestamp.



89
90
91
92
93
94
95
96
97
# File 'lib/nfqueue.rb', line 89

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