2015-02-20 07:12:47 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// Package udp implements a UDP BitTorrent tracker per BEP 15.
|
2015-02-20 07:12:47 +01:00
|
|
|
// IPv6 is currently unsupported as there is no widely-implemented standard.
|
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
2015-02-21 20:02:00 +01:00
|
|
|
"errors"
|
2015-02-20 07:12:47 +01:00
|
|
|
"net"
|
2015-02-20 19:39:19 +01:00
|
|
|
"time"
|
2015-02-20 07:12:47 +01:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/pushrax/bufferpool"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/config"
|
|
|
|
"github.com/chihaya/chihaya/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents a UDP torrent tracker.
|
|
|
|
type Server struct {
|
2015-02-21 20:02:00 +01:00
|
|
|
config *config.Config
|
|
|
|
tracker *tracker.Tracker
|
|
|
|
done bool
|
|
|
|
booting chan struct{}
|
|
|
|
sock *net.UDPConn
|
|
|
|
|
2015-02-20 21:38:27 +01:00
|
|
|
connIDGen *ConnectionIDGenerator
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
|
2015-02-21 19:16:21 +01:00
|
|
|
func (s *Server) serve(listenAddr string) error {
|
2015-02-21 20:02:00 +01:00
|
|
|
if s.sock != nil {
|
|
|
|
return errors.New("server already booted")
|
|
|
|
}
|
|
|
|
|
2015-02-21 19:16:21 +01:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", listenAddr)
|
2015-02-20 07:12:47 +01:00
|
|
|
if err != nil {
|
2015-02-21 20:02:00 +01:00
|
|
|
close(s.booting)
|
2015-02-20 07:12:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-21 19:16:21 +01:00
|
|
|
sock, err := net.ListenUDP("udp", udpAddr)
|
2015-02-20 07:12:47 +01:00
|
|
|
defer sock.Close()
|
|
|
|
if err != nil {
|
2015-02-21 20:02:00 +01:00
|
|
|
close(s.booting)
|
2015-02-20 07:12:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:39:19 +01:00
|
|
|
if s.config.UDPReadBufferSize > 0 {
|
|
|
|
sock.SetReadBuffer(s.config.UDPReadBufferSize)
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pool := bufferpool.New(1000, 2048)
|
2015-02-21 20:02:00 +01:00
|
|
|
s.sock = sock
|
|
|
|
close(s.booting)
|
2015-02-20 07:12:47 +01:00
|
|
|
|
2015-02-20 19:39:19 +01:00
|
|
|
for !s.done {
|
2015-02-20 07:12:47 +01:00
|
|
|
buffer := pool.TakeSlice()
|
2015-02-20 19:39:19 +01:00
|
|
|
sock.SetReadDeadline(time.Now().Add(time.Second))
|
2015-02-20 07:12:47 +01:00
|
|
|
n, addr, err := sock.ReadFromUDP(buffer)
|
2015-02-20 19:39:19 +01:00
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
if err != nil {
|
2015-02-20 19:39:19 +01:00
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
|
|
|
|
continue
|
|
|
|
}
|
2015-02-20 07:12:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
go func() {
|
2015-02-20 20:08:46 +01:00
|
|
|
response, action := s.handlePacket(buffer[:n], addr)
|
2015-02-21 20:35:21 +01:00
|
|
|
pool.GiveSlice(buffer)
|
|
|
|
|
2015-02-20 07:12:47 +01:00
|
|
|
if response != nil {
|
|
|
|
sock.WriteToUDP(response, addr)
|
|
|
|
}
|
2015-02-20 20:08:46 +01:00
|
|
|
|
|
|
|
if glog.V(2) {
|
|
|
|
duration := time.Since(start)
|
2015-02-20 21:38:27 +01:00
|
|
|
glog.Infof("[UDP - %9s] %s %s", duration, action, addr)
|
2015-02-20 20:08:46 +01:00
|
|
|
}
|
2015-02-20 07:12:47 +01:00
|
|
|
}()
|
|
|
|
}
|
2015-02-20 19:39:19 +01:00
|
|
|
|
|
|
|
return nil
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// Serve runs a UDP server, blocking until the server has shut down.
|
2015-02-21 19:16:21 +01:00
|
|
|
func (s *Server) Serve(addr string) {
|
|
|
|
glog.V(0).Info("Starting UDP on ", addr)
|
2015-02-20 07:12:47 +01:00
|
|
|
|
2015-02-20 21:38:27 +01:00
|
|
|
go func() {
|
|
|
|
// Generate a new IV every hour.
|
|
|
|
for range time.Tick(time.Hour) {
|
|
|
|
s.connIDGen.NewIV()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-02-21 19:16:21 +01:00
|
|
|
if err := s.serve(addr); err != nil {
|
2015-02-20 07:12:47 +01:00
|
|
|
glog.Errorf("Failed to run UDP server: %s", err.Error())
|
2015-02-20 19:39:19 +01:00
|
|
|
} else {
|
|
|
|
glog.Info("UDP server shut down cleanly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop cleanly shuts down the server.
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
s.done = true
|
2015-02-21 20:02:00 +01:00
|
|
|
s.sock.SetReadDeadline(time.Now())
|
2015-02-20 19:39:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new UDP server for a given configuration and tracker.
|
|
|
|
func NewServer(cfg *config.Config, tkr *tracker.Tracker) *Server {
|
2015-02-20 21:38:27 +01:00
|
|
|
gen := &ConnectionIDGenerator{}
|
|
|
|
if err := gen.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:39:19 +01:00
|
|
|
return &Server{
|
2015-02-20 21:38:27 +01:00
|
|
|
config: cfg,
|
|
|
|
tracker: tkr,
|
|
|
|
connIDGen: gen,
|
2015-02-21 20:02:00 +01:00
|
|
|
booting: make(chan struct{}),
|
2015-02-20 07:12:47 +01:00
|
|
|
}
|
|
|
|
}
|