-
Notifications
You must be signed in to change notification settings - Fork 0
/
responder.go
47 lines (43 loc) · 1.38 KB
/
responder.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
package conpass
import (
"fmt"
"io"
"log"
"github.com/mblarer/conpass/segment"
)
// Responder represents a CONPASS agent in the responder role.
type Responder struct {
// Filter is the segment filter according to which the Responder gives
// consent to certain segments or combinations of segments.
Filter segment.Filter
// Verbose is a flag which makes the Responder more verbose if true.
Verbose bool
}
// NegotiateOver makes the Responder negotiate consent over a given bytestream.
// If the negotiation is successful, the method returns the set of segments
// that have bilateral consent. Otherwise, an error is returned.
func (agent Responder) NegotiateOver(stream io.ReadWriter) (segment.SegmentSet, error) {
segsin, accsegs, srcIA, dstIA, err := segment.ReadSegments(stream, []segment.Segment{})
if err != nil {
return segment.SegmentSet{}, err
}
if agent.Verbose {
log.Println("request contains", len(segsin), "segments:")
for _, segment := range segsin {
fmt.Println(" ", segment)
}
}
segsetout := agent.Filter.Filter(segment.SegmentSet{
Segments: accsegs,
SrcIA: srcIA,
DstIA: dstIA,
})
if agent.Verbose {
log.Println("responding with", len(segsetout.Segments), "segments:")
for _, segment := range segsetout.Segments {
fmt.Println(" ", segment)
}
}
segment.WriteSegments(stream, segsetout.Segments, segsin, srcIA, dstIA)
return segsetout, nil
}