1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
pub mod compatibility;
pub mod events;
pub mod telnet;

pub use bytes;

use crate::telnet::op_command::*;

#[cfg(test)]
mod tests;

use bytes::{BufMut, Bytes, BytesMut};
use compatibility::*;

pub enum EventType {
  None(Bytes),
  IAC(Bytes),
  SubNegotiation(Bytes, Option<Bytes>),
  Neg(Bytes),
}

#[macro_export]
/// Macro for calling `Bytes::copy_from_slice()`
macro_rules! vbytes {
  ($slice:expr) => {
    Bytes::copy_from_slice($slice)
  };
}

/// A telnet parser that handles the main parts of the protocol.
pub struct Parser {
  pub options: CompatibilityTable,
  buffer: BytesMut,
}

impl Default for Parser {
  fn default() -> Parser {
    Parser {
      options: CompatibilityTable::new(),
      buffer: BytesMut::with_capacity(128),
    }
  }
}

impl Parser {
  /// Create a default, empty Parser with an internal buffer capacity of 128 bytes.
  pub fn new() -> Self {
    Self::default()
  }
  /// Create an empty parser, setting the initial internal buffer capcity.
  pub fn with_capacity(size: usize) -> Self {
    Self {
      options: CompatibilityTable::new(),
      buffer: BytesMut::with_capacity(size),
    }
  }
  /// Create an parser, setting the initial internal buffer capacity and directly supplying a CompatibilityTable.
  pub fn with_support_and_capacity(size: usize, table: CompatibilityTable) -> Self {
    Self {
      options: table,
      buffer: BytesMut::with_capacity(size),
    }
  }
  /// Create a parser, directly supplying a CompatibilityTable.
  ///
  /// Uses the default initial buffer capacity of 128 bytes.
  pub fn with_support(table: CompatibilityTable) -> Self {
    Self {
      options: table,
      buffer: BytesMut::with_capacity(128),
    }
  }
  /// Receive bytes into the internal buffer.
  ///
  /// # Arguments
  ///
  /// * `data` - The bytes to be received. This should be sourced from the remote side of a connection.
  ///
  /// # Returns
  ///
  /// `Vec<events::TelnetEvents>` - Any events parsed from the internal buffer with the new bytes.
  ///
  pub fn receive(&mut self, data: &[u8]) -> Vec<events::TelnetEvents> {
    self.buffer.put(data);
    self.process()
  }
  /// Get whether the remote end supports and is using linemode.
  pub fn linemode_enabled(&mut self) -> bool {
    let opt = self.options.get_option(telnet::op_option::LINEMODE);
    opt.remote && opt.remote_state
  }
  /// Escape IAC bytes in data that is to be transmitted and treated as a non-IAC sequence.
  ///
  /// # Example
  /// `[255, 1, 6, 2]` -> `[255, 255, 1, 6, 2]`
  pub fn escape_iac<T>(data: T) -> Bytes
  where
    Bytes: From<T>,
  {
    let data = Bytes::from(data);
    let mut t = BytesMut::with_capacity(data.len());
    for byte in data.iter() {
      t.put_u8(*byte);
      if *byte == 255 {
        t.put_u8(255);
      }
    }
    t.freeze()
  }
  /// Reverse escaped IAC bytes for non-IAC sequences and data.
  ///
  /// # Example
  /// `[255, 255, 1, 6, 2]` -> `[255, 1, 6, 2]`
  pub fn unescape_iac<T>(data: T) -> Bytes
  where
    Bytes: From<T>,
  {
    let data = Bytes::from(data);
    let mut t = BytesMut::with_capacity(data.len());
    let mut last = 0u8;
    for val in data.iter() {
      if *val == 255 && last == 255 {
        continue;
      }
      last = *val;
      t.put_u8(*val);
    }
    t.freeze()
  }
  /// Negotiate an option.
  ///
  /// # Arguments
  ///
  /// `command` - A `u8` representing the telnet command code to be negotiated with. Example: WILL (251), WONT (252), DO (253), DONT (254)
  ///
  /// `option` - A `u8` representing the telnet option code that is being negotiated.
  ///
  /// # Returns
  ///
  /// `events::TelnetEvents::DataSend` - A DataSend event to be processed.
  ///
  /// # Usage
  ///
  /// This and other methods meant for sending data to the remote end will generate a `TelnetEvents::Send(DataEvent)` event.
  ///
  /// These Send events contain a buffer that should be sent directly to the remote end, as it will have already been encoded properly.
  pub fn negotiate(&mut self, command: u8, option: u8) -> events::TelnetEvents {
    events::TelnetEvents::build_send(events::TelnetNegotiation::new(command, option).into())
  }
  /// Indicate to the other side that you are able and wanting to utilize an option.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code that you want to enable locally.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - The DataSend event to be processed, or None if not supported.
  ///
  /// # Notes
  ///
  /// This method will do nothing if the option is not "supported" locally via the `CompatibilityTable`.
  pub fn _will(&mut self, option: u8) -> Option<events::TelnetEvents> {
    let mut opt = self.options.get_option(option);
    if opt.local && !opt.local_state {
      opt.local_state = true;
      self.options.set_option(option, opt);
      Some(self.negotiate(251, option))
    } else {
      None
    }
  }
  /// Indicate to the other side that you are not wanting to utilize an option.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code that you want to disable locally.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - A DataSend event to be processed, or None if the option is already disabled.
  ///
  pub fn _wont(&mut self, option: u8) -> Option<events::TelnetEvents> {
    let mut opt = self.options.get_option(option);
    if opt.local_state {
      opt.local_state = false;
      self.options.set_option(option, opt);
      Some(self.negotiate(252, option))
    } else {
      None
    }
  }
  /// Indicate to the other side that you would like them to utilize an option.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code that you want to enable remotely.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - A DataSend event to be processed, or None if the option is not supported or already enabled.
  ///
  /// # Notes
  ///
  /// This method will do nothing if the option is not "supported" remotely via the `CompatibilityTable`.
  pub fn _do(&mut self, option: u8) -> Option<events::TelnetEvents> {
    let opt = self.options.get_option(option);
    if opt.remote && !opt.remote_state {
      Some(self.negotiate(253, option))
    } else {
      None
    }
  }
  /// Indicate to the other side that you would like them to stop utilizing an option.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code that you want to disable remotely.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - A DataSend event to be processed, or None if the option is already disabled.
  ///
  pub fn _dont(&mut self, option: u8) -> Option<events::TelnetEvents> {
    let opt = self.options.get_option(option);
    if opt.remote_state {
      Some(self.negotiate(254, option))
    } else {
      None
    }
  }
  /// Send a subnegotiation for a locally supported option.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code for the negotiation.
  ///
  /// `data` - A `Bytes` containing the data to be sent in the subnegotiation. This data will have all IAC (255) byte values escaped.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - A DataSend event to be processed, or None if the option is not supported or is currently disabled.
  ///
  /// # Notes
  ///
  /// This method will do nothing if the option is not "supported" locally via the `CompatibilityTable`.
  pub fn subnegotiation<T>(&mut self, option: u8, data: T) -> Option<events::TelnetEvents>
  where
    Bytes: From<T>,
  {
    let opt = self.options.get_option(option);
    if opt.local && opt.local_state {
      Some(events::TelnetEvents::build_send(
        events::TelnetSubnegotiation::new(option, Bytes::from(data)).into(),
      ))
    } else {
      None
    }
  }
  /// Send a subnegotiation for a locally supported option, using a string instead of raw byte values.
  ///
  /// # Arguments
  ///
  /// `option` - A `u8` representing the telnet option code for the negotiation.
  ///
  /// `text` - A `&str` representing the text to be sent in the subnegotation. This data will have all IAC (255) byte values escaped.
  ///
  /// # Returns
  ///
  /// `Option<events::TelnetEvents::DataSend>` - A DataSend event to be processed, or None if the option is not supported or is currently disabled.
  ///
  /// # Notes
  ///
  /// This method will do nothing if the option is not "supported" locally via the `CompatibilityTable`.
  pub fn subnegotiation_text(&mut self, option: u8, text: &str) -> Option<events::TelnetEvents> {
    self.subnegotiation(option, Bytes::copy_from_slice(&text.as_bytes()))
  }
  /// Directly send a string, with appended `\r\n`, to the remote end, along with an `IAC (255) GOAHEAD (249)` sequence.
  ///
  /// # Returns
  ///
  /// `events::TelnetEvents::DataSend` - A DataSend event to be processed.
  ///
  /// # Notes
  ///
  /// The string will have IAC (255) bytes escaped before being sent.
  pub fn send_text(&mut self, text: &str) -> events::TelnetEvents {
    events::TelnetEvents::build_send(Bytes::copy_from_slice(&Parser::escape_iac(
      format!("{}\r\n", text).into_bytes(),
    )))
  }

