net_android_linux.go 1.7 KB

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