aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model/PeerConfiguration.swift
blob: 7fd3f87e856e0226889f56829f3ca3dccf688859 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: MIT
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

import Foundation

struct PeerConfiguration {
    var publicKey: Data
    var preSharedKey: Data? {
        didSet(value) {
            if let value = value {
                if value.count != TunnelConfiguration.keyLength {
                    fatalError("Invalid preshared key")
                }
            }
        }
    }
    var allowedIPs = [IPAddressRange]()
    var endpoint: Endpoint?
    var persistentKeepAlive: UInt16?
    var rxBytes: UInt64?
    var txBytes: UInt64?
    var lastHandshakeTime: Date?

    init(publicKey: Data) {
        self.publicKey = publicKey
        if publicKey.count != TunnelConfiguration.keyLength {
            fatalError("Invalid public key")
        }
    }
}

extension PeerConfiguration: Equatable {
    static func == (lhs: PeerConfiguration, rhs: PeerConfiguration) -> Bool {
        return lhs.publicKey == rhs.publicKey &&
            lhs.preSharedKey == rhs.preSharedKey &&
            Set(lhs.allowedIPs) == Set(rhs.allowedIPs) &&
            lhs.endpoint == rhs.endpoint &&
            lhs.persistentKeepAlive == rhs.persistentKeepAlive
    }
}

extension PeerConfiguration: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(publicKey)
        hasher.combine(preSharedKey)
        hasher.combine(Set(allowedIPs))
        hasher.combine(endpoint)
        hasher.combine(persistentKeepAlive)

    }
}