github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/net_android_linux.go (about)

     1  //go:build android || linux
     2  // +build android linux
     3  
     4  /*
     5   * Copyright (c) 2020, Psiphon Inc.
     6   * All rights reserved.
     7   *
     8   * This program is free software: you can redistribute it and/or modify
     9   * it under the terms of the GNU General Public License as published by
    10   * the Free Software Foundation, either version 3 of the License, or
    11   * (at your option) any later version.
    12   *
    13   * This program is distributed in the hope that it will be useful,
    14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16   * GNU General Public License for more details.
    17   *
    18   * You should have received a copy of the GNU General Public License
    19   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20   *
    21   */
    22  
    23  package psiphon
    24  
    25  import (
    26  	"net"
    27  	"strconv"
    28  	"unsafe"
    29  
    30  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
    31  	"golang.org/x/net/bpf"
    32  	"golang.org/x/sys/unix"
    33  )
    34  
    35  func ClientBPFEnabled() bool {
    36  	return true
    37  }
    38  
    39  func setSocketBPF(BPFProgramInstructions []bpf.RawInstruction, socketFD int) error {
    40  
    41  	// Tactics parameters validation ensures BPFProgramInstructions has len >= 1.
    42  	err := unix.SetsockoptSockFprog(
    43  		socketFD,
    44  		unix.SOL_SOCKET,
    45  		unix.SO_ATTACH_FILTER,
    46  		&unix.SockFprog{
    47  			Len:    uint16(len(BPFProgramInstructions)),
    48  			Filter: (*unix.SockFilter)(unsafe.Pointer(&BPFProgramInstructions[0])),
    49  		})
    50  	return errors.Trace(err)
    51  }
    52  
    53  func setAdditionalSocketOptions(_ int) {
    54  }
    55  
    56  func makeLocalProxyListener(listenIP string, port int) (net.Listener, bool, error) {
    57  	listener, err := net.Listen("tcp", net.JoinHostPort(listenIP, strconv.Itoa(port)))
    58  	if err != nil {
    59  		return nil, IsAddressInUseError(err), errors.Trace(err)
    60  	}
    61  	return listener, false, nil
    62  }