You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/udp/hub.go

176 lines
3.7 KiB

package udp
9 years ago
import (
"context"
9 years ago
"net"
8 years ago
"v2ray.com/core/app/log"
"v2ray.com/core/common/buf"
8 years ago
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet/internal"
9 years ago
)
// Payload represents a single UDP payload.
type Payload struct {
payload *buf.Buffer
source v2net.Destination
originalDest v2net.Destination
8 years ago
}
// PayloadHandler is function to handle Payload.
type PayloadHandler func(payload *buf.Buffer, source v2net.Destination, originalDest v2net.Destination)
9 years ago
// PayloadQueue is a queue of Payload.
type PayloadQueue struct {
queue []chan Payload
callback PayloadHandler
8 years ago
}
// NewPayloadQueue returns a new PayloadQueue.
func NewPayloadQueue(option ListenOption) *PayloadQueue {
queue := &PayloadQueue{
8 years ago
callback: option.Callback,
queue: make([]chan Payload, option.Concurrency),
8 years ago
}
for i := range queue.queue {
queue.queue[i] = make(chan Payload, 64)
8 years ago
go queue.Dequeue(queue.queue[i])
}
return queue
}
func (v *PayloadQueue) Enqueue(payload Payload) {
8 years ago
size := len(v.queue)
8 years ago
idx := 0
if size > 1 {
idx = dice.Roll(size)
}
8 years ago
for i := 0; i < size; i++ {
select {
8 years ago
case v.queue[idx%size] <- payload:
8 years ago
return
default:
8 years ago
idx++
8 years ago
}
}
}
func (v *PayloadQueue) Dequeue(queue <-chan Payload) {
for payload := range queue {
v.callback(payload.payload, payload.source, payload.originalDest)
8 years ago
}
}
func (v *PayloadQueue) Close() {
8 years ago
for _, queue := range v.queue {
8 years ago
close(queue)
}
9 years ago
}
type ListenOption struct {
Callback PayloadHandler
ReceiveOriginalDest bool
8 years ago
Concurrency int
}
type Hub struct {
8 years ago
conn *net.UDPConn
cancel context.CancelFunc
queue *PayloadQueue
8 years ago
option ListenOption
}
func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*Hub, error) {
8 years ago
if option.Concurrency < 1 {
option.Concurrency = 1
}
9 years ago
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: address.IP(),
9 years ago
Port: int(port),
})
if err != nil {
return nil, err
}
log.Trace(newError("listening UDP on ", address, ":", port))
if option.ReceiveOriginalDest {
fd, err := internal.GetSysFd(udpConn)
if err != nil {
return nil, newError("failed to get fd").Base(err)
}
err = SetOriginalDestOptions(fd)
if err != nil {
return nil, newError("failed to set socket options").Base(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
hub := &Hub{
conn: udpConn,
queue: NewPayloadQueue(option),
8 years ago
option: option,
cancel: cancel,
9 years ago
}
go hub.start(ctx)
9 years ago
return hub, nil
}
func (v *Hub) Close() {
v.cancel()
8 years ago
v.conn.Close()
9 years ago
}
func (v *Hub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
8 years ago
return v.conn.WriteToUDP(payload, &net.UDPAddr{
IP: dest.Address.IP(),
Port: int(dest.Port),
9 years ago
})
}
func (v *Hub) start(ctx context.Context) {
oobBytes := make([]byte, 256)
L:
for {
select {
case <-ctx.Done():
break L
default:
}
buffer := buf.New()
var noob int
var addr *net.UDPAddr
8 years ago
err := buffer.AppendSupplier(func(b []byte) (int, error) {
n, nb, _, a, e := ReadUDPMsg(v.conn, b, oobBytes)
noob = nb
addr = a
8 years ago
return n, e
})
9 years ago
if err != nil {
log.Trace(newError("failed to read UDP msg").Base(err))
9 years ago
buffer.Release()
continue
}
payload := Payload{
payload: buffer,
}
payload.source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
8 years ago
if v.option.ReceiveOriginalDest && noob > 0 {
payload.originalDest = RetrieveOriginalDest(oobBytes[:noob])
}
v.queue.Enqueue(payload)
9 years ago
}
v.queue.Close()
9 years ago
}
8 years ago
// Connection returns the net.Conn underneath this hub.
// Private: Visible for testing only
func (v *Hub) Connection() net.Conn {
8 years ago
return v.conn
}
8 years ago
func (v *Hub) Addr() net.Addr {
8 years ago
return v.conn.LocalAddr()
8 years ago
}