  /// Extract sub-buffers from the current buffer
  fn extract_event_data(&mut self) -> Vec<EventType> {
    enum State {
      Normal,
      IAC,
      Neg,
      Sub,
    }
    let mut iter_state = State::Normal;

    let mut events: Vec<EventType> = Vec::with_capacity(4);
    let iter = self.buffer.iter().enumerate();
    let mut cmd_begin: usize = 0;

    for (index, &val) in iter {
      match iter_state {
        State::Normal => {
          if val == IAC {
            if cmd_begin < index {
              events.push(EventType::None(vbytes!(&self.buffer[cmd_begin..index])));
            }
            cmd_begin = index;
            iter_state = State::IAC;
          }
        }
        State::IAC => {
          match val {
            IAC => iter_state = State::Normal, // Double IAC, ignore
            GA | EOR | NOP => {
              events.push(EventType::IAC(vbytes!(&self.buffer[cmd_begin..index + 1])));
              cmd_begin = index + 1;
              iter_state = State::Normal;
            }
            SB => iter_state = State::Sub,
            _ => iter_state = State::Neg, // WILL | WONT | DO | DONT | IS | SEND
          }
        }
        State::Neg => {
          events.push(EventType::Neg(vbytes!(&self.buffer[cmd_begin..index + 1])));
          cmd_begin = index + 1;
          iter_state = State::Normal;
        }
        State::Sub => {
          if val == SE {
            let opt = &self.buffer[cmd_begin + 2];
            if *opt == telnet::op_option::MCCP2 || *opt == telnet::op_option::MCCP3 {
              // MCCP2/MCCP3 MUST DECOMPRESS DATA AFTER THIS!
              events.push(EventType::SubNegotiation(
                vbytes!(&self.buffer[cmd_begin..index + 1]),
                Some(vbytes!(&self.buffer[index + 1..])),
              ));
              cmd_begin = self.buffer.len();
              break;
            } else {
              events.push(EventType::SubNegotiation(
                vbytes!(&self.buffer[cmd_begin..index + 1]),
                None,
              ));
              cmd_begin = index + 1;
              iter_state = State::Normal;
            }
          }
        }
      }
    }
    if cmd_begin < self.buffer.len() {
      match iter_state {
        State::Sub => events.push(EventType::SubNegotiation(
          vbytes!(&self.buffer[cmd_begin..]),
          None,
        )),
        _ => events.push(EventType::None(vbytes!(&self.buffer[cmd_begin..]))),
      }
    }

    // Empty the buffer when we are done
    self.buffer.clear();
    events
  }

