mirror of
https://github.com/zerotier/zerotier-sockets-apple-framework.git
synced 2026-05-22 16:28:12 -07:00
Add iOS app example project
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>IDEDidComputeMac32BitWarning</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// ios-example-app
|
||||
//
|
||||
// Created by zt on 7/17/23.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
var body: some View {
|
||||
VStack {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundColor(.accentColor)
|
||||
Text("Hello, world!")
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentView()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
import Foundation
|
||||
|
||||
// Import libzt
|
||||
import zt
|
||||
|
||||
// Helper function to display details about local ZeroTier Node (on event)
|
||||
let printNodeDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.node;
|
||||
print(String(format: "\t- node_id : %llx", d!.pointee.node_id));
|
||||
print(String(format: "\t- version : %d.%d.%d", d!.pointee.ver_major, d!.pointee.ver_minor, d!.pointee.ver_rev));
|
||||
print(String(format: "\t- port_primary : %d", d!.pointee.port_primary));
|
||||
print(String(format: "\t- port_secondary : %d", d!.pointee.port_secondary));
|
||||
}
|
||||
|
||||
// Helper function to display details about local ZeroTier network (on event)
|
||||
let printNetworkDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.network;
|
||||
let name = ""; // String(d!.pointee.name);
|
||||
|
||||
print(String(format: "\t- net_id : %llx", d!.pointee.net_id));
|
||||
print(String(format: "\t- mac : %lx", d!.pointee.mac));
|
||||
print(String(format: "\t- name : %s", name));
|
||||
print(String(format: "\t- type : %d", Int(d!.pointee.type.rawValue)));
|
||||
/* MTU for the virtual network can be set via our web API */
|
||||
print(String(format: "\t- mtu : %d", d!.pointee.mtu));
|
||||
print(String(format: "\t- dhcp : %d", d!.pointee.dhcp));
|
||||
print(String(format: "\t- bridge : %d", d!.pointee.bridge));
|
||||
print(String(format: "\t- broadcast_enabled : %d", d!.pointee.broadcast_enabled));
|
||||
print(String(format: "\t- port_error : %d", d!.pointee.port_error));
|
||||
print(String(format: "\t- netconf_rev : %d", d!.pointee.netconf_rev));
|
||||
print(String(format: "\t- route_count : %d", d!.pointee.route_count));
|
||||
print(String(format: "\t- multicast_sub_count : %d", d!.pointee.multicast_sub_count));
|
||||
|
||||
/*
|
||||
var addresses: [zts_sockaddr_storage] = convertTupleToArray(from: d!.pointee.assignedAddresses)
|
||||
|
||||
print("\t- addresses:\n");
|
||||
for i in 0...d!.pointee.assignedAddressCount {
|
||||
if (addresses[Int(i)].ss_family == ZTS_AF_INET) {
|
||||
// Allocate a byte array that can hold the largest possible IPv4 human-readable string
|
||||
var ipCharByteArray = Array<Int8>(repeating: 0, count: Int(ZTS_INET_ADDRSTRLEN))
|
||||
// Cast unsafe pointer from zts_sockaddr_storage to zts_sockaddr_in
|
||||
var addr:zts_sockaddr_in = withUnsafePointer(to: &(addresses[Int(i)])) {
|
||||
$0.withMemoryRebound(to: zts_sockaddr_in.self, capacity: 1) {
|
||||
$0.pointee
|
||||
}
|
||||
}
|
||||
// Pass unsafe pointer (addr) to a ntop to convert into human-readable byte array
|
||||
zts_inet_ntop(ZTS_AF_INET, &(addr.sin_addr), &ipCharByteArray, UInt32(ZTS_INET_ADDRSTRLEN))
|
||||
//print(ipCharByteArray) // [49, 55, 50, 46, 50, 55, 46, 49, 49, 54, 46, 49, 54, 55, 0, 0]
|
||||
// Somehow convery Int8 byte array to Swift String ???
|
||||
//let ipString = String(bytes: ipStr, encoding: .utf8)
|
||||
//print(ipString)
|
||||
|
||||
// Pass unsafe pointer (addr) to a ntop to convert into human-readable byte array
|
||||
// convert to UInt8 byte array
|
||||
let uintArray = ipCharByteArray.map { UInt8(bitPattern: $0) }
|
||||
if let string = String(bytes: uintArray, encoding: .utf8) {
|
||||
print("\t\t-", string)
|
||||
}
|
||||
}
|
||||
if (addresses[Int(i)].ss_family == ZTS_AF_INET6) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Helper function to display details about remote ZeroTier peer node (on event)
|
||||
let printPeerDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.peer;
|
||||
print(String(format: "\t- peer_id : %llx", d!.pointee.peer_id));
|
||||
print(String(format: "\t- role : %d", Int(d!.pointee.role.rawValue)));
|
||||
print(String(format: "\t- latency : %llx", d!.pointee.latency));
|
||||
print(String(format: "\t- path_count : %llx", d!.pointee.path_count));
|
||||
print(String(format: "\t- version : %d.%d.%d", d!.pointee.ver_major, d!.pointee.ver_minor, d!.pointee.ver_rev));
|
||||
print(String(format: "\t- paths:\n"));
|
||||
|
||||
/*
|
||||
for i in 0...d!.pointee.pathCount {
|
||||
// ...
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Helper function to display details about local ZeroTier netif (on event) -- Can be ignored
|
||||
let printNetifDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.netif;
|
||||
print(String(format: "\t- net_id : %llx", d!.pointee.net_id));
|
||||
print(String(format: "\t- mac : %llx", d!.pointee.mac));
|
||||
print(String(format: "\t- mtu : %d", d!.pointee.mtu));
|
||||
}
|
||||
|
||||
// Handle ZeroTier events (e.g. successful node start, join, leave, etc) --- Can be ignored
|
||||
let onZeroTierEvent : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let eventCode = zts_event_t(rawValue: UInt32(msg!.pointee.event_code));
|
||||
let network = msg?.pointee.network;
|
||||
let peer = msg?.pointee.peer;
|
||||
|
||||
print("code:", eventCode);
|
||||
|
||||
|
||||
|
||||
// Node events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_UP)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_UP\n")
|
||||
//printNodeDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_ONLINE)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_ONLINE\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_OFFLINE)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_OFFLINE\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_DOWN)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_FATAL_ERROR)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_FATAL_ERROR\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Network events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_NOT_FOUND)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_NOT_FOUND (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_REQ_CONFIG (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_ACCESS_DENIED)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_ACCESS_DENIED (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_READY_IP4)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_READY_IP4 (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_READY_IP6)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_READY_IP6 (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_DOWN)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_DOWN (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_UPDATE)
|
||||
{
|
||||
print("ZTS_EVENT_NETWORK_UPDATE\n")
|
||||
//printNetworkDetails(msg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Address assignment events
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_ADDED_IP4)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_ADDED_IP4\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_ADDED_IP6)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_ADDED_IP6\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_REMOVED_IP4)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_REMOVED_IP4\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_REMOVED_IP6)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_REMOVED_IP6\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Remote peer events
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_DIRECT)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_DIRECT (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_RELAY)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_RELAY (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_PATH_DISCOVERED (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_PATH_DEAD)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_PATH_DEAD (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Network stack events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_UP) {
|
||||
print("ZTS_EVENT_NETIF_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_DOWN) {
|
||||
print("ZTS_EVENT_NETIF_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_REMOVED) {
|
||||
print("ZTS_EVENT_NETIF_REMOVED\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_LINK_UP) {
|
||||
print("ZTS_EVENT_NETIF_LINK_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_LINK_DOWN) {
|
||||
print("ZTS_EVENT_NETIF_LINK_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_STACK_UP) {
|
||||
print("ZTS_EVENT_STACK_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_STACK_DOWN) {
|
||||
print("ZTS_EVENT_STACK_DOWN\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Example user application logic
|
||||
func main()
|
||||
{
|
||||
// Configure local ZeroTier node
|
||||
|
||||
zts_init_set_event_handler(onZeroTierEvent);
|
||||
zts_init_set_port(9993);
|
||||
|
||||
// Start local ZeroTier node
|
||||
|
||||
zts_node_start();
|
||||
|
||||
// Wait for local ZeroTier node to come online
|
||||
|
||||
while(zts_node_is_online() != 1) {
|
||||
print("waiting for node", String(format:"%llX", zts_node_get_id()), "to come online ...");
|
||||
zts_util_delay(1000);
|
||||
}
|
||||
|
||||
// Join ZeroTier network
|
||||
|
||||
let nwid : UInt64 = 0xdb64858fed5a98b7; // Specify your network ID here
|
||||
print("Joining network", String(format:"%llX", nwid));
|
||||
zts_net_join(nwid);
|
||||
|
||||
// Example using raw BSD sockets
|
||||
|
||||
// example_raw_sockets();
|
||||
|
||||
// Idle so that app doesn't exit
|
||||
|
||||
print("Idling...");
|
||||
while(true) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
func example_raw_sockets()
|
||||
{
|
||||
// Create address structure
|
||||
let addr_str = "0.0.0.0"
|
||||
let port = 8080
|
||||
var in4 = zts_sockaddr_in(sin_len: UInt8(MemoryLayout<zts_sockaddr_in>.size),
|
||||
sin_family: UInt8(ZTS_AF_INET),
|
||||
sin_port: UInt16(port).bigEndian,
|
||||
sin_addr: zts_in_addr(s_addr: 0),
|
||||
sin_zero: (0,0,0,0,0,0,0,0))
|
||||
zts_inet_pton(ZTS_AF_INET, addr_str, &(in4.sin_addr));
|
||||
|
||||
print("fd=", zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0));
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,337 @@
|
||||
import Foundation
|
||||
|
||||
// Import libzt
|
||||
import zt
|
||||
|
||||
// Helper function to display details about local ZeroTier Node (on event)
|
||||
let printNodeDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.node;
|
||||
print(String(format: "\t- node_id : %llx", d!.pointee.node_id));
|
||||
print(String(format: "\t- version : %d.%d.%d", d!.pointee.ver_major, d!.pointee.ver_minor, d!.pointee.ver_rev));
|
||||
print(String(format: "\t- port_primary : %d", d!.pointee.port_primary));
|
||||
print(String(format: "\t- port_secondary : %d", d!.pointee.port_secondary));
|
||||
}
|
||||
|
||||
// Helper function to display details about local ZeroTier network (on event)
|
||||
let printNetworkDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.network;
|
||||
let name = ""; // String(d!.pointee.name);
|
||||
|
||||
print(String(format: "\t- net_id : %llx", d!.pointee.net_id));
|
||||
print(String(format: "\t- mac : %lx", d!.pointee.mac));
|
||||
print(String(format: "\t- name : %s", name));
|
||||
print(String(format: "\t- type : %d", Int(d!.pointee.type.rawValue)));
|
||||
/* MTU for the virtual network can be set via our web API */
|
||||
print(String(format: "\t- mtu : %d", d!.pointee.mtu));
|
||||
print(String(format: "\t- dhcp : %d", d!.pointee.dhcp));
|
||||
print(String(format: "\t- bridge : %d", d!.pointee.bridge));
|
||||
print(String(format: "\t- broadcast_enabled : %d", d!.pointee.broadcast_enabled));
|
||||
print(String(format: "\t- port_error : %d", d!.pointee.port_error));
|
||||
print(String(format: "\t- netconf_rev : %d", d!.pointee.netconf_rev));
|
||||
print(String(format: "\t- route_count : %d", d!.pointee.route_count));
|
||||
print(String(format: "\t- multicast_sub_count : %d", d!.pointee.multicast_sub_count));
|
||||
|
||||
/*
|
||||
var addresses: [zts_sockaddr_storage] = convertTupleToArray(from: d!.pointee.assignedAddresses)
|
||||
|
||||
print("\t- addresses:\n");
|
||||
for i in 0...d!.pointee.assignedAddressCount {
|
||||
if (addresses[Int(i)].ss_family == ZTS_AF_INET) {
|
||||
// Allocate a byte array that can hold the largest possible IPv4 human-readable string
|
||||
var ipCharByteArray = Array<Int8>(repeating: 0, count: Int(ZTS_INET_ADDRSTRLEN))
|
||||
// Cast unsafe pointer from zts_sockaddr_storage to zts_sockaddr_in
|
||||
var addr:zts_sockaddr_in = withUnsafePointer(to: &(addresses[Int(i)])) {
|
||||
$0.withMemoryRebound(to: zts_sockaddr_in.self, capacity: 1) {
|
||||
$0.pointee
|
||||
}
|
||||
}
|
||||
// Pass unsafe pointer (addr) to a ntop to convert into human-readable byte array
|
||||
zts_inet_ntop(ZTS_AF_INET, &(addr.sin_addr), &ipCharByteArray, UInt32(ZTS_INET_ADDRSTRLEN))
|
||||
//print(ipCharByteArray) // [49, 55, 50, 46, 50, 55, 46, 49, 49, 54, 46, 49, 54, 55, 0, 0]
|
||||
// Somehow convery Int8 byte array to Swift String ???
|
||||
//let ipString = String(bytes: ipStr, encoding: .utf8)
|
||||
//print(ipString)
|
||||
|
||||
// Pass unsafe pointer (addr) to a ntop to convert into human-readable byte array
|
||||
// convert to UInt8 byte array
|
||||
let uintArray = ipCharByteArray.map { UInt8(bitPattern: $0) }
|
||||
if let string = String(bytes: uintArray, encoding: .utf8) {
|
||||
print("\t\t-", string)
|
||||
}
|
||||
}
|
||||
if (addresses[Int(i)].ss_family == ZTS_AF_INET6) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Helper function to display details about remote ZeroTier peer node (on event)
|
||||
let printPeerDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.peer;
|
||||
print(String(format: "\t- peer_id : %llx", d!.pointee.peer_id));
|
||||
print(String(format: "\t- role : %d", Int(d!.pointee.role.rawValue)));
|
||||
print(String(format: "\t- latency : %llx", d!.pointee.latency));
|
||||
print(String(format: "\t- path_count : %llx", d!.pointee.path_count));
|
||||
print(String(format: "\t- version : %d.%d.%d", d!.pointee.ver_major, d!.pointee.ver_minor, d!.pointee.ver_rev));
|
||||
print(String(format: "\t- paths:\n"));
|
||||
|
||||
/*
|
||||
for i in 0...d!.pointee.pathCount {
|
||||
// ...
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Helper function to display details about local ZeroTier netif (on event) -- Can be ignored
|
||||
let printNetifDetails : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let d = msg?.pointee.netif;
|
||||
print(String(format: "\t- net_id : %llx", d!.pointee.net_id));
|
||||
print(String(format: "\t- mac : %llx", d!.pointee.mac));
|
||||
print(String(format: "\t- mtu : %d", d!.pointee.mtu));
|
||||
}
|
||||
|
||||
// Handle ZeroTier events (e.g. successful node start, join, leave, etc) --- Can be ignored
|
||||
let onZeroTierEvent : @convention(c) (UnsafeMutableRawPointer?) -> Void =
|
||||
{
|
||||
(msgPtr) -> Void in
|
||||
let msg = msgPtr?.bindMemory(to: zts_event_msg_t.self, capacity: 1)
|
||||
let eventCode = zts_event_t(rawValue: UInt32(msg!.pointee.event_code));
|
||||
let network = msg?.pointee.network;
|
||||
let peer = msg?.pointee.peer;
|
||||
|
||||
print("code:", eventCode);
|
||||
|
||||
|
||||
|
||||
// Node events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_UP)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_UP\n")
|
||||
//printNodeDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_ONLINE)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_ONLINE\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_OFFLINE)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_OFFLINE\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_DOWN)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NODE_FATAL_ERROR)
|
||||
{
|
||||
print("ZTS_EVENT_NODE_FATAL_ERROR\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Network events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_NOT_FOUND)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_NOT_FOUND (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_REQ_CONFIG (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_ACCESS_DENIED)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_ACCESS_DENIED (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_READY_IP4)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_READY_IP4 (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_READY_IP6)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_READY_IP6 (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_DOWN)
|
||||
{
|
||||
let networkId:UInt64 = network!.pointee.net_id
|
||||
print(String(format: "ZTS_EVENT_NETWORK_DOWN (%llx)", networkId))
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETWORK_UPDATE)
|
||||
{
|
||||
print("ZTS_EVENT_NETWORK_UPDATE\n")
|
||||
//printNetworkDetails(msg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Address assignment events
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_ADDED_IP4)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_ADDED_IP4\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_ADDED_IP6)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_ADDED_IP6\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_REMOVED_IP4)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_REMOVED_IP4\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_ADDR_REMOVED_IP6)
|
||||
{
|
||||
print("ZTS_EVENT_ADDR_REMOVED_IP6\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Remote peer events
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_DIRECT)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_DIRECT (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_RELAY)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_RELAY (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_PATH_DISCOVERED (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_PEER_PATH_DEAD)
|
||||
{
|
||||
let peerId:UInt64 = peer!.pointee.peer_id
|
||||
print(String(format: "ZTS_EVENT_PEER_PATH_DEAD (%llx)", peerId))
|
||||
//printPeerDetails(msg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Network stack events
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_UP) {
|
||||
print("ZTS_EVENT_NETIF_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_DOWN) {
|
||||
print("ZTS_EVENT_NETIF_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_REMOVED) {
|
||||
print("ZTS_EVENT_NETIF_REMOVED\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_LINK_UP) {
|
||||
print("ZTS_EVENT_NETIF_LINK_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_NETIF_LINK_DOWN) {
|
||||
print("ZTS_EVENT_NETIF_LINK_DOWN\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_STACK_UP) {
|
||||
print("ZTS_EVENT_STACK_UP\n")
|
||||
}
|
||||
|
||||
if (eventCode == ZTS_EVENT_STACK_DOWN) {
|
||||
print("ZTS_EVENT_STACK_DOWN\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Example user application logic
|
||||
func main()
|
||||
{
|
||||
// Configure local ZeroTier node
|
||||
|
||||
zts_init_set_event_handler(onZeroTierEvent);
|
||||
zts_init_set_port(9993);
|
||||
|
||||
// Start local ZeroTier node
|
||||
|
||||
zts_node_start();
|
||||
|
||||
// Wait for local ZeroTier node to come online
|
||||
|
||||
while(zts_node_is_online() != 1) {
|
||||
print("waiting for node", String(format:"%llX", zts_node_get_id()), "to come online ...");
|
||||
zts_util_delay(1000);
|
||||
}
|
||||
|
||||
// Join ZeroTier network
|
||||
|
||||
let nwid : UInt64 = 0xdb64858fed5a98b7; // Specify your network ID here
|
||||
print("Joining network", String(format:"%llX", nwid));
|
||||
zts_net_join(nwid);
|
||||
|
||||
// Example using raw BSD sockets
|
||||
|
||||
// example_raw_sockets();
|
||||
|
||||
// Idle so that app doesn't exit
|
||||
|
||||
print("Idling...");
|
||||
while(true) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
func example_raw_sockets()
|
||||
{
|
||||
// Create address structure
|
||||
let addr_str = "0.0.0.0"
|
||||
let port = 8080
|
||||
var in4 = zts_sockaddr_in(sin_len: UInt8(MemoryLayout<zts_sockaddr_in>.size),
|
||||
sin_family: UInt8(ZTS_AF_INET),
|
||||
sin_port: UInt16(port).bigEndian,
|
||||
sin_addr: zts_in_addr(s_addr: 0),
|
||||
sin_zero: (0,0,0,0,0,0,0,0))
|
||||
zts_inet_pton(ZTS_AF_INET, addr_str, &(in4.sin_addr));
|
||||
|
||||
print("fd=", zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0));
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// ios_example_appTests.swift
|
||||
// ios-example-appTests
|
||||
//
|
||||
// Created by zt on 7/17/23.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import ios_example_app
|
||||
|
||||
final class ios_example_appTests: XCTestCase {
|
||||
|
||||
override func setUpWithError() throws {
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
override func tearDownWithError() throws {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
func testExample() throws {
|
||||
// This is an example of a functional test case.
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct results.
|
||||
// Any test you write for XCTest can be annotated as throws and async.
|
||||
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
|
||||
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
|
||||
}
|
||||
|
||||
func testPerformanceExample() throws {
|
||||
// This is an example of a performance test case.
|
||||
self.measure {
|
||||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// ios_example_appUITests.swift
|
||||
// ios-example-appUITests
|
||||
//
|
||||
// Created by zt on 7/17/23.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
final class ios_example_appUITests: XCTestCase {
|
||||
|
||||
override func setUpWithError() throws {
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
|
||||
// In UI tests it is usually best to stop immediately when a failure occurs.
|
||||
continueAfterFailure = false
|
||||
|
||||
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
|
||||
}
|
||||
|
||||
override func tearDownWithError() throws {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
func testExample() throws {
|
||||
// UI tests must launch the application that they test.
|
||||
let app = XCUIApplication()
|
||||
app.launch()
|
||||
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct results.
|
||||
}
|
||||
|
||||
func testLaunchPerformance() throws {
|
||||
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
|
||||
// This measures how long it takes to launch your application.
|
||||
measure(metrics: [XCTApplicationLaunchMetric()]) {
|
||||
XCUIApplication().launch()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// ios_example_appUITestsLaunchTests.swift
|
||||
// ios-example-appUITests
|
||||
//
|
||||
// Created by zt on 7/17/23.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
final class ios_example_appUITestsLaunchTests: XCTestCase {
|
||||
|
||||
override class var runsForEachTargetApplicationUIConfiguration: Bool {
|
||||
true
|
||||
}
|
||||
|
||||
override func setUpWithError() throws {
|
||||
continueAfterFailure = false
|
||||
}
|
||||
|
||||
func testLaunch() throws {
|
||||
let app = XCUIApplication()
|
||||
app.launch()
|
||||
|
||||
// Insert steps here to perform after app launch but before taking a screenshot,
|
||||
// such as logging into a test account or navigating somewhere in the app
|
||||
|
||||
let attachment = XCTAttachment(screenshot: app.screenshot())
|
||||
attachment.name = "Launch Screen"
|
||||
attachment.lifetime = .keepAlways
|
||||
add(attachment)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
framework module zt {
|
||||
umbrella header "ZeroTierSockets.h"
|
||||
|
||||
export *
|
||||
module * { export * }
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user