Class: Anachronism::Telnet

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

Instance Method Summary collapse

Constructor Details

#initializeTelnet

Returns a new instance of Telnet.

Raises:

  • (NoMemoryError)


2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/anachronism/nvt.rb', line 2

def initialize
  # Make sure these aren't GC'd while the object exists.
  @_on_nvt_event = proc {|_, ev| process_nvt_event(ev)}
  @_on_telopt_event = proc {|_, telopt, ev| process_telopt_event(telopt, ev)}
  
  @telnet_nvt = Anachronism::Native.new_nvt(
    @_on_nvt_event,
    @_on_telopt_event,
    FFI::Pointer.new(0))
  raise NoMemoryError if @telnet_nvt == FFI::Pointer.new(0)
  @telopt_callbacks = Array.new(256)
end

Instance Method Details

#bind(telopt, receiver) ⇒ Object



80
81
82
83
# File 'lib/anachronism/nvt.rb', line 80

def bind (telopt, receiver)
  raise "Can't bind to a bound telopt." if @telopt_callbacks[telopt]
  @telopt_callbacks[telopt] = receiver
end

#interrupt_parserObject

Halts the Telnet parser at the current point, usually so the remainder of the data can be preprocessed before continuing.



24
25
26
27
# File 'lib/anachronism/nvt.rb', line 24

def interrupt_parser
  Anachronism::Native.interrupt(@telnet_nvt)
  nil
end

#local_enabled?(telopt) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/anachronism/nvt.rb', line 73

def local_enabled? (telopt)
  ptr = FFI::MemoryPointer.new :int
  Anachronism::Native.telopt_status_remote(@telnet_nvt, telopt, ptr)
  !!ptr.read_int
end

#on_command(command) ⇒ Object



96
97
# File 'lib/anachronism/nvt.rb', line 96

def on_command (command)
end

#on_send(data) ⇒ Object



99
100
# File 'lib/anachronism/nvt.rb', line 99

def on_send (data)
end

#on_text(text) ⇒ Object

Callbacks



93
94
# File 'lib/anachronism/nvt.rb', line 93

def on_text (text)
end

#on_warning(message, position) ⇒ Object



102
103
# File 'lib/anachronism/nvt.rb', line 102

def on_warning (message, position)
end

#process_nvt_event(event_ptr) ⇒ Object

:nodoc:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/anachronism/nvt.rb', line 106

def process_nvt_event (event_ptr) # :nodoc:
  ev = Anachronism::Native::Event.new(event_ptr)
  
  case ev[:type]
  when :data
    ev = Anachronism::Native::DataEvent.new(event_ptr)
    on_text ev[:data].read_string(ev[:length])
  when :command
    ev = Anachronism::Native::CommandEvent.new(event_ptr)
    on_command ev[:command]
  when :send
    ev = Anachronism::Native::SendEvent.new(event_ptr)
    on_send ev[:data].read_string(ev[:length])
  when :warning
    ev = Anachronism::Native::WarningEvent.new(event_ptr)
    on_warning ev[:message], ev[:position]
  end
end

#process_telopt_event(telopt, event_ptr) ⇒ Object

:nodoc:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/anachronism/nvt.rb', line 125

def process_telopt_event (telopt, event_ptr) # :nodoc:
  receiver = @telopt_callbacks[telopt]
  return unless receiver
  
  ev = Anachronism::Native::TeloptEvent.new(event_ptr)
  case ev[:type]
  when :toggle
    ev = Anachronism::Native::ToggleTeloptEvent.new(event_ptr)
    if ev[:where] == :local
      receiver.on_local_toggle(ev[:status] == :on)
    elsif ev[:where] == :remote
      receiver.on_remote_toggle(ev[:status] == :on)
    end
  when :focus
    ev = Anachronism::Native::FocusTeloptEvent.new(event_ptr)
    if ev[:focus] == 0
      receiver.on_blur
    else
      receiver.on_focus
    end
  when :data
    ev = Anachronism::Native::DataTeloptEvent.new(event_ptr)
    receiver.on_text ev[:data].read_string(ev[:length])
  end
end

#receive(data) ⇒ Object

Raises:

  • (NoMemoryError)


15
16
17
18
19
20
# File 'lib/anachronism/nvt.rb', line 15

def receive (data)
  ptr = FFI::MemoryPointer.new :pointer
  err = Anachronism::Native.receive(@telnet_nvt, data, data.length, ptr)
  raise NoMemoryError if err == :alloc
  data[ptr.read_int..-1]
end

#remote_enabled?(telopt) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/anachronism/nvt.rb', line 67

def remote_enabled? (telopt)
  ptr = FFI::MemoryPointer.new :int
  Anachronism::Native.telopt_status_local(@telnet_nvt, telopt, ptr)
  !!ptr.read_int
end

#request_local_disable(telopt) ⇒ Object



51
52
53
54
# File 'lib/anachronism/nvt.rb', line 51

def request_local_disable (telopt)
  Anachronism::Native.telopt_disable_local(@telnet_nvt, telopt)
  nil
end

#request_local_enable(telopt, opts = {}) ⇒ Object



46
47
48
49
# File 'lib/anachronism/nvt.rb', line 46

def request_local_enable (telopt, opts={})
  Anachronism::Native.telopt_enable_local(@telnet_nvt, telopt, (opts[:lazy]) ? 1 : 0)
  nil
end

#request_remote_disable(telopt) ⇒ Object



61
62
63
64
# File 'lib/anachronism/nvt.rb', line 61

def request_remote_disable (telopt)
  Anachronism::Native.telopt_disable_remote(@telnet_nvt, telopt)
  nil
end

#request_remote_enable(telopt, opts = {}) ⇒ Object



56
57
58
59
# File 'lib/anachronism/nvt.rb', line 56

def request_remote_enable (telopt, opts={})
  Anachronism::Native.telopt_enable_remote(@telnet_nvt, telopt, (opts[:lazy]) ? 1 : 0)
  nil
end

#send_command(command) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/anachronism/nvt.rb', line 35

def send_command (command)
  err = Anachronism::Native.send_command(@telnet_nvt, command)
  raise ArgumentError, "#{command} is not a valid single-byte command" if err == :invalid_command
end

#send_subnegotiation(telopt, data) ⇒ Object

Raises:

  • (NoMemoryError)


40
41
42
43
# File 'lib/anachronism/nvt.rb', line 40

def send_subnegotiation (telopt, data)
  err = Anachronism::Native.send_subnegotiation(@telnet_nvt, telopt, data, data.length)
  raise NoMemoryError if err == :alloc
end

#send_text(data) ⇒ Object

Raises:

  • (NoMemoryError)


30
31
32
33
# File 'lib/anachronism/nvt.rb', line 30

def send_text (data)
  err = Anachronism::Native.send_data(@telnet_nvt, data, data.length)
  raise NoMemoryError if err == :alloc
end

#unbind(telopt) ⇒ Object



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

def unbind (telopt)
  @telopt_callbacks[telopt] = nil
end