  /// The internal parser method that takes the current buffer and generates the corresponding events.
  fn process(&mut self) -> Vec<events::TelnetEvents> {
    let mut event_list: Vec<events::TelnetEvents> = Vec::with_capacity(2);
    for event in self.extract_event_data() {
      match event {
        EventType::None(buffer) | EventType::IAC(buffer) | EventType::Neg(buffer) => {
          if buffer.is_empty() {
            continue;
          }
          if buffer[0] == IAC {
            match buffer.len() {
              2 => {
                if buffer[1] != SE {
                  // IAC command
                  event_list.push(events::TelnetEvents::build_iac(buffer[1]));
                }
              }
              3 => {
                // Negotiation
                let mut opt = self.options.get_option(buffer[2]);
                let event = events::TelnetNegotiation::new(buffer[1], buffer[2]);
                match buffer[1] {
                  WILL => {
                    if opt.remote && !opt.remote_state {
                      opt.remote_state = true;
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, DO, buffer[2]
                      ])));
                      self.options.set_option(buffer[2], opt);
                      event_list.push(events::TelnetEvents::Negotiation(event));
                    } else if !opt.remote {
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, DONT, buffer[2]
                      ])));
                    }
                  }
                  WONT => {
                    if opt.remote_state {
                      opt.remote_state = false;
                      self.options.set_option(buffer[2], opt);
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, DONT, buffer[2]
                      ])));
                    }
                    event_list.push(events::TelnetEvents::Negotiation(event));
                  }
                  DO => {
                    if opt.local && !opt.local_state {
                      opt.local_state = true;
                      opt.remote_state = true;
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, WILL, buffer[2]
                      ])));
                      self.options.set_option(buffer[2], opt);
                      event_list.push(events::TelnetEvents::Negotiation(event));
                    } else if !opt.local {
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, WONT, buffer[2]
                      ])));
                    }
                  }
                  DONT => {
                    if opt.local_state {
                      opt.local_state = false;
                      self.options.set_option(buffer[2], opt);
                      event_list.push(events::TelnetEvents::build_send(vbytes!(&[
                        IAC, WONT, buffer[2]
                      ])));
                    }
                    event_list.push(events::TelnetEvents::Negotiation(event));
                  }
                  _ => (),
                }
              }
              _ => (),
            }
          } else {
            // Not an iac sequence, it's data!
            event_list.push(events::TelnetEvents::build_receive(buffer));
          }
        }
        EventType::SubNegotiation(buffer, remaining) => {
          let len: usize = buffer.len();
          if buffer[len - 2] == IAC && buffer[len - 1] == SE {
            // Valid ending
            let opt = self.options.get_option(buffer[2]);
            if opt.local && opt.local_state {
              let dbuffer = vbytes!(&buffer[3..len - 2]);
              event_list.push(events::TelnetEvents::build_subnegotiation(
                buffer[2], dbuffer,
              ));
              if let Some(rbuf) = remaining {
                event_list.push(events::TelnetEvents::DecompressImmediate(rbuf));
              }
            }
          } else {
            // Missing the rest
            self.buffer.put(&buffer[..]);
          }
        }
      }
    }
    event_list
  }
}