summaryrefslogtreecommitdiffhomepage
path: root/app/tools/libwg-go/src/tun/api-android.go
blob: 4f0dd96830c9ef764f4f6fcf8975c5e5f45b5c58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* SPDX-License-Identifier: Apache-2.0
 *
 * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

package tun

import (
	"git.zx2c4.com/wireguard-go/rwcancel"
	"os"
)

func CreateTUNFromFD(tun_fd int) (TUNDevice, string, error) {
	file := os.NewFile(uintptr(tun_fd), "/dev/tun")
	tun := &nativeTun{
		tunFile: file,
		fd:      file.Fd(),
		events:  make(chan TUNEvent, 5),
		errors:  make(chan error, 5),
		nopi:    true,
	}
	var err error
	tun.fdCancel, err = rwcancel.NewRWCancel(tun_fd)
	if err != nil {
		return nil, "", err
	}
	name, err := tun.Name()
	if err != nil {
		tun.fdCancel.Cancel()
		return nil, "", err
	}
	return tun, name, nil
}