]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Started migration to sub-packages
authorMathias Hall-Andersen <mathias@hall-andersen.dk>
Fri, 9 Feb 2018 17:56:00 +0000 (18:56 +0100)
committerMathias Hall-Andersen <mathias@hall-andersen.dk>
Fri, 9 Feb 2018 17:56:00 +0000 (18:56 +0100)
cookie.go
internal/events/event.go [new file with mode: 0644]
internal/xchacha20poly1305/xchacha20.go [moved from xchacha20.go with 94% similarity]
internal/xchacha20poly1305/xchacha20_test.go [moved from xchacha20_test.go with 96% similarity]
tun.go
tun_linux.go

index a13ad49e2dc4b4b441b18210c1eea79d4652427d..7cea75c458a7166eaa81c50a22ac075a851aeefd 100644 (file)
--- 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 (file)
index 0000000..4412bbb
--- /dev/null
@@ -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
+}
similarity index 94%
rename from xchacha20.go
rename to internal/xchacha20poly1305/xchacha20.go
index 5d963e0a0da69da5ce74ec289482a837d011de53..a6e59f0cf990d3712aa5c25639a3b2916c37813a 100644 (file)
@@ -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)
similarity index 96%
rename from xchacha20_test.go
rename to internal/xchacha20poly1305/xchacha20_test.go
index 0f41cf8c799fe3581db7206fcbe84c79e47b7859..5d5b78fd2a548b46709032145a9ba639076e26f9 100644 (file)
@@ -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 6259f33a49c97f58798b5dda1b312238c708e317..3365845ee9dbe8fd67730c98ce08f744e309808e 100644 (file)
--- 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()
        }
 }
index daa2462ad7650c27825cf4eec3074d0f57597f25..4585b13fff72e7620fd6da544088ba13d77ea53a 100644 (file)
@@ -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),
        }