forked from Juniper/go-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
198 lines (168 loc) · 4.4 KB
/
transport.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
// Go NETCONF Client
//
// Copyright (c) 2013-2018, Juniper Networks, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package netconf
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"regexp"
)
const (
// msgseparator is used to separate sent messages via NETCONF
msgseparator = "]]>]]>"
msgseparator_v11 = "\n##\n"
)
// DefaultCapabilities sets the default capabilities of the client library
var DefaultCapabilities = []string{
"urn:ietf:params:netconf:base:1.0",
"urn:ietf:params:netconf:base:1.1",
}
// HelloMessage is used when bringing up a NETCONF session
type HelloMessage struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 hello"`
Capabilities []string `xml:"capabilities>capability"`
SessionID int `xml:"session-id,omitempty"`
}
// Transport interface defines what characteristics make up a NETCONF transport
// layer object.
type Transport interface {
Send([]byte) error
Receive() ([]byte, error)
Close() error
ReceiveHello() (*HelloMessage, error)
SendHello(*HelloMessage) error
SetVersion(version string)
}
type TransportBasicIO struct {
io.ReadWriteCloser
//new add
version string
}
func (t *TransportBasicIO) SetVersion(version string) {
t.version = version
}
// Sends a well formated NETCONF rpc message as a slice of bytes adding on the
// nessisary framining messages.
func (t *TransportBasicIO) Send(data []byte) error {
var separator []byte
var dataInfo []byte
//headlen := 0
if t.version == "v1.1" {
separator = append(separator, []byte(msgseparator_v11)...)
} else {
separator = append(separator, []byte(msgseparator)...)
}
if t.version == "v1.1" {
header := fmt.Sprintf("\n#%d\n", len(string(data)))
dataInfo = append(dataInfo, header...)
//t.Write([]byte(header))
//headlen = len([]byte(header))
}
dataInfo = append(dataInfo, data...)
dataInfo = append(dataInfo, separator...)
_, err := t.Write(dataInfo)
return err
}
func (t *TransportBasicIO) Receive() ([]byte, error) {
var separator []byte
if t.version == "v1.1" {
separator = append(separator, []byte(msgseparator_v11)...)
} else {
separator = append(separator, []byte(msgseparator)...)
}
return t.WaitForBytes([]byte(separator))
}
func (t *TransportBasicIO) SendHello(hello *HelloMessage) error {
val, err := xml.Marshal(hello)
if err != nil {
return err
}
header := []byte(xml.Header)
val = append(header, val...)
err = t.Send(val)
return err
}
func (t *TransportBasicIO) ReceiveHello() (*HelloMessage, error) {
hello := new(HelloMessage)
val, err := t.Receive()
if err != nil {
return hello, err
}
err = xml.Unmarshal(val, hello)
return hello, err
}
func (t *TransportBasicIO) Writeln(b []byte) (int, error) {
t.Write(b)
t.Write([]byte("\n"))
return 0, nil
}
func (t *TransportBasicIO) WaitForFunc(f func([]byte) (int, error)) ([]byte, error) {
var out bytes.Buffer
buf := make([]byte, 8192)
pos := 0
for {
n, err := t.Read(buf[pos : pos+(len(buf)/2)])
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
if n > 0 {
end, err := f(buf[0 : pos+n])
if err != nil {
return nil, err
}
if end > -1 {
out.Write(buf[0:end])
return out.Bytes(), nil
}
if pos > 0 {
out.Write(buf[0:pos])
copy(buf, buf[pos:pos+n])
}
pos = n
}
}
return nil, fmt.Errorf("WaitForFunc failed")
}
func (t *TransportBasicIO) WaitForBytes(b []byte) ([]byte, error) {
return t.WaitForFunc(func(buf []byte) (int, error) {
return bytes.Index(buf, b), nil
})
}
func (t *TransportBasicIO) WaitForString(s string) (string, error) {
out, err := t.WaitForBytes([]byte(s))
if out != nil {
return string(out), err
}
return "", err
}
func (t *TransportBasicIO) WaitForRegexp(re *regexp.Regexp) ([]byte, [][]byte, error) {
var matches [][]byte
out, err := t.WaitForFunc(func(buf []byte) (int, error) {
loc := re.FindSubmatchIndex(buf)
if loc != nil {
for i := 2; i < len(loc); i += 2 {
matches = append(matches, buf[loc[i]:loc[i+1]])
}
return loc[1], nil
}
return -1, nil
})
return out, matches, err
}
// ReadWriteCloser represents a combined IO Reader and WriteCloser
type ReadWriteCloser struct {
io.Reader
io.WriteCloser
}
// NewReadWriteCloser creates a new combined IO Reader and Write Closer from the
// provided objects
func NewReadWriteCloser(r io.Reader, w io.WriteCloser) *ReadWriteCloser {
return &ReadWriteCloser{r, w}
}