2017-08-25 14:53:23 +02:00
|
|
|
// +build !linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2017-11-17 17:25:45 +01:00
|
|
|
/* This code is meant to be a temporary solution
|
|
|
|
* on platforms for which the sticky socket / source caching behavior
|
|
|
|
* has not yet been implemented.
|
|
|
|
*
|
|
|
|
* See conn_linux.go for an implementation on the linux platform.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
type NativeBind struct {
|
|
|
|
ipv4 *net.UDPConn
|
|
|
|
ipv6 *net.UDPConn
|
|
|
|
}
|
|
|
|
|
|
|
|
type NativeEndpoint net.UDPAddr
|
|
|
|
|
|
|
|
var _ Bind = (*NativeBind)(nil)
|
|
|
|
var _ Endpoint = (*NativeEndpoint)(nil)
|
|
|
|
|
|
|
|
func CreateEndpoint(s string) (Endpoint, error) {
|
|
|
|
addr, err := parseEndpoint(s)
|
2017-11-19 13:14:15 +01:00
|
|
|
return (*NativeEndpoint)(addr), err
|
2017-11-19 00:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *NativeEndpoint) ClearSrc() {}
|
|
|
|
|
|
|
|
func (e *NativeEndpoint) DstIP() net.IP {
|
|
|
|
return (*net.UDPAddr)(e).IP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NativeEndpoint) SrcIP() net.IP {
|
|
|
|
return nil // not supported
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
func (e *NativeEndpoint) DstToBytes() []byte {
|
|
|
|
addr := (*net.UDPAddr)(e)
|
2017-11-19 13:14:15 +01:00
|
|
|
out := addr.IP
|
2017-11-19 00:21:58 +01:00
|
|
|
out = append(out, byte(addr.Port&0xff))
|
|
|
|
out = append(out, byte((addr.Port>>8)&0xff))
|
|
|
|
return out
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
func (e *NativeEndpoint) DstToString() string {
|
|
|
|
return (*net.UDPAddr)(e).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NativeEndpoint) SrcToString() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
func listenNet(network string, port int) (*net.UDPConn, int, error) {
|
2017-11-19 00:21:58 +01:00
|
|
|
|
|
|
|
// listen
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
conn, err := net.ListenUDP(network, &net.UDPAddr{Port: port})
|
2017-11-19 00:21:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve port
|
|
|
|
|
|
|
|
laddr := conn.LocalAddr()
|
2017-11-19 13:14:15 +01:00
|
|
|
uaddr, err := net.ResolveUDPAddr(
|
2017-11-19 00:21:58 +01:00
|
|
|
laddr.Network(),
|
|
|
|
laddr.String(),
|
|
|
|
)
|
2017-11-19 13:14:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2017-11-19 00:21:58 +01:00
|
|
|
return conn, uaddr.Port, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
func CreateBind(uport uint16) (Bind, uint16, error) {
|
|
|
|
var err error
|
|
|
|
var bind NativeBind
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
port := int(uport)
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
bind.ipv4, port, err = listenNet("udp4", port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2017-11-17 17:25:45 +01:00
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
|
|
|
|
bind.ipv6, port, err = listenNet("udp6", port)
|
2017-11-17 17:25:45 +01:00
|
|
|
if err != nil {
|
2017-11-19 13:14:15 +01:00
|
|
|
bind.ipv4.Close()
|
2017-11-17 17:25:45 +01:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
return &bind, uint16(port), nil
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
func (bind *NativeBind) Close() error {
|
|
|
|
err1 := bind.ipv4.Close()
|
|
|
|
err2 := bind.ipv6.Close()
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bind *NativeBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) {
|
|
|
|
n, endpoint, err := bind.ipv4.ReadFromUDP(buff)
|
|
|
|
return n, (*NativeEndpoint)(endpoint), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bind *NativeBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) {
|
|
|
|
n, endpoint, err := bind.ipv6.ReadFromUDP(buff)
|
|
|
|
return n, (*NativeEndpoint)(endpoint), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bind *NativeBind) Send(buff []byte, endpoint Endpoint) error {
|
|
|
|
var err error
|
|
|
|
nend := endpoint.(*NativeEndpoint)
|
|
|
|
if nend.IP.To16() != nil {
|
|
|
|
_, err = bind.ipv6.WriteToUDP(buff, (*net.UDPAddr)(nend))
|
|
|
|
} else {
|
|
|
|
_, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bind *NativeBind) SetMark(_ uint32) error {
|
|
|
|
return nil
|
2017-11-17 17:25:45 +01:00
|
|
|
}
|