aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-09 18:56:00 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-09 18:56:00 +0100
commitb461343171726d99df20bfc4b4741f0fad0c95e0 (patch)
tree1ce014fb41a75fe66801d19879c665b652fdf756
parentGo treats underscores specially (diff)
downloadwireguard-go-b461343171726d99df20bfc4b4741f0fad0c95e0.tar.xz
wireguard-go-b461343171726d99df20bfc4b4741f0fad0c95e0.zip
Started migration to sub-packages
-rw-r--r--cookie.go5
-rw-r--r--internal/events/event.go36
-rw-r--r--internal/xchacha20poly1305/xchacha20.go (renamed from xchacha20.go)12
-rw-r--r--internal/xchacha20poly1305/xchacha20_test.go (renamed from xchacha20_test.go)6
-rw-r--r--tun.go14
-rw-r--r--tun_linux.go25
6 files changed, 69 insertions, 29 deletions
diff --git a/cookie.go b/cookie.go
index a13ad49..7cea75c 100644
--- a/cookie.go
+++ b/cookie.go
@@ -3,6 +3,7 @@ package main
import (
"crypto/hmac"
"crypto/rand"
+ "git.zx2c4.com/wireguard-go/internal/xchacha20poly1305"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/chacha20poly1305"
"sync"
@@ -154,7 +155,7 @@ func (st *CookieChecker) CreateReply(
return nil, err
}
- XChaCha20Poly1305Encrypt(
+ xchacha20poly1305.Encrypt(
reply.Cookie[:0],
&reply.Nonce,
cookie[:],
@@ -198,7 +199,7 @@ func (st *CookieGenerator) ConsumeReply(msg *MessageCookieReply) bool {
var cookie [blake2s.Size128]byte
- _, err := XChaCha20Poly1305Decrypt(
+ _, err := xchacha20poly1305.Decrypt(
cookie[:0],
&msg.Nonce,
msg.Cookie[:],
diff --git a/internal/events/event.go b/internal/events/event.go
new file mode 100644
index 0000000..4412bbb
--- /dev/null
+++ b/internal/events/event.go
@@ -0,0 +1,36 @@
+package events
+
+import (
+ "sync"
+)
+
+type Event interface {
+ Contains(int) bool
+ Processed()
+ WaitForProcessed()
+}
+
+type EventStruct struct {
+ code int
+ lock sync.Mutex
+}
+
+func (event EventStruct) Contains(code int) bool {
+ return event.code&code != 0
+}
+
+func (event *EventStruct) WaitForProcessed() {
+ event.lock.Lock()
+}
+
+func (event *EventStruct) Processed() {
+ event.lock.Unlock()
+}
+
+func NewEvent(code int) Event {
+ event := &EventStruct{
+ code: code,
+ }
+ event.lock.Lock()
+ return event
+}
diff --git a/xchacha20.go b/internal/xchacha20poly1305/xchacha20.go
index 5d963e0..a6e59f0 100644
--- a/xchacha20.go
+++ b/internal/xchacha20poly1305/xchacha20.go
@@ -2,14 +2,14 @@
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
-package main
+package xchacha20poly1305
import (
"encoding/binary"
"golang.org/x/crypto/chacha20poly1305"
)
-func HChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
+func hChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
v00 := uint32(0x61707865)
v01 := uint32(0x3320646e)
@@ -138,7 +138,7 @@ func HChaCha20(out *[32]byte, nonce []byte, key *[32]byte) {
binary.LittleEndian.PutUint32(out[28:], v15)
}
-func XChaCha20Poly1305Encrypt(
+func Encrypt(
dst []byte,
nonceFull *[24]byte,
plaintext []byte,
@@ -147,13 +147,13 @@ func XChaCha20Poly1305Encrypt(
) []byte {
var nonce [chacha20poly1305.NonceSize]byte
var derivedKey [chacha20poly1305.KeySize]byte
- HChaCha20(&derivedKey, nonceFull[:16], key)
+ hChaCha20(&derivedKey, nonceFull[:16], key)
aead, _ := chacha20poly1305.New(derivedKey[:])
copy(nonce[4:], nonceFull[16:])
return aead.Seal(dst, nonce[:], plaintext, additionalData)
}
-func XChaCha20Poly1305Decrypt(
+func Decrypt(
dst []byte,
nonceFull *[24]byte,
plaintext []byte,
@@ -162,7 +162,7 @@ func XChaCha20Poly1305Decrypt(
) ([]byte, error) {
var nonce [chacha20poly1305.NonceSize]byte
var derivedKey [chacha20poly1305.KeySize]byte
- HChaCha20(&derivedKey, nonceFull[:16], key)
+ hChaCha20(&derivedKey, nonceFull[:16], key)
aead, _ := chacha20poly1305.New(derivedKey[:])
copy(nonce[4:], nonceFull[16:])
return aead.Open(dst, nonce[:], plaintext, additionalData)
diff --git a/xchacha20_test.go b/internal/xchacha20poly1305/xchacha20_test.go
index 0f41cf8..5d5b78f 100644
--- a/xchacha20_test.go
+++ b/internal/xchacha20poly1305/xchacha20_test.go
@@ -1,4 +1,4 @@
-package main
+package xchacha20poly1305
import (
"encoding/hex"
@@ -60,7 +60,7 @@ func TestXChaCha20(t *testing.T) {
// test encryption
- ct := XChaCha20Poly1305Encrypt(
+ ct := Encrypt(
nil,
&nonceArray,
pt,
@@ -74,7 +74,7 @@ func TestXChaCha20(t *testing.T) {
// test decryption
- ptp, err := XChaCha20Poly1305Decrypt(
+ ptp, err := Decrypt(
nil,
&nonceArray,
ct,
diff --git a/tun.go b/tun.go
index 6259f33..3365845 100644
--- a/tun.go
+++ b/tun.go
@@ -1,14 +1,13 @@
package main
import (
+ "git.zx2c4.com/wireguard-go/internal/events"
"os"
"sync/atomic"
)
const DefaultMTU = 1420
-type TUNEvent int
-
const (
TUNEventUp = 1 << iota
TUNEventDown
@@ -21,7 +20,7 @@ type TUNDevice interface {
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
MTU() (int, error) // returns the MTU of the device
Name() string // returns the current name
- Events() chan TUNEvent // returns a constant channel of events related to the device
+ Events() chan events.Event // returns a constant channel of events related to the device
Close() error // stops the device and closes the event channel
}
@@ -30,7 +29,8 @@ func (device *Device) RoutineTUNEventReader() {
logError := device.log.Error
for event := range device.tun.device.Events() {
- if event&TUNEventMTUUpdate != 0 {
+
+ if event.Contains(TUNEventMTUUpdate) {
mtu, err := device.tun.device.MTU()
old := atomic.LoadInt32(&device.tun.mtu)
if err != nil {
@@ -45,14 +45,16 @@ func (device *Device) RoutineTUNEventReader() {
}
}
- if event&TUNEventUp != 0 && !device.isUp.Get() {
+ if event.Contains(TUNEventUp) && !device.isUp.Get() {
logInfo.Println("Interface set up")
device.Up()
}
- if event&TUNEventDown != 0 && device.isUp.Get() {
+ if event.Contains(TUNEventDown) && device.isUp.Get() {
logInfo.Println("Interface set down")
device.Down()
}
+
+ event.Processed()
}
}
diff --git a/tun_linux.go b/tun_linux.go
index daa2462..4585b13 100644
--- a/tun_linux.go
+++ b/tun_linux.go
@@ -7,6 +7,7 @@ import (
"encoding/binary"
"errors"
"fmt"
+ "git.zx2c4.com/wireguard-go/internal/events"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
"net"
@@ -52,10 +53,10 @@ const (
type NativeTun struct {
fd *os.File
- index int32 // if index
- name string // name of interface
- errors chan error // async error handling
- events chan TUNEvent // device related events
+ index int32 // if index
+ name string // name of interface
+ errors chan error // async error handling
+ events chan events.Event // device related events
}
func (tun *NativeTun) File() *os.File {
@@ -71,9 +72,9 @@ func (tun *NativeTun) RoutineHackListener() {
_, err := unix.Write(fd, nil)
switch err {
case unix.EINVAL:
- tun.events <- TUNEventUp
+ tun.events <- events.NewEvent(TUNEventUp)
case unix.EIO:
- tun.events <- TUNEventDown
+ tun.events <- events.NewEvent(TUNEventDown)
default:
}
time.Sleep(time.Second / 10)
@@ -118,14 +119,14 @@ func (tun *NativeTun) RoutineNetlinkListener() {
}
if info.Flags&unix.IFF_RUNNING != 0 {
- tun.events <- TUNEventUp
+ tun.events <- events.NewEvent(TUNEventUp)
}
if info.Flags&unix.IFF_RUNNING == 0 {
- tun.events <- TUNEventDown
+ tun.events <- events.NewEvent(TUNEventDown)
}
- tun.events <- TUNEventMTUUpdate
+ tun.events <- events.NewEvent(TUNEventMTUUpdate)
default:
remain = remain[hdr.Len:]
@@ -288,7 +289,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
}
}
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan events.Event {
return tun.events
}
@@ -300,7 +301,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
device := &NativeTun{
fd: fd,
name: name,
- events: make(chan TUNEvent, 5),
+ events: make(chan events.Event, 5),
errors: make(chan error, 5),
}
@@ -357,7 +358,7 @@ func CreateTUN(name string) (TUNDevice, error) {
device := &NativeTun{
fd: fd,
name: newName,
- events: make(chan TUNEvent, 5),
+ events: make(chan events.Event, 5),
errors: make(chan error, 5),
}