forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessiondtls.go
163 lines (140 loc) · 4.26 KB
/
sessiondtls.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
package coap
import (
"bytes"
"context"
"crypto/x509"
"fmt"
"net"
"github.com/coapcloud/go-coap/codes"
coapNet "github.com/coapcloud/go-coap/net"
dtls "github.com/pion/dtls/v2"
)
type sessionDTLS struct {
*sessionBase
connection *coapNet.Conn
peerCertificates []*x509.Certificate
}
// newSessionDTLS create new session for DTLS connection
func newSessionDTLS(connection *coapNet.Conn, srv *Server) (networkSession, error) {
BlockWiseTransfer := true
BlockWiseTransferSzx := BlockWiseSzx1024
if srv.BlockWiseTransfer != nil {
BlockWiseTransfer = *srv.BlockWiseTransfer
}
if srv.BlockWiseTransferSzx != nil {
BlockWiseTransferSzx = *srv.BlockWiseTransferSzx
}
if BlockWiseTransfer && BlockWiseTransferSzx == BlockWiseSzxBERT {
return nil, ErrInvalidBlockWiseSzx
}
s := sessionDTLS{
sessionBase: newBaseSession(BlockWiseTransfer, BlockWiseTransferSzx, srv),
connection: connection,
}
dtlsConn := connection.Connection().(*dtls.Conn)
cert := dtlsConn.ConnectionState().PeerCertificates
if len(cert) > 0 {
flatCerts := bytes.Join(cert, nil)
peerCertificates, err := x509.ParseCertificates(flatCerts)
if err != nil {
return nil, err
}
s.peerCertificates = peerCertificates
}
return &s, nil
}
// LocalAddr implements the networkSession.LocalAddr method.
func (s *sessionDTLS) LocalAddr() net.Addr {
return s.connection.LocalAddr()
}
// RemoteAddr implements the networkSession.RemoteAddr method.
func (s *sessionDTLS) RemoteAddr() net.Addr {
return s.connection.RemoteAddr()
}
// PeerCertificates implements the networkSession.PeerCertificates method.
func (s *sessionDTLS) PeerCertificates() []*x509.Certificate {
return s.peerCertificates
}
// BlockWiseTransferEnabled
func (s *sessionDTLS) blockWiseEnabled() bool {
return s.blockWiseTransfer
}
func (s *sessionDTLS) blockWiseIsValid(szx BlockWiseSzx) bool {
return szx <= BlockWiseSzx1024
}
// Ping send ping over udp(unicast) and wait for response.
func (s *sessionDTLS) PingWithContext(ctx context.Context) error {
//provoking to get a reset message - "CoAP ping" in RFC-7252
//https://tools.ietf.org/html/rfc7252#section-4.2
//https://tools.ietf.org/html/rfc7252#section-4.3
//https://tools.ietf.org/html/rfc7252#section-1.2 "Reset Message"
// BUG of iotivity: https://jira.iotivity.org/browse/IOT-3149
req := s.NewMessage(MessageParams{
Type: Confirmable,
Code: codes.Empty,
MessageID: GenerateMessageID(),
})
resp, err := s.ExchangeWithContext(ctx, req)
if err != nil {
return err
}
if resp.Type() == Reset || resp.Type() == Acknowledgement {
return nil
}
return ErrInvalidResponse
}
func (s *sessionDTLS) closeWithError(err error) error {
if s.sessionBase.Close() == nil {
c := ClientConn{commander: &ClientCommander{s}}
s.srv.NotifySessionEndFunc(&c, err)
e := s.connection.Close()
if e == nil {
e = err
}
return e
}
return nil
}
// Close implements the networkSession.Close method
func (s *sessionDTLS) Close() error {
return s.closeWithError(nil)
}
// NewMessage Create message for response
func (s *sessionDTLS) NewMessage(p MessageParams) Message {
return NewDgramMessage(p)
}
// Close implements the networkSession.Close method
func (s *sessionDTLS) IsTCP() bool {
return false
}
func (s *sessionDTLS) ExchangeWithContext(ctx context.Context, req Message) (Message, error) {
return s.exchangeWithContext(ctx, req, s.WriteMsgWithContext)
}
// Write implements the networkSession.Write method.
func (s *sessionDTLS) WriteMsgWithContext(ctx context.Context, req Message) error {
buffer := bytes.NewBuffer(make([]byte, 0, 1500))
err := req.MarshalBinary(buffer)
if err != nil {
return fmt.Errorf("cannot write msg to tcp connection %v", err)
}
return s.connection.WriteWithContext(ctx, buffer.Bytes())
}
func (s *sessionDTLS) sendPong(w ResponseWriter, r *Request) error {
resp := r.Client.NewMessage(MessageParams{
Type: Reset,
Code: codes.Empty,
MessageID: r.Msg.MessageID(),
})
return w.WriteMsgWithContext(r.Ctx, resp)
}
func (s *sessionDTLS) handleSignals(w ResponseWriter, r *Request) bool {
switch r.Msg.Code() {
// handle of udp ping
case codes.Empty:
if r.Msg.Type() == Confirmable && r.Msg.AllOptions().Len() == 0 && (r.Msg.Payload() == nil || len(r.Msg.Payload()) == 0) {
s.sendPong(w, r)
return true
}
}
return false
}