1- package csconn
1+ /*
2+ * Copyright 2019 The CovenantSQL Authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package noconn
218
319import (
420 "bytes"
@@ -14,45 +30,49 @@ import (
1430)
1531
1632const (
17- // HeaderSize is the header size with ETLSHeader + NodeID + Nonce
33+ // HeaderSize is the header size with ETLSHeader + NodeID + Nonce.
1834 HeaderSize = etls .MagicSize + hash .HashBSize + cpuminer .Uint256Size
1935)
2036
21- type CSConn struct {
37+ // NOConn defines node-oriented connection based on ETLS crypto connection.
38+ type NOConn struct {
2239 * etls.CryptoConn
2340 isClient bool
2441
2542 // The following fields may be rewritten during handshake.
26- isAnonymous bool
27- remoteNodeID proto.RawNodeID
43+ isAnonymous bool
44+ remote proto.RawNodeID
2845}
2946
30- func NewServerConn (conn net.Conn ) * CSConn {
31- return & CSConn {
47+ // NewServerConn takes a raw connection and returns a new server side NOConn.
48+ func NewServerConn (conn net.Conn ) * NOConn {
49+ return & NOConn {
3250 CryptoConn : etls .NewConn (conn , nil ), // at server side, cipher will be set during handshake
3351 }
3452}
3553
36- //func NewClientConn(conn net.Conn) *CSConn {
37- // return &CSConn {
54+ //func NewClientConn(conn net.Conn) *NOConn {
55+ // return &NOConn {
3856// CryptoConn: etls.NewConn(conn, nil),
3957// isClient: true,
4058// isAnonymous: true,
4159// }
4260//}
4361
44- func (c * CSConn ) RemoteNodeID () proto.RawNodeID {
45- return c .remoteNodeID
62+ // Remote returns the remote node ID of the NOConn.
63+ func (c * NOConn ) Remote () proto.RawNodeID {
64+ return c .remote
4665}
4766
48- func (c * CSConn ) Handshake () (err error ) {
67+ // Handshake does the initial handshaking according to the connection role.
68+ func (c * NOConn ) Handshake () (err error ) {
4969 if c .isClient {
5070 return c .clientHandshake ()
5171 }
5272 return c .serverHandshake ()
5373}
5474
55- func (c * CSConn ) serverHandshake () (err error ) {
75+ func (c * NOConn ) serverHandshake () (err error ) {
5676 headerBuf := make ([]byte , HeaderSize )
5777 rCount , err := c .CryptoConn .Conn .Read (headerBuf )
5878 if err != nil {
@@ -74,7 +94,7 @@ func (c *CSConn) serverHandshake() (err error) {
7494 idHash , _ := hash .NewHash (headerBuf [etls .MagicSize : etls .MagicSize + hash .HashBSize ])
7595 rawNodeID := & proto.RawNodeID {Hash : * idHash }
7696 // TODO(auxten): compute the nonce and check difficulty
77- cpuminer .Uint256FromBytes (headerBuf [etls .MagicSize + hash .HashBSize :])
97+ _ , _ = cpuminer .Uint256FromBytes (headerBuf [etls .MagicSize + hash .HashBSize :])
7898
7999 isAnonymous := rawNodeID .IsEqual (& kms .AnonymousRawNodeID .Hash )
80100 symmetricKey , err := GetSharedSecretWith (defaultResolver , rawNodeID , isAnonymous )
@@ -83,14 +103,14 @@ func (c *CSConn) serverHandshake() (err error) {
83103 return
84104 }
85105 cipher := etls .NewCipher (symmetricKey )
86- c .CryptoConn .Cipher = cipher
87- c .remoteNodeID = * rawNodeID
106+ c .CryptoConn .Cipher = cipher // reset cipher
107+ c .remote = * rawNodeID
88108 c .isAnonymous = isAnonymous
89109
90110 return
91111}
92112
93- func (c * CSConn ) clientHandshake () (err error ) {
113+ func (c * NOConn ) clientHandshake () (err error ) {
94114 writeBuf := make ([]byte , HeaderSize )
95115 copy (writeBuf , etls .MagicBytes [:])
96116 if c .isAnonymous {
@@ -127,12 +147,12 @@ func (c *CSConn) clientHandshake() (err error) {
127147}
128148
129149// Accept takes the ownership of conn and accepts it as a CSConn.
130- func Accept (conn net.Conn ) (* CSConn , error ) {
131- csconn := NewServerConn (conn )
132- if err := csconn .Handshake (); err != nil {
150+ func Accept (conn net.Conn ) (* NOConn , error ) {
151+ noconn := NewServerConn (conn )
152+ if err := noconn .Handshake (); err != nil {
133153 return nil , err
134154 }
135- return csconn , nil
155+ return noconn , nil
136156}
137157
138158// Dial connects to the node with remote node id.
@@ -176,17 +196,17 @@ func DialEx(remote proto.NodeID, isAnonymous bool) (conn net.Conn, err error) {
176196 return
177197 }
178198
179- csconn := & CSConn {
180- CryptoConn : etls .NewConn (iconn , cipher ),
181- isAnonymous : isAnonymous ,
182- isClient : true ,
183- remoteNodeID : * rawNodeID ,
199+ noconn := & NOConn {
200+ CryptoConn : etls .NewConn (iconn , cipher ),
201+ isAnonymous : isAnonymous ,
202+ isClient : true ,
203+ remote : * rawNodeID ,
184204 }
185205
186- if err = csconn .Handshake (); err != nil {
206+ if err = noconn .Handshake (); err != nil {
187207 err = errors .Wrapf (err , "connect %s %s failed" , rawNodeID .String (), nodeAddr )
188208 return
189209 }
190210
191- return csconn , nil
211+ return noconn , nil
192212}
0 commit comments