-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexisting_conn_dialer.go
More file actions
112 lines (95 loc) · 2.74 KB
/
existing_conn_dialer.go
File metadata and controls
112 lines (95 loc) · 2.74 KB
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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://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 channel
import (
"errors"
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/identity"
"net"
"time"
)
type existingConnDialer struct {
id *identity.TokenId
peer net.Conn
headers map[int32][]byte
}
func NewExistingConnDialer(id *identity.TokenId, peer net.Conn, headers map[int32][]byte) UnderlayFactory {
return &existingConnDialer{
id: id,
peer: peer,
headers: headers,
}
}
func (self *existingConnDialer) Create(timeout time.Duration) (Underlay, error) {
log := pfxlog.Logger()
log.Debug("started")
defer log.Debug("exited")
version := uint32(2)
tryCount := 0
defer func() {
if err := self.peer.SetDeadline(time.Time{}); err != nil { // clear write deadline
log.WithError(err).Error("unable to clear write deadline")
}
}()
for {
impl := newExistingImpl(self.peer, version)
if timeout > 0 {
if err := self.peer.SetDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
}
if err := self.sendHello(impl); err != nil {
if tryCount > 0 {
return nil, err
} else {
log.WithError(err).Warnf("error initiating channel with hello")
}
tryCount++
if retryVersion, _ := GetRetryVersion(err); retryVersion != version {
version = retryVersion
} else {
return nil, err
}
log.Warnf("Retrying dial with protocol version %v", version)
continue
}
impl.id = self.id
return impl, nil
}
}
func (self *existingConnDialer) sendHello(impl *existingConnImpl) error {
log := pfxlog.ContextLogger(impl.Label())
defer log.Debug("exited")
log.Debug("started")
request := NewHello(self.id.Token, self.headers)
request.sequence = HelloSequence
if err := impl.Tx(request); err != nil {
_ = impl.peer.Close()
return err
}
response, err := impl.Rx()
if err != nil {
return err
}
if !response.IsReplyingTo(request.sequence) || response.ContentType != ContentTypeResultType {
return fmt.Errorf("channel synchronization error, expected %v, got %v", request.sequence, response.ReplyFor())
}
result := UnmarshalResult(response)
if !result.Success {
return errors.New(result.Message)
}
impl.connectionId = string(response.Headers[ConnectionIdHeader])
impl.headers = response.Headers
return nil
}