Option negotiation |
This section discuss the telnet protocol, the commands used for option negotiation and some of the classes in Telnet Factory for .NET used to perform option negotiation.
Option Negotiation
Upon establishing a connection with a telnet server the client and server may enter into option negotiation. Option negotiation is the process of both the client and server agreeing to a common set of features also known as options that will be supported by both the client and server terminals. See Events later in this section.
Some examples of options that may be negotiated include the type of character emulation to use and whether or not to echo data sent by the client back to the client terminal. As their name implies, options are optional and may be initiated by either the client or the server.
When requesting or responding to an option the option must be preceded by a option negotiation command. There are four (4) option negotiation commands that may be used:
Command |
Description |
WILL |
Sender wants to use the option. |
WONT |
Sender will not support the option. |
DO |
Sender requests that receiver support the option. |
DONT |
Sender requests that receiver not support the option. |
Although initiating an option is optional, responding to an option is required to complete the option negotiation process. Below is as list of valid responses to option negotiation command.
Command |
Response |
Telnet Method Used |
Option Enabled |
WILL Sender wants to use option. |
DONT Receiver refuses option. |
Telnet.SendDontOption |
No |
WILL Sender wants to use option. |
DO Receiver accepts option. |
Telnet.SendDoOption |
Yes |
WONT Sender will not support option. |
DONT Receiver will not enable option. |
Telnet.SendDontOption |
No |
DO Sender requests that receiver support option. |
WONT Receiver refuses option. |
Telnet.SendWontOption |
No |
DO Sender requests that receiver support option. |
WILL Receiver accepts option. |
Telnet.SendWillOption |
Yes |
DONT Sender requests that receiver not support option. |
WONT Receiver will not enable option. |
Telnet.SendWontOption |
No |
Note
For the WONT command the only valid response is DONT and for the DONT command the only valid response is WONT. This is because options as their name implies are optional, meaning you cannot force a terminal to enable an option that it does not want to support or is incapable of supporting.
Option Subnegotiation
Some options require that additional information be sent with the option to complete negotiation. This additional information is known as option subnegotiation data.
An example of an option requiring subnegotiation data is the TerminalType option. Upon both client and server agreeing to use the TerminalType option the server will ask the client for the terminal type that the client wishes to use. Examples of valid terminal types include vt100 and vt220. The client will then send this data back to the server.
For each option command sent by the telnet server a corresponding option related event is fired by the Telnet class.
• | The DoOptionEvent is fired when the telnet server sends a DO command followed by an option code. |
• | The DontOptionEvent is fired when the telnet server sends a DONT command followed by an option code. |
• | The WillOptionEvent is fired when the telnet server sends a WILL command followed by an option code. |
• | The WontOptionEvent is fired when the telnet server sends a WONT command followed by an option code. |
• | The SubOptionEvent is fired when the telnet server sends a DO SUB command followed by an option code. |
Note
To successfully complete option negotiation you must capture and respond to these events when fired.
The event model used in the .NET framework, and in Telnet Factory for .NET is based on having an event and a corresponding delegate responsible for handling events fired.
Capturing the DoOptionEvent event
To capture and respond to the DoOptionEvent do the following:
1. | Create a method that has the same signature as the DoOptionEventHandler delegate. |
2. | Add code within the newly created method to handle option and send response to telnet server. |
3. | Subscribe newly created method to capture the DoOptionEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// Set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnDoOption method To capture DoOptionEvent
telnet.DoOptionEvent += new Telnet.DoOptionEventHandler(this.OnDoOption);
}
public void OnDoOption(Object sender, TelnetDoOptionEventArgs e) {
// Get Option
TelnetOption Option = e.Option;
// handle Option ... In this Case refusing Option
telnet.SendWontOption(Option);
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnDoOption(ByVal sender As Object, ByVal e As TelnetDoOptionEventArgs) Handles myTelnet.DoOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' handle option ... in this case refusing option
myTelnet.SendWontOption(opt)
End Sub
End Class
Capturing the DontOptionEvent event
To capture and respond to the DontOptionEvent do the following:
1. | Create a method that has the same signature as the DontOptionEventHandler delegate. |
2. | Add code within the newly created method to handle option and send response to telnet server. |
3. | Subscribe newly created method to capture the DontOptionEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnDontOption method to capture DontOptionEvent
telnet.DontOptionEvent += new Telnet.DontOptionEventHandler(this.OnDontOption);
}
public void OnDontOption(object sender, TelnetDontOptionEventArgs e) {
// get option
TelnetOption option = e.Option;
// handle option ... in this case not enabling option
telnet.SendWontOption(option);
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnDontOption(ByVal sender As Object, ByVal e As TelnetDontOptionEventArgs) Handles myTelnet.DontOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' handle option ... in this case refusing option
myTelnet.SendWontOption(opt)
End Sub
End Class
Capturing the WillOptionEvent event
To capture and respond to the WillOptionEvent do the following:
1. | Create a method that has the same signature as the WillOptionEventHandler delegate. |
2. | Add code within the newly created method to handle option and send response to telnet server. |
3. | Subscribe newly created method to capture the WillOptionEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnWillOption method to capture WillOptionEvent
telnet.WillOptionEvent += new Telnet.WillOptionEventHandler(this.OnWillOption);
}
public void OnWillOption(object sender, TelnetWillOptionEventArgs e) {
// get option
TelnetOption option = e.Option;
// handle option ... in this case refusing option
telnet.SendDontOption(option);
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnWillOption(ByVal sender As Object, ByVal e As TelnetWillOptionEventArgs) Handles myTelnet.WillOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' handle option ... in this case refusing option
myTelnet.SendDontOption(opt)
End Sub
End Class
Capturing the WontOptionEvent event
To capture and respond to the WontOptionEvent do the following:
1. | Create a method that has the same signature as the WontOptionEventHandler delegate. |
2. | Add code within the newly created method to handle option and send response to telnet server. |
3. | Subscribe newly created method to capture the WontOptionEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnWontOption method to capture WontOptionEvent
telnet.WontOptionEvent += new Telnet.WontOptionEventHandler(this.OnWontOption);
}
public void OnWontOption(object sender, TelnetWontOptionEventArgs e) {
// get option
TelnetOption option = e.Option;
// handle option ... in this case not enabling option
telnet.SendDontOption(option);
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnWontOption(ByVal sender As Object, ByVal e As TelnetWontOptionEventArgs) Handles myTelnet.WontOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' handle option ... in this case refusing option
myTelnet.SendDontOption(opt)
End Sub
End Class
Capturing the SubOptionEvent event
To capture and respond to the SubOptionEvent do the following:
1. | Create a method that has the same signature as the SubOptionEventHandler delegate. |
2. | Add code within the newly created method to handle option and send response to telnet server. |
3. | Subscribe newly created method to capture the SubOptionEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
using Jscape.Telnet.Options;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnSubOption method to capture SubOptionEvent
telnet.SubOptionEvent += new Telnet.SubOptionEventHandler(this.OnSubOption);
// delegate OnDoOption method to capture DoOptionEvent
telnet.DoOptionEvent += new Telnet.DoOptionEventHandler(this.OnDoOption);
}
public void OnDoOption(object sender, TelnetDoOptionEventArgs e) {
TelnetOption option = e.Option;
// if option code is 24 TERMINAL TYPE then accept, otherwise refuse
if(option.OptionCode == 24) {
telnet.SendWillOption(option);
} else {
telnet.SendWontOption(e.Option);
}
}
public void OnSubOption(object sender, TelnetSubOptionEventArgs e) {
// get option
TelnetOption option = e.Option;
// check if option is TERMINAL TYPE option
if(option.OptionCode == 24) {
// create TerminalType instance for vt100 emulation
TerminalType termType = new TerminalType("vt100");
// instruct telnet server to use vt100 emulation
telnet.SendSubOption(termType);
}
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Imports Jscape.Telnet.Options
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnDoOption(ByVal sender As Object, ByVal e As TelnetDoOptionEventArgs) Handles myTelnet.DoOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' if option code is 24 TERMINAL TYPE then accept, otherwise refuse
If (opt.OptionCode = 24) Then
myTelnet.SendWillOption(opt)
Else
myTelnet.SendWontOption(opt)
End If
End Sub
Private Sub OnSubOption(ByVal sender As Object, ByVal e As TelnetSubOptionEventArgs) Handles myTelnet.SubOptionEvent
' get option
Dim opt As TelnetOption = e.Option
' check if option is TERMINAL TYPE option
If (opt.OptionCode = 24) Then
' instruct telnet server to use vt100 emulation
Dim termType As TerminalType = New TerminalType("vt100")
myTelnet.SendSubOption(termType)
End If
End Sub
End Class