/
negotiate.go
225 lines (199 loc) · 4.99 KB
/
negotiate.go
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
// Copyright (c) 2018 Eric Barkie. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package telnet
import (
"encoding/hex"
"errors"
"fmt"
)
// Errors.
var (
ErrNegAskDenied = errors.New("ask violates let")
)
// negState is a RFC1143 option negotiation state.
type negState byte
const (
nsNo negState = iota // Disabled
nsYes // Enabled
nsWantNo // Negotiating for disable
nsWantNoOpp // Want to enable but previous disable negotiation not complete
nsWantYes // Negotiating for enable
nsWantYesOpp // Want to disable but previous enable negotiation not complete
)
func (t *Ctx) indicate(cmd Command, code byte) {
s := t.os.load(code)
Trace.Printf("Indicating %s option %s", cmd, s.opt)
t.rw.Write([]byte{byte(iac), byte(cmd), code})
}
func (t *Ctx) ask(cmd Command, opt Option) (err error) {
Trace.Printf("Asking %s option %s", cmd, opt)
s := t.os.load(opt.Byte())
switch cmd {
case will:
// We are asking if we can enable an option.
switch s.us {
case nsNo:
if s.opt.LetUs() {
t.indicate(will, opt.Byte())
s.us = nsWantYes
} else {
err = ErrNegAskDenied
}
case nsWantNo:
s.us = nsWantNoOpp
case nsWantYesOpp:
s.us = nsWantYes
}
case wont:
// We are indicating that we are disabling an option.
switch s.us {
case nsYes:
t.indicate(wont, opt.Byte())
s.us = nsWantNo
case nsWantNoOpp:
s.us = nsWantNo
case nsWantYes:
s.us = nsWantYesOpp
}
case do:
// We are asking that he enable an option.
switch s.him {
case nsNo:
if s.opt.LetHim() {
t.indicate(do, opt.Byte())
s.him = nsWantYes
} else {
err = ErrNegAskDenied
}
case nsWantNo:
s.him = nsWantNoOpp
case nsWantYesOpp:
s.him = nsWantYes
}
case dont:
// We are asking that he disable an option.
switch s.him {
case nsYes:
t.indicate(dont, opt.Byte())
s.him = nsWantNo
case nsWantNoOpp:
s.him = nsWantNo
case nsWantYes:
s.him = nsWantYesOpp
}
}
t.os.store(s)
return
}
func (t *Ctx) negotiate(cmd Command, code byte) (err error) {
s := t.os.load(code)
Trace.Printf("Received %s option %s", cmd, s.opt)
switch cmd {
case will:
// He is asking if he can enable an option or accepting our
// request that he enable an option.
switch s.him {
case nsNo:
if s.opt.LetHim() {
t.indicate(do, code)
s.him = nsYes
Debug.Printf("Option %s enabled for him", s.opt)
s.opt.SetHim(t, true)
} else {
t.indicate(dont, code)
}
case nsYes:
// Ignore
case nsWantNo:
err = fmt.Errorf("%s option %s answered answered by %s", dont, s.opt, will)
s.him = nsNo
Debug.Printf("Option %s disabled for him", s.opt)
s.opt.SetHim(t, false)
case nsWantNoOpp:
err = fmt.Errorf("%s option %s answered answered by %s", dont, s.opt, will)
fallthrough
case nsWantYes:
s.him = nsYes
Debug.Printf("Option %s enabled for him", s.opt)
s.opt.SetHim(t, true)
case nsWantYesOpp:
t.indicate(dont, code)
s.him = nsWantNo
}
case wont:
// He is indicating that he is disabling an option, accepting our
// request that he disable an option, or refusing our request for him
// to enable an option.
switch s.him {
case nsNo:
// Ignore
case nsYes:
t.indicate(dont, code)
fallthrough
case nsWantNo, nsWantYes, nsWantYesOpp:
s.him = nsNo
Debug.Printf("Option %s disabled for him", s.opt)
s.opt.SetHim(t, false)
case nsWantNoOpp:
t.indicate(do, code)
s.him = nsWantYes
}
case do:
// He is accepting our request for us to enable an option or asking us
// to enable an option.
switch s.us {
case nsNo:
if s.opt.LetUs() {
t.indicate(will, code)
s.us = nsYes
Debug.Printf("Option %s enabled for us", s.opt)
s.opt.SetUs(t, true)
} else {
t.indicate(wont, code)
}
case nsYes:
// Ignore
case nsWantNo:
err = fmt.Errorf("%s option %s answered answered by %s", wont, s.opt, do)
s.us = nsNo
Debug.Printf("Option %s disabled for us", s.opt)
s.opt.SetUs(t, false)
case nsWantNoOpp:
err = fmt.Errorf("%s option %s answered answered by %s", wont, s.opt, do)
fallthrough
case nsWantYes:
s.us = nsYes
Debug.Printf("Option %s enabled for us", s.opt)
s.opt.SetUs(t, true)
case nsWantYesOpp:
t.indicate(wont, code)
s.us = nsWantNo
}
case dont:
// He is refusing our request for us to enable an option or asking us
// to disable an option.
switch s.us {
case nsNo:
// Ignore
case nsYes:
t.indicate(wont, code)
fallthrough
case nsWantNo, nsWantYes, nsWantYesOpp:
s.us = nsNo
Debug.Printf("Option %s disabled for us", s.opt)
s.opt.SetUs(t, false)
case nsWantNoOpp:
t.indicate(will, code)
s.us = nsWantYes
}
}
t.os.store(s)
return
}
func (t *Ctx) subnegotiate(code byte, params []byte) {
s := t.os.load(code)
Trace.Printf("Subnegotation option %s", s.opt)
Trace.Printf("Parameters\n%s", hex.Dump(params))
s.opt.Params(t, params)
}