aboutsummaryrefslogtreecommitdiffstats
path: root/src/conn_linux.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2017-11-19 00:21:58 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2017-11-19 00:21:58 +0100
commita79fdc13a2d7be07b20ea499da9210ebe69f1958 (patch)
tree15e8f2cc4512130efe83ed532deff53f801f6df3 /src/conn_linux.go
parentMoved endpoint into interface and simplified peer (diff)
downloadwireguard-go-a79fdc13a2d7be07b20ea499da9210ebe69f1958.tar.xz
wireguard-go-a79fdc13a2d7be07b20ea499da9210ebe69f1958.zip
Begin generic Bind implementation
Diffstat (limited to 'src/conn_linux.go')
-rw-r--r--src/conn_linux.go75
1 files changed, 36 insertions, 39 deletions
diff --git a/src/conn_linux.go b/src/conn_linux.go
index 46f873f..cdba74f 100644
--- a/src/conn_linux.go
+++ b/src/conn_linux.go
@@ -50,11 +50,44 @@ func ntohs(val uint16) uint16 {
return binary.BigEndian.Uint16((*tmp)[:])
}
-func NewEndpoint() Endpoint {
- return &NativeEndpoint{}
+func CreateEndpoint(s string) (Endpoint, error) {
+ var end NativeEndpoint
+ addr, err := parseEndpoint(s)
+ if err != nil {
+ return nil, err
+ }
+
+ ipv4 := addr.IP.To4()
+ if ipv4 != nil {
+ dst := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
+ dst.Family = unix.AF_INET
+ dst.Port = htons(uint16(addr.Port))
+ dst.Zero = [8]byte{}
+ copy(dst.Addr[:], ipv4)
+ end.ClearSrc()
+ return &end, nil
+ }
+
+ ipv6 := addr.IP.To16()
+ if ipv6 != nil {
+ zone, err := zoneToUint32(addr.Zone)
+ if err != nil {
+ return nil, err
+ }
+ dst := &end.dst
+ dst.Family = unix.AF_INET6
+ dst.Port = htons(uint16(addr.Port))
+ dst.Flowinfo = 0
+ dst.Scope_id = zone
+ copy(dst.Addr[:], ipv6[:])
+ end.ClearSrc()
+ return &end, nil
+ }
+
+ return nil, errors.New("Failed to recognize IP address format")
}
-func CreateUDPBind(port uint16) (Bind, uint16, error) {
+func CreateBind(port uint16) (Bind, uint16, error) {
var err error
var bind NativeBind
@@ -325,42 +358,6 @@ func create6(port uint16) (int, uint16, error) {
return fd, uint16(addr.Port), err
}
-func (end *NativeEndpoint) SetDst(s string) error {
- addr, err := parseEndpoint(s)
- if err != nil {
- return err
- }
-
- ipv4 := addr.IP.To4()
- if ipv4 != nil {
- dst := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
- dst.Family = unix.AF_INET
- dst.Port = htons(uint16(addr.Port))
- dst.Zero = [8]byte{}
- copy(dst.Addr[:], ipv4)
- end.ClearSrc()
- return nil
- }
-
- ipv6 := addr.IP.To16()
- if ipv6 != nil {
- zone, err := zoneToUint32(addr.Zone)
- if err != nil {
- return err
- }
- dst := &end.dst
- dst.Family = unix.AF_INET6
- dst.Port = htons(uint16(addr.Port))
- dst.Flowinfo = 0
- dst.Scope_id = zone
- copy(dst.Addr[:], ipv6[:])
- end.ClearSrc()
- return nil
- }
-
- return errors.New("Failed to recognize IP address format")
-}
-
func send6(sock int, end *NativeEndpoint, buff []byte) error {
// construct message header