]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/cmd/govpn-client/tcp.go
Configure MTU on per-user basis
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
index 511955c4400a8a48d8e337e45a9b7b57b2d8443a..88ddeeb2226c4dd4856a4db1b021dee87776303d 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,14 +35,15 @@ 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)
 }
 
 func handleTCP(conn *net.TCPConn, timeouted, rehandshaking, termination chan struct{}) {
        hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
-       buf := make([]byte, govpn.MTU)
+       buf := make([]byte, 2*(govpn.EncLessEnlargeSize+*mtu)+*mtu)
        var n int
        var err error
        var prev int
@@ -56,19 +56,22 @@ HandshakeCycle:
                        break HandshakeCycle
                default:
                }
-               if prev == govpn.MTU {
+               if prev == len(buf) {
+                       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:
@@ -123,14 +125,15 @@ TransportCycle:
                        break TransportCycle
                default:
                }
-               if prev == govpn.MTU {
+               if prev == len(buf) {
+                       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
                }
@@ -144,6 +147,7 @@ TransportCycle:
                        continue
                }
                if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
+                       log.Println("Unauthenticated packet, dropping connection")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
@@ -152,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
@@ -162,4 +165,5 @@ TransportCycle:
                terminator <- struct{}{}
        }
        peer.Zero()
+       conn.Close()
 }