-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen_proxy.go
51 lines (41 loc) · 1.34 KB
/
listen_proxy.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package coap
import (
"errors"
"time"
)
type ProxyFunction func(rawReq []byte, to string) error
func (s *Server) ProxySend(prefix string, rawReq []byte, from string) ([]byte, error) {
var req Message
if err := req.unmarshalBinary(rawReq); err != nil {
logError(nil, err, "coap: error parsing COAP header")
return nil, err
}
req.Meta.RemoteAddr = prefix + ":" + from
req.Meta.ListenerName = prefix
req.Meta.ReceivedAt = time.Now().UTC()
req.Meta.Server = s
sniffActivity("udp", SniffRead, req.Meta.RemoteAddr, s.udpListener.socket.LocalAddr().String(), rawReq)
rsp := s.handleMessage(&req)
if rsp != nil {
rawRsp, err := rsp.marshalBinary()
if err != nil {
logError(nil, err, "coap: error marshaling COAP response")
return nil, err
}
if rawRsp != nil {
return rawRsp, nil
}
}
return nil, nil
}
func proxyRecv(s *Server, prefix string, addr string, data []byte) error {
sniffActivity("udp", SniffWrite, s.udpListener.socket.LocalAddr().String(), addr, data)
cb, found := s.config.ProxyCallbacks[prefix]
if !found {
return errors.New("callback not found for prefix: " + prefix)
}
return cb(data, addr[len(prefix)+1:])
}