]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/server/udp.go
Language mistakes and stylistic fixes
[govpn.git] / src / cypherpunks.ru / govpn / server / udp.go
index cd8c2fccefefc37b7e7e3484fc48495f69dcdd64..c8cb72f680ed982fc31a217faf64bf6a0c7adcf1 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2017 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
@@ -36,10 +36,6 @@ func (c udpSender) Write(data []byte) (int, error) {
        return c.conn.WriteToUDP(data, c.addr)
 }
 
-// TODO move to udpSender (?)
-// buffers for UDP parallel processing
-var udpBufs = make(chan []byte, 1<<8)
-
 func (s *Server) startUDP() {
        bind, err := net.ResolveUDPAddr("udp", s.configuration.BindAddress)
        if err != nil {
@@ -56,7 +52,15 @@ func (s *Server) startUDP() {
                "func": logFuncPrefix + "Server.startUDP",
                "bind": bind.String(),
        }
-       s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Listen")
+       s.logger.WithFields(
+               fields,
+       ).WithFields(
+               s.LogFields(),
+       ).WithFields(
+               s.configuration.LogFields(),
+       ).Info("Listen")
+
+       udpBufs := make(chan []byte, 1<<8)
        udpBufs <- make([]byte, govpn.MTUMax)
        go func() {
                var buf []byte
@@ -71,22 +75,34 @@ func (s *Server) startUDP() {
                var peerID *govpn.PeerID
                var conf *govpn.PeerConf
                for {
-                       s.logger.WithFields(fields).Debug("Wait for UDP buffer")
+                       s.logger.WithFields(fields).Debug("Waiting for UDP buffer")
                        buf = <-udpBufs
                        n, raddr, err = conn.ReadFromUDP(buf)
                        if err != nil {
-                               s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Receive failure")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       s.LogFields(),
+                               ).WithError(err).Debug("Receive failed")
                                break
                        }
                        addr = raddr.String()
                        loopFields := logrus.Fields{"addr": addr}
 
-                       s.logger.WithFields(fields).WithFields(loopFields).Debug("Got UDP buffer, check if peer exists")
+                       s.logger.WithFields(
+                               fields,
+                       ).WithFields(
+                               loopFields,
+                       ).Debug("Got UDP buffer, checking if peer exists")
                        s.peersLock.RLock()
                        ps, exists = s.peers[addr]
                        s.peersLock.RUnlock()
                        if exists {
-                               s.logger.WithFields(fields).WithFields(loopFields).Debug("Already known peer, PktProcess")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).Debug("Already known peer, PktProcess")
                                go func(peer *govpn.Peer, tap *govpn.TAP, buf []byte, n int) {
                                        peer.PktProcess(buf[:n], tap, true)
                                        udpBufs <- buf
@@ -99,29 +115,57 @@ func (s *Server) startUDP() {
                        hs, exists = s.handshakes[addr]
                        s.hsLock.RUnlock()
                        if !exists {
-                               logrus.WithFields(fields).WithFields(loopFields).Debug("No handshake yet, try to figure peer ID")
+                               logrus.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).Debug("No handshake yet, trying to figure peer ID")
                                peerID, err = s.idsCache.Find(buf[:n])
                                if err != nil {
-                                       s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).WithError(err).Debug("Couldn't lookup for peer in ids")
+                                       s.logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               loopFields,
+                                       ).WithFields(
+                                               s.LogFields(),
+                                       ).WithError(err).Debug("Can not lookup for peer in ids")
                                        udpBufs <- buf
                                        continue
                                }
                                if peerID == nil {
-                                       s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).Debug("Identity unknown")
+                                       s.logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               loopFields,
+                                       ).WithFields(
+                                               s.LogFields(),
+                                       ).Debug("Identity unknown")
                                        udpBufs <- buf
                                        continue
                                }
 
                                loopFields["peer_id"] = peerID.String()
-                               s.logger.WithFields(fields).WithFields(loopFields).Debug("Found peer ID")
+                               s.logger.WithFields(fields).WithFields(loopFields).Debug("Peer ID found")
                                conf = s.confs.Get(*peerID)
                                if conf == nil {
-                                       s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Error("Peer try to connect, but not configured")
+                                       s.logger.WithFields(
+                                               loopFields,
+                                       ).WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               s.LogFields(),
+                                       ).WithFields(
+                                               s.configuration.LogFields(),
+                                       ).Error("Peer try to connect, but not configured")
                                        udpBufs <- buf
                                        continue
                                }
 
-                               s.logger.WithFields(loopFields).WithFields(fields).Debug("Got configuration, perform handshake")
+                               s.logger.WithFields(
+                                       loopFields,
+                               ).WithFields(
+                                       fields,
+                               ).Debug("Got configuration, performing handshake")
                                hs = govpn.NewHandshake(
                                        addr,
                                        udpSender{conn: conn, addr: raddr},
@@ -130,10 +174,22 @@ func (s *Server) startUDP() {
                                _, err := hs.Server(buf[:n])
                                udpBufs <- buf
                                if err != nil {
-                                       s.logger.WithFields(loopFields).WithFields(fields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer: handshake failed")
+                                       s.logger.WithFields(
+                                               loopFields,
+                                       ).WithFields(
+                                               fields,
+                                       ).WithError(err).WithFields(
+                                               s.LogFields(),
+                                       ).Error("Can not create new peer: handshake failed")
                                        continue
                                }
-                               s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).Info("Hashshake started, continue next packet")
+                               s.logger.WithFields(
+                                       loopFields,
+                               ).WithFields(
+                                       fields,
+                               ).WithFields(
+                                       s.LogFields(),
+                               ).Info("Hashshake started, continuing for the next packet")
 
                                s.hsLock.Lock()
                                s.handshakes[addr] = hs
@@ -141,20 +197,44 @@ func (s *Server) startUDP() {
                                continue
                        }
 
-                       logrus.WithFields(fields).WithFields(loopFields).Debug("Already go handshake, finish it")
+                       logrus.WithFields(
+                               fields,
+                       ).WithFields(
+                               loopFields,
+                       ).Debug("Already got handshake, finishing it")
                        peer, err := hs.Server(buf[:n])
                        if err != nil {
-                               s.logger.WithFields(fields).WithFields(loopFields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer: handshake failed")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).WithError(err).WithFields(
+                                       s.LogFields(),
+                               ).Error("Can not create new peer: handshake failed")
                                udpBufs <- buf
                                continue
                        }
                        if peer == nil {
-                               s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).Error("Couldn't continue handshake")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).WithFields(
+                                       s.LogFields(),
+                               ).Error("Can not continue handshake")
                                udpBufs <- buf
                                continue
                        }
 
-                       s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(loopFields).WithFields(peer.LogFields()).Info("Handshake completed")
+                       s.logger.WithFields(
+                               fields,
+                       ).WithFields(
+                               s.LogFields(),
+                       ).WithFields(
+                               loopFields,
+                       ).WithFields(
+                               peer.LogFields(),
+                       ).Info("Handshake completed")
 
                        hs.Zero()
                        s.hsLock.Lock()
@@ -170,7 +250,11 @@ func (s *Server) startUDP() {
                        s.peersByIDLock.RUnlock()
 
                        if exists {
-                               s.logger.WithFields(fields).WithFields(loopFields).Debug("Peer already exists")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).Debug("Peer already exists")
                                s.peersLock.Lock()
                                s.peers[addrPrev].terminator <- struct{}{}
                                psNew := &PeerState{
@@ -197,13 +281,33 @@ func (s *Server) startUDP() {
                                s.peersByIDLock.Unlock()
                                s.kpLock.Unlock()
 
-                               s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Debug("Rehandshake completed")
+                               s.logger.WithFields(
+                                       fields,
+                               ).WithFields(
+                                       loopFields,
+                               ).WithFields(
+                                       s.LogFields(),
+                               ).WithFields(
+                                       peer.LogFields(),
+                               ).Debug("Rehandshake completed")
                        } else {
                                go func(addr string, peer *govpn.Peer) {
-                                       s.logger.WithFields(fields).WithFields(loopFields).Debug("Peer do not already exists")
+                                       s.logger.WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               loopFields,
+                                       ).Debug("Peer does not exist")
                                        tap, err := s.callUp(peer, govpn.ProtocolUDP)
                                        if err != nil {
-                                               s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).WithError(err).Error("TAP failed")
+                                               s.logger.WithFields(
+                                                       loopFields,
+                                               ).WithFields(
+                                                       fields,
+                                               ).WithFields(
+                                                       s.LogFields(),
+                                               ).WithFields(
+                                                       peer.LogFields(),
+                                               ).WithError(err).Error("TAP failed")
                                                return
                                        }
                                        psNew := &PeerState{
@@ -226,7 +330,15 @@ func (s *Server) startUDP() {
                                        s.peersLock.Unlock()
                                        s.peersByIDLock.Unlock()
                                        s.kpLock.Unlock()
-                                       s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Peer initialized")
+                                       s.logger.WithFields(
+                                               loopFields,
+                                       ).WithFields(
+                                               fields,
+                                       ).WithFields(
+                                               s.LogFields(),
+                                       ).WithFields(
+                                               peer.LogFields(),
+                                       ).Info("Peer initialized")
                                }(addr, peer)
                        }
                        udpBufs <- buf