Class: FFWD::Plugin::Tunnel::BinaryProtocol

Inherits:
Tunnel::Plugin
  • Object
show all
Includes:
Lifecycle, Logging
Defined in:
lib/ffwd/plugin/tunnel/binary_protocol.rb

Defined Under Namespace

Classes: Header, PeerAddrAfInet, PeerAddrAfInet6, State

Constant Summary collapse

HeaderFormat =
'nnnCC'
HeaderSize =
8
PeerAddrAfInetFormat =
"a4n"
PeerAddrAfInetSize =
6
PeerAddrAfInet6Format =
"a16n"
PeerAddrAfInet6Size =
18
StateFormat =
'n'
StateSize =
2
STATE =
0x0000
DATA =
0x0001
OPEN =
0x0000
CLOSE =
0x0001

Instance Method Summary collapse

Constructor Details

#initialize(core, c) ⇒ BinaryProtocol

Returns a new instance of BinaryProtocol.



55
56
57
58
59
60
61
62
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 55

def initialize core, c
  @c = c
  @tcp_bind = {}
  @udp_bind = {}

  @header = nil
  @parent = core
end

Instance Method Details

#parse_addr_format(family) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 152

def parse_addr_format family
  if family == Socket::AF_INET
    return PeerAddrAfInetFormat, PeerAddrAfInetSize
  end

  if family == Socket::AF_INET6
    return PeerAddrAfInet6Format, PeerAddrAfInet6Size
  end

  raise "Unsupported address family: #{family}"
end

#peer_addr_pack(family, addr) ⇒ Object



164
165
166
167
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 164

def peer_addr_pack family, addr
  format, size = parse_addr_format family
  return addr.pack(format), size
end

#peer_addr_unpack(family, data) ⇒ Object



169
170
171
172
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 169

def peer_addr_unpack family, data
  format, size = parse_addr_format family
  return data[0,size].unpack(format), size
end

#read_metadata(data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 106

def  data
  d = {}

  d[:tags] = FFWD.merge_sets @parent.tags, data["tags"]
  d[:attributes] = FFWD.merge_sets @parent.attributes, data["attributes"]

  if host = data["host"]
    d[:host] = host
  end

  if ttl = data["ttl"]
    d[:ttl] = ttl
  end

  d
end

#receive_binary_data(data) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 187

def receive_binary_data data
  if @header
    addr, addr_size = peer_addr_unpack @header.family, data
    data = data[addr_size,data.size - addr_size]
    receive_frame @header, addr, data
    @header = nil
    @c.set_text_mode HeaderSize
    return
  end

  fields = data.unpack HeaderFormat
  @header = Header.new(*fields)
  rest = @header.length - HeaderSize
  @c.set_text_mode rest
end

#receive_frame(header, addr, data) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 174

def receive_frame header, addr, data
  if header.type == DATA
    tunnel_data header, addr, data
    return
  end

  if header.type == STATE
    state = data.unpack(StateFormat)[0]
    tunnel_state header, addr, state
    return
  end
end

#receive_line(line) ⇒ Object



146
147
148
149
150
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 146

def receive_line line
  raise "already have metadata" if @metadata
   JSON.load(line)
  @c.set_text_mode HeaderSize
end

#receive_metadata(data) ⇒ Object



141
142
143
144
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 141

def  data
  setup (data)
  send_config
end

#send_configObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 123

def send_config
  response = {}

  tcp = @tcp_bind.keys.map{|port, family|
    {:protocol => Socket::SOCK_STREAM,
     :family => family,
     :port => port}}
  udp = @udp_bind.keys.map{|port, family|
    {:protocol => Socket::SOCK_DGRAM,
     :family => family,
     :port => port}}

  response[:bind] = tcp + udp

  response = JSON.dump(response)
  @c.send_data "#{response}\n"
end

#send_data(protocol, family, port, addr, data) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 203

def send_data protocol, family, port, addr, data
  addr_data, addr_size = peer_addr_pack family, addr
  length = HeaderSize + addr_size + data.size
  # Struct.new(:length, :type, :port, :family, :protocol)
  header_data = [
    length, DATA, port, family, protocol].pack HeaderFormat
  frame = header_data + addr_data + data
  @c.send_data frame
end

#setup(metadata) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 64

def setup 
  unless id = [:host]
    id = @c.get_peer
  end

  input = FFWD::PluginChannel.build "tunnel.input/#{id}"
  core = @parent.reconnect input

  # setup a small core
  emitter = FFWD::Core::Emitter.build core.output, 
  processor = FFWD::Core::Processor.build input, emitter, core.processors

  if core.debug
    core.debug.monitor input, FFWD::Debug::Input
  end

  if core.statistics
    reporters = [input, processor]
    reporter = FFWD::Core::Reporter.new reporters
    core.statistics.register self, "tunnel/#{id}", reporter
  end

  core.tunnel_plugins.each do |factory|
    factory.call(core, self).depend_on self
  end

  input.depend_on self
  core.depend_on self

  start
end

#tcp(port, &block) ⇒ Object



96
97
98
99
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 96

def tcp port, &block
  @tcp_bind[[port, Socket::AF_INET]] = BindTCP.new(
    port, Socket::AF_INET, self, block)
end

#tunnel_data(header, addr, data) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 213

def tunnel_data header, addr, data
  if header.protocol == Socket::SOCK_DGRAM
    if udp = @udp_bind[[header.port, header.family]]
      udp.data! addr, data
    end

    return
  end

  if header.protocol == Socket::SOCK_STREAM
    unless bind = @tcp_bind[[header.port, header.family]]
      log.error "Nothing listening on tcp/#{header.port}"
      return
    end

    bind.data addr, data
    return
  end

  log.error "DATA: Unsupported protocol: #{header.protocol}"
end

#tunnel_state(header, addr, state) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 235

def tunnel_state header, addr, state
  if header.protocol == Socket::SOCK_DGRAM
    log.error "UDP does not handle: #{state}"
    return
  end

  if header.protocol == Socket::SOCK_STREAM
    unless bind = @tcp_bind[[header.port, header.family]]
      log.error "Nothing listening on tcp/#{header.port}"
      return
    end

    if state == OPEN
      bind.open addr
      return
    end

    if state == CLOSE
      bind.close addr
      return
    end

    log.error "Unknown state: #{state}"
    return
  end

  log.error "STATE: Unsupported protocol: #{header.protocol}"
end

#udp(port, &block) ⇒ Object



101
102
103
104
# File 'lib/ffwd/plugin/tunnel/binary_protocol.rb', line 101

def udp port, &block
  @udp_bind[[port, Socket::AF_INET]] = BindUDP.new(
    port, Socket::AF_INET, self, block)
end