Class: Mu::Pcap::IOWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/mu/pcap/io_wrapper.rb

Constant Summary collapse

MAX_RECEIVE_SIZE =

Impose upper limit to protect against memory exhaustion.

1048576

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ios, reader) ⇒ IOWrapper

Returns a new instance of IOWrapper.



12
13
14
15
16
17
18
19
# File 'lib/mu/pcap/io_wrapper.rb', line 12

def initialize ios, reader
    @ios       = ios
    @reader    = reader
    # parse state for reader
    @state     = {}
    # read off of underlying io but not yet processed by @reader
    @unread    = "" 
end

Instance Attribute Details

#iosObject (readonly)

Returns the value of attribute ios.



10
11
12
# File 'lib/mu/pcap/io_wrapper.rb', line 10

def ios
  @ios
end

#stateObject (readonly)

Returns the value of attribute state.



10
11
12
# File 'lib/mu/pcap/io_wrapper.rb', line 10

def state
  @state
end

#unreadObject (readonly)

Returns the value of attribute unread.



10
11
12
# File 'lib/mu/pcap/io_wrapper.rb', line 10

def unread
  @unread
end

Instance Method Details

#closeObject



70
71
72
# File 'lib/mu/pcap/io_wrapper.rb', line 70

def close
    @ios.close
end

#openObject



58
59
60
61
62
63
64
# File 'lib/mu/pcap/io_wrapper.rb', line 58

def open  
    if block_given?
        @ios.open { yield }
    else
        @ios.open
    end
end

#open?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mu/pcap/io_wrapper.rb', line 66

def open?
    @ios.open?
end

#readObject

Returns next higher level protocol message.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mu/pcap/io_wrapper.rb', line 25

def read
    until message = @reader.read_message!(@unread, @state)
        bytes = @ios.read
        if bytes and not bytes.empty?
            @unread << bytes
        else
            return nil 
        end
        if @unread.size > MAX_RECEIVE_SIZE 
            raise "Maximum message size (#{MAX_RECEIVE_SIZE}) exceeded"
        end
    end

    return message
end

#record_write(bytes) ⇒ Object

Parser may need to see requests to understand responses.



42
43
44
# File 'lib/mu/pcap/io_wrapper.rb', line 42

def record_write bytes
    @reader.record_write bytes, @state
end

#write(bytes, *args) ⇒ Object



46
47
48
49
50
# File 'lib/mu/pcap/io_wrapper.rb', line 46

def write bytes, *args
    w = @ios.write bytes, *args
    record_write bytes
    w
end

#write_to(bytes, *args) ⇒ Object



52
53
54
55
56
# File 'lib/mu/pcap/io_wrapper.rb', line 52

def write_to bytes, *args
    w = @ios.write_to bytes, *args
    record_write bytes
    w
end