source: launchers/macosx/I2PLauncher/routermgmt/RouterProcessStatus.swift @ 3c0a8cf

Last change on this file since 3c0a8cf was 3c0a8cf, checked in by meeh <meeh@…>, 21 months ago

Mac OSX Launcher: A lot of bugfixes, refactoring and cleanup.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1//
2//  RouterProcessStatus.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 18/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10import AppKit
11
12@objc class RouterProcessStatus : NSObject {
13 
14  /**
15   *
16   * Why the functions bellow? Because the Objective-C bridge is limited, it can't do Swift "static's" over it.
17   *
18   **/
19 
20  @objc func setRouterStatus(_ isRunning: Bool = false) {
21    RouterProcessStatus.isRouterRunning = isRunning
22  }
23 
24  @objc func setRouterRanByUs(_ ranByUs: Bool = false) {
25    RouterProcessStatus.isRouterChildProcess = ranByUs
26  }
27 
28  @objc func getRouterIsRunning() -> Bool {
29    if (RouterProcessStatus.isRouterRunning) {
30      return false;
31    } else {
32      let running: Bool = RouterProcessStatus.isRouterRunning
33      return running
34    }
35  }
36 
37  @objc func getJavaHome() -> String {
38    return RouterProcessStatus.knownJavaBinPath!
39  }
40 
41  @objc func setJavaHome(_ home: String) {
42    NSLog("Setting known java to %s", home)
43    RouterProcessStatus.knownJavaBinPath = home
44  }
45}
46
47extension RouterProcessStatus {
48  static var isRouterRunning : Bool = false
49  static var isRouterChildProcess : Bool = false
50  static var routerVersion : String? = Optional.none
51  static var routerUptime : String? = Optional.none
52  static var routerStartedAt : Date? = Optional.none
53  static var knownJavaBinPath : String? = Optional.none
54  static var i2pDirectoryPath : String = NSHomeDirectory() + "/Library/I2P"
55 
56  static var knownRouterSubTaskRef : I2PSubprocess? = Optional.none
57 
58}
59
60
61
62
63extension RouterProcessStatus {
64  static func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String){
65   
66    let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
67    if socketFileDescriptor == -1 {
68      return (false, "SocketCreationFailed, \(descriptionOfLastError())")
69    }
70   
71    var addr = sockaddr_in()
72    let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
73    addr.sin_len = __uint8_t(sizeOfSockkAddr)
74    addr.sin_family = sa_family_t(AF_INET)
75    addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
76    addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
77    addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
78    var bind_addr = sockaddr()
79    memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
80   
81    if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
82      let details = descriptionOfLastError()
83      release(socket: socketFileDescriptor)
84      return (false, "\(port), BindFailed, \(details)")
85    }
86    if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
87      let details = descriptionOfLastError()
88      release(socket: socketFileDescriptor)
89      return (false, "\(port), ListenFailed, \(details)")
90    }
91    release(socket: socketFileDescriptor)
92    return (true, "\(port) is free for use")
93  }
94 
95  static func release(socket: Int32) {
96    Darwin.shutdown(socket, SHUT_RDWR)
97    close(socket)
98  }
99  static func descriptionOfLastError() -> String {
100    return String(cString: UnsafePointer(strerror(errno)))
101  }
102}
103
Note: See TracBrowser for help on using the repository browser.