Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. use v6.c;
  2. unit class Net::Telnet::Chunk;
  3.  
  4. enum TelnetOption is export (
  5. 'TRANSMIT_BINARY' => 0x00.chr,
  6. 'ECHO' => 0x01.chr,
  7. 'RCP' => 0x02.chr, # Reconnection
  8. 'SGA' => 0x03.chr, # Suppress Go Ahead
  9. 'NAMS' => 0x04.chr, # Message Size Negotiation
  10. 'STATUS' => 0x05.chr,
  11. 'TIMING_MARK' => 0x06.chr,
  12. 'RCTE' => 0x07.chr, # Remote Controlled Trans and Echo
  13. 'NAOL' => 0x08.chr, # Output Line Width
  14. 'NAOP' => 0x09.chr, # Output Line Height
  15. 'NAOCRD' => 0x0A.chr, # Output Carriage-Return Disposition
  16. 'NAOHTS' => 0x0B.chr, # Output Horizontal Tab Stops
  17. 'NAOHTD' => 0x0C.chr, # Output Horizontal Tab Disposition
  18. 'NAOFFD' => 0x0D.chr, # Output Formfeed Disposition
  19. 'NAOVTS' => 0x0E.chr, # Output Vertical Stops
  20. 'NAOVTD' => 0x0F.chr, # Output Vertical Disposition
  21. 'NAOLFD' => 0x10.chr, # Output Linefeed Disposition
  22. 'XASCII' => 0x11.chr, # Extended ASCII
  23. 'LOGOUT' => 0x12.chr,
  24. 'BM' => 0x13.chr, # Byte Macro
  25. 'DET' => 0x14.chr, # Data Entry Terminal
  26. 'SUPDUP' => 0x15.chr,
  27. 'SUPDUP_OUTPUT' => 0x16.chr,
  28. 'SEND_LOCATION' => 0x17.chr,
  29. 'TERMINAL_TYPE' => 0x18.chr,
  30. 'END_OF_RECORD' => 0x19.chr,
  31. 'TUID' => 0x1A.chr, # TACACS User Identification
  32. 'OUTMRK' => 0x1B.chr, # Output Marking
  33. 'TTYLOC' => 0x1C.chr, # Terminal Location Number
  34. '3270_REGIME' => 0x1D.chr,
  35. 'X_3_PAD' => 0x1E.chr,
  36. 'NAWS' => 0x1F.chr, # Negotiate About Window Size
  37. 'TERMINAL_SPEED' => 0x20.chr,
  38. 'TOGGLE_FLOW_CONTROL' => 0x21.chr,
  39. 'LINEMODE' => 0x22.chr,
  40. 'XDISPLOC' => 0x23.chr, # X Display Location
  41. 'ENVIRON' => 0x24.chr, # Environment
  42. 'AUTHENTICATION' => 0x25.chr,
  43. 'ENCRYPT' => 0x26.chr,
  44. 'NEW_ENVIRON' => 0x27.chr,
  45. 'TN3270E' => 0x28.chr,
  46. 'XAUTH' => 0x29.chr,
  47. 'CHARSET' => 0x2A.chr,
  48. 'RSP' => 0x2B.chr, # Remote Serial Port
  49. 'COM_PORT_OPTION' => 0x2C.chr,
  50. 'SLE' => 0x2D.chr, # Suppress Local Echo
  51. 'START_TLS' => 0x2E.chr,
  52. 'KERMIT' => 0x2F.chr,
  53. 'SEND_URL' => 0x30.chr,
  54. 'FORWARD_X' => 0x31.chr,
  55. 'PRAGMA_LOGON' => 0x8A.chr,
  56. 'SSPI_LOGON' => 0x8B.chr,
  57. 'PRAGMA_HEARTBEAT' => 0x8C.chr,
  58. 'EXOPL' => 0xFF.chr
  59. );
  60.  
  61. enum TelnetCommand is export (
  62. 'SE' => 0xF0.chr, # Subnegotiation End
  63. 'NOP' => 0xF1.chr, # No Operation
  64. 'DM' => 0xF2.chr, # Data Mark
  65. 'BRK' => 0xF3.chr, # Break
  66. 'IP' => 0xF4.chr, # Interrupt Process
  67. 'AO' => 0xF5.chr, # Abort Output
  68. 'AYT' => 0xF6.chr, # Are You There?
  69. 'EC' => 0xF7.chr, # Erase Character
  70. 'EL' => 0xF8.chr, # Erase Line
  71. 'GA' => 0xF9.chr, # Go Ahead
  72. 'SB' => 0xFA.chr, # Subnegotiation Begin
  73. 'WILL' => 0xFB.chr,
  74. 'WONT' => 0xFC.chr,
  75. 'DO' => 0xFD.chr,
  76. 'DONT' => 0xFE.chr,
  77. 'IAC' => 0xFF.chr # Interpret As Command
  78. );
  79.  
  80. grammar Grammar {
  81. method debug($/, $message) {
  82. note $message;
  83. note "Commannd: $/";
  84. }
  85.  
  86. token TOP { <chunk>+ }
  87.  
  88. token chunk {
  89. :my TelnetCommand $*ACTION;
  90. :my Bool $*SUPPRESS-GO-AHEAD;
  91. <action> | <data>
  92. }
  93.  
  94. token data { <:ascii>+ }
  95.  
  96. proto token action {*}
  97. token action:sym(SE) { <{IAC}> <command=sym> }
  98. token action:sym(NOP) { <{IAC}> <command=sym> }
  99. token action:sym(DM) { <{IAC}> <command=sym> }
  100. token action:sym(BRK) { <{IAC}> <command=sym> }
  101. token action:sym(IP) { <{IAC}> <command=sym> }
  102. token action:sym(AO) { <{IAC}> <command=sym> }
  103. token action:sym(AYT) { <{IAC}> <command=sym> }
  104. token action:sym(EC) { <{IAC}> <command=sym> }
  105. token action:sym(EL) { <{IAC}> <command=sym> }
  106. token action:sym(GA) {
  107. | { $*SUPPRESS-GO-AHEAD == False } <{IAC}> <command=sym>
  108. | { $*SUPPRESS-GO-AHEAD == True } <.debug: "$/", "Received GA when SUPPRESS-GO-AHEAD was already enabled">
  109. }
  110. token action:sym(SB) {
  111. <(
  112. <{IAC}> <command=sym>
  113. { $*ACTION = SB }
  114. <subnegotiation>
  115. )>
  116. <?action:sym(SE)>
  117. }
  118. token action:sym(WILL) {
  119. <{IAC}> <command=sym>
  120. { $*ACTION = WILL }
  121. <negotiation>
  122. }
  123. token action:sym(WONT) {
  124. {IAC}> <command=sym>
  125. { $*ACTION = WONT }
  126. <negotiation>
  127. }
  128. token action:sym(DO) {
  129. <{IAC}> <command=sym>
  130. { $*ACTION = DO }
  131. <negotiation>
  132. }
  133. token action:sym(DONT) {
  134. <{IAC}> <command=sym>
  135. { $*ACTION = DONT }
  136. <negotiation>
  137. }
  138.  
  139. proto token negotiation {*}
  140. token negotiation:sym(SGA)($action) {
  141. <option=sym>
  142. {
  143. given $*ACTION {
  144. when WILL { $*SUPPRESS-GO-AHEAD = True }
  145. when WONT { $*SUPPRESS-GO-AHEAD = False }
  146. when DO { $*SUPPRESS-GO-AHEAD = True }
  147. when DONT { $*SUPPRESS-GO-AHEAD = False }
  148. when SB { self.debug: $/, "SUPPRESS-GO-AHEAD does no subnegotiatioddn"> }
  149. }
  150. }
  151. }
  152.  
  153. proto token subnegotiation {*}
  154. token subnegotiation:sym(NAWS) {
  155. <option=sym>
  156. <width-upper=<:latin>>
  157. <width-lower=<:latin>>
  158. <height-upper=<:latin>>
  159. <height-lower=<:latin>>
  160. }
  161. }
  162.  
  163. role Command {
  164. has TelnetCommand $.command;
  165. proto method Str(--> Str) { "{IAC.key} {$!command.key} {{*}}" }
  166. }
  167.  
  168. class Negotiation does Command {
  169. has TelnetOption $.option;
  170. multi method Str(--> Str) { $!option.key }
  171. }
  172.  
  173. role Subnegotiation does Command {
  174. has TelnetOption $.option;
  175. multi method Str(--> Str) { "{$!option.key} {{*}} {SE.key}" }
  176. }
  177.  
  178. class Subnegotiation::NAMS does Subnegotiation {
  179. has Int $.width;
  180. has Int $.height;
  181.  
  182. method new(
  183. Str :$option,
  184. Str :$width-upper,
  185. Str :$width-lower,
  186. Str :$height-upper,
  187. Str :$height-lower
  188. ) {
  189. my $option = TelnetOption($sym);
  190. my $width = $width-upper.ord +< 8 +| $width-lower.ord;
  191. my $height = $height-upper.ord +< 8 +| $height-lower.ord;
  192. self.bless: :$option, :$width, :$height;
  193. }
  194.  
  195. multi method Str(--> Str) { "$!width $!height" }
  196. }
  197.  
  198. class Actions {
  199. method TOP($/) {
  200. # ???
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement