summaryrefslogtreecommitdiff
path: root/satp
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2017-11-01 19:26:01 +0100
committerChristian Pointner <equinox@anytun.org>2017-11-01 19:26:01 +0100
commita00f35c25b992ab8b999710acb36a7edb6369942 (patch)
tree8de04cdfa4aac7ee316bff7300901cdc3e097630 /satp
parentuse sync.Map in SA (diff)
added endpoint handling to security assoc (needs testing!!!)
Diffstat (limited to 'satp')
-rw-r--r--satp/derived.gen.go17
-rw-r--r--satp/security-association.go35
2 files changed, 46 insertions, 6 deletions
diff --git a/satp/derived.gen.go b/satp/derived.gen.go
new file mode 100644
index 0000000..1ccfa0f
--- /dev/null
+++ b/satp/derived.gen.go
@@ -0,0 +1,17 @@
+// Code generated by goderive DO NOT EDIT.
+
+package satp
+
+import (
+ "bytes"
+ "net"
+)
+
+// deriveEqual returns whether this and that are equal.
+func deriveEqual(this, that *net.UDPAddr) bool {
+ return (this == nil && that == nil) ||
+ this != nil && that != nil &&
+ bytes.Equal(this.IP, that.IP) &&
+ this.Port == that.Port &&
+ this.Zone == that.Zone
+}
diff --git a/satp/security-association.go b/satp/security-association.go
index 48d6c5c..b004f47 100644
--- a/satp/security-association.go
+++ b/satp/security-association.go
@@ -1,3 +1,5 @@
+//go:generate goderive .
+//
//
// Copyright (c) 2017 anygone contributors (see AUTHORS file)
// All rights reserved.
@@ -33,8 +35,14 @@ package satp
import (
"net"
"sync"
+ "sync/atomic"
+ "unsafe"
)
+func EndpointsEqual(this, that *net.UDPAddr) bool {
+ return deriveEqual(this, that)
+}
+
type SecurityAssociation struct {
kd KeyDerivation
endpoints []*net.UDPAddr
@@ -48,16 +56,31 @@ func (sa *SecurityAssociation) KeyGenerate(dir Direction, usage KeyUsage, sequen
}
func (sa *SecurityAssociation) EndpointUpdate(idx uint, ep *net.UDPAddr) {
- // TODO: implement this
+ if idx >= uint(len(sa.endpoints)) {
+ return
+ }
+ atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&(sa.endpoints[idx]))), unsafe.Pointer(ep))
}
-func (sa *SecurityAssociation) EndpointCompareAndUpdate(idx uint, ep *net.UDPAddr) {
- // TODO: implement this
+func (sa *SecurityAssociation) EndpointCompareAndUpdate(idx uint, ep *net.UDPAddr) bool {
+ if idx >= uint(len(sa.endpoints)) {
+ return false
+ }
+ old := (*net.UDPAddr)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&(sa.endpoints[idx])))))
+ if !EndpointsEqual(old, ep) {
+ atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&(sa.endpoints[idx]))), unsafe.Pointer(ep))
+ return true
+ }
+ return false
}
-func (sa *SecurityAssociation) EndpointsAndSequenceNumber() (seqNum uint32, eps []*net.UDPAddr) {
- // TODO: implement this
- return 0, nil
+func (sa *SecurityAssociation) GetEndpointsAndNextSequenceNumber() (seqNum uint32, eps []*net.UDPAddr) {
+ seqNum = atomic.AddUint32(&sa.nextSeqNr, 1) - 1
+ eps = make([]*net.UDPAddr, len(sa.endpoints))
+ for i := range sa.endpoints {
+ eps[i] = (*net.UDPAddr)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&(sa.endpoints[i])))))
+ }
+ return
}
func (sa *SecurityAssociation) SquenceNumberCheck(senderID uint16, sequenceNumber uint32) bool {