mirror of
				https://git.zx2c4.com/wireguard-go
				synced 2025-10-31 11:56:22 +08:00 
			
		
		
		
	Begin generic Bind implementation
This commit is contained in:
		| @@ -13,11 +13,68 @@ import ( | ||||
|  * See conn_linux.go for an implementation on the linux platform. | ||||
|  */ | ||||
|  | ||||
| type Endpoint *net.UDPAddr | ||||
| type NativeBind struct { | ||||
| 	ipv4 *net.UDPConn | ||||
| 	ipv6 *net.UDPConn | ||||
| } | ||||
|  | ||||
| type NativeBind *net.UDPConn | ||||
| type NativeEndpoint net.UDPAddr | ||||
|  | ||||
| func CreateUDPBind(port uint16) (UDPBind, uint16, error) { | ||||
| var _ Bind = (*NativeBind)(nil) | ||||
| var _ Endpoint = (*NativeEndpoint)(nil) | ||||
|  | ||||
| func CreateEndpoint(s string) (Endpoint, error) { | ||||
| 	addr, err := parseEndpoint(s) | ||||
| 	return (addr).(*NativeEndpoint), err | ||||
| } | ||||
|  | ||||
| func (_ *NativeEndpoint) ClearSrc() {} | ||||
|  | ||||
| func (e *NativeEndpoint) DstIP() net.IP { | ||||
| 	return (*net.UDPAddr)(e).IP | ||||
| } | ||||
|  | ||||
| func (e *NativeEndpoint) SrcIP() net.IP { | ||||
| 	return nil // not supported | ||||
| } | ||||
|  | ||||
| func (e *NativeEndpoint) DstToBytes() []byte { | ||||
| 	addr := (*net.UDPAddr)(e) | ||||
| 	out := addr.IP.([]byte) | ||||
| 	out = append(out, byte(addr.Port&0xff)) | ||||
| 	out = append(out, byte((addr.Port>>8)&0xff)) | ||||
| 	return out | ||||
| } | ||||
|  | ||||
| func (e *NativeEndpoint) DstToString() string { | ||||
| 	return (*net.UDPAddr)(e).String() | ||||
| } | ||||
|  | ||||
| func (e *NativeEndpoint) SrcToString() string { | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| func listenNet(net string, port int) (*net.UDPConn, int, error) { | ||||
|  | ||||
| 	// listen | ||||
|  | ||||
| 	conn, err := net.ListenUDP("udp", &UDPAddr{Port: port}) | ||||
| 	if err != nil { | ||||
| 		return nil, 0, err | ||||
| 	} | ||||
|  | ||||
| 	// retrieve port | ||||
|  | ||||
| 	laddr := conn.LocalAddr() | ||||
| 	uaddr, _ = net.ResolveUDPAddr( | ||||
| 		laddr.Network(), | ||||
| 		laddr.String(), | ||||
| 	) | ||||
|  | ||||
| 	return conn, uaddr.Port, nil | ||||
| } | ||||
|  | ||||
| func CreateBind(port uint16) (Bind, uint16, error) { | ||||
|  | ||||
| 	// listen | ||||
|  | ||||
| @@ -38,9 +95,3 @@ func CreateUDPBind(port uint16) (UDPBind, uint16, error) { | ||||
| 	) | ||||
| 	return uaddr.Port | ||||
| } | ||||
|  | ||||
| func (_ Endpoint) ClearSrc() {} | ||||
|  | ||||
| func SetMark(conn *net.UDPConn, value uint32) error { | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Mathias Hall-Andersen
					Mathias Hall-Andersen