Browse Source

Fix discosrv build, build as part of all (fixes #257)

Jakob Borg 11 years ago
parent
commit
2e1c33206f
3 changed files with 34 additions and 25 deletions
  1. 2 0
      .gitignore
  2. 4 0
      build.sh
  3. 28 25
      discover/cmd/discosrv/main.go

+ 2 - 0
.gitignore

@@ -6,3 +6,5 @@ stcli.exe
 *.zip
 *.asc
 *.sublime*
+discosrv
+stpidx

+ 4 - 0
build.sh

@@ -112,6 +112,10 @@ case "$1" in
 		test || exit 1
 		assets
 
+		godep go build ./discover/cmd/discosrv
+		godep go build ./cmd/stpidx
+		godep go build ./cmd/stcli
+
 		for os in darwin-amd64 linux-386 linux-amd64 freebsd-amd64 windows-amd64 windows-386 ; do
 			export GOOS=${os%-*}
 			export GOARCH=${os#*-}

+ 28 - 25
discover/cmd/discosrv/main.go

@@ -5,6 +5,7 @@ import (
 	"encoding/hex"
 	"flag"
 	"fmt"
+	"io"
 	"log"
 	"net"
 	"os"
@@ -16,18 +17,18 @@ import (
 	"github.com/juju/ratelimit"
 )
 
-type Node struct {
-	Addresses []Address
-	Updated   time.Time
+type node struct {
+	addresses []address
+	updated   time.Time
 }
 
-type Address struct {
-	IP   []byte
-	Port uint16
+type address struct {
+	ip   []byte
+	port uint16
 }
 
 var (
-	nodes     = make(map[string]Node)
+	nodes     = make(map[string]node)
 	lock      sync.Mutex
 	queries   = 0
 	announces = 0
@@ -134,7 +135,7 @@ func limit(addr *net.UDPAddr) bool {
 func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
 	var pkt discover.AnnounceV2
 	err := pkt.UnmarshalXDR(buf)
-	if err != nil {
+	if err != nil && err != io.EOF {
 		log.Println("AnnounceV2 Unmarshal:", err)
 		log.Println(hex.Dump(buf))
 		return
@@ -152,25 +153,25 @@ func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
 		ip = addr.IP.To16()
 	}
 
-	var addrs []Address
-	for _, addr := range pkt.Addresses {
+	var addrs []address
+	for _, addr := range pkt.This.Addresses {
 		tip := addr.IP
 		if len(tip) == 0 {
 			tip = ip
 		}
-		addrs = append(addrs, Address{
-			IP:   tip,
-			Port: addr.Port,
+		addrs = append(addrs, address{
+			ip:   tip,
+			port: addr.Port,
 		})
 	}
 
-	node := Node{
-		Addresses: addrs,
-		Updated:   time.Now(),
+	node := node{
+		addresses: addrs,
+		updated:   time.Now(),
 	}
 
 	lock.Lock()
-	nodes[pkt.NodeID] = node
+	nodes[pkt.This.ID] = node
 	lock.Unlock()
 }
 
@@ -191,19 +192,21 @@ func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) {
 	queries++
 	lock.Unlock()
 
-	if ok && len(node.Addresses) > 0 {
-		pkt := discover.AnnounceV2{
-			Magic:  discover.AnnouncementMagicV2,
-			NodeID: pkt.NodeID,
+	if ok && len(node.addresses) > 0 {
+		ann := discover.AnnounceV2{
+			Magic: discover.AnnouncementMagicV2,
+			This: discover.Node{
+				ID: pkt.NodeID,
+			},
 		}
-		for _, addr := range node.Addresses {
-			pkt.Addresses = append(pkt.Addresses, discover.Address{IP: addr.IP, Port: addr.Port})
+		for _, addr := range node.addresses {
+			ann.This.Addresses = append(ann.This.Addresses, discover.Address{IP: addr.ip, Port: addr.port})
 		}
 		if debug {
 			log.Printf("-> %v %#v", addr, pkt)
 		}
 
-		tb := pkt.MarshalXDR()
+		tb := ann.MarshalXDR()
 		_, _, err = conn.WriteMsgUDP(tb, nil, addr)
 		if err != nil {
 			log.Println("QueryV2 response write:", err)
@@ -235,7 +238,7 @@ func logStats(file string, intv int) {
 
 		var deleted = 0
 		for id, node := range nodes {
-			if time.Since(node.Updated) > 60*time.Minute {
+			if time.Since(node.updated) > 60*time.Minute {
 				delete(nodes, id)
 				deleted++
 			}