Removed cruft from project
This commit is contained in:
36
BUILDING.md
36
BUILDING.md
@@ -1,36 +0,0 @@
|
||||
## Build Instructions for various platforms
|
||||
|
||||
### Linux
|
||||
|
||||
- `make static_lib`
|
||||
- `make tests`
|
||||
|
||||
### macOS
|
||||
|
||||
- `make static_lib`
|
||||
- `make tests`
|
||||
|
||||
### iOS
|
||||
|
||||
- `make ios_framework`
|
||||
|
||||
### FreeBSD
|
||||
|
||||
- `make static_lib`
|
||||
- `make tests`
|
||||
|
||||
### Windows
|
||||
|
||||
- [Prebuilt Binaries\Libraries Here]()
|
||||
|
||||
Otherwise,
|
||||
|
||||
- Set up [mingw-w64](https://mingw-w64.org/doku.php)
|
||||
- Install a copy of [MSYS](http://www.mingw.org/wiki/MSYS)
|
||||
- Run `msys.bat` (should be in the base directory of your MSYS installation)
|
||||
- Tell it where your `mingw-64` installation is
|
||||
- `make static_lib`
|
||||
- `make tests`
|
||||
|
||||
This will output binaries to `build/mingw-w64`
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
# of your own application.
|
||||
#
|
||||
|
||||
cmake_minimum_required (VERSION 2.8)
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
project (libzt)
|
||||
|
||||
# --- SETUP
|
||||
@@ -54,9 +54,14 @@ if (WIN32)
|
||||
set (LWIP_PORT_DIR ${PROJ_DIR}/ext/lwip-contrib/ports/win32)
|
||||
set (COMMON_DEBUG_FLAGS "-DLIBZT_TRACE=1 -DLIBZT_DEBUG=1 -DNS_TRACE=1 -DNS_DEBUG=1")
|
||||
else()
|
||||
set (CMAKE_C_FLAGS "-fstack-protector -DZT_SDK=1")
|
||||
set (CMAKE_C_FLAGS "-fstack-protector -DLWIP_DEBUG=1 -DZT_SDK=1")
|
||||
set (LWIP_PORT_DIR ${PROJ_DIR}/ext/lwip-contrib/ports/unix/port)
|
||||
set (COMMON_DEBUG_FLAGS "-g -DLIBZT_TRACE=1 -DLIBZT_DEBUG=1 -DNS_TRACE=1 -DNS_DEBUG=1")
|
||||
set (COMMON_DEBUG_FLAGS "-g -DLIBZT_TRACE=1 -DLWIP_DEBUG=1 -DLIBZT_DEBUG=1 -DNS_TRACE=1 -DNS_DEBUG=1")
|
||||
endif()
|
||||
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Android")
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -DSOCKLEN_T_DEFINED=1")
|
||||
include_directories (/Users/joseph/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi)
|
||||
endif()
|
||||
|
||||
# Flags for various build types
|
||||
@@ -94,10 +99,9 @@ add_library (lwip STATIC ${lwip_src_glob})
|
||||
add_library (lwip_pic STATIC ${lwip_src_glob})
|
||||
set_target_properties (lwip_pic PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
|
||||
# zto
|
||||
# ZeroTier Core Service
|
||||
set (ZTO_SRC_DIR ${PROJ_DIR}/zto)
|
||||
set (ZTO_SRC_DIR ${PROJ_DIR}/ext/ZeroTierOne)
|
||||
include_directories (${ZTO_SRC_DIR}/include)
|
||||
include_directories (${ZTO_SRC_DIR}/osdep)
|
||||
include_directories (${ZTO_SRC_DIR}/node)
|
||||
@@ -135,7 +139,6 @@ include_directories ("${LIBZT_SRC_DIR}")
|
||||
include_directories ("${PROJ_DIR}/include")
|
||||
file (GLOB libzt_src_glob ${LIBZT_SRC_DIR}/*.cpp)
|
||||
add_library (zt ${libzt_src_glob})
|
||||
target_link_libraries (zt pthread lwip zto)
|
||||
if (WIN32)
|
||||
target_link_libraries (zto ws2_32)
|
||||
target_link_libraries (zto ${lshlwapi_LIBRARY_PATH})
|
||||
@@ -146,41 +149,47 @@ set_target_properties (zt PROPERTIES OUTPUT_NAME zt)
|
||||
add_library (ztshared SHARED ${libzt_src_glob})
|
||||
target_link_libraries (ztshared zto_pic lwip_pic)
|
||||
set_target_properties (ztshared PROPERTIES OUTPUT_NAME zt)
|
||||
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Android")
|
||||
target_link_libraries (zt lwip zto android log)
|
||||
else()
|
||||
target_link_libraries (zt pthread lwip zto)
|
||||
endif()
|
||||
|
||||
# --- Test applications, examples, etc ---
|
||||
|
||||
file (GLOB APP_SOURCES
|
||||
${PROJ_DIR}/examples/bindings/cpp/ipv4simple/*.cpp
|
||||
${PROJ_DIR}/examples/bindings/cpp/ipv6simple/*.cpp
|
||||
${PROJ_DIR}/examples/bindings/cpp/ipv6adhoc/*.cpp
|
||||
${PROJ_DIR}/examples/bindings/cpp/sharedlib/*.cpp
|
||||
${PROJ_DIR}/examples/apps/ztproxy/*.cpp
|
||||
)
|
||||
foreach (testsourcefile ${APP_SOURCES})
|
||||
string (REPLACE ".cpp" "" testname ${testsourcefile})
|
||||
get_filename_component (testname ${testname} NAME)
|
||||
add_executable (${testname} ${testsourcefile})
|
||||
if (WIN32)
|
||||
target_link_libraries (${testname} lwip zto zt)
|
||||
else()
|
||||
target_link_libraries (${testname} lwip zto zt pthread dl)
|
||||
endif()
|
||||
endforeach (testsourcefile ${APP_SOURCES})
|
||||
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Android")
|
||||
file (GLOB APP_SOURCES
|
||||
${PROJ_DIR}/examples/cpp/ipv4simple/*.cpp
|
||||
${PROJ_DIR}/examples/cpp/ipv6simple/*.cpp
|
||||
${PROJ_DIR}/examples/cpp/ipv6adhoc/*.cpp
|
||||
${PROJ_DIR}/examples/cpp/sharedlib/*.cpp
|
||||
${PROJ_DIR}/examples/ztproxy/*.cpp
|
||||
)
|
||||
foreach (testsourcefile ${APP_SOURCES})
|
||||
string (REPLACE ".cpp" "" testname ${testsourcefile})
|
||||
get_filename_component (testname ${testname} NAME)
|
||||
add_executable (${testname} ${testsourcefile})
|
||||
if (WIN32)
|
||||
target_link_libraries (${testname} lwip zto zt)
|
||||
else()
|
||||
target_link_libraries (${testname} lwip zto zt pthread dl)
|
||||
endif()
|
||||
endforeach (testsourcefile ${APP_SOURCES})
|
||||
|
||||
# selftest
|
||||
add_executable (selftest ${PROJ_DIR}/test/selftest.cpp)
|
||||
target_compile_options (selftest PRIVATE -D__SELFTEST__)
|
||||
if (WIN32)
|
||||
target_link_libraries (selftest lwip zto zt ${ws2_32_LIBRARY_PATH} ${lshlwapi_LIBRARY_PATH} ${liphlpapi_LIBRARY_PATH})
|
||||
else()
|
||||
target_link_libraries (selftest pthread lwip zto zt)
|
||||
endif()
|
||||
# selftest
|
||||
add_executable (selftest ${PROJ_DIR}/test/selftest.cpp)
|
||||
target_compile_options (selftest PRIVATE -D__SELFTEST__)
|
||||
if (WIN32)
|
||||
target_link_libraries (selftest lwip zto zt ${ws2_32_LIBRARY_PATH} ${lshlwapi_LIBRARY_PATH} ${liphlpapi_LIBRARY_PATH})
|
||||
else()
|
||||
target_link_libraries (selftest pthread lwip zto zt)
|
||||
endif()
|
||||
|
||||
# nativetest
|
||||
add_executable (nativetest ${PROJ_DIR}/test/selftest.cpp)
|
||||
target_compile_options (nativetest PRIVATE -D__NATIVETEST__)
|
||||
target_link_libraries (nativetest lwip zto zt)
|
||||
# nativetest
|
||||
add_executable (nativetest ${PROJ_DIR}/test/selftest.cpp)
|
||||
target_compile_options (nativetest PRIVATE -D__NATIVETEST__)
|
||||
target_link_libraries (nativetest lwip zto zt)
|
||||
endif()
|
||||
|
||||
# Clean up intermediate library targets
|
||||
#file (REMOVE ${LIBRARY_OUTPUT_PATH}/libhttp.a)
|
||||
@@ -197,7 +206,7 @@ target_link_libraries (nativetest lwip zto zt)
|
||||
|
||||
# --- CONFIGURATION
|
||||
|
||||
if (JNI EQUAL 1)
|
||||
if (JNI EQUAL 1 OR ${CMAKE_SYSTEM_NAME} MATCHES "Android")
|
||||
MESSAGE (STATUS "Looking for JNI headers")
|
||||
find_package (JNI)
|
||||
if (JNI_FOUND)
|
||||
|
||||
30
FAQ.md
30
FAQ.md
@@ -1,30 +0,0 @@
|
||||
## Frequently Asked Questions
|
||||
***
|
||||
|
||||
### Can I use this in my commercial or closed-source application or service?
|
||||
|
||||
Yes! - Just let us know, and we will work out a licensing scheme. You will need to build the library with the `lwIP` network stack. In the case that you're a non-profit, or are developing open source software, you can use this entirely for free and may choose either `picoTCP` or `lwIP` as your network stack.
|
||||
|
||||
### My application or service won't fully come online
|
||||
|
||||
In rare circumstances it can take a substantial amount of time for a libzt instance to come online and become reachable. In cases where it never seems to progress beyond this stage you should check to make sure there are no rogue processes on the machines using the same ZeroTier identity files, or no other instances on your ZeroTier network using said identity files. If this doesn't help. Contact us.
|
||||
|
||||
### How do I get debug output?
|
||||
|
||||
In order of relevance, enable the following: For `libzt`-specific debug output which basically includes the POSIX socket emulation layer and network stack *drivers*, enable `LIBZT_DEBUG=1`. For situations where you suspect that the problem might lie within the network stack itself, enable `NS_DEBUG=1`. You should also check out `include/libzt.h` for additional fine-grained debug options for each network stack. If you think that your problem is with the ZeroTier protocol itself, enable `ZT_DEBUG=1`. This will output what's happening in the ZeroTier core. Note, you can enable all of these at once if you're brave. Additionally, you can use the `LIBZT_SANITIZE=1` flag to build against the [AddressSanitization](https://github.com/google/sanitizers/wiki/AddressSanitizer) library.
|
||||
|
||||
### Versioning
|
||||
|
||||
Each version of libzt will include a specific (and unrelated) version of the ZeroTier core, and network stacks. For instance: Version 1.1.5 of libzt might include version 1.2.5 of ZeroTier, there is no connection between these versioning schemes. Additionally, an update to libzt doesn't necessarily translate to an update to the ZeroTier core.
|
||||
|
||||
### Code Quality and Standardization
|
||||
|
||||
Explicit coding style rules are specified in `test/vera++/libzt` and enforced periodically by running the source through `vera++`. Before each commit or merge into `master` one should run `test/selftest` and `test/nativetest` as specified in [TESTING.md](TESTING.md). Also, on the tabs vs. spaces debate, for this project we have chosen `tabs`.
|
||||
|
||||
### How do I run unit tests?
|
||||
|
||||
See: [TESTING.md](TESTING.md)
|
||||
|
||||
### I want to use a different network stack. How do I do this?
|
||||
|
||||
See [src/ExampleStackDriver.cpp](src/ExampleStackDriver.cpp)
|
||||
24
README.md
24
README.md
@@ -10,9 +10,7 @@ A library version of [ZeroTier](https://github.com/zerotier/ZeroTierOne), **libz
|
||||
|
||||
[](https://webchat.freenode.net/?channels=zerotier)
|
||||
|
||||
- Pre-Built binaries: [zerotier.com/download.shtml](https://zerotier.com/download.shtml?pk_campaign=github_libzt)
|
||||
- Windows DLL (x64) can be found here: [libzt_win_x64_1.1.5.zip](https://download.zerotier.com/dist/libzt_win_x64_1.1.5.zip)
|
||||
- Bindings for popular languages like [Scala](examples/bindings/scala), [Swift](examples/bindings/swift), [Java](examples/bindings/java), [Python](examples/bindings/python), etc. can be found [here](examples/bindings)
|
||||
- Bindings for popular languages like [Scala](examples/scala), [Swift](examples/swift), [Java](examples/java), [Python](examples/python), etc. can be found [here](examples/bindings)
|
||||
|
||||
***
|
||||
|
||||
@@ -56,7 +54,7 @@ int main()
|
||||
}
|
||||
```
|
||||
|
||||
For an example using only the [Virtual Layer 2](https://www.zerotier.com/manual.shtml#2_2?pk_campaign=github_libzt), see [test/layer2.cpp](test/layer2.cpp)
|
||||
For an example using only the [Virtual Layer 2](https://www.zerotier.com/manual.shtml#2_2?pk_campaign=github_libzt), see [examples/layer2](examples/layer2/layer2.cpp)
|
||||
|
||||
***
|
||||
|
||||
@@ -67,30 +65,20 @@ We recommend using [CMake](https://cmake.org/) for its extensive cross-platform
|
||||
```
|
||||
git submodule init
|
||||
git submodule update
|
||||
./ext/patch_submodules.sh
|
||||
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=DEBUG
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
Use `libzt.a` or `libzt.dylib` in your application. They are placed in `bin\lib`. Change `CMAKE_BUILD_TYPE` to `RELEASE` for a smaller and optmized build.
|
||||
|
||||
### Install
|
||||
|
||||
- Install: `make install`
|
||||
- Uninstall: `xargs rm < install_manifest.txt`
|
||||
Builds are placed in `bin\lib`. Use `libzt.a` or `libzt.dylib` in your application. Change `CMAKE_BUILD_TYPE` to `RELEASE` for a smaller and optimized build.
|
||||
|
||||
***
|
||||
|
||||
### Testing and Debugging
|
||||
- See [TESTING.md](TESTING.md)
|
||||
|
||||
|
||||
### Contributing
|
||||
|
||||
Please make pull requests against the `dev` branch. The `master` branch is release, and `edge` is for unstable and work in progress changes and is not likely to work.
|
||||
|
||||
### Commercial License
|
||||
- If you want a commercial license to use libzt in your product contact us directly via `contact@zerotier.com`.
|
||||
|
||||
### Adding a custom network stack
|
||||
- If you wish to use something other than lwIP or picoTCP, you can easily add your own API function calls in `src/libzt.cpp` or `src/VirtualSocket.cpp` depending on whether your stack's API exposes a POSIX-socket API or a raw API, respectively.
|
||||
|
||||
If you want a commercial license to use libzt in your product contact us directly via `contact@zerotier.com`.
|
||||
|
||||
|
||||
154
RELEASE-NOTES.md
154
RELEASE-NOTES.md
@@ -1,154 +0,0 @@
|
||||
Current version: 1.1.5
|
||||
***
|
||||
|
||||
### 2017-11-10 -- Version 1.1.6
|
||||
- [In progress]
|
||||
- API simplification
|
||||
- Removed protocol specific address getters
|
||||
- Changed how network IDs and node IDs are passed to the library (everything is now uint64_t)
|
||||
- Initial steps for C-- efforts
|
||||
- Removed classes for stack drivers
|
||||
- Removed namespaces
|
||||
- Names of key classes changed slightly to prevent collisions
|
||||
- Re-classification of Virtual Socket Layer (and associated stack drivers) as experimental
|
||||
- lwIP sequential socket API is now default
|
||||
- Slightly reworked lwIP interface setup code
|
||||
|
||||
### 2017-10-19 -- Version 1.1.5 (Current)
|
||||
|
||||
- Improvements in API consistency and coverage
|
||||
- Re-introduction of lwIP driver, also upgraded lwIP to 2.0.3
|
||||
- Added lwIP sequential driver (basically a wrapper), and downgraded previous raw driver to experimental
|
||||
- Various bug fixes for POSIX socket emulation layer
|
||||
- Rework of VirtualSocket lock scheme
|
||||
- Performance upgrades for RX and TX
|
||||
- Windows Support
|
||||
|
||||
### 2017-06-07 -- Version 1.1.4
|
||||
|
||||
- Library can now be run in IPV4 and IPV6 mode simultaneously
|
||||
|
||||
|
||||
### 2017-06-05 -- Version 1.1.3
|
||||
|
||||
- Updated picoTCP to 1.4.0
|
||||
- Improved selftest
|
||||
|
||||
|
||||
### 2017-05-30 -- Version 1.1.2
|
||||
|
||||
- Minor ZTO source update
|
||||
- Improved selftest
|
||||
- Improved documentation
|
||||
|
||||
|
||||
### 2017-05-04 -- Version 1.1.1
|
||||
|
||||
- Updated ZTO source to 1.2.4
|
||||
- Various bug fixes
|
||||
|
||||
|
||||
### 2017-05-02 -- Version 1.1.0
|
||||
|
||||
- Added new socket multiplexing logic
|
||||
- Significant restructuring of picoTCP stack driver
|
||||
|
||||
|
||||
### 2017-04-06 -- Version 1.0.0
|
||||
|
||||
- Refactored library structure
|
||||
- Dynamic loading of stack no longer required
|
||||
- lwIP Support dropped
|
||||
|
||||
|
||||
### 2017-03-28 -- Version 0.9.0
|
||||
|
||||
- Updated ZTO source for tptr feature
|
||||
|
||||
|
||||
### 2017-03-14 -- Version 0.8.0
|
||||
|
||||
- Updated ZTO source to 1.2.0
|
||||
- Various bug fixes
|
||||
|
||||
|
||||
### 2017-03-07 -- Version 0.7.0
|
||||
|
||||
- Updated ZTO source to 1.1.17
|
||||
- Updated documentation
|
||||
- Various bug fixes
|
||||
|
||||
|
||||
### 2016-11-21 -- Version 0.6.2
|
||||
|
||||
- Updated ZTO source for reliability
|
||||
|
||||
|
||||
### 2016-10-28 -- Version 0.6.1
|
||||
|
||||
- Stack driver support for IPv4 XOR IPv6
|
||||
|
||||
|
||||
### 2016-10-21 -- Version 0.6.0
|
||||
|
||||
- Updated ZTO source to 1.1.14
|
||||
|
||||
|
||||
### 2016-10-18 -- Version 0.5.0
|
||||
|
||||
- Implemented driver for picoTCP network stack
|
||||
|
||||
|
||||
### 2016-09-29 -- Version 0.4.2
|
||||
|
||||
- IPv6-related fixes
|
||||
|
||||
|
||||
### 2016-09-27 -- Version 0.4.1
|
||||
|
||||
- Added IPv6 support to lwIP stack driver
|
||||
|
||||
|
||||
### 2016-09-11 -- Version 0.4.0
|
||||
|
||||
- Upgraded to lwIP 2.0.0
|
||||
|
||||
|
||||
### 2016-09-07 -- Version 0.3.5
|
||||
|
||||
- Introduced prettier and less complex Swift API
|
||||
|
||||
|
||||
### 2016-09-06 -- Version 0.3.4
|
||||
|
||||
- Improved debugging traces
|
||||
|
||||
|
||||
### 2016-09-01 -- Version 0.3.3
|
||||
|
||||
- Improved API parameter naming consistency
|
||||
|
||||
|
||||
### 2016-08-17 -- Version 0.3.2
|
||||
|
||||
- Improved proxy server controls
|
||||
|
||||
|
||||
### 2016-08-15 -- Version 0.3.1
|
||||
|
||||
- API consistency improvements
|
||||
|
||||
|
||||
### 2016-07-25 -- Version 0.3.0
|
||||
|
||||
- Updated ZTO source to 1.1.4
|
||||
|
||||
|
||||
### 2016-07-20 -- Version 0.2.1
|
||||
|
||||
- Normalized zt_ API naming conventions
|
||||
|
||||
|
||||
### 2016-06-14 -- Version 0.2.0
|
||||
|
||||
- Old netcon project moved into its own repo
|
||||
123
TESTING.md
123
TESTING.md
@@ -1,123 +0,0 @@
|
||||
## Testing via [selftest.cpp](test/selftest.cpp)
|
||||
|
||||
### Step 1. Enabling debug output
|
||||
|
||||
- `make static_lib SDK_DEBUG=1`: For debugging libzt
|
||||
- `make static_lib ZT_DEBUG=1`: For debugging the ZeroTier core protocol (you usually won't need this)
|
||||
|
||||
### Step 2. Build the test programs:
|
||||
|
||||
- `make tests`
|
||||
|
||||
This will output `selftest` and `echotest` to `build/$PLATFORM/`
|
||||
|
||||
*Note, the following examples assume your testing environment is `linux`, you'll see this in the build output path. If this is not true, change it to `darwin`, `freebsd`, or `win` depending on what you're running.*
|
||||
|
||||
|
||||
### Step 3. Define your test configuration in `test/selftest.conf`:
|
||||
|
||||
This is essentially just a listing of libzt-based app identities and host machine identities. We will be conducting `library-to-remote-library`, `library-to-remote-host`, and `remote-host-to-library` tests over the network. For this reason we need to define who should be talking to who.
|
||||
|
||||
A simple test configutation might look like the following. This will create an `alice` and `bob` test personality, that is, we will run one instance of the library as a server (alice), and one instance of the `echotest` on the same host machine. `echotest` is merely a program to record timings for transmitted data and also generate data for the library to receive). Additionally we will be running a library as a client `bob` on another remote host as well as another instance of `echotest` on that same machine. In this configuration the following will happen:
|
||||
|
||||
- `alice` libzt will tx/rx to/from `bob` libzt
|
||||
- `alice` libzt will send X bytes to `bob`'s `echotest` to test maximum TX rate
|
||||
- `alice` libzt will request X bytes from `bob`'s `echotest` to test maximum RX rate
|
||||
- `bob` libzt will send X bytes to `alice`'s `echotest` to test maximum TX rate
|
||||
- `bob` libzt will request X bytes from `alice`'s `echotest` to test maximum RX rate
|
||||
|
||||
```
|
||||
# Tests will use ports starting from 'port' to 'port+n' where 'n' is the number of tests
|
||||
|
||||
|
||||
# Alice
|
||||
name alice
|
||||
mode server
|
||||
nwid 17d7094b2c2c7319
|
||||
test comprehensive
|
||||
port 7000
|
||||
path test/alice
|
||||
ipv4 172.30.30.10
|
||||
ipv6 fd12:d719:4b6c:6c53:f799:13c4:07e0:abb8
|
||||
echo_ipv4 172.30.30.1
|
||||
echo_ipv6 fd11:d759:136e:2b53:6791:9328:31ce:618a
|
||||
;
|
||||
|
||||
# Bob
|
||||
name bob
|
||||
mode client
|
||||
nwid 17d7094b2c2c7319
|
||||
test comprehensive
|
||||
port 7000
|
||||
path test/bob
|
||||
ipv4 172.30.30.20
|
||||
ipv6 fd11:d759:136e:2b53:6791:9328:31ce:618a
|
||||
echo_ipv4 172.30.30.2
|
||||
echo_ipv6 fd12:d719:4b6c:6c53:f799:13c4:07e0:abb8
|
||||
```
|
||||
|
||||
|
||||
Build outputs are as follows:
|
||||
|
||||
```
|
||||
build
|
||||
|
|
||||
|--darwin
|
||||
| |-libzt.a
|
||||
| |-selftest
|
||||
| |-echotest
|
||||
|
|
||||
|--linux
|
||||
| |-libzt.a
|
||||
| |-selftest
|
||||
| |-echotest
|
||||
|
|
||||
|--freebsd
|
||||
| |-libzt.a
|
||||
| |-selftest
|
||||
| |-echotest
|
||||
|
|
||||
|--win
|
||||
|-libzt.a
|
||||
|-selftest
|
||||
|-echotest
|
||||
```
|
||||
|
||||
The self test will be performed over the network in the following configuration (addresses and ports are subject to change depending on what you define in your `test/*.conf` files):
|
||||

|
||||
|
||||
### Test sets:
|
||||
|
||||
- Test set A: Tests for correctness, error handling, blocking behaviour, on-system performance, etc
|
||||
- Test set B: Tests RX performance (from non-libzt app)
|
||||
- Test set C: Tests TX performance (to non-libzt app)
|
||||
|
||||
### Types of tests (defined in `selftest.cpp`)
|
||||
|
||||
#### Simple Tests:
|
||||
|
||||
- Simple tests merely test one aspect of the library. For instance, its role as an IPv4 server, or IPv6 client.
|
||||
|
||||
#### Sustained Tests
|
||||
|
||||
- Sustained tests will test the library's ability to support long-duration connections and data transfers.
|
||||
|
||||
#### Slam Tests
|
||||
|
||||
- Slam tests will test the library's ability to handle many repeated API calls or repeated common sequences of API calls that a typical application may make. For instance, it will try to create as many sockets as possible, or try to create a socket, bind to an address, listen, and accept over and over. This is useful for detecting memory leaks and architectural limitations in the stack drivers.
|
||||
|
||||
#### Comprehensive Tests
|
||||
|
||||
- A comprehensive test will test each aspect of the library one time.
|
||||
|
||||
#### Random Tests
|
||||
|
||||
- Makes random API calls with random (or plausible arguments/data) to test for proper error handling
|
||||
|
||||
#### Performance Tests
|
||||
|
||||
- Test's the library's performance characteristics
|
||||
|
||||
#### Correctness Tests
|
||||
|
||||
- Tests's the library's error handling, address treatment, and blocking/non-blocking behaviour.
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 9.4 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 27 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 4.1 KiB |
473
attic/Makefile
473
attic/Makefile
@@ -1,473 +0,0 @@
|
||||
#
|
||||
# ZeroTier SDK - Network Virtualization Everywhere
|
||||
# Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# --
|
||||
#
|
||||
# You can be released from the requirements of the license by purchasing
|
||||
# a commercial license. Buying such a license is mandatory as soon as you
|
||||
# develop commercial closed-source software that incorporates or links
|
||||
# directly against ZeroTier software without disclosing the source code
|
||||
# of your own application.
|
||||
#
|
||||
|
||||
# Automagically pick clang or gcc, with preference for clang
|
||||
# This is only done if we have not overridden these with an environment or CLI variable
|
||||
ifeq ($(origin CC),default)
|
||||
CC=$(shell if [ -e /usr/bin/clang ]; then echo clang; else echo gcc; fi)
|
||||
endif
|
||||
ifeq ($(origin CXX),default)
|
||||
CXX=$(shell if [ -e /usr/bin/clang++ ]; then echo clang++; else echo g++; fi)
|
||||
endif
|
||||
|
||||
OSTYPE=$(shell uname -s | tr '[A-Z]' '[a-z]')
|
||||
BUILDPATH=build/$(OSTYPE)
|
||||
LIBPATH=lib
|
||||
LWIPCONTRIBDIR=ext/lwip-contrib
|
||||
|
||||
# default
|
||||
CFLAGS+=-Wall
|
||||
|
||||
# Windows
|
||||
ifeq ($(OSTYPE),mingw32_nt-6.2)
|
||||
ARTOOL=ar
|
||||
ARFLAGS=rcs
|
||||
|
||||
CC=gcc
|
||||
CXX=g++
|
||||
CFLAGS=
|
||||
CXXFLAGS+=-fpermissive -Wno-unknown-pragmas -Wno-pointer-arith -Wno-deprecated-declarations -Wno-conversion-null
|
||||
WINDEFS=-lws2_32 -lshlwapi -liphlpapi -static -static-libgcc -static-libstdc++
|
||||
LWIPARCHINCLUDE=$(LWIPCONTRIBDIR)/ports/win32/include
|
||||
STATIC_LIB=libzt.a
|
||||
SHARED_LIB=libzt.dll
|
||||
endif
|
||||
# Darwin
|
||||
ifeq ($(OSTYPE),darwin)
|
||||
CFLAGS+=-fvisibility=hidden -fstack-protector
|
||||
ARTOOL=libtool
|
||||
ARFLAGS=-static
|
||||
LWIPARCHINCLUDE=$(LWIPCONTRIBDIR)/ports/unix/include
|
||||
STATIC_LIB=libzt.a
|
||||
SHARED_LIB=libzt.dylib
|
||||
endif
|
||||
# Linux
|
||||
ifeq ($(OSTYPE),linux)
|
||||
CFLAGS+=-fvisibility=hidden -fstack-protector
|
||||
ARTOOL=ar
|
||||
ARFLAGS=rcs
|
||||
LWIPARCHINCLUDE=$(LWIPCONTRIBDIR)/ports/unix/include
|
||||
STATIC_LIB=libzt.a
|
||||
SHARED_LIB=libzt.so
|
||||
endif
|
||||
# FreeBSD
|
||||
ifeq ($(OSTYPE),freebsd)
|
||||
CFLAGS+=-fvisibility=hidden -fstack-protector
|
||||
ARTOOL=ar
|
||||
ARFLAGS=rcs
|
||||
LWIPARCHINCLUDE=$(LWIPCONTRIBDIR)/ports/unix/include
|
||||
STATIC_LIB=libzt.a
|
||||
SHARED_LIB=libzt.so
|
||||
endif
|
||||
# OpenBSD
|
||||
ifeq ($(OSTYPE),openbsd)
|
||||
CFLAGS+=-fvisibility=hidden -fstack-protector
|
||||
ARTOOL=ar
|
||||
ARFLAGS=rcs
|
||||
LWIPARCHINCLUDE=$(LWIPCONTRIBDIR)/ports/unix/include
|
||||
STATIC_LIB=libzt.a
|
||||
SHARED_LIB=libzt.so
|
||||
endif
|
||||
|
||||
##############################################################################
|
||||
## Objects and includes ##
|
||||
##############################################################################
|
||||
|
||||
include zto/objects.mk
|
||||
|
||||
ZTO_OBJS=\
|
||||
zto/node/C25519.o \
|
||||
zto/node/Capability.o \
|
||||
zto/node/CertificateOfMembership.o \
|
||||
zto/node/CertificateOfOwnership.o \
|
||||
zto/node/Identity.o \
|
||||
zto/node/IncomingPacket.o \
|
||||
zto/node/InetAddress.o \
|
||||
zto/node/Membership.o \
|
||||
zto/node/Multicaster.o \
|
||||
zto/node/Network.o \
|
||||
zto/node/NetworkConfig.o \
|
||||
zto/node/Node.o \
|
||||
zto/node/OutboundMulticast.o \
|
||||
zto/node/Packet.o \
|
||||
zto/node/Path.o \
|
||||
zto/node/Peer.o \
|
||||
zto/node/Poly1305.o \
|
||||
zto/node/Revocation.o \
|
||||
zto/node/Salsa20.o \
|
||||
zto/node/SelfAwareness.o \
|
||||
zto/node/SHA512.o \
|
||||
zto/node/Switch.o \
|
||||
zto/node/Tag.o \
|
||||
zto/node/Topology.o \
|
||||
zto/node/Trace.o \
|
||||
zto/node/Utils.o \
|
||||
zto/controller/EmbeddedNetworkController.o \
|
||||
zto/controller/DB.o \
|
||||
zto/controller/FileDB.o \
|
||||
zto/osdep/ManagedRoute.o \
|
||||
zto/osdep/Http.o \
|
||||
zto/osdep/OSUtils.o \
|
||||
zto/service/SoftwareUpdater.o \
|
||||
zto/service/OneService.o \
|
||||
zto/ext/http-parser/http_parser.o
|
||||
|
||||
ZT_INCLUDES+=-Iext \
|
||||
-Izto/osdep \
|
||||
-Izto/node \
|
||||
-Izto/service \
|
||||
-Izto/include \
|
||||
|
||||
LIBZT_INCLUDES+=-Iinclude \
|
||||
-I../include \
|
||||
-Isrc
|
||||
|
||||
##############################################################################
|
||||
## General Build Configuration ##
|
||||
##############################################################################
|
||||
|
||||
# ZT_ Configuration options for ZeroTier core
|
||||
# LIBZT_ Configuration options for libzt
|
||||
# NS_ Configuration options for userspace network stack
|
||||
|
||||
STRIP=strip
|
||||
# Determine if PIC is needed for the intended build target
|
||||
ifeq ($(MAKECMDGOALS),static_lib)
|
||||
CFLAGS?=
|
||||
#-fPIE
|
||||
endif
|
||||
ifeq ($(MAKECMDGOALS),shared_lib)
|
||||
CFLAGS?=
|
||||
#-fPIC
|
||||
endif
|
||||
# ZeroTier core debug and tracing
|
||||
ifeq ($(ZT_DEBUG),1)
|
||||
CFLAGS?=-g
|
||||
ZT_DEFS?=-DZT_TRACE=1
|
||||
STRIP=echo
|
||||
else
|
||||
CFLAGS?=-Ofast
|
||||
endif
|
||||
# For consistency. ZT_DEBUG=1 will also turn this on
|
||||
ifeq ($(ZT_TRACE),1)
|
||||
ZT_DEFS?=-DZT_TRACE
|
||||
endif
|
||||
# Don't optimize, add debug symbols
|
||||
ifeq ($(LIBZT_DEBUG),1)
|
||||
CFLAGS?=-g
|
||||
STRIP=echo
|
||||
else
|
||||
CFLAGS?=-Ofast
|
||||
endif
|
||||
# Turns on file/function/line debug output logging
|
||||
ifeq ($(LIBZT_TRACE),1)
|
||||
LIBZT_DEFS?=-DLIBZT_TRACE
|
||||
endif
|
||||
# Experimental stack drivers which interface via raw API's
|
||||
ifeq ($(LIBZT_RAW),1)
|
||||
LIBZT_DEFS?=-DLIBZT_RAW=1
|
||||
endif
|
||||
# Debug the userspace stack
|
||||
ifeq ($(NS_DEBUG),1)
|
||||
CFLAGS?=-g
|
||||
STRIP=echo
|
||||
endif
|
||||
|
||||
# Build with address sanitization library for advanced debugging (clang)
|
||||
# TODO: Add GCC version as well
|
||||
ifeq ($(LIBZT_SANITIZE),1)
|
||||
SANFLAGS+=-x c++ -g -fsanitize=address -DASAN_OPTIONS=symbolize=1 \
|
||||
-DASAN_SYMBOLIZER_PATH=$(shell which llvm-symbolizer)
|
||||
endif
|
||||
|
||||
# JNI (Java Native Interface)
|
||||
ifeq ($(OSTYPE),darwin)
|
||||
JNI_INCLUDES+=-I$(shell /usr/libexec/java_home)/include
|
||||
JNI_INCLUDES+=-I$(shell /usr/libexec/java_home)/include/$(OSTYPE)
|
||||
endif
|
||||
ifeq ($(OSTYPE),linux)
|
||||
JNI_INCLUDES+=-I$(shell dirname $(shell dirname $(shell readlink -f $(shell which javac))))/include
|
||||
JNI_INCLUDES+=-I$(shell dirname $(shell dirname $(shell readlink -f $(shell which javac))))/include/$(OSTYPE)
|
||||
endif
|
||||
|
||||
CXXFLAGS+=$(CFLAGS) -Wno-format -fno-rtti -std=c++11
|
||||
ZT_DEFS+=-DZT_SDK -DZT_SOFTWARE_UPDATE_DEFAULT="\"disable\""
|
||||
LIBZT_FILES:=src/VirtualTap.cpp src/libzt.cpp src/Utilities.cpp
|
||||
|
||||
##############################################################################
|
||||
## Stack Configuration ##
|
||||
##############################################################################
|
||||
|
||||
#ifeq ($(NO_STACK),1)
|
||||
#STACK_DRIVER_DEFS+=-DNO_STACK
|
||||
#endif
|
||||
|
||||
ifeq ($(NS_DEBUG),1)
|
||||
STACK_DEFS+=LWIP_DEBUG=1
|
||||
endif
|
||||
|
||||
# picoTCP
|
||||
STACK_INCLUDES+=-Iext/picotcp/include -Iext/picotcp/build/include
|
||||
STACK_DRIVER_FILES:=src/picoTCP.cpp
|
||||
|
||||
# lwIP
|
||||
LWIPDIR=ext/lwip/src
|
||||
STACK_INCLUDES+=-I$(LWIPARCHINCLUDE) -Iext/lwip/src/include/lwip \
|
||||
-I$(LWIPDIR)/include \
|
||||
-I$(LWIPARCH)/include \
|
||||
-I$(LWIPDIR)/include/ipv4 \
|
||||
-I$(LWIPDIR) \
|
||||
-Iext
|
||||
STACK_DRIVER_FILES:=src/lwIP.cpp
|
||||
|
||||
##############################################################################
|
||||
## Targets ##
|
||||
##############################################################################
|
||||
|
||||
%.o : %.cpp
|
||||
@mkdir -p $(BUILDPATH) obj
|
||||
$(CXX) $(CXXFLAGS) $(STACK_DRIVER_DEFS) $(ZT_DEFS) \
|
||||
$(ZT_INCLUDES) $(LIBZT_INCLUDES) -c $^ -o obj/$(@F)
|
||||
|
||||
%.o : %.c
|
||||
@mkdir -p $(BUILDPATH) obj
|
||||
$(CC) $(CFLAGS) -c $^ -o obj/$(@F)
|
||||
|
||||
core:
|
||||
cd zto; make core
|
||||
mv zto/libzerotiercore.a $(BUILDPATH)
|
||||
|
||||
picotcp:
|
||||
cd ext/picotcp; make lib ARCH=shared IPV4=1 IPV6=1
|
||||
|
||||
lwip:
|
||||
echo $(STACK_DEFS)
|
||||
make -f make-liblwip.mk liblwip.a LIBZT_IPV4=1 IPV4=1
|
||||
|
||||
lwip_driver:
|
||||
$(CXX) $(CXXFLAGS) -c src/lwIP.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(STACK_INCLUDES) $(LIBZT_DEFS) $(LIBZT_INCLUDES) -DZT_DRIVER_MODULE
|
||||
|
||||
picotcp_driver:
|
||||
$(CXX) $(CXXFLAGS) -c src/picoTCP.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(STACK_INCLUDES) $(LIBZT_DEFS) $(LIBZT_INCLUDES) -DZT_DRIVER_MODULE
|
||||
|
||||
libzt_socket_layer:
|
||||
$(CXX) $(CXXFLAGS) -c src/VirtualSocket.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(LIBZT_DEFS)
|
||||
$(CXX) $(CXXFLAGS) -c src/VirtualSocketLayer.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(STACK_INCLUDES) $(LIBZT_INCLUDES) $(LIBZT_DEFS)
|
||||
$(CXX) $(CXXFLAGS) -c src/VirtualTap.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_DEFS) $(LIBZT_INCLUDES)
|
||||
$(CXX) $(CXXFLAGS) -c src/ZT1Service.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(LIBZT_DEFS)
|
||||
$(CXX) $(CXXFLAGS) -c src/libzt.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(STACK_INCLUDES) $(LIBZT_DEFS) $(LIBZT_INCLUDES)
|
||||
$(CXX) $(CXXFLAGS) -c src/RingBuffer.cpp $(LIBZT_INCLUDES)
|
||||
|
||||
jni_socket_wrapper:
|
||||
$(CXX) $(CXXFLAGS) -DSDK_JNI -c src/libztJNI.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(STACK_INCLUDES) $(JNI_INCLUDES) $(LIBZT_DEFS) $(LIBZT_INCLUDES)
|
||||
|
||||
utilities:
|
||||
$(CXX) $(CXXFLAGS) -c src/SysUtils.cpp \
|
||||
$(LIBZT_INCLUDES)
|
||||
$(CXX) $(CXXFLAGS) -c src/Utilities.cpp \
|
||||
$(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(STACK_INCLUDES)
|
||||
|
||||
remove_objs:
|
||||
rm -rf *.o
|
||||
|
||||
prereqs: remove_objs lwip lwip_driver libzt_socket_layer jni_socket_wrapper utilities
|
||||
|
||||
static_lib: prereqs $(ZTO_OBJS)
|
||||
mv *.o obj
|
||||
mkdir -p lib
|
||||
$(ARTOOL) $(ARFLAGS) obj/*.o -o $(LIBPATH)/$(STATIC_LIB)
|
||||
|
||||
shared_lib: prereqs $(ZTO_OBJS)
|
||||
mv *.o obj
|
||||
mkdir -p lib
|
||||
$(CXX) $(CXXFLAGS) obj/*.o $(SOLIBTYPE) -o $(LIBPATH)/$(SHARED_LIB) -lpthread
|
||||
|
||||
dynamic_lib: prereqs $(ZTO_OBJS)
|
||||
|
||||
ifdef JNI
|
||||
ZT_DEFS+=-DSDK_JNI
|
||||
SOLIBTYPE=-shared
|
||||
else
|
||||
SOLIBTYPE=-shared
|
||||
# user dynamiclib for macOS
|
||||
endif
|
||||
ifdef STATIC
|
||||
CFLAGS+=
|
||||
#-fPIE
|
||||
lib: static_lib
|
||||
endif
|
||||
ifdef SHARED
|
||||
CFLAGS+=
|
||||
#-fPIC
|
||||
lib: shared_lib
|
||||
endif
|
||||
|
||||
# windows DLL
|
||||
win_dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
|
||||
# First we use mingw to build our DLL
|
||||
@mkdir -p $(BUILDPATH) obj
|
||||
mv *.o obj
|
||||
windres -i res/libztdll.rc -o obj/libztdllres.o
|
||||
$(CXX) $(CXXFLAGS) -shared -o $(BUILDPATH)/libzt.dll obj/*.o -Wl,--output-def,$(BUILDPATH)/libzt.def,--out-implib,$(BUILDPATH)/libzt.a $(WINDEFS)
|
||||
$(STRIP) $(BUILDPATH)/libzt.dll
|
||||
# Then do the following to generate the MSVC DLL from the def file (which was generated from the MinGW DLL):
|
||||
# lib /machine:x64 /def:libzt.def
|
||||
# or just execute: makelib
|
||||
|
||||
# ordinary shared library
|
||||
#shared_lib: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
|
||||
# @mkdir -p $(BUILDPATH) obj
|
||||
# mv *.o obj
|
||||
# $(CXX) $(CXXFLAGS) -shared -o $(BUILDPATH)/libzt.so obj/*.o
|
||||
|
||||
# dynamic library for use with Java JNI, scala, etc
|
||||
#shared_jni_lib: lwip lwip_driver libzt_socket_layer jni_socket_wrapper utilities $(ZTO_OBJS)
|
||||
# @mkdir -p $(BUILDPATH) obj
|
||||
# mv *.o obj
|
||||
#$(CXX) $(CXXFLAGS) -shared -o $(BUILDPATH)/libzt.so obj/*.o
|
||||
# $(CXX) $(CXXFLAGS) -dynamiclib obj/*.o -o $(BUILDPATH)/libzt.dylib -lpthread
|
||||
|
||||
# static library for use with Java JNI, scala, etc
|
||||
#static_jni_lib: lwip lwip_driver libzt_socket_layer jni_socket_wrapper utilities $(ZTO_OBJS)
|
||||
# @mkdir -p $(BUILDPATH) obj
|
||||
# mv *.o obj
|
||||
# $(ARTOOL) $(ARFLAGS) -o $(BUILDPATH)/$(STATIC_LIB) obj/*.o
|
||||
|
||||
# static library
|
||||
#static_lib: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
|
||||
# @mkdir -p $(BUILDPATH) obj
|
||||
# mv *.o obj
|
||||
# mv ext/picotcp/build/lib/*.o obj
|
||||
# mv ext/picotcp/build/modules/*.o obj
|
||||
# $(ARTOOL) $(ARFLAGS) -o $(BUILDPATH)/$(STATIC_LIB) obj/*.o
|
||||
|
||||
##############################################################################
|
||||
## iOS/macOS App Frameworks ##
|
||||
##############################################################################
|
||||
|
||||
ios_app_framework:
|
||||
cd examples/apple/ZeroTierSDK_Apple; xcodebuild -configuration Release \
|
||||
-scheme ZeroTierSDK_iOS build SYMROOT="../../../$(BUILDPATH)/ios_app_framework"
|
||||
cd examples/apple/ZeroTierSDK_Apple; xcodebuild -configuration Debug \
|
||||
-scheme ZeroTierSDK_iOS build SYMROOT="../../../$(BUILDPATH)/ios_app_framework"
|
||||
|
||||
macos_app_framework:
|
||||
cd examples/apple/ZeroTierSDK_Apple; xcodebuild -configuration Release \
|
||||
-scheme ZeroTierSDK_OSX build SYMROOT="../../../$(BUILDPATH)/macos_app_framework"
|
||||
cd examples/apple/ZeroTierSDK_Apple; xcodebuild -configuration Debug \
|
||||
-scheme ZeroTierSDK_OSX build SYMROOT="../../../$(BUILDPATH)/macos_app_framework"
|
||||
|
||||
##############################################################################
|
||||
## Python module ##
|
||||
##############################################################################
|
||||
|
||||
python_module:
|
||||
swig -cpperraswarn -python -c++ -o examples/python/libzt.cc examples/python/swig_libzt.i
|
||||
python examples/python/setup.py build_ext --inplace --swig-opts="-modern -I../../zto/include"
|
||||
|
||||
##############################################################################
|
||||
## Unit Tests ##
|
||||
##############################################################################
|
||||
|
||||
tests: selftest nativetest ztproxy ipv4simple ipv6simple ipv6adhoc
|
||||
# intercept
|
||||
|
||||
ZT_UTILS:=zto/node/Utils.cpp -Izto/node
|
||||
|
||||
sample:
|
||||
$(CXX) $(CXXFLAGS) -D__SELFTEST__ $(STACK_DRIVER_DEFS) $(LIBZT_DEFS) \
|
||||
$(SANFLAGS) $(LIBZT_INCLUDES) $(ZT_INCLUDES) $(ZT_UTILS) test/sample.cpp -o \
|
||||
$(BUILDPATH)/sample -L$(LIBPATH) -lzt
|
||||
selftest:
|
||||
$(CXX) $(CXXFLAGS) -D__SELFTEST__ $(STACK_DRIVER_DEFS) $(LIBZT_DEFS) \
|
||||
$(SANFLAGS) $(LIBZT_INCLUDES) $(ZT_INCLUDES) $(ZT_UTILS) test/selftest.cpp -o \
|
||||
$(BUILDPATH)/selftest -L$(LIBPATH) -lzt -lpthread
|
||||
nativetest:
|
||||
$(CXX) $(CXXFLAGS) -D__NATIVETEST__ $(STACK_DRIVER_DEFS) $(SANFLAGS) \
|
||||
$(LIBZT_INCLUDES) $(ZT_INCLUDES) test/selftest.cpp -o $(BUILDPATH)/nativetest
|
||||
ztproxy:
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) $(ZT_INCLUDES) \
|
||||
examples/apps/ztproxy/ztproxy.cpp -o $(BUILDPATH)/ztproxy $< -L$(LIBPATH) -lzt -lpthread $(WINDEFS)
|
||||
intercept:
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(STACK_DRIVER_DEFS) $(LIBZT_INCLUDES) \
|
||||
$(ZT_INCLUDES) examples/intercept/intercept.cpp -D_GNU_SOURCE \
|
||||
-shared -o $(BUILDPATH)/intercept.so $< -ldl
|
||||
ipv4simple:
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv4simple/client.cpp -o $(BUILDPATH)/ipv4client -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv4simple/server.cpp -o $(BUILDPATH)/ipv4server -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
ipv6simple:
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv6simple/client.cpp -o $(BUILDPATH)/ipv6client -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv6simple/server.cpp -o $(BUILDPATH)/ipv6server -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
ipv6adhoc:
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv6adhoc/client.cpp -o $(BUILDPATH)/ipv6adhocclient -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
$(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) \
|
||||
examples/bindings/cpp/ipv6adhoc/server.cpp -o $(BUILDPATH)/ipv6adhocserver -L$(LIBPATH) -lpthread -lzt $(WINDEFS)
|
||||
dlltest:
|
||||
$(CXX) $(CXXFLAGS)
|
||||
|
||||
##############################################################################
|
||||
## Installation and Uninstallation ##
|
||||
##############################################################################
|
||||
|
||||
.PHONY: install
|
||||
install:
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/lib
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/include
|
||||
cp $(BUILDPATH)/$(STATIC_LIB) $(DESTDIR)$(PREFIX)/lib/
|
||||
cp include/libzt.h $(DESTDIR)$(PREFIX)/include/
|
||||
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(PREFIX)/*.a
|
||||
rm -f $(DESTDIR)$(PREFIX)/include/*.h
|
||||
|
||||
##############################################################################
|
||||
## Misc ##
|
||||
##############################################################################
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm f $(LIBPATH)/*
|
||||
-rm -rf $(BUILDPATH)/*
|
||||
-rm f obj/*
|
||||
-rm f *.o *.s *.exp *.lib .depend* *.core core
|
||||
-rm -rf .depend
|
||||
-find . -type f \( -name '*.a' -o -name '*.o' -o -name '*.so' -o -name \
|
||||
'*.o.d' -o -name '*.out' -o -name '*.log' -o -name '*.dSYM' \) -delete
|
||||
|
||||
16
check.sh
16
check.sh
@@ -1,16 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
red=`tput setaf 1`
|
||||
green=`tput setaf 2`
|
||||
reset=`tput sgr0`
|
||||
|
||||
FILE="$1"
|
||||
|
||||
if ([ -f "$FILE" ] && [ -s "$FILE" ]) || ([ -d "$FILE" ] && [ "$(ls -A "$FILE")" ]);
|
||||
then
|
||||
echo "${green}[OK ]${reset} $FILE"
|
||||
exit 0
|
||||
else
|
||||
echo "${red}[FAIL]${reset} $FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 86 KiB |
@@ -41,14 +41,6 @@
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Returns the thread-id. Used in debug traces.
|
||||
*
|
||||
* @usage For internal use only.
|
||||
* @return
|
||||
*/
|
||||
inline unsigned int gettid();
|
||||
|
||||
/**
|
||||
* @brief Current time in milliseconds since epoch, platform-aware convenience function.
|
||||
*
|
||||
|
||||
@@ -86,7 +86,7 @@ public:
|
||||
/**
|
||||
* Registers a device with the given address
|
||||
*/
|
||||
bool registerIpWithStack(const ZeroTier::InetAddress &ip);
|
||||
void registerIpWithStack(const ZeroTier::InetAddress &ip);
|
||||
|
||||
/**
|
||||
* Adds an address to the userspace stack interface associated with this VirtualTap
|
||||
@@ -157,72 +157,14 @@ public:
|
||||
void (*_handler)(void *, void *, uint64_t, const ZeroTier::MAC &, const ZeroTier::MAC &, unsigned int, unsigned int,
|
||||
const void *, unsigned int);
|
||||
|
||||
/**
|
||||
* Signals us to close the TcpVirtualSocket associated with this PhySocket
|
||||
*/
|
||||
void phyOnUnixClose(ZeroTier::PhySocket *sock, void **uptr);
|
||||
|
||||
/**
|
||||
* Notifies us that there is data to be read from an application's socket
|
||||
*/
|
||||
void phyOnUnixData(ZeroTier::PhySocket *sock, void **uptr, void *data, ssize_t len);
|
||||
|
||||
/**
|
||||
* Notifies us that we can write to an application's socket
|
||||
*/
|
||||
void phyOnUnixWritable(ZeroTier::PhySocket *sock, void **uptr, bool stack_invoked);
|
||||
|
||||
/**
|
||||
* Adds a route to the virtual tap
|
||||
*/
|
||||
bool routeAdd(const ZeroTier::InetAddress &ip, const ZeroTier::InetAddress &nm, const ZeroTier::InetAddress &gw);
|
||||
|
||||
/**
|
||||
* Deletes a route from the virtual tap
|
||||
*/
|
||||
bool routeDelete(const ZeroTier::InetAddress &ip, const ZeroTier::InetAddress &nm);
|
||||
|
||||
/**
|
||||
* Assign a VirtualSocket to the VirtualTap
|
||||
*/
|
||||
void addVirtualSocket(VirtualSocket *vs);
|
||||
|
||||
/**
|
||||
* Remove a VirtualSocket from the VirtualTap
|
||||
*/
|
||||
void removeVirtualSocket();
|
||||
|
||||
/****************************************************************************/
|
||||
/* DNS */
|
||||
/****************************************************************************/
|
||||
|
||||
/**
|
||||
* Registers a DNS nameserver with the network stack
|
||||
*/
|
||||
int add_DNS_Nameserver(struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* Un-registers a DNS nameserver from the network stack
|
||||
*/
|
||||
int del_DNS_Nameserver(struct sockaddr *addr);
|
||||
|
||||
/****************************************************************************/
|
||||
/* Vars */
|
||||
/****************************************************************************/
|
||||
|
||||
#if defined(STACK_PICO)
|
||||
bool should_start_stack = false;
|
||||
struct pico_device *picodev = NULL;
|
||||
|
||||
/****************************************************************************/
|
||||
/* Guarded RX Frame Buffer for picoTCP */
|
||||
/****************************************************************************/
|
||||
|
||||
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
|
||||
int pico_frame_rxbuf_tot = 0;
|
||||
Mutex _pico_frame_rxbuf_m;
|
||||
#endif
|
||||
|
||||
std::vector<std::pair<ZeroTier::InetAddress, ZeroTier::InetAddress> > routes;
|
||||
void *zt1ServiceRef = NULL;
|
||||
|
||||
@@ -261,56 +203,6 @@ public:
|
||||
*/
|
||||
uint64_t last_housekeeping_ts = 0;
|
||||
|
||||
/****************************************************************************/
|
||||
/* In these, we will call the stack's corresponding functions, this is */
|
||||
/* where one would put logic to select between different stacks */
|
||||
/****************************************************************************/
|
||||
|
||||
/**
|
||||
* Connect to a remote host via the userspace stack interface associated with this VirtualTap
|
||||
*/
|
||||
int Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Bind to the userspace stack interface associated with this VirtualTap
|
||||
*/
|
||||
int Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Listen for a VirtualSocket
|
||||
*/
|
||||
int Listen(VirtualSocket *vs, int backlog);
|
||||
|
||||
/**
|
||||
* Accepts an incoming VirtualSocket
|
||||
*/
|
||||
VirtualSocket* Accept(VirtualSocket *vs);
|
||||
|
||||
/**
|
||||
* Move data from RX buffer to application's "socket"
|
||||
*/
|
||||
int Read(VirtualSocket *vs, PhySocket *sock, void **uptr, bool stack_invoked);
|
||||
|
||||
/**
|
||||
* Move data from application's "socket" into network stack
|
||||
*/
|
||||
int Write(VirtualSocket *vs, void *data, ssize_t len);
|
||||
|
||||
/**
|
||||
* Send data to specified host
|
||||
*/
|
||||
int SendTo(VirtualSocket *vs, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Closes a VirtualSocket
|
||||
*/
|
||||
int Close(VirtualSocket *vs);
|
||||
|
||||
/**
|
||||
* Shuts down some aspect of a VirtualSocket
|
||||
*/
|
||||
int Shutdown(VirtualSocket *vs, int how);
|
||||
|
||||
/**
|
||||
* Disposes of previously-closed VirtualSockets
|
||||
*/
|
||||
|
||||
@@ -223,7 +223,7 @@ ZT_SOCKET_API int ZTCALL zts_get_num_assigned_addresses(const uint64_t nwid);
|
||||
* @return The number of addresses assigned
|
||||
*/
|
||||
ZT_SOCKET_API int ZTCALL zts_get_address_at_index(
|
||||
const uint64_t nwid, const int index, struct sockaddr_storage *addr);
|
||||
const uint64_t nwid, const int index, struct sockaddr *addr, socklen_t *addrlen);
|
||||
|
||||
/**
|
||||
* @brief Get IP address for this device on a given network
|
||||
@@ -231,7 +231,7 @@ ZT_SOCKET_API int ZTCALL zts_get_address_at_index(
|
||||
* @usage FIXME: Only returns first address found, good enough for most cases
|
||||
* @param nwid Network ID
|
||||
* @param addr Destination structure for address
|
||||
* @param address_family To designate what family of addresses we want to return. AF_INET or AF_INET6
|
||||
* @param addrlen size of destination address buffer, will be changed to size of returned address
|
||||
* @return 0 if an address was successfully found, -1 if failure
|
||||
*/
|
||||
ZT_SOCKET_API int ZTCALL zts_get_address(
|
||||
@@ -269,16 +269,6 @@ ZT_SOCKET_API void ZTCALL zts_get_rfc4193_addr(
|
||||
*/
|
||||
ZT_SOCKET_API unsigned long zts_get_peer_count();
|
||||
|
||||
/**
|
||||
* @brief Get the virtual address of a peer given its Node ID
|
||||
*
|
||||
* @usage Call this after zts_start() has succeeded
|
||||
* @param peer Returned peer address
|
||||
* @param nodeId Provided Node ID
|
||||
* @return
|
||||
*/
|
||||
ZT_SOCKET_API int ZTCALL zts_get_peer_address(char *peer, const uint64_t nodeId);
|
||||
|
||||
/****************************************************************************/
|
||||
/* POSIX-like socket API */
|
||||
/****************************************************************************/
|
||||
|
||||
@@ -75,13 +75,11 @@
|
||||
|
||||
#define ZT_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // short
|
||||
|
||||
extern unsigned int gettid();
|
||||
|
||||
#ifdef __linux__
|
||||
#define ZT_THREAD_ID syscall(SYS_gettid)
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
#define ZT_THREAD_ID (long)0 //(long)gettid()
|
||||
#define ZT_THREAD_ID (long)0
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
#define ZT_THREAD_ID (long)0
|
||||
@@ -99,8 +97,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_STACK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"STACK[%ld]: %17s:%5d:%20s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_STACK(fmt, ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#else
|
||||
@@ -115,8 +112,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_TEST(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"TEST : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_TEST(fmt, ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt "\n" \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#else
|
||||
@@ -132,8 +128,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_ERROR(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"ERROR: %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_ERROR(fmt, ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt "\n" \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#else
|
||||
@@ -149,8 +144,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_INFO(fmt, ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#else
|
||||
@@ -166,8 +160,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"TRANS: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_TRANS(fmt, ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt "\n" \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#else
|
||||
@@ -183,8 +176,7 @@ extern unsigned int gettid();
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"EXTRA: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#define DEBUG_EXTRA(fmt, ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt "\n" \
|
||||
ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__, (long)0)
|
||||
#else
|
||||
|
||||
@@ -33,87 +33,15 @@
|
||||
#ifndef LIBZT_DEFINES_H
|
||||
#define LIBZT_DEFINES_H
|
||||
|
||||
#define LIBZT_IPV4 1
|
||||
#define LIBZT_IPV6 1
|
||||
|
||||
/**
|
||||
* Default port that libzt will use to support all virtual communication
|
||||
*/
|
||||
#define LIBZT_DEFAULT_PORT 9994
|
||||
/**
|
||||
* Use ZeroTier Virtual Socket layer to abstract network stack raw API
|
||||
*/
|
||||
#define ZT_VIRTUAL_SOCKET 0
|
||||
/**
|
||||
* Use lwIP sockets API
|
||||
*/
|
||||
#define ZT_LWIP_SEQ_SOCKET 1
|
||||
/**
|
||||
* Use pico BSD socket API
|
||||
*/
|
||||
#define ZT_PICO_BSD_SOCKET 0
|
||||
|
||||
#define STACK_LWIP 1
|
||||
#define STACK_PICO 0
|
||||
#define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code)
|
||||
|
||||
/* sanity checks for userspace network stack and socket API layer choices
|
||||
|
||||
EX.
|
||||
zts_socket()
|
||||
1. ) ZT_VIRTUAL_SOCKET? -> virt_socket() --- Choose this if the default socket layer isn't doing what you need
|
||||
STACK_LWIP? -> raw lwip_ API
|
||||
STACK_PICO? -> raw pico_ API
|
||||
otherStack? -> raw API
|
||||
|
||||
2.) ZT_LWIP_SEQ_SOCKET? (default) -> lwip_socket() --- currently provides greatest safety and performance
|
||||
3.) ZT_PICO_BSD_SOCKET? -> pico_ socket API
|
||||
otherStack? -> other_stack_socket()
|
||||
|
||||
Default is: STACK_LWIP=1 ZT_LWIP_SEQ_SOCKET=1
|
||||
|
||||
*/
|
||||
|
||||
#if (STACK_LWIP+STACK_PICO) > 1
|
||||
#error "Multiple network stacks specified. Pick only one."
|
||||
#endif
|
||||
#if STACK_LWIP==0 && STACK_PICO==0 && NO_STACK==0
|
||||
#error "No network stacks specified and NO_STACK wasn't set. Pick one."
|
||||
#endif
|
||||
#if ZT_VIRTUAL_SOCKET==0 && ZT_LWIP_SEQ_SOCKET==0 && ZT_PICO_BSD_SOCKET==0
|
||||
#error "No socket handling layer specified. Pick one."
|
||||
#endif
|
||||
#if (ZT_VIRTUAL_SOCKET + ZT_LWIP_SEQ_SOCKET + ZT_PICO_BSD_SOCKET) > 1
|
||||
#error "Multiple socket handling layers specified. Pick only one."
|
||||
#endif
|
||||
#if ZT_LWIP_SEQ_SOCKET==1 && STACK_LWIP==0
|
||||
#error "ZT_LWIP_SEQ_SOCKET is selected as socket handling layer, but STACK_LWIP isn't set"
|
||||
#endif
|
||||
#if ZT_PICO_BSD_SOCKET==1 && STACK_PICO==0
|
||||
#error "ZT_PICO_BSD_SOCKET is selected as socket handling layer, but STACK_PICO isn't set"
|
||||
#endif
|
||||
|
||||
#if STACK_LWIP==1
|
||||
#undef STACK_PICO
|
||||
#undef NO_STACK
|
||||
#endif
|
||||
#if STACK_PICO==1
|
||||
#undef STACK_LWIP
|
||||
#undef NO_STACK
|
||||
#endif
|
||||
#if NO_STACK==1
|
||||
#undef STACK_LWIP
|
||||
#undef STACK_PICO
|
||||
#endif
|
||||
#if ZT_VIRTUAL_SOCKET==1
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#endif
|
||||
#if ZT_LWIP_SEQ_SOCKET==1
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#endif
|
||||
#if ZT_PICO_BSD_SOCKET==1
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#endif
|
||||
#define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code)
|
||||
|
||||
/**
|
||||
* Maximum MTU size for ZeroTier
|
||||
@@ -166,8 +94,6 @@ struct sockaddr_ll {
|
||||
|
||||
// For LWIP configuration see: include/lwipopts.h
|
||||
|
||||
#if defined(STACK_LWIP)
|
||||
|
||||
/* The following three quantities are related and govern how incoming frames are fed into the
|
||||
network stack's core:
|
||||
|
||||
@@ -208,7 +134,6 @@ typedef signed char err_t;
|
||||
#define LWIP_STATUS_TMR_INTERVAL 500
|
||||
|
||||
// #define LWIP_CHKSUM <your_checksum_routine>, See: RFC1071 for inspiration
|
||||
#endif
|
||||
|
||||
/****************************************************************************/
|
||||
/* Defines */
|
||||
@@ -259,11 +184,6 @@ typedef signed char err_t;
|
||||
*/
|
||||
#define ZT_API_CHECK_INTERVAL 50
|
||||
|
||||
/**
|
||||
* Maximum size of guarded RX buffer (for picoTCP raw driver only)
|
||||
*/
|
||||
#define MAX_PICO_FRAME_RX_BUF_SZ ZT_MAX_MTU * 128
|
||||
|
||||
/**
|
||||
* Size of TCP TX buffer for VirtualSockets used in raw network stack drivers
|
||||
*/
|
||||
@@ -384,4 +304,6 @@ typedef signed char err_t;
|
||||
#define ZT_IOCTL_SIG int fd, unsigned long request, void *argp
|
||||
#define ZT_SYSCALL_SIG long number, ...
|
||||
|
||||
#define LIBZT_SERVICE_NOT_STARTED_STR "service not started yet, call zts_startjoin()"
|
||||
|
||||
#endif // _H
|
||||
|
||||
149
include/lwIP.h
149
include/lwIP.h
@@ -34,8 +34,7 @@
|
||||
#define ZT_LWIP_HPP
|
||||
|
||||
#include "libztDefs.h"
|
||||
|
||||
#ifdef STACK_LWIP
|
||||
#include "lwip/err.h"
|
||||
|
||||
namespace ZeroTier {
|
||||
class MAC;
|
||||
@@ -43,8 +42,6 @@ namespace ZeroTier {
|
||||
struct InetAddress;
|
||||
}
|
||||
|
||||
//#include "lwip/err.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize network stack semaphores, threads, and timers.
|
||||
*
|
||||
@@ -105,146 +102,4 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p);
|
||||
void lwip_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC &to, unsigned int etherType,
|
||||
const void *data, unsigned int len);
|
||||
|
||||
/****************************************************************************/
|
||||
/* Raw API driver */
|
||||
/****************************************************************************/
|
||||
|
||||
#ifdef ZT_VIRTUAL_SOCKET
|
||||
|
||||
class VirtualSocket;
|
||||
|
||||
/**
|
||||
* Returns the number of TCP PCBs currently allocated
|
||||
*/
|
||||
int rd_lwip_num_current_tcp_pcbs();
|
||||
|
||||
/**
|
||||
* Returns the number of UDP PCBs currently allocated
|
||||
*/
|
||||
int rd_lwip_num_current_udp_pcbs();
|
||||
|
||||
/**
|
||||
* Returns the number of RAW PCBs currently allocated
|
||||
*/
|
||||
int rd_lwip_num_current_raw_pcbs();
|
||||
|
||||
/**
|
||||
* Returns the total number of PCBs of any time or state
|
||||
*/
|
||||
int rd_lwip_num_total_pcbs();
|
||||
|
||||
/**
|
||||
* Registers a DNS nameserver with the network stack
|
||||
*/
|
||||
int rd_lwip_add_dns_nameserver(struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* Un-registers a DNS nameserver from the network stack
|
||||
*/
|
||||
int rd_lwip_del_dns_nameserver(struct sockaddr *addr);
|
||||
|
||||
/**
|
||||
* Main stack loop
|
||||
*/
|
||||
void rd_lwip_loop(VirtualTap *tap);
|
||||
|
||||
/**
|
||||
* Creates a stack-specific "socket" or "VirtualSocket object"
|
||||
*/
|
||||
int rd_lwip_socket(void **pcb, int socket_family, int socket_type, int protocol);
|
||||
|
||||
/**
|
||||
* Connect to remote host via userspace network stack interface - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Bind to a userspace network stack interface - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_bind(VirtualTap *tap, VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Listen for incoming VirtualSockets - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_listen(VirtualSocket *vs, int backlog);
|
||||
|
||||
/**
|
||||
* Accept an incoming VirtualSocket - Called from VirtualTap
|
||||
*/
|
||||
VirtualSocket* rd_lwip_accept(VirtualSocket *vs);
|
||||
|
||||
/**
|
||||
* Read from RX buffer to application - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_read(VirtualSocket *vs, bool lwip_invoked);
|
||||
|
||||
/**
|
||||
* Write to userspace network stack - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_write(VirtualSocket *vs, void *data, ssize_t len);
|
||||
|
||||
/**
|
||||
* Close a VirtualSocket - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_close(VirtualSocket *vs);
|
||||
|
||||
/**
|
||||
* Shuts down some aspect of a VirtualSocket - Called from VirtualTap
|
||||
*/
|
||||
int rd_lwip_shutdown(VirtualSocket *vs, int how);
|
||||
|
||||
/**
|
||||
* Sets a property of a socket
|
||||
*/
|
||||
int rd_lwip_setsockopt(VirtualSocket *vs, int level, int optname, const void *optval, socklen_t optlen);
|
||||
|
||||
/**
|
||||
* Gets a property of a socket
|
||||
*/
|
||||
int rd_lwip_getsockopt(VirtualSocket *vs, int level, int optname, void *optval, socklen_t *optlen);
|
||||
|
||||
// --- Callbacks from network stack ---
|
||||
|
||||
#ifdef ZT_DRIVER_MODULE // only include these symbols if we're building the full driver
|
||||
|
||||
/**
|
||||
* Callback for handling received UDP packets (already processed by network stack)
|
||||
*/
|
||||
static err_t rd_lwip_cb_tcp_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
|
||||
|
||||
/**
|
||||
* Callback for handling accepted connection
|
||||
*/
|
||||
static err_t rd_lwip_cb_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
|
||||
|
||||
/**
|
||||
* Callback for handling received TCP packets (already processed by stack)
|
||||
*/
|
||||
static void rd_lwip_cb_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
|
||||
|
||||
/**
|
||||
* Callback for handling errors from within the network stack
|
||||
*/
|
||||
static void rd_lwip_cb_err(void *arg, err_t err);
|
||||
|
||||
/**
|
||||
* Callback for handling periodic background tasks
|
||||
*/
|
||||
static err_t rd_lwip_cb_poll(void* arg, struct tcp_pcb *PCB);
|
||||
|
||||
/**
|
||||
* Callback for handling confirmation of sent packets
|
||||
*/
|
||||
static err_t rd_lwip_cb_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
|
||||
|
||||
/**
|
||||
* Callback for handling successful connections
|
||||
*/
|
||||
static err_t rd_lwip_cb_connected(void *arg, struct tcp_pcb *PCB, err_t err);
|
||||
|
||||
#endif // ZT_DRIVER_MODULE
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ZT_VIRTUAL_SOCKET
|
||||
#endif // STACK_LWIP
|
||||
#endif // _H
|
||||
|
||||
@@ -44,7 +44,12 @@
|
||||
/*
|
||||
* Provides its own errno
|
||||
*/
|
||||
|
||||
#if __ANDROID__
|
||||
#define LWIP_PROVIDE_ERRNO 0
|
||||
#else
|
||||
#define LWIP_PROVIDE_ERRNO 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Provides core locking machinery
|
||||
@@ -56,6 +61,7 @@
|
||||
*/
|
||||
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
|
||||
|
||||
#define LWIP_NOASSERT 1
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@@ -70,8 +76,10 @@
|
||||
/*
|
||||
* Provides network/host byte transformation macros
|
||||
*/
|
||||
#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS 1
|
||||
|
||||
#if __ANDROID__
|
||||
#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Include user defined options first. Anything not defined in these files
|
||||
@@ -80,8 +88,8 @@
|
||||
#include "lwip/debug.h"
|
||||
|
||||
// IP Protocol version
|
||||
#define LWIP_IPV6 1
|
||||
#define LWIP_IPV4 1
|
||||
#define LWIP_IPV6 1
|
||||
|
||||
// --- DEBUG ---
|
||||
|
||||
@@ -115,7 +123,7 @@
|
||||
// interfaces
|
||||
#define SLIP_DEBUG LWIP_DBG_OFF
|
||||
#define NETIF_DEBUG LWIP_DBG_OFF
|
||||
// API (not used in libzt)
|
||||
// API
|
||||
#define API_LIB_DEBUG LWIP_DBG_OFF
|
||||
#define API_MSG_DEBUG LWIP_DBG_OFF
|
||||
#define SOCKETS_DEBUG LWIP_DBG_OFF
|
||||
@@ -474,7 +482,7 @@ happening sooner than they should.
|
||||
/**
|
||||
* LWIP_DHCP==1: Enable DHCP module.
|
||||
*/
|
||||
#define LWIP_DHCP 1
|
||||
#define LWIP_DHCP 0
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
|
||||
105
make-liblwip.mk
105
make-liblwip.mk
@@ -1,105 +0,0 @@
|
||||
|
||||
|
||||
#
|
||||
# Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
# 3. The name of the author may not be used to endorse or promote products
|
||||
# derived from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
# SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
# OF SUCH DAMAGE.
|
||||
#
|
||||
# This file is part of the lwIP TCP/IP stack.
|
||||
#
|
||||
# Author: Adam Dunkels <adam@sics.se>
|
||||
#
|
||||
|
||||
CONTRIBDIR=ext/lwip-contrib
|
||||
LWIPDIR=ext/lwip/src
|
||||
|
||||
# Automagically pick clang or gcc, with preference for clang
|
||||
# This is only done if we have not overridden these with an environment or CLI variable
|
||||
ifeq ($(origin CC),default)
|
||||
CC=$(shell if [ -e /usr/bin/clang ]; then echo clang; else echo gcc; fi)
|
||||
endif
|
||||
|
||||
OSTYPE=$(shell uname -s | tr '[A-Z]' '[a-z]')
|
||||
BUILD=build/$(OSTYPE)
|
||||
CCDEP=$(CC)
|
||||
|
||||
# Windows
|
||||
ifeq ($(OSTYPE),mingw32_nt-6.2)
|
||||
CC=gcc
|
||||
WINDEFS=-Wno-c++11-compat -std=c++98
|
||||
LWIPARCH=$(CONTRIBDIR)/ports/win32
|
||||
endif
|
||||
ifeq ($(OSTYPE),linux)
|
||||
LWIPARCH=$(CONTRIBDIR)/ports/unix
|
||||
CFLAGS+=-nostdlib
|
||||
endif
|
||||
ifeq ($(OSTYPE),darwin)
|
||||
LWIPARCH=$(CONTRIBDIR)/ports/unix
|
||||
endif
|
||||
ifeq ($(OSTYPE),freebsd)
|
||||
LWIPARCH=$(CONTRIBDIR)/ports/unix
|
||||
endif
|
||||
|
||||
LWIPINCLUDES:=-I$(LWIPDIR)/include -I$(LWIPARCH) -I$(LWIPARCH)/include -I$(LWIPDIR) -I. -Iext -Iinclude
|
||||
CFLAGS+=$(WINDEFS) -Wno-format -Wno-deprecated -O3 -g -Wall -fPIC $(LWIPINCLUDES)
|
||||
|
||||
ifeq ($(NS_DEBUG),1)
|
||||
CFLAGS+=-DLWIP_DEBUG=1
|
||||
endif
|
||||
#ifeq ($(IPV4),1)
|
||||
CFLAGS+=-DLWIP_IPV4=1 -DIPv4
|
||||
#endif
|
||||
#ifeq ($(IPV6),1)
|
||||
CFLAGS+=-DLWIP_IPV6=1 -DIPv6
|
||||
#endif
|
||||
|
||||
UNIXLIB=liblwip.a
|
||||
|
||||
all: $(UNIXLIB)
|
||||
.PHONY: all
|
||||
|
||||
include $(LWIPDIR)/Filelists.mk
|
||||
|
||||
# ARCHFILES: Architecture specific files.
|
||||
ARCHFILES=$(wildcard $(LWIPARCH)/port/*.c $(LWIPARCH)/*.c $(LWIPARCH)tapif.c $(LWIPARCH)/netif/list.c $(LWIPARCH)/netif/tcpdump.c)
|
||||
|
||||
LWIPNOAPPSFILES+=$(ARCHFILES)
|
||||
LWIPNOAPPSFILESW=$(wildcard $(LWIPNOAPPSFILES))
|
||||
LWIPNOAPPSOBJS=$(notdir $(LWIPNOAPPSFILESW:.c=.o))
|
||||
|
||||
%.o:
|
||||
$(CC) $(CFLAGS) -c $(<:.o=.c)
|
||||
|
||||
clean:
|
||||
rm -f *.o $(LWIPNOAPPSOBJS) *.s .depend* *.core core
|
||||
|
||||
depend dep: .depend
|
||||
|
||||
include .depend
|
||||
|
||||
$(UNIXLIB): $(LWIPNOAPPSOBJS)
|
||||
$(CC) $(CFLAGS) -g -nostartfiles -shared -o obj/$@ $^
|
||||
|
||||
.depend: $(LWIPNOAPPSFILES)
|
||||
$(CC) $(CFLAGS) -MM $^ > .depend || rm -f .depend
|
||||
@@ -1,2 +0,0 @@
|
||||
REM For conversion of MinGW DLL to MSVC lib
|
||||
lib /machine:x64 /def:build\mingw32_nt-6.2\libzt.def /OUT:build\mingw32_nt-6.2\libzt.lib
|
||||
@@ -1,36 +0,0 @@
|
||||
#define APSTUDIO_READONLY_SYMBOLS
|
||||
#include "windows.h"
|
||||
#undef APSTUDIO_READONLY_SYMBOLS
|
||||
|
||||
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
|
||||
#pragma code_page(1252)
|
||||
|
||||
VS_VERSION_INFO VERSIONINFO
|
||||
FILEVERSION 1,1,5,0
|
||||
PRODUCTVERSION 1,1,5,0
|
||||
FILEFLAGSMASK 0x3fL
|
||||
FILEFLAGS 0x0L
|
||||
FILEOS 0x40004L
|
||||
FILETYPE 0x1L
|
||||
FILESUBTYPE 0x0L
|
||||
BEGIN
|
||||
BLOCK "StringFileInfo"
|
||||
BEGIN
|
||||
BLOCK "080904b0"
|
||||
BEGIN
|
||||
VALUE "CompanyName", "ZeroTier\0"
|
||||
VALUE "FileDescription", "ZeroTier Socket API library\0"
|
||||
VALUE "FileVersion", "1.1.5.0\0"
|
||||
VALUE "InternalName", "my-real-file\0"
|
||||
VALUE "LegalCopyright", "Copyright (C) 2017 ZeroTier, Inc.\0"
|
||||
VALUE "OriginalFilename", "libzt.dll\0"
|
||||
VALUE "PrivateBuild", "820\0"
|
||||
VALUE "ProductName", "ZeroTier: libzt\0"
|
||||
VALUE "ProductVersion", "1.1.5.0\0"
|
||||
END
|
||||
END
|
||||
BLOCK "VarFileInfo"
|
||||
BEGIN
|
||||
VALUE "Translation", 0x809, 1200
|
||||
END
|
||||
END
|
||||
@@ -30,17 +30,6 @@
|
||||
* Platform-specific implementations of common functions
|
||||
*/
|
||||
|
||||
#if defined(STACK_LWIP)
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "dns.h"
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <sys/socket.h>
|
||||
#include <pthread.h>
|
||||
@@ -52,18 +41,4 @@
|
||||
#ifdef __linux__
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
inline unsigned int gettid()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
//return GetCurrentThreadId();
|
||||
return 0;
|
||||
#elif defined(__linux__)
|
||||
return static_cast<unsigned int>(syscall(__NR_gettid));
|
||||
#elif defined(__APPLE__)
|
||||
uint64_t tid64;
|
||||
pthread_threadid_np(0, &tid64);
|
||||
return static_cast<unsigned int>(tid64);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -40,18 +40,13 @@ typedef unsigned short u16_t;
|
||||
#include "libzt.h"
|
||||
#include "libztDebug.h"
|
||||
#include "lwIP.h"
|
||||
#include "picoTCP.h"
|
||||
#include "ZT1Service.h"
|
||||
#include "VirtualSocket.h"
|
||||
#include "VirtualSocketLayer.h"
|
||||
#include "SysUtils.h"
|
||||
|
||||
#include "Mutex.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "OneService.hpp"
|
||||
|
||||
//class VirtualSocket;
|
||||
|
||||
int VirtualTap::devno = 0;
|
||||
|
||||
VirtualTap::VirtualTap(
|
||||
@@ -87,11 +82,8 @@ VirtualTap::VirtualTap(
|
||||
// set virtual tap interface name (abbreviated)
|
||||
memset(vtap_abbr_name, 0, sizeof(vtap_abbr_name));
|
||||
snprintf(vtap_abbr_name, sizeof(vtap_abbr_name), "libzt%d", devno);
|
||||
#if defined(STACK_LWIP)
|
||||
// initialize network stacks
|
||||
lwip_driver_init();
|
||||
#endif
|
||||
// start vtap thread and stack I/O loops
|
||||
// start virtual tap thread and stack I/O loops
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
@@ -113,44 +105,26 @@ bool VirtualTap::enabled() const
|
||||
return _enabled;
|
||||
}
|
||||
|
||||
bool VirtualTap::registerIpWithStack(const InetAddress &ip)
|
||||
void VirtualTap::registerIpWithStack(const InetAddress &ip)
|
||||
{
|
||||
#if defined(STACK_LWIP)
|
||||
lwip_init_interface((void*)this, this->_mac, ip);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
pico_register_address(this, ip);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VirtualTap::addIp(const InetAddress &ip)
|
||||
{
|
||||
#if defined(NO_STACK)
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
DEBUG_INFO("addIp=%s, nwid=%llx", ip.toString(ipbuf), (unsigned long long)_nwid);
|
||||
_ips.push_back(ip);
|
||||
std::sort(_ips.begin(),_ips.end());
|
||||
return true;
|
||||
#endif
|
||||
#if defined(STACK_PICO) || defined(STACK_LWIP)
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
DEBUG_EXTRA("addr=%s, nwid=%llx", ip.toString(ipbuf), (unsigned long long)_nwid);
|
||||
Mutex::Lock _l(_ips_m);
|
||||
if (registerIpWithStack(ip)) {
|
||||
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
||||
_ips.push_back(ip);
|
||||
std::sort(_ips.begin(),_ips.end());
|
||||
}
|
||||
return true;
|
||||
registerIpWithStack(ip);
|
||||
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
||||
_ips.push_back(ip);
|
||||
std::sort(_ips.begin(),_ips.end());
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VirtualTap::removeIp(const InetAddress &ip)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
Mutex::Lock _l(_ips_m);
|
||||
std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
|
||||
//if (i == _ips.end()) {
|
||||
@@ -158,10 +132,10 @@ bool VirtualTap::removeIp(const InetAddress &ip)
|
||||
//}
|
||||
_ips.erase(i);
|
||||
if (ip.isV4()) {
|
||||
// FIXME: De-register from network stacks
|
||||
// FIXME: De-register from network stack
|
||||
}
|
||||
if (ip.isV6()) {
|
||||
// FIXME: De-register from network stacks
|
||||
// FIXME: De-register from network stack
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -175,12 +149,7 @@ std::vector<InetAddress> VirtualTap::ips() const
|
||||
void VirtualTap::put(const MAC &from,const MAC &to,unsigned int etherType,
|
||||
const void *data,unsigned int len)
|
||||
{
|
||||
#if defined(STACK_LWIP)
|
||||
lwip_eth_rx(this, from, to, etherType, data, len);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
rd_pico_eth_rx(this,from,to,etherType,data,len);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string VirtualTap::deviceName() const
|
||||
@@ -238,366 +207,10 @@ void VirtualTap::setMtu(unsigned int mtu)
|
||||
void VirtualTap::threadMain()
|
||||
throw()
|
||||
{
|
||||
#if defined(STACK_LWIP) && !defined(LIBZT_RAW)
|
||||
while (true) {
|
||||
_phy.poll(ZT_PHY_POLL_INTERVAL);
|
||||
Housekeeping();
|
||||
}
|
||||
#endif
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
rd_lwip_loop(this); // use driver loop
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
pico_init_interface(this);
|
||||
if (this->should_start_stack) {
|
||||
rd_pico_loop(this); // use driver loop
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void VirtualTap::phyOnUnixClose(PhySocket *sock, void **uptr)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
}
|
||||
|
||||
void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
#if defined(LIBZT_RAW)
|
||||
VirtualSocket *vs = (VirtualSocket*)*uptr;
|
||||
if (vs == NULL) {
|
||||
return;
|
||||
}
|
||||
if (len > 0) {
|
||||
Write(vs, data, len);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void VirtualTap::phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invoked)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
#if defined(LIBZT_RAW)
|
||||
if (sock) {
|
||||
Read(sock,uptr,stack_invoked);
|
||||
} else {
|
||||
DEBUG_ERROR("!sock");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool VirtualTap::routeAdd(const InetAddress &ip, const InetAddress &nm, const InetAddress &gw)
|
||||
{
|
||||
bool err = false;
|
||||
DEBUG_EXTRA("");
|
||||
#if defined(STACK_LWIP)
|
||||
// general_lwip_init_interface(this, NULL, "n1", _mac, ip, nm, gw);
|
||||
// general_turn_on_interface(NULL);
|
||||
return true;
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
return rd_pico_route_add(this, ip, nm, gw, 0);
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
// nothing to do
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
bool VirtualTap::routeDelete(const InetAddress &ip, const InetAddress &nm)
|
||||
{
|
||||
bool err = false;
|
||||
DEBUG_EXTRA("");
|
||||
#if defined(STACK_LWIP)
|
||||
// general_lwip_init_interface(this, NULL, "n1", _mac, ip, nm, gw);
|
||||
// general_turn_on_interface(NULL);
|
||||
return true;
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
return rd_pico_route_del(this, ip, nm, 0);
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
// nothing to do
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
void VirtualTap::addVirtualSocket(VirtualSocket *vs)
|
||||
{
|
||||
#if defined(LIBZT_RAW)
|
||||
DEBUG_EXTRA("");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
_VirtualSockets.push_back(vs);
|
||||
#endif
|
||||
}
|
||||
|
||||
void VirtualTap::removeVirtualSocket()
|
||||
{
|
||||
#if defined(LIBZT_RAW)
|
||||
DEBUG_EXTRA("");
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
for (int i=0; i<_VirtualSockets.size(); i++) {
|
||||
if (vs == _VirtualSockets[i]) {
|
||||
_VirtualSockets.erase(_VirtualSockets.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* DNS */
|
||||
/****************************************************************************/
|
||||
|
||||
int VirtualTap::add_DNS_Nameserver(struct sockaddr *addr)
|
||||
{
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_add_dns_nameserver(addr);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
rd_pico_add_dns_nameserver(addr);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
int VirtualTap::del_DNS_Nameserver(struct sockaddr *addr)
|
||||
{
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_del_dns_nameserver(addr);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
err = rd_pico_del_dns_nameserver(addr);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* SDK Socket API */
|
||||
/****************************************************************************/
|
||||
|
||||
// Connect
|
||||
int VirtualTap::Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
return -1;
|
||||
#endif
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_connect(vs, addr, addrlen);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
err = rd_pico_connect(vs, addr, addrlen);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Bind VirtualSocket to a network stack's interface
|
||||
int VirtualTap::Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
return rd_lwip_bind(this, vs, addr, addrlen);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
return rd_pico_bind(vs, addr, addrlen);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Listen for an incoming VirtualSocket
|
||||
int VirtualTap::Listen(VirtualSocket *vs, int backlog)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
return -1;
|
||||
#endif
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
err = rd_lwip_listen(vs, backlog);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
return rd_pico_listen(vs, backlog);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Accept a VirtualSocket
|
||||
VirtualSocket *VirtualTap::Accept(VirtualSocket *vs)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return NULL;
|
||||
#endif
|
||||
VirtualSocket *new_vs = NULL;
|
||||
#if defined(NO_STACK)
|
||||
new_vs = NULL;
|
||||
#endif
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_vs = rd_lwip_accept(vs);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
// TODO: separation of church and state
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
new_vs = rd_pico_accept(vs);
|
||||
#endif
|
||||
return new_vs;
|
||||
}
|
||||
|
||||
// Read from stack/buffers into the app's socket
|
||||
int VirtualTap::Read(VirtualSocket *vs, PhySocket *sock, void **uptr, bool stack_invoked)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
return -1;
|
||||
#endif
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_read((VirtualSocket*)*(_phy.getuptr(sock)), stack_invoked);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
err = rd_pico_read(this, sock, (VirtualSocket*)uptr, stack_invoked);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Write data from app socket to the virtual wire, either raw over VL2, or via network stack
|
||||
int VirtualTap::Write(VirtualSocket *vs, void *data, ssize_t len)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
return -1;
|
||||
#endif
|
||||
DEBUG_EXTRA("vs=%p, fd=%d, data=%p, len=%lu", vs, vs->app_fd, data, (unsigned long)len);
|
||||
int err = -1;
|
||||
#if defined(LIBZT_RAW)
|
||||
// VL2, SOCK_RAW, no network stack
|
||||
if (vs->socket_type == SOCK_RAW) {
|
||||
struct ether_header *eh = (struct ether_header *) data;
|
||||
MAC src_mac;
|
||||
MAC dest_mac;
|
||||
src_mac.setTo(eh->ether_shost, 6);
|
||||
dest_mac.setTo(eh->ether_dhost, 6);
|
||||
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_write(vs, data, len);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
err = rd_pico_write(vs, data, len);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Send data to a specified host
|
||||
int VirtualTap::SendTo(VirtualSocket *vs, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
#if !defined(ZT_VIRTUAL_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
int err = -1;
|
||||
#if defined(STACK_LWIP) && defined(ZT_VIRTUAL_SOCKET)
|
||||
if ((err = rd_lwip_connect(vs, addr, addrlen)) < 0) { // implicit
|
||||
return err;
|
||||
}
|
||||
if ((err = rd_lwip_write(vs, (void*)buf, len)) < 0) {
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
#if defined(STACK_PICO) && defined(ZT_VIRTUAL_SOCKET)
|
||||
if ((err = rd_pico_connect(vs, addr, addrlen)) < 0) { // implicit
|
||||
errno = ENOTCONN;
|
||||
return err;
|
||||
}
|
||||
if ((err = rd_pico_write(vs, (void*)buf, len)) < 0) {
|
||||
errno = ENOBUFS; // TODO: translate pico err to something more useful
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Remove VritualSocket from VirtualTap, and instruct network stacks to dismantle their
|
||||
// respective protocol control structures
|
||||
int VirtualTap::Close(VirtualSocket *vs)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
int err = 0;
|
||||
#if defined(LIBZT_RAW)
|
||||
if (vs == NULL) {
|
||||
DEBUG_ERROR("invalid VirtualSocket");
|
||||
return -1;
|
||||
}
|
||||
if (vs->sock) {
|
||||
DEBUG_EXTRA("calling _phy.close()");
|
||||
_phy.close(vs->sock, true);
|
||||
}
|
||||
removeVirtualSocket(vs);
|
||||
#endif
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_close(vs);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
/*
|
||||
if (vs->get_state() != VS_STATE_CLOSED && vs->get_state() != VS_STATE_LISTENING) {
|
||||
DEBUG_EXTRA("vs=%p, vs->get_state()=%d, vs->picosock->state=%d", vs, vs->get_state(), vs->picosock->state);
|
||||
// doesn't make sense to shut down a listening socket, just close it
|
||||
if ((err = vs->tap->Shutdown(vs, SHUT_RDWR)) < 0) {
|
||||
DEBUG_ERROR("error while shutting down socket");
|
||||
return - 1;
|
||||
}
|
||||
}
|
||||
rd_pico_Close(vs);
|
||||
removeVirtualSocket(vs);
|
||||
if (vs->socket_type == SOCK_STREAM) {
|
||||
while (!(vs->picosock->state & PICO_SOCKET_STATE_CLOSED)) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL);
|
||||
DEBUG_EXTRA("virtual lingering on socket, ps=%p, buf remaining=%d",vs->picosock, vs->TXbuf->count());
|
||||
}
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
// Shuts down some aspect of a connection
|
||||
int VirtualTap::Shutdown(VirtualSocket *vs, int how)
|
||||
{
|
||||
#if !defined(LIBZT_RAW)
|
||||
return -1;
|
||||
#endif
|
||||
int err = 0;
|
||||
#if defined(STACK_LWIP) && defined(LIBZT_RAW)
|
||||
err = rd_lwip_shutdown(vs, how);
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
err = rd_pico_shutdown(vs, how);
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
void VirtualTap::Housekeeping()
|
||||
@@ -618,6 +231,7 @@ void VirtualTap::Housekeeping()
|
||||
char ipbuf[INET6_ADDRSTRLEN], ipbuf2[INET6_ADDRSTRLEN], ipbuf3[INET6_ADDRSTRLEN];
|
||||
// TODO: Rework this when we have time
|
||||
// check if pushed route exists in tap (add)
|
||||
/*
|
||||
for (int i=0; i<ZT_MAX_NETWORK_ROUTES; i++) {
|
||||
found = false;
|
||||
target_addr = managed_routes->at(i).target;
|
||||
@@ -657,6 +271,7 @@ void VirtualTap::Housekeeping()
|
||||
routeDelete(routes[i].first, routes[i].second);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
// TODO: Clean up VirtualSocket objects
|
||||
last_housekeeping_ts = time_now();
|
||||
|
||||
@@ -272,7 +272,7 @@ int zts_get_num_assigned_addresses(const uint64_t nwid)
|
||||
}
|
||||
|
||||
int zts_get_address_at_index(
|
||||
const uint64_t nwid, const int index, struct sockaddr_storage *addr)
|
||||
const uint64_t nwid, const int index, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
if (!zt1Service) {
|
||||
return -1;
|
||||
@@ -283,8 +283,9 @@ int zts_get_address_at_index(
|
||||
return err;
|
||||
}
|
||||
_vtaps_lock.lock();
|
||||
if (index <= tap->_ips.size()) {
|
||||
memcpy(addr, &(tap->_ips[index]), sizeof(struct sockaddr_storage));
|
||||
if (index > -1 && index <= tap->_ips.size()) {
|
||||
memcpy(addr, &(tap->_ips[index]), *addrlen);
|
||||
*addrlen = tap->_ips[index].isV4() ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
err = 0;
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
@@ -514,25 +515,6 @@ unsigned long zts_get_peer_count()
|
||||
}
|
||||
}
|
||||
|
||||
int zts_get_peer_address(char *peer, const uint64_t nodeId)
|
||||
{
|
||||
/*
|
||||
if (zt1Service) {
|
||||
ZT_PeerList *pl = zt1Service->getNode()->peers();
|
||||
// uint64_t addr;
|
||||
for (size_t i=0; i<pl->peerCount; i++) {
|
||||
// ZT_Peer *p = &(pl->peers[i]);
|
||||
// DEBUG_INFO("peer[%d] = %lx", i, p->address);
|
||||
}
|
||||
return pl->peerCount;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool _ipv6_in_subnet(ZeroTier::InetAddress *subnet, ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::InetAddress r(addr);
|
||||
|
||||
332
src/libzt.cpp
332
src/libzt.cpp
@@ -27,25 +27,15 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Application-facing, partially-POSIX-compliant socket API
|
||||
* Application-facing, socket-like API
|
||||
*/
|
||||
|
||||
#include "libztDefs.h"
|
||||
|
||||
#if defined(STACK_LWIP)
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/netdb.h"
|
||||
//#include "dns.h"
|
||||
#endif
|
||||
#if defined(NO_STACK)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#if defined(STACK_PICO)
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include "VirtualSocketLayer.h"
|
||||
#include "libztDebug.h"
|
||||
|
||||
#include <string.h>
|
||||
@@ -62,104 +52,58 @@ int zts_socket(int socket_family, int socket_type, int protocol)
|
||||
{
|
||||
DEBUG_EXTRA("family=%d, type=%d, proto=%d", socket_family, socket_type, protocol);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
/* with this option, the VirtualSocket layer will abstract a stack's raw API
|
||||
into something that resembles a POSIX socket API, this driver shall be implemented in
|
||||
src/stack_name.cpp and include/stack_name.h */
|
||||
return virt_socket(socket_family, socket_type, protocol);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
/* use the lwIP community's own socket API, this provides thread safety and core
|
||||
locking */
|
||||
int socket_family_adj = platform_adjusted_socket_family(socket_family);
|
||||
int err = lwip_socket(socket_family_adj, socket_type, protocol);
|
||||
return err;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
// return pico_bsd_socket(socket_family, socket_type, protocol);
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d",fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_connect(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return lwip_connect(fd, (struct sockaddr*)&ss, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_bind(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return lwip_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_listen(int fd, int backlog)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_listen(fd, backlog);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_listen(fd, backlog);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_accept(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_accept(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
@@ -167,19 +111,11 @@ int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_accept4(fd, addr, addrlen, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
// return lwip_accept4(fd, addr, addrlen, flags);
|
||||
// lwip_accept4(fd, addr, addrlen, flags);
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -187,120 +123,68 @@ int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_setsockopt(fd, level, optname, optval, optlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_setsockopt(fd, level, optname, optval, optlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_getsockopt(fd, level, optname, optval, optlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_getsockopt(fd, level, optname, optval, optlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_getsockname(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_getsockname(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_getpeername(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_getpeername(fd, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_gethostname(char *name, size_t len)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_sethostname(const char *name, size_t len)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct hostent *zts_gethostbyname(const char *name)
|
||||
{
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return NULL;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
//return virt_gethostbyname(name);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
// TODO: Test thread safety
|
||||
/*
|
||||
char buf[256];
|
||||
@@ -320,30 +204,16 @@ struct hostent *zts_gethostbyname(const char *name)
|
||||
return lwip_gethostbyname(name);
|
||||
*/
|
||||
return NULL;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
#endif
|
||||
#if defined(STACK_LWIP)
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int zts_close(int fd)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_close(fd);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_close(fd);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
@@ -355,17 +225,7 @@ int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
//return poll(fds, nfds, timeout);
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
//return poll(fds, nfds, timeout);
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
return poll(fds, nfds, timeout);
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
@@ -373,35 +233,20 @@ int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
||||
struct timeval *timeout)
|
||||
{
|
||||
//DEBUG_EXTRA("");
|
||||
/*
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
*/
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_fcntl(int fd, int cmd, int flags)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d, cmd=%d, flags=%d", fd, cmd, flags);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_fcntl(fd, cmd, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
// translation from platform flag values to stack flag values
|
||||
int translated_flags = 0;
|
||||
#if defined(__linux__)
|
||||
@@ -415,28 +260,16 @@ int zts_fcntl(int fd, int cmd, int flags)
|
||||
}
|
||||
#endif
|
||||
return lwip_fcntl(fd, cmd, translated_flags);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_ioctl(int fd, unsigned long request, void *argp)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_ioctl(fd, request, argp);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_ioctl(fd, request, argp);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
||||
@@ -444,76 +277,43 @@ ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
//return virt_sendto();
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return lwip_sendto(fd, buf, len, flags, (struct sockaddr*)&ss, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_send(fd, buf, len, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_send(fd, buf, len, flags);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_sendmsg(fd, msg, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_sendmsg(fd, msg, flags);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t zts_recv(int fd, void *buf, size_t len, int flags)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_recv(fd, buf, len, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_recv(fd, buf, len, flags);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
||||
@@ -521,133 +321,73 @@ ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_recvfrom(fd, buf, len, flags, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_recvfrom(fd, buf, len, flags, addr, addrlen);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t zts_recvmsg(int fd, struct msghdr *msg, int flags)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d", fd);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_recvmsg(fd, msg, flags);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
//return lwip_recvmsg(fd, msg, flags);
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
return -1; // lwip_recvmsg(fd, msg, flags);
|
||||
// Not currently implemented by stack
|
||||
}
|
||||
|
||||
int zts_read(int fd, void *buf, size_t len)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_read(fd, buf, len);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_read(fd, buf, len);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
// return pico_read(fd, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_write(int fd, const void *buf, size_t len)
|
||||
{
|
||||
DEBUG_TRANS("fd=%d, len=%zu", fd, len);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_write(fd, buf, len);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_write(fd, buf, len);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_shutdown(int fd, int how)
|
||||
{
|
||||
DEBUG_EXTRA("fd=%d, how=%d", fd, how);
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
return virt_shutdown(fd, how);
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return lwip_shutdown(fd, how);
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int zts_add_dns_nameserver(struct sockaddr *addr)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
// TODO
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)&addr;
|
||||
static ip4_addr_t ipaddr;
|
||||
ipaddr.addr = in4->sin_addr.s_addr;
|
||||
// TODO: manage DNS server indices
|
||||
//dns_setserver(0, (const ip_addr_t*)&ipaddr);
|
||||
return 0;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int zts_del_dns_nameserver(struct sockaddr *addr)
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
if (zts_ready() == false) {
|
||||
DEBUG_ERROR("service not started yet, call zts_startjoin()");
|
||||
DEBUG_ERROR(LIBZT_SERVICE_NOT_STARTED_STR);
|
||||
return -1;
|
||||
}
|
||||
#if defined(ZT_VIRTUAL_SOCKET)
|
||||
// TODO
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_LWIP_SEQ_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
#if defined(ZT_PICO_BSD_SOCKET)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* The rationale for the following correctional methods is as follows:
|
||||
@@ -689,7 +429,6 @@ void fix_addr_socket_family(struct sockaddr *addr)
|
||||
#if defined(__linux__) || defined(_WIN32)
|
||||
/* struct sockaddr on Linux and Windows don't contain an sa_len field
|
||||
so we must adjust it here before feeding it into the stack. */
|
||||
#if defined(STACK_LWIP)
|
||||
if (addr->sa_len == 2) {
|
||||
if (addr->sa_family == 0) {
|
||||
addr->sa_family = addr->sa_len;
|
||||
@@ -702,7 +441,6 @@ void fix_addr_socket_family(struct sockaddr *addr)
|
||||
addr->sa_len = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* once we've moved the value to its anticipated location, convert it from
|
||||
its platform-specific value to one that the network stack can work with */
|
||||
#endif
|
||||
|
||||
381
src/libztJNI.cpp
381
src/libztJNI.cpp
@@ -48,13 +48,12 @@ extern "C" {
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
// prototype
|
||||
jobject ss2inet(JNIEnv *env, struct sockaddr_storage *src_ss);
|
||||
int sockinet2ss(JNIEnv *env, jobject src_inet, struct sockaddr_storage *dest_ss);
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier service controls */
|
||||
/****************************************************************************/
|
||||
/* ZeroTier service controls */
|
||||
/****************************************************************************/
|
||||
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_start(
|
||||
JNIEnv *env, jobject thisObj, jstring path, jboolean blocking)
|
||||
@@ -122,23 +121,20 @@ namespace ZeroTier {
|
||||
return zts_get_node_id();
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API uint64_t ZTCALL zts_get_node_id_from_file(const char *filepath);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_get_1num_1assigned_1addresses(
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_get_1num_1assigned_1addresses(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid)
|
||||
{
|
||||
return zts_get_num_assigned_addresses(nwid);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_1address_1at_1index(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint index)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_get_1address_1at_1index(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint index, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
if((err = zts_get_address_at_index(nwid, index, &ss)) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return ss2inet(env, &ss);
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_get_address_at_index(nwid, index, (struct sockaddr*)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_has_1address(
|
||||
@@ -147,31 +143,29 @@ namespace ZeroTier {
|
||||
return zts_has_address(nwid);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_1address(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint address_family)
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_get_1address(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint address_family, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
if ((err = zts_get_address((uint64_t)nwid, &ss, address_family)) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return ss2inet(env, &ss);
|
||||
int err = zts_get_address((uint64_t)nwid, &ss, address_family);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_6plane_addr(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_get_6plane_addr(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
zts_get_6plane_addr(&ss, nwid, nodeId);
|
||||
return ss2inet(env, &ss);
|
||||
ss2zta(env, &ss, addr);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_get_rfc4193_addr(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId)
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_get_rfc4193_addr(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
zts_get_rfc4193_addr(&ss, nwid, nodeId);
|
||||
return ss2inet(env, &ss);
|
||||
ss2zta(env, &ss, addr);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_zerotier_ZeroTier_get_peer_count(
|
||||
@@ -180,11 +174,9 @@ namespace ZeroTier {
|
||||
return zts_get_peer_count();
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API int ZTCALL zts_get_peer_address(char *peer, const uint64_t nodeId);
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier Socket API */
|
||||
/****************************************************************************/
|
||||
/* ZeroTier Socket API */
|
||||
/****************************************************************************/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_socket(
|
||||
JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol)
|
||||
@@ -196,10 +188,7 @@ namespace ZeroTier {
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
if(sockinet2ss(env, addr, &ss) < 0) {
|
||||
return -1; // possibly invalid address format
|
||||
// TODO: set errno
|
||||
}
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
return zts_connect(fd, (struct sockaddr *)&ss, addrlen);
|
||||
}
|
||||
@@ -209,14 +198,9 @@ namespace ZeroTier {
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
if(sockinet2ss(env, addr, &ss) < 0) {
|
||||
return -1; // possibly invalid address format
|
||||
// TODO: set errno
|
||||
}
|
||||
//DEBUG_TEST("RESULT => %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
err = zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
return err;
|
||||
return zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_listen(
|
||||
@@ -229,26 +213,20 @@ namespace ZeroTier {
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
if ((err = zts_accept(fd, (struct sockaddr *)&ss, &addrlen)) < 0) {
|
||||
return err;
|
||||
}
|
||||
addr = ss2inet(env, &ss);
|
||||
int err = zts_accept(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_accept4(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port, jint flags)
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port, jint flags)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
if ((err = zts_accept4(fd, (struct sockaddr *)&ss, &addrlen, flags)) < 0) {
|
||||
return err;
|
||||
}
|
||||
addr = ss2inet(env, &ss);
|
||||
int err = zts_accept4(fd, (struct sockaddr *)&ss, &addrlen, flags);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
@@ -265,139 +243,99 @@ namespace ZeroTier {
|
||||
return zts_getsockopt(fd, level, optname, (void*)(uintptr_t)optval, (socklen_t *)optlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int err = zts_getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
|
||||
jfieldID fid;
|
||||
jclass c = (*env).GetObjectClass(ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_getsockname(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject ztaddr)
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int err = zts_getpeername(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
|
||||
jfieldID fid;
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
struct sockaddr_storage ss;
|
||||
int err = zts_getpeername(fd, (struct sockaddr *)&ss, (socklen_t *)sizeof(struct sockaddr_storage));
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API struct hostent *zts_gethostbyname(const char *name);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_close(
|
||||
JNIEnv *env, jobject thisObj, jint fd)
|
||||
{
|
||||
return zts_close(fd);
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API int ZTCALL zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_fcntl(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags)
|
||||
{
|
||||
return zts_fcntl(fd, cmd, flags);
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API int ZTCALL zts_ioctl(int fd, unsigned long request, void *argp);
|
||||
JNIEXPORT int JNICALL Java_zerotier_ZeroTier_ioctl(jint fd, jlong request, void *argp)
|
||||
{
|
||||
return zts_ioctl(fd, request, argp);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
int w = zts_send(fd, body, len, flags);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
int written_bytes = zts_write(fd, body, len);
|
||||
return written_bytes;
|
||||
return w;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_sendto(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr)
|
||||
JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject addr)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int sent_bytes = 0;
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
jfieldID f = (*env).GetFieldID(c, "port", "I");
|
||||
addr.sin_port = htons((*env).GetIntField( ztaddr, f));
|
||||
f = (*env).GetFieldID(c, "_rawAddr", "J");
|
||||
addr.sin_addr.s_addr = (*env).GetLongField( ztaddr, f);
|
||||
addr.sin_family = AF_INET;
|
||||
//LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
|
||||
// TODO: Optimize this
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
// "connect" and send buffer contents
|
||||
sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
|
||||
}
|
||||
return sent_bytes;
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
int w = zts_sendto(fd, body, len, flags, (struct sockaddr *)&ss, addrlen);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
return w;
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API ssize_t ZTCALL zts_sendmsg(int fd, const struct msghdr *msg, int flags);
|
||||
// TODO: ZT_SOCKET_API ssize_t ZTCALL zts_recv(int fd, void *buf, size_t len, int flags);
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_recv(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jarray buf, jint len, jint flags)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
int r = zts_recv(fd, body, len, flags);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_recvfrom(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint len, jint flags, jobject ztaddr)
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint len, jint flags, jobject addr)
|
||||
{
|
||||
/*
|
||||
struct sockaddr_in addr;
|
||||
jbyte *body = (*env).GetByteArrayElements( buf, 0);
|
||||
unsigned char buffer[ZT_SDK_MTU];
|
||||
int payload_offset = sizeof(int32_t) + sizeof(struct sockaddr_storage);
|
||||
int rxbytes = zts_recvfrom(fd, &buffer, len, flags, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr_storage));
|
||||
if (rxbytes > 0) {
|
||||
memcpy(body, (jbyte*)buffer + payload_offset, rxbytes);
|
||||
}
|
||||
(*env).ReleaseByteArrayElements( buf, body, 0);
|
||||
// Update fields of Java ZTAddress object
|
||||
jfieldID fid;
|
||||
jclass c = (*env).GetObjectClass( ztaddr);
|
||||
if (c) {
|
||||
fid = (*env).GetFieldID(c, "port", "I");
|
||||
(*env).SetIntField(ztaddr, fid, addr.sin_port);
|
||||
fid = (*env).GetFieldID(c,"_rawAddr", "J");
|
||||
(*env).SetLongField(ztaddr, fid,addr.sin_addr.s_addr);
|
||||
}
|
||||
*/
|
||||
return 1;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
struct sockaddr_storage ss;
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
int r = zts_recvfrom(fd, body, len, flags, (struct sockaddr *)&ss, &addrlen);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
ss2zta(env, &ss, addr);
|
||||
return r;
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API ssize_t ZTCALL zts_recvmsg(int fd, struct msghdr *msg,int flags);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jarray buf, jint len)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
int read_bytes = zts_read(fd, body, len);
|
||||
int r = zts_read(fd, body, len);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
return read_bytes;
|
||||
return r;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_write(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jarray buf, jint len)
|
||||
{
|
||||
jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
|
||||
char * bufp = (char *)malloc(sizeof(char)*len);
|
||||
memcpy(bufp, body, len);
|
||||
int w = zts_write(fd, body, len);
|
||||
(*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
|
||||
int written_bytes = zts_write(fd, body, len);
|
||||
return written_bytes;
|
||||
return w;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_shutdown(
|
||||
@@ -405,113 +343,88 @@ namespace ZeroTier {
|
||||
{
|
||||
return zts_shutdown(fd, how);
|
||||
}
|
||||
|
||||
// TODO: ZT_SOCKET_API int ZTCALL zts_add_dns_nameserver(struct sockaddr *addr);
|
||||
// TODO: ZT_SOCKET_API int ZTCALL zts_del_dns_nameserver(struct sockaddr *addr);
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* Helpers (for moving data across the JNI barrier) */
|
||||
/****************************************************************************/
|
||||
|
||||
// convenience function
|
||||
jobject ss2inet(JNIEnv *env, struct sockaddr_storage *src_ss)
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jobject dest_inet;
|
||||
if(src_ss->ss_family == AF_INET)
|
||||
{
|
||||
DEBUG_ERROR("converting from INET");
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)src_ss;
|
||||
int arrlen = 4;
|
||||
jbyteArray bytes = (*env).NewByteArray(arrlen);
|
||||
jbyte *java_address_bytes;
|
||||
java_address_bytes = (*env).GetByteArrayElements(bytes, NULL);
|
||||
memcpy(java_address_bytes, &(in4->sin_addr.s_addr), arrlen);
|
||||
(*env).ReleaseByteArrayElements(bytes, java_address_bytes, 0);
|
||||
jclass cls = (*env).FindClass("java/net/InetAddress");
|
||||
jmethodID mid = (*env).GetStaticMethodID(cls, "getByAddress", "([B)Ljava/net/InetAddress;");
|
||||
dest_inet = (*env).CallStaticObjectMethod(cls, mid, bytes);
|
||||
(*env).DeleteLocalRef(bytes);
|
||||
}
|
||||
if(src_ss->ss_family == AF_INET6)
|
||||
{
|
||||
DEBUG_ERROR("converting from INET6");
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)src_ss;
|
||||
int arrlen = 16;
|
||||
jbyteArray bytes = (*env).NewByteArray(arrlen);
|
||||
(*env).SetByteArrayRegion(bytes, 0, 16, (const jbyte *)&(in6->sin6_addr));
|
||||
jclass cls = (*env).FindClass("java/net/InetAddress");
|
||||
jmethodID mid = (*env).GetStaticMethodID(cls, "getByAddress", "([B)Ljava/net/InetAddress;");
|
||||
dest_inet = (*env).CallStaticObjectMethod(cls, mid, bytes);
|
||||
(*env).DeleteLocalRef(bytes);
|
||||
}
|
||||
return dest_inet;
|
||||
}
|
||||
|
||||
|
||||
int sockinet2ss(JNIEnv *env, jobject src_inet, struct sockaddr_storage *dest_ss)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)dest_ss;
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)dest_ss;
|
||||
int port = 0;
|
||||
int socket_family = 0;
|
||||
socklen_t addrlen;
|
||||
|
||||
// ---
|
||||
|
||||
jclass c = (*env).GetObjectClass(src_inet);
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return -1;
|
||||
return;
|
||||
}
|
||||
// get port
|
||||
jmethodID getPort = (*env).GetMethodID(c, "getPort", "()I");
|
||||
if (!getPort) {
|
||||
return -1;
|
||||
if(ss->ss_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in4->sin_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetLongField(addr, fid, (in4->sin_family));
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in4->sin_addr.s_addr), 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
port = (*env).CallIntMethod(src_inet, getPort);
|
||||
// get internal InetAddress
|
||||
jobject inetaddr;
|
||||
jmethodID getAddress = (*env).GetMethodID(c, "getAddress", "()Ljava/net/InetAddress;");
|
||||
if (!getAddress) {
|
||||
return -1;
|
||||
if(ss->ss_family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in6->sin6_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetLongField(addr, fid, (in6->sin6_family));
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in6->sin6_addr.s6_addr), 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
inetaddr = (*env).CallObjectMethod(src_inet, getAddress);
|
||||
if (!inetaddr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
jclass inetClass = (*env).GetObjectClass(inetaddr);
|
||||
if (!inetClass) {
|
||||
return -1;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_family", "I");
|
||||
int family = (*env).GetIntField(addr, fid);
|
||||
if (family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in4->sin_port = htons((*env).GetIntField(addr, fid));
|
||||
in4->sin_family = AF_INET;
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in4->sin_addr.s_addr), data, 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
// string representation of IP address
|
||||
jmethodID getHostAddress = (*env).GetMethodID(inetClass, "getHostAddress", "()Ljava/lang/String;");
|
||||
jstring addrstr = (jstring)(*env).CallObjectMethod(inetaddr, getHostAddress);
|
||||
const char *addr_str = (*env).GetStringUTFChars(addrstr, NULL);
|
||||
for (int i=0; i<strlen(addr_str); i++) {
|
||||
if (addr_str[i]=='.') {
|
||||
DEBUG_INFO("ipv4, inet_addr");
|
||||
socket_family = AF_INET;
|
||||
in4->sin_family = AF_INET;
|
||||
in4->sin_port = htons(port);
|
||||
in4->sin_addr.s_addr = inet_addr(addr_str);
|
||||
/*
|
||||
if (!inet_pton(AF_INET, addr_str, &(in4->sin_addr))) {
|
||||
DEBUG_ERROR("error converting address %s", addr_str);
|
||||
}
|
||||
*/
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
break;
|
||||
}
|
||||
if (addr_str[i]==':') {
|
||||
DEBUG_INFO("ipv6");
|
||||
socket_family = AF_INET6;
|
||||
if (!inet_pton(AF_INET6, addr_str, &(in6->sin6_addr))) {
|
||||
DEBUG_ERROR("error converting address %s", addr_str);
|
||||
}
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
break;
|
||||
}
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in6->sin6_port = htons((*env).GetIntField(addr, fid));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
in6->sin6_family = AF_INET6;
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in6->sin6_addr.s6_addr), data, 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
(*env).ReleaseStringUTFChars(addrstr, addr_str);
|
||||
DEBUG_TEST("RESULT => %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
1639
src/lwIP.cpp
1639
src/lwIP.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,15 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Starting ZT service");
|
||||
zts_startjoin("my_config_path",0x0000000000000000);
|
||||
|
||||
printf("Dummy. Going into infinite loop. Ping me or something\n");
|
||||
while(1) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
194
test/layer2.cpp
194
test/layer2.cpp
@@ -1,194 +0,0 @@
|
||||
// This file is built with libzt.a via `make tests`
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/udp.h>
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <net/ethernet.h>
|
||||
#endif
|
||||
#if defined(__linux__)
|
||||
#include <netinet/ether.h>
|
||||
#include <sys/socket.h>
|
||||
#include <linux/if_packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
|
||||
unsigned short csum(unsigned short *buf, int nwords)
|
||||
{
|
||||
unsigned long sum;
|
||||
for(sum=0; nwords>0; nwords--)
|
||||
sum += *buf++;
|
||||
sum = (sum >> 16) + (sum &0xffff);
|
||||
sum += (sum >> 16);
|
||||
return (unsigned short)(~sum);
|
||||
}
|
||||
|
||||
int main(int argc , char *argv[])
|
||||
{
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "usage: layer2 <zt_home_dir> <nwid>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// initialize library
|
||||
printf("Starting libzt...\n");
|
||||
zts_startjoin(argv[1], strtoull(argv[2], NULL, 16));
|
||||
uint64_t device_id = zts_get_node_id();
|
||||
fprintf(stderr, "Complete. I am %llx\n", device_id);
|
||||
|
||||
// create socket
|
||||
int fd;
|
||||
if ((fd = zts_socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) {
|
||||
printf("There was a problem creating the raw socket\n");
|
||||
exit(-1);
|
||||
}
|
||||
fprintf(stderr, "Created raw socket (%d)\n", fd);
|
||||
|
||||
#if defined(__APPLE__)
|
||||
fprintf(stderr, "SOCK_RAW not supported on mac builds yet. exiting");
|
||||
exit(0);
|
||||
#endif
|
||||
#if defined(__linux__) // The rest of this file isn't yet supported on non-linux platforms
|
||||
// get interface index to bind on
|
||||
struct ifreq if_idx;
|
||||
memset(&if_idx, 0, sizeof(struct ifreq));
|
||||
strncpy(if_idx.ifr_name, "libzt0", IFNAMSIZ-1);
|
||||
if (zts_ioctl(fd, SIOCGIFINDEX, &if_idx) < 0) {
|
||||
perror("SIOCGIFINDEX");
|
||||
exit(-1);
|
||||
}
|
||||
fprintf(stderr, "if_idx.ifr_ifindex=%d\n", if_idx.ifr_ifindex);
|
||||
|
||||
// get MAC address of interface to send on
|
||||
struct ifreq if_mac;
|
||||
memset(&if_mac, 0, sizeof(struct ifreq));
|
||||
strncpy(if_mac.ifr_name, "libzt0", IFNAMSIZ-1);
|
||||
if (zts_ioctl(fd, SIOCGIFHWADDR, &if_mac) < 0) {
|
||||
perror("SIOCGIFHWADDR");
|
||||
exit(-1);
|
||||
}
|
||||
const unsigned char* mac=(unsigned char*)if_mac.ifr_hwaddr.sa_data;
|
||||
fprintf(stderr, "hwaddr=%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
|
||||
|
||||
// get IP address of interface to send on
|
||||
struct ifreq if_ip;
|
||||
memset(&if_ip, 0, sizeof(struct ifreq));
|
||||
strncpy(if_ip.ifr_name, "libzt0", IFNAMSIZ-1);
|
||||
if (zts_ioctl(fd, SIOCGIFADDR, &if_ip) < 0) {
|
||||
perror("SIOCGIFADDR");
|
||||
exit(-1);
|
||||
}
|
||||
char ipv4_str[INET_ADDRSTRLEN];
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in *)&if_ip.ifr_addr;
|
||||
inet_ntop(AF_INET, (const void *)&in4->sin_addr.s_addr, ipv4_str, INET_ADDRSTRLEN);
|
||||
fprintf(stderr, "addr=%s", ipv4_str);
|
||||
|
||||
// construct ethernet header
|
||||
int tx_len = 0;
|
||||
char sendbuf[1024];
|
||||
struct ether_header *eh = (struct ether_header *) sendbuf;
|
||||
memset(sendbuf, 0, 1024);
|
||||
|
||||
// Ethernet header
|
||||
eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
|
||||
eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
|
||||
eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
|
||||
eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
|
||||
eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
|
||||
eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
|
||||
|
||||
// set destination MAC
|
||||
int MY_DEST_MAC0 = 0x72;
|
||||
int MY_DEST_MAC1 = 0x92;
|
||||
int MY_DEST_MAC2 = 0xd4;
|
||||
int MY_DEST_MAC3 = 0xfd;
|
||||
int MY_DEST_MAC4 = 0x43;
|
||||
int MY_DEST_MAC5 = 0x45;
|
||||
|
||||
eh->ether_dhost[0] = MY_DEST_MAC0;
|
||||
eh->ether_dhost[1] = MY_DEST_MAC1;
|
||||
eh->ether_dhost[2] = MY_DEST_MAC2;
|
||||
eh->ether_dhost[3] = MY_DEST_MAC3;
|
||||
eh->ether_dhost[4] = MY_DEST_MAC4;
|
||||
eh->ether_dhost[5] = MY_DEST_MAC5;
|
||||
eh->ether_type = htons(ETH_P_IP);
|
||||
tx_len += sizeof(struct ether_header);
|
||||
|
||||
|
||||
// Construct the IP header
|
||||
int ttl = 64;
|
||||
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
|
||||
iph->ihl = 5;
|
||||
iph->version = 4;
|
||||
iph->tos = 16; // Low delay
|
||||
iph->id = htons(54321);
|
||||
iph->ttl = ttl; // hops
|
||||
iph->protocol = 17; // UDP
|
||||
// Source IP address, can be spoofed
|
||||
iph->saddr = inet_addr(inet_ntoa(((struct sockaddr_in *)&if_ip.ifr_addr)->sin_addr));
|
||||
// iph->saddr = inet_addr("192.168.0.112");
|
||||
// Destination IP address
|
||||
iph->daddr = inet_addr("10.7.7.1");
|
||||
tx_len += sizeof(struct iphdr);
|
||||
|
||||
// Construct UDP header
|
||||
struct udphdr *udph = (struct udphdr *) (sendbuf + sizeof(struct iphdr) + sizeof(struct ether_header));
|
||||
udph->source = htons(3423);
|
||||
udph->dest = htons(5342);
|
||||
udph->check = 0; // skip
|
||||
tx_len += sizeof(struct udphdr);
|
||||
|
||||
// Fill in UDP payload
|
||||
sendbuf[tx_len++] = 0xde;
|
||||
sendbuf[tx_len++] = 0xad;
|
||||
sendbuf[tx_len++] = 0xbe;
|
||||
sendbuf[tx_len++] = 0xef;
|
||||
|
||||
// Fill in remaining header info
|
||||
// Length of UDP payload and header
|
||||
udph->len = htons(tx_len - sizeof(struct ether_header) - sizeof(struct iphdr));
|
||||
// Length of IP payload and header
|
||||
iph->tot_len = htons(tx_len - sizeof(struct ether_header));
|
||||
// Calculate IP checksum on completed header
|
||||
iph->check = csum((unsigned short *)(sendbuf+sizeof(struct ether_header)), sizeof(struct iphdr)/2);
|
||||
|
||||
// Send packet
|
||||
// Destination address
|
||||
struct sockaddr_ll socket_address;
|
||||
// Index of the network device
|
||||
socket_address.sll_ifindex = if_idx.ifr_ifindex;
|
||||
// Address length
|
||||
socket_address.sll_halen = ETH_ALEN;
|
||||
// Destination MAC
|
||||
socket_address.sll_addr[0] = MY_DEST_MAC0;
|
||||
socket_address.sll_addr[1] = MY_DEST_MAC1;
|
||||
socket_address.sll_addr[2] = MY_DEST_MAC2;
|
||||
socket_address.sll_addr[3] = MY_DEST_MAC3;
|
||||
socket_address.sll_addr[4] = MY_DEST_MAC4;
|
||||
socket_address.sll_addr[5] = MY_DEST_MAC5;
|
||||
|
||||
while(1)
|
||||
{
|
||||
usleep(10000);
|
||||
// Send packet
|
||||
if (zts_sendto(fd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
|
||||
fprintf(stderr, "Send failed\n");
|
||||
}
|
||||
|
||||
// dismantle all zt virtual taps
|
||||
zts_stop();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
# Source files should not use the 'r' (CR) character
|
||||
rule=F001
|
||||
|
||||
# File names should be well-formed
|
||||
rule=F002
|
||||
|
||||
# No trailing whitespace
|
||||
rule=L001
|
||||
parameter=strict-trailing-space=0
|
||||
|
||||
# Don't use tab characters
|
||||
# project deliberately uses TABS, so this rule should be left commented out
|
||||
# rule=L002
|
||||
|
||||
# No leading and no trailing empty lines
|
||||
rule=L003
|
||||
|
||||
# Line cannot be too long
|
||||
rule=L004
|
||||
parameter=max-line-length=120
|
||||
|
||||
# There should not be too many consecutive empty lines
|
||||
rule=L005
|
||||
parameter=max-consecutive-empty-lines=5
|
||||
|
||||
# Source file should not be too long
|
||||
# rule=L006
|
||||
# parameter=max-file-length=3000
|
||||
|
||||
# One-line comments should not have forced continuation
|
||||
rule=T001
|
||||
|
||||
# Reserved names should not be used for preprocessor macros
|
||||
rule=T002
|
||||
|
||||
# Some keywords should be followed by a single space
|
||||
rule=T003
|
||||
|
||||
# Some keywords should be immediately followed by a colon
|
||||
rule=T004
|
||||
|
||||
# Keywords break and continue should be immediately followed by a semicolon
|
||||
rule=T005
|
||||
|
||||
# Keywords return and throw should be immediately followed by a semicolon or a single space
|
||||
rule=T006
|
||||
|
||||
# Semicolons should not be isolated by spaces or comments from
|
||||
rule=T007
|
||||
|
||||
# Keywords catch, for, if, switch and while should be followed by a single space
|
||||
rule=T008
|
||||
|
||||
# Comma should not be preceded by whitespace, but should be followed by one
|
||||
rule=T009
|
||||
|
||||
# Identifiers should not be composed of 'l' and 'O' characters only
|
||||
rule=T010
|
||||
|
||||
# Curly brackets from the same pair should be either in the same line or in the same column
|
||||
# rule=T011
|
||||
|
||||
# Negation operator should not be used in its short form
|
||||
rule=T012
|
||||
|
||||
# Source files should contain the copyright notice
|
||||
rule=T013
|
||||
|
||||
#
|
||||
# rule=T014
|
||||
|
||||
# HTML links in comments and string literals should be correct
|
||||
rule=T015
|
||||
|
||||
# Calls to min/max should be protected against accidental macro
|
||||
rule=T016
|
||||
|
||||
# Unnamed namespaces are not allowed in header files
|
||||
rule=T017
|
||||
|
||||
# Using namespace is not allowed in header files
|
||||
rule=T018
|
||||
|
||||
# Control structures should have complete curly-braced block of code
|
||||
rule=T019
|
||||
Reference in New Issue
Block a user