|
|
@ -7,14 +7,11 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
// Payload represents a single UDP payload.
|
|
|
|
// Payload represents a single UDP payload.
|
|
|
|
type Payload struct {
|
|
|
|
type Payload struct {
|
|
|
|
payload *buf.Buffer
|
|
|
|
Content *buf.Buffer
|
|
|
|
source net.Destination
|
|
|
|
Source net.Destination
|
|
|
|
originalDest net.Destination
|
|
|
|
OriginalDestination net.Destination
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PayloadHandler is function to handle Payload.
|
|
|
|
|
|
|
|
type PayloadHandler func(payload *buf.Buffer, source net.Destination, originalDest net.Destination)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type HubOption func(h *Hub)
|
|
|
|
type HubOption func(h *Hub)
|
|
|
|
|
|
|
|
|
|
|
|
func HubCapacity(cap int) HubOption {
|
|
|
|
func HubCapacity(cap int) HubOption {
|
|
|
@ -31,12 +28,12 @@ func HubReceiveOriginalDestination(r bool) HubOption {
|
|
|
|
|
|
|
|
|
|
|
|
type Hub struct {
|
|
|
|
type Hub struct {
|
|
|
|
conn *net.UDPConn
|
|
|
|
conn *net.UDPConn
|
|
|
|
callback PayloadHandler
|
|
|
|
cache chan *Payload
|
|
|
|
capacity int
|
|
|
|
capacity int
|
|
|
|
recvOrigDest bool
|
|
|
|
recvOrigDest bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ListenUDP(address net.Address, port net.Port, callback PayloadHandler, options ...HubOption) (*Hub, error) {
|
|
|
|
func ListenUDP(address net.Address, port net.Port, options ...HubOption) (*Hub, error) {
|
|
|
|
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
|
|
|
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
Port: int(port),
|
|
|
@ -48,13 +45,14 @@ func ListenUDP(address net.Address, port net.Port, callback PayloadHandler, opti
|
|
|
|
hub := &Hub{
|
|
|
|
hub := &Hub{
|
|
|
|
conn: udpConn,
|
|
|
|
conn: udpConn,
|
|
|
|
capacity: 256,
|
|
|
|
capacity: 256,
|
|
|
|
callback: callback,
|
|
|
|
|
|
|
|
recvOrigDest: false,
|
|
|
|
recvOrigDest: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(hub)
|
|
|
|
opt(hub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hub.cache = make(chan *Payload, hub.capacity)
|
|
|
|
|
|
|
|
|
|
|
|
if hub.recvOrigDest {
|
|
|
|
if hub.recvOrigDest {
|
|
|
|
rawConn, err := udpConn.SyscallConn()
|
|
|
|
rawConn, err := udpConn.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -70,10 +68,7 @@ func ListenUDP(address net.Address, port net.Port, callback PayloadHandler, opti
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c := make(chan *Payload, hub.capacity)
|
|
|
|
go hub.start()
|
|
|
|
|
|
|
|
|
|
|
|
go hub.start(c)
|
|
|
|
|
|
|
|
go hub.process(c)
|
|
|
|
|
|
|
|
return hub, nil
|
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -90,13 +85,8 @@ func (h *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Hub) process(c <-chan *Payload) {
|
|
|
|
func (h *Hub) start() {
|
|
|
|
for p := range c {
|
|
|
|
c := h.cache
|
|
|
|
h.callback(p.payload, p.source, p.originalDest)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Hub) start(c chan<- *Payload) {
|
|
|
|
|
|
|
|
defer close(c)
|
|
|
|
defer close(c)
|
|
|
|
|
|
|
|
|
|
|
|
oobBytes := make([]byte, 256)
|
|
|
|
oobBytes := make([]byte, 256)
|
|
|
@ -119,13 +109,13 @@ func (h *Hub) start(c chan<- *Payload) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
payload := &Payload{
|
|
|
|
payload := &Payload{
|
|
|
|
payload: buffer,
|
|
|
|
Content: buffer,
|
|
|
|
|
|
|
|
Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
payload.source = net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port))
|
|
|
|
|
|
|
|
if h.recvOrigDest && noob > 0 {
|
|
|
|
if h.recvOrigDest && noob > 0 {
|
|
|
|
payload.originalDest = RetrieveOriginalDest(oobBytes[:noob])
|
|
|
|
payload.OriginalDestination = RetrieveOriginalDest(oobBytes[:noob])
|
|
|
|
if payload.originalDest.IsValid() {
|
|
|
|
if payload.OriginalDestination.IsValid() {
|
|
|
|
newError("UDP original destination: ", payload.originalDest).AtDebug().WriteToLog()
|
|
|
|
newError("UDP original destination: ", payload.OriginalDestination).AtDebug().WriteToLog()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
newError("failed to read UDP original destination").WriteToLog()
|
|
|
|
newError("failed to read UDP original destination").WriteToLog()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -143,3 +133,7 @@ func (h *Hub) start(c chan<- *Payload) {
|
|
|
|
func (h *Hub) Addr() net.Addr {
|
|
|
|
func (h *Hub) Addr() net.Addr {
|
|
|
|
return h.conn.LocalAddr()
|
|
|
|
return h.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *Hub) Receive() <-chan *Payload {
|
|
|
|
|
|
|
|
return h.cache
|
|
|
|
|
|
|
|
}
|
|
|
|