Class: Netfilter::Packet

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

Overview

Class representing a packet captured by Netfilter::Log.

Defined Under Namespace

Classes: HardwareAddress, Timeval

Instance Method Summary collapse

Constructor Details

#initialize(nflog, nfad) ⇒ Packet

Returns a new instance of Packet.



51
52
53
# File 'lib/nflog.rb', line 51

def initialize(nflog, nfad)
    @nflog, @nfad = nflog, nfad
end

Instance Method Details

#dataObject

The packet contents.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nflog.rb', line 146

def data
    hwhdrlen = Log.nflog_get_msg_packet_hwhdrlen(@nfad)
    
    if hwhdrlen > 0
        hwhdr = Log.nflog_get_msg_packet_hwhdr(@nfad)
        link_header = hwhdr.read_bytes(hwhdrlen)
    else
        link_header = ''
    end

    payload_ptr = FFI::MemoryPointer.new(:pointer, 1)
    payload_size = Log.nflog_get_payload(@nfad, payload_ptr)
    if payload_size < 0
        raise LogError, "nflog_get_payload has failed"
    end

    payload = payload_ptr.read_pointer.read_bytes(payload_size)

    [ link_header, payload ]
end

#gidObject

The GID of the user that generated the packet.



216
217
218
219
220
221
222
223
# File 'lib/nflog.rb', line 216

def gid
    gid = FFI::Buffer.new(FFI.type_size(FFI::Type::UINT32))
    if Log.nflog_get_gid(@nfad, gid) < 0
        return 0 
    end

    gid.read_bytes(gid.total).unpack("I")[0]
end

#hw_addrObject

The source MAC address.



134
135
136
137
138
139
140
141
# File 'lib/nflog.rb', line 134

def hw_addr
    phw = Log.nflog_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 interface the packet was received through.



78
79
80
# File 'lib/nflog.rb', line 78

def indev
    Log.nflog_get_indev(@nfad)
end

#indev_nameObject

The name of the interface the packet was received through.



85
86
87
# File 'lib/nflog.rb', line 85

def indev_name
    get_interface_name(self.indev)
end

#nfmarkObject

The netfilter mark.



58
59
60
# File 'lib/nflog.rb', line 58

def nfmark
    Log.nflog_get_nfmark(@nfad)
end

#outdevObject

The index of the interface the packet will be routed to.



106
107
108
# File 'lib/nflog.rb', line 106

def outdev
    Log.nflog_get_outdev(@nfad)
end

#outdev_nameObject

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



113
114
115
# File 'lib/nflog.rb', line 113

def outdev_name
    get_interface_name(self.outdev)
end

#phys_indevObject

The index of the physical interface the packet was received through.



92
93
94
# File 'lib/nflog.rb', line 92

def phys_indev
    Log.nflog_get_physindev(@nfad)
end

#phys_indev_nameObject

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



99
100
101
# File 'lib/nflog.rb', line 99

def phys_indev_name
    get_interface_name(self.phys_indev)
end

#phys_outdevObject

The index of the physical interface the packet will be routed to.



120
121
122
# File 'lib/nflog.rb', line 120

def phys_outdev
    Log.nflog_get_physoutdev(@nfad)
end

#phys_outdev_nameObject

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



127
128
129
# File 'lib/nflog.rb', line 127

def phys_outdev_name
    get_interface_name(self.phys_outdev)
end

#prefixObject

The logging string.

Raises:



170
171
172
173
174
175
# File 'lib/nflog.rb', line 170

def prefix
    logstr = Log.nflog_get_prefix(@nfad)
    raise LogError, "nflog_get_prefix has failed" if logstr.null?

    logstr.read_string
end

#seqObject

The NFLOG sequence number.



180
181
182
183
184
185
186
187
# File 'lib/nflog.rb', line 180

def seq
    seqnum = FFI::Buffer.new(FFI.type_size(FFI::Type::UINT32))
    if Log.nflog_get_seq(@nfad, seqnum) < 0
        raise LogError, "nflog_get_seq has failed"
    end

    seqnum.read_bytes(seqnum.total).unpack("I")[0]
end

#seq_globalObject

The global NFLOG sequence number.



192
193
194
195
196
197
198
199
# File 'lib/nflog.rb', line 192

def seq_global
    seqnum = FFI::Buffer.new(FFI.type_size(FFI::Type::UINT32))
    if Log.nflog_get_seq_global(@nfad, seqnum) < 0
        raise LogError, "nflog_get_seq_global has failed"
    end

    seqnum.read_bytes(seqnum.total).unpack("I")[0]
end

#timestampObject

The packet timestamp.



65
66
67
68
69
70
71
72
73
# File 'lib/nflog.rb', line 65

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

#uidObject

The UID of the user that generated the packet.



204
205
206
207
208
209
210
211
# File 'lib/nflog.rb', line 204

def uid
    uid = FFI::Buffer.new(FFI.type_size(FFI::Type::UINT32))
    if Log.nflog_get_uid(@nfad, uid) < 0
        return 0
    end

    uid.read_bytes(uid.total).unpack("I")[0]
end