punchnet-macos/Tun/SDLMessage.swift
2025-05-12 16:25:33 +08:00

157 lines
3.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SDLPacketType.swift
// Tun
//
// Created by on 2024/4/10.
//
import Foundation
import NIOCore
//
enum SDLPacketType: UInt8 {
case empty = 0x00
case registerSuper = 0x01
case registerSuperAck = 0x02
case registerSuperNak = 0x04
case unregisterSuper = 0x05
case queryInfo = 0x06
case peerInfo = 0x07
case ping = 0x08
case pong = 0x09
//
case event = 0x10
// ,
case command = 0x11
case commandAck = 0x12
//
case flowTracer = 0x15
case register = 0x20
case registerAck = 0x21
case stunRequest = 0x30
case stunReply = 0x31
case stunProbe = 0x32
case stunProbeReply = 0x33
case data = 0xFF
}
//
enum SDLUpgradeType: UInt32 {
case none = 0
case normal = 1
case force = 2
}
// Id
struct SDLIdGenerator {
// id
private var packetId: UInt32
init(seed packetId: UInt32) {
self.packetId = packetId
}
mutating func nextId() -> UInt32 {
let packetId = self.packetId
self.packetId = packetId + 1
return packetId
}
}
//
//
enum SDLEventType: UInt8 {
case natChanged = 0x03
case sendRegister = 0x04
case networkShutdown = 0xFF
}
enum SDLEvent {
case natChanged(SDLNatChangedEvent)
case sendRegister(SDLSendRegisterEvent)
case networkShutdown(SDLNetworkShutdownEvent)
}
// --MARK:
enum SDLCommandType: UInt8 {
case changeNetwork = 0x01
}
enum SDLCommand {
case changeNetwork(SDLChangeNetworkCommand)
}
// --MARK:
// Attr
enum SDLProbeAttr: UInt8 {
case none = 0
case port = 1
case peer = 2
}
// Nak
enum SDLNAKErrorCode: UInt8 {
case invalidToken = 1
case nodeDisabled = 2
case noIpAddress = 3
case networkFault = 4
case internalFault = 5
}
extension SDLV4Info {
func socketAddress() -> SocketAddress? {
let address = "\(v4[0]).\(v4[1]).\(v4[2]).\(v4[3])"
return try? SocketAddress.makeAddressResolvingHost(address, port: Int(port))
}
}
extension SDLStunProbeReply {
func socketAddress() -> SocketAddress? {
let address = SDLUtil.int32ToIp(self.ip)
return try? SocketAddress.makeAddressResolvingHost(address, port: Int(port))
}
}
// --MARK: ,
enum SDLHoleInboundMessage {
case stunReply(SDLStunReply)
case stunProbeReply(SDLStunProbeReply)
case data(SDLData)
case register(SDLRegister)
case registerAck(SDLRegisterAck)
}
// --MARK:
struct SDLSuperInboundMessage {
let msgId: UInt32
let packet: InboundPacket
enum InboundPacket {
case empty
case registerSuperAck(SDLRegisterSuperAck)
case registerSuperNak(SDLRegisterSuperNak)
case peerInfo(SDLPeerInfo)
case pong
case event(SDLEvent)
case command(SDLCommand)
}
}