forked from nanomsg/mangos-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
265 lines (228 loc) · 6.71 KB
/
conn.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
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
// Copyright 2016 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mangos
import (
"encoding/binary"
"io"
"net"
)
// conn implements the Pipe interface on top of net.Conn. The
// assumption is that transports using this have similar wire protocols,
// and conn is meant to be used as a building block.
type conn struct {
c net.Conn
proto Protocol
sock Socket
open bool
props map[string]interface{}
maxrx int64
}
// connipc is *almost* like a regular conn, but the IPC protocol insists
// on stuffing a leading byte (valued 1) in front of messages. This is for
// compatibility with nanomsg -- the value cannot ever be anything but 1.
type connipc struct {
conn
}
// Recv implements the Pipe Recv method. The message received is expected as
// a 64-bit size (network byte order) followed by the message itself.
func (p *conn) Recv() (*Message, error) {
var sz int64
var err error
var msg *Message
if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
// Limit messages to the maximum receive value, if not
// unlimited. This avoids a potential denaial of service.
if sz < 0 || (p.maxrx > 0 && sz > p.maxrx) {
return nil, ErrTooLong
}
msg = NewMessage(int(sz))
msg.Body = msg.Body[0:sz]
if _, err = io.ReadFull(p.c, msg.Body); err != nil {
msg.Free()
return nil, err
}
return msg, nil
}
// Send implements the Pipe Send method. The message is sent as a 64-bit
// size (network byte order) followed by the message itself.
func (p *conn) Send(msg *Message) error {
l := uint64(len(msg.Header) + len(msg.Body))
if msg.Expired() {
msg.Free()
return nil
}
// send length header
if err := binary.Write(p.c, binary.BigEndian, l); err != nil {
return err
}
if _, err := p.c.Write(msg.Header); err != nil {
return err
}
// hope this works
if _, err := p.c.Write(msg.Body); err != nil {
return err
}
msg.Free()
return nil
}
// LocalProtocol returns our local protocol number.
func (p *conn) LocalProtocol() uint16 {
return p.proto.Number()
}
// RemoteProtocol returns our peer's protocol number.
func (p *conn) RemoteProtocol() uint16 {
return p.proto.PeerNumber()
}
// Close implements the Pipe Close method.
func (p *conn) Close() error {
p.open = false
return p.c.Close()
}
// IsOpen implements the PipeIsOpen method.
func (p *conn) IsOpen() bool {
return p.open
}
func (p *conn) GetProp(n string) (interface{}, error) {
if v, ok := p.props[n]; ok {
return v, nil
}
return nil, ErrBadProperty
}
// NewConnPipe allocates a new Pipe using the supplied net.Conn, and
// initializes it. It performs the handshake required at the SP layer,
// only returning the Pipe once the SP layer negotiation is complete.
//
// Stream oriented transports can utilize this to implement a Transport.
// The implementation will also need to implement PipeDialer, PipeAccepter,
// and the Transport enclosing structure. Using this layered interface,
// the implementation needn't bother concerning itself with passing actual
// SP messages once the lower layer connection is established.
func NewConnPipe(c net.Conn, sock Socket, props ...interface{}) (Pipe, error) {
p := &conn{c: c, proto: sock.GetProtocol(), sock: sock}
if err := p.handshake(props); err != nil {
return nil, err
}
return p, nil
}
// NewConnPipeIPC allocates a new Pipe using the IPC exchange protocol.
func NewConnPipeIPC(c net.Conn, sock Socket, props ...interface{}) (Pipe, error) {
p := &connipc{conn: conn{c: c, proto: sock.GetProtocol(), sock: sock}}
if err := p.handshake(props); err != nil {
return nil, err
}
return p, nil
}
func (p *connipc) Send(msg *Message) error {
l := uint64(len(msg.Header) + len(msg.Body))
one := [1]byte{1}
var err error
// send length header
if _, err = p.c.Write(one[:]); err != nil {
return err
}
if err = binary.Write(p.c, binary.BigEndian, l); err != nil {
return err
}
if _, err = p.c.Write(msg.Header); err != nil {
return err
}
// hope this works
if _, err = p.c.Write(msg.Body); err != nil {
return err
}
msg.Free()
return nil
}
func (p *connipc) Recv() (*Message, error) {
var sz int64
var err error
var msg *Message
var one [1]byte
if _, err = p.c.Read(one[:]); err != nil {
return nil, err
}
if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
// Limit messages to the maximum receive value, if not
// unlimited. This avoids a potential denaial of service.
if sz < 0 || (p.maxrx > 0 && sz > p.maxrx) {
return nil, ErrTooLong
}
msg = NewMessage(int(sz))
msg.Body = msg.Body[0:sz]
if _, err = io.ReadFull(p.c, msg.Body); err != nil {
msg.Free()
return nil, err
}
return msg, nil
}
// connHeader is exchanged during the initial handshake.
type connHeader struct {
Zero byte // must be zero
S byte // 'S'
P byte // 'P'
Version byte // only zero at present
Proto uint16
Rsvd uint16 // always zero at present
}
// handshake establishes an SP connection between peers. Both sides must
// send the header, then both sides must wait for the peer's header.
// As a side effect, the peer's protocol number is stored in the conn.
// Also, various properties are initialized.
func (p *conn) handshake(props []interface{}) error {
var err error
p.props = make(map[string]interface{})
p.props[PropLocalAddr] = p.c.LocalAddr()
p.props[PropRemoteAddr] = p.c.RemoteAddr()
for len(props) >= 2 {
switch name := props[0].(type) {
case string:
p.props[name] = props[1]
default:
return ErrBadProperty
}
props = props[2:]
}
if v, e := p.sock.GetOption(OptionMaxRecvSize); e == nil {
// socket guarantees this is an integer
p.maxrx = int64(v.(int))
}
h := connHeader{S: 'S', P: 'P', Proto: p.proto.Number()}
if err = binary.Write(p.c, binary.BigEndian, &h); err != nil {
return err
}
if err = binary.Read(p.c, binary.BigEndian, &h); err != nil {
p.c.Close()
return err
}
if h.Zero != 0 || h.S != 'S' || h.P != 'P' || h.Rsvd != 0 {
p.c.Close()
return ErrBadHeader
}
// The only version number we support at present is "0", at offset 3.
if h.Version != 0 {
p.c.Close()
return ErrBadVersion
}
// The protocol number lives as 16-bits (big-endian) at offset 4.
if h.Proto != p.proto.PeerNumber() {
p.c.Close()
return ErrBadProto
}
p.open = true
return nil
}