]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/cmd/govpn-client/tcp.go
Little log messages refactoring
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
index aa868da3d861a7c87e86c3cca1716518e9b31936..9a1794a59d6c30f0cb4e586a8d3a342b67c3f356 100644 (file)
@@ -19,77 +19,153 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "bytes"
        "encoding/binary"
-       "io"
        "log"
        "net"
+       "sync/atomic"
+       "time"
 
        "govpn"
 )
 
-// TCPSender prepends size prefix to each outgoing packet.
-type TCPSender struct {
-       conn *net.TCPConn
-}
-
-func (c TCPSender) Write(data []byte) (int, error) {
-       size := make([]byte, 2)
-       binary.BigEndian.PutUint16(size, uint16(len(data)))
-       return c.conn.Write(append(size, data...))
-}
-
-func startTCP() (io.Writer, chan []byte, chan struct{}) {
+func startTCP(timeouted, rehandshaking, termination chan struct{}) {
        remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
        if err != nil {
                log.Fatalln("Can not resolve remote address:", err)
        }
-       c, err := net.DialTCP("tcp", nil, remote)
-       conn := TCPSender{c}
+       conn, err := net.DialTCP("tcp", nil, remote)
        if err != nil {
-               log.Fatalln("Can not connect TCP:", err)
+               log.Fatalln("Can not connect to address:", err)
        }
-       sink := make(chan []byte)
-       ready := make(chan struct{})
-       go func() {
-               var err error
-               var n int
-               var sizeNbuf int
-               sizeBuf := make([]byte, 2)
-               var sizeNeed uint16
-               var bufN uint16
-               buf := make([]byte, govpn.MTU)
-               for {
-                       <-ready
-                       if sizeNbuf != 2 {
-                               n, err = c.Read(sizeBuf[sizeNbuf:2])
-                               if err != nil {
-                                       break
-                               }
-                               sizeNbuf += n
-                               if sizeNbuf == 2 {
-                                       sizeNeed = binary.BigEndian.Uint16(sizeBuf)
-                                       if sizeNeed > uint16(govpn.MTU)-2 {
-                                               log.Println("Invalid TCP size, skipping")
-                                               sizeNbuf = 0
-                                               sink <- nil
-                                               continue
-                                       }
-                                       bufN = 0
-                               }
-                       }
-               ReadMore:
-                       if sizeNeed != bufN {
-                               n, err = c.Read(buf[bufN:sizeNeed])
-                               if err != nil {
-                                       break
+       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)
+       var n int
+       var err error
+       var prev int
+       var peer *govpn.Peer
+       var terminator chan struct{}
+HandshakeCycle:
+       for {
+               select {
+               case <-termination:
+                       break 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 {
+                       log.Println("Connection timeouted")
+                       timeouted <- struct{}{}
+                       break HandshakeCycle
+               }
+
+               prev += n
+               peerId := govpn.IDsCache.Find(buf[:prev])
+               if peerId == nil {
+                       continue
+               }
+               peer = hs.Client(buf[:prev])
+               prev = 0
+               if peer == nil {
+                       continue
+               }
+               log.Println("Handshake completed")
+               knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
+               if firstUpCall {
+                       go govpn.ScriptCall(*upPath, *ifaceName)
+                       firstUpCall = false
+               }
+               hs.Zero()
+               terminator = make(chan struct{})
+               go func() {
+                       heartbeat := time.NewTicker(peer.Timeout)
+                       var data []byte
+               Processor:
+                       for {
+                               select {
+                               case <-heartbeat.C:
+                                       peer.EthProcess(nil)
+                               case <-terminator:
+                                       break Processor
+                               case data = <-tap.Sink:
+                                       peer.EthProcess(data)
                                }
-                               bufN += uint16(n)
-                               goto ReadMore
                        }
-                       sizeNbuf = 0
-                       sink <- buf[:sizeNeed]
+                       heartbeat.Stop()
+                       peer.Zero()
+               }()
+               break HandshakeCycle
+       }
+       if hs != nil {
+               hs.Zero()
+       }
+       if peer == nil {
+               return
+       }
+
+       nonceExpectation := make([]byte, govpn.NonceSize)
+       binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
+       peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+       prev = 0
+       var i int
+TransportCycle:
+       for {
+               select {
+               case <-termination:
+                       break TransportCycle
+               default:
+               }
+               if prev == govpn.MTU {
+                       log.Println("Timeouted waiting for the packet")
+                       timeouted <- struct{}{}
+                       break TransportCycle
                }
-       }()
-       go func() { ready <- struct{}{} }()
-       return conn, sink, ready
+               conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
+               n, err = conn.Read(buf[prev:])
+               if err != nil {
+                       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
+               }
+               if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
+                       log.Println("Need rehandshake")
+                       rehandshaking <- struct{}{}
+                       break TransportCycle
+               }
+               binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
+               peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
+               copy(buf, buf[i+govpn.NonceSize:prev])
+               prev = prev - i - govpn.NonceSize
+               goto CheckMore
+       }
+       if terminator != nil {
+               terminator <- struct{}{}
+       }
+       peer.Zero()
 }