|
| 1 | +// Copyright 2023 The Outline Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Use the following command line to generate a C header file: |
| 16 | +// go tool cgo -exportheader ./build/libtun2socks.h ./outline/electron/lib.go |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +/* |
| 21 | +#include <stdint.h> // for uint32_t |
| 22 | +*/ |
| 23 | +import "C" |
| 24 | + |
| 25 | +import ( |
| 26 | + "runtime/cgo" |
| 27 | + "unsafe" |
| 28 | + |
| 29 | + oss "github.com/Jigsaw-Code/outline-go-tun2socks/outline/shadowsocks" |
| 30 | +) |
| 31 | + |
| 32 | +// Function returns a tuple [status, pErr]. If pErr is nil (means no errors), |
| 33 | +// status is one of the following: |
| 34 | +// - 0 (NoError): can connect by TCP and UDP |
| 35 | +// - 4 (UDPConnectivity): can only connect by TCP (UDP failed) |
| 36 | +// - 3 (AuthenticationFailure): wrong server credentials |
| 37 | +// - 5 (Unreachable): server is not reachable at all |
| 38 | +// |
| 39 | +// Otherwise if pErr is not nil, it means there are unexpected errors (status |
| 40 | +// will be 1 (Unexpected)). |
| 41 | +// |
| 42 | +// The caller must call ReleaseError(pErr) later to make sure Go will garbage |
| 43 | +// collect the error object, otherwise memory leak will happen. |
| 44 | +// |
| 45 | +//export CheckConnectivity |
| 46 | +func CheckConnectivity() (status C.uint32_t, pErr unsafe.Pointer) { |
| 47 | + status = oss.Unexpected |
| 48 | + pErr = nil |
| 49 | + return |
| 50 | +} |
| 51 | + |
| 52 | +// If pErr points to an existing error object, this function will return pErr |
| 53 | +// object to Go's garbage collector. If pErr is nil, we will do nothing. |
| 54 | +// |
| 55 | +// In either case, the caller should not use pErr object any more. |
| 56 | +// |
| 57 | +//export ReleaseError |
| 58 | +func ReleaseError(pErr unsafe.Pointer) { |
| 59 | + p := (*cgo.Handle)(pErr) |
| 60 | + if p != nil { |
| 61 | + (*p).Delete() |
| 62 | + } |
| 63 | +} |
0 commit comments