]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/cmd/govpn-client/tcp.go
Raise copyright years
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
index 49d3ae92cf02e86a9f5c2e5937997ae756b124f7..e96d5bf41ccb52d1e8115d45be4afb3893126ec5 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -20,7 +20,6 @@ package main
 
 import (
        "bytes"
-       "encoding/binary"
        "log"
        "net"
        "sync/atomic"
@@ -36,8 +35,9 @@ func startTCP(timeouted, rehandshaking, termination chan struct{}) {
        }
        conn, err := net.DialTCP("tcp", nil, remote)
        if err != nil {
-               log.Fatalln("Can not connect to remote address:", err)
+               log.Fatalln("Can not connect to address:", err)
        }
+       log.Println("Connected to TCP:" + *remoteAddr)
        handleTCP(conn, timeouted, rehandshaking, termination)
 }
 
@@ -57,18 +57,21 @@ HandshakeCycle:
                default:
                }
                if prev == govpn.MTU {
+                       log.Println("Timeouted waiting for the packet")
+                       timeouted <- struct{}{}
                        break HandshakeCycle
                }
 
                conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
                n, err = conn.Read(buf[prev:])
                if err != nil {
-                       // Either EOFed or timeouted
+                       log.Println("Connection timeouted")
+                       timeouted <- struct{}{}
                        break HandshakeCycle
                }
 
                prev += n
-               peerId := govpn.IDsCache.Find(buf[:prev])
+               peerId := idsCache.Find(buf[:prev])
                if peerId == nil {
                        continue
                }
@@ -112,8 +115,7 @@ HandshakeCycle:
        }
 
        nonceExpectation := make([]byte, govpn.NonceSize)
-       binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
-       peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+       peer.NonceExpectation(nonceExpectation)
        prev = 0
        var i int
 TransportCycle:
@@ -124,23 +126,28 @@ TransportCycle:
                default:
                }
                if prev == govpn.MTU {
+                       log.Println("Timeouted waiting for the packet")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
                conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
                n, err = conn.Read(buf[prev:])
                if err != nil {
-                       // Either EOFed or timeouted
+                       log.Println("Connection timeouted")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
                prev += n
        CheckMore:
+               if prev < govpn.MinPktLength {
+                       continue
+               }
                i = bytes.Index(buf[:prev], nonceExpectation)
                if i == -1 {
                        continue
                }
                if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
+                       log.Println("Unauthenticated packet, dropping connection")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
@@ -149,8 +156,7 @@ TransportCycle:
                        rehandshaking <- struct{}{}
                        break TransportCycle
                }
-               binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
-               peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+               peer.NonceExpectation(nonceExpectation)
                copy(buf, buf[i+govpn.NonceSize:prev])
                prev = prev - i - govpn.NonceSize
                goto CheckMore
@@ -159,4 +165,5 @@ TransportCycle:
                terminator <- struct{}{}
        }
        peer.Zero()
+       conn.Close()
 }