|
| 1 | +// |
| 2 | +// Socket+Android.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 26/09/2024. |
| 6 | +// Copyright © 2024 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +#if canImport(Android) |
| 33 | +import Android |
| 34 | + |
| 35 | +// from linux/eventpoll.h, not imported by clang importer |
| 36 | +let EPOLLET: UInt32 = 1 << 31; |
| 37 | + |
| 38 | +public extension Socket { |
| 39 | + typealias FileDescriptorType = Int32 |
| 40 | +} |
| 41 | + |
| 42 | +extension Socket.FileDescriptor { |
| 43 | + static let invalid = Socket.FileDescriptor(rawValue: -1) |
| 44 | +} |
| 45 | + |
| 46 | +extension Socket { |
| 47 | + static let stream = Int32(SOCK_STREAM) |
| 48 | + static let in_addr_any = Android.in_addr(s_addr: Android.in_addr_t(0)) |
| 49 | + |
| 50 | + static func makeAddressINET(port: UInt16) -> Android.sockaddr_in { |
| 51 | + Android.sockaddr_in( |
| 52 | + sin_family: sa_family_t(AF_INET), |
| 53 | + sin_port: port.bigEndian, |
| 54 | + sin_addr: in_addr_any, |
| 55 | + __pad: (0, 0, 0, 0, 0, 0, 0, 0) |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + static func makeAddressINET6(port: UInt16) -> Android.sockaddr_in6 { |
| 60 | + Android.sockaddr_in6( |
| 61 | + sin6_family: sa_family_t(AF_INET6), |
| 62 | + sin6_port: port.bigEndian, |
| 63 | + sin6_flowinfo: 0, |
| 64 | + sin6_addr: in6addr_any, |
| 65 | + sin6_scope_id: 0 |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + static func makeAddressLoopback(port: UInt16) -> Android.sockaddr_in6 { |
| 70 | + Android.sockaddr_in6( |
| 71 | + sin6_family: sa_family_t(AF_INET6), |
| 72 | + sin6_port: port.bigEndian, |
| 73 | + sin6_flowinfo: 0, |
| 74 | + sin6_addr: in6addr_loopback, |
| 75 | + sin6_scope_id: 0 |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + static func makeAddressUnix(path: String) -> Android.sockaddr_un { |
| 80 | + var addr = Android.sockaddr_un() |
| 81 | + addr.sun_family = sa_family_t(AF_UNIX) |
| 82 | + let pathCount = min(path.utf8.count, 104) |
| 83 | + let len = UInt8(MemoryLayout<UInt8>.size + MemoryLayout<sa_family_t>.size + pathCount + 1) |
| 84 | + _ = withUnsafeMutablePointer(to: &addr.sun_path.0) { ptr in |
| 85 | + path.withCString { |
| 86 | + strncpy(ptr, $0, Int(len)) |
| 87 | + } |
| 88 | + } |
| 89 | + return addr |
| 90 | + } |
| 91 | + |
| 92 | + static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> FileDescriptorType { |
| 93 | + Android.socket(domain, type, `protocol`) |
| 94 | + } |
| 95 | + |
| 96 | + static func socketpair(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> (FileDescriptorType, FileDescriptorType) { |
| 97 | + var sockets: [Int32] = [-1, -1] |
| 98 | + _ = Android.socketpair(domain, type, `protocol`, &sockets) |
| 99 | + return (sockets[0], sockets[1]) |
| 100 | + } |
| 101 | + |
| 102 | + static func fcntl(_ fd: Int32, _ cmd: Int32) -> Int32 { |
| 103 | + Android.fcntl(fd, cmd) |
| 104 | + } |
| 105 | + |
| 106 | + static func fcntl(_ fd: Int32, _ cmd: Int32, _ value: Int32) -> Int32 { |
| 107 | + Android.fcntl(fd, cmd, value) |
| 108 | + } |
| 109 | + |
| 110 | + static func setsockopt(_ fd: Int32, _ level: Int32, _ name: Int32, |
| 111 | + _ value: UnsafeRawPointer!, _ len: socklen_t) -> Int32 { |
| 112 | + Android.setsockopt(fd, level, name, value, len) |
| 113 | + } |
| 114 | + |
| 115 | + static func getsockopt(_ fd: Int32, _ level: Int32, _ name: Int32, |
| 116 | + _ value: UnsafeMutableRawPointer!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 { |
| 117 | + Android.getsockopt(fd, level, name, value, len) |
| 118 | + } |
| 119 | + |
| 120 | + static func getpeername(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 { |
| 121 | + Android.getpeername(fd, addr, len) |
| 122 | + } |
| 123 | + |
| 124 | + static func getsockname(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 { |
| 125 | + Android.getsockname(fd, addr, len) |
| 126 | + } |
| 127 | + |
| 128 | + static func inet_ntop(_ domain: Int32, _ addr: UnsafeRawPointer!, |
| 129 | + _ buffer: UnsafeMutablePointer<CChar>!, _ addrLen: socklen_t) throws { |
| 130 | + if Android.inet_ntop(domain, addr, buffer, addrLen) == nil { |
| 131 | + throw SocketError.makeFailed("inet_ntop") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + static func inet_pton(_ domain: Int32, _ buffer: UnsafePointer<CChar>!, _ addr: UnsafeMutableRawPointer!) -> Int32 { |
| 136 | + Android.inet_pton(domain, buffer, addr) |
| 137 | + } |
| 138 | + |
| 139 | + static func bind(_ fd: Int32, _ addr: UnsafePointer<sockaddr>!, _ len: socklen_t) -> Int32 { |
| 140 | + Android.bind(fd, addr, len) |
| 141 | + } |
| 142 | + |
| 143 | + static func listen(_ fd: Int32, _ backlog: Int32) -> Int32 { |
| 144 | + Android.listen(fd, backlog) |
| 145 | + } |
| 146 | + |
| 147 | + static func accept(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 { |
| 148 | + Android.accept(fd, addr, len) |
| 149 | + } |
| 150 | + |
| 151 | + static func connect(_ fd: Int32, _ addr: UnsafePointer<sockaddr>!, _ len: socklen_t) -> Int32 { |
| 152 | + Android.connect(fd, addr, len) |
| 153 | + } |
| 154 | + |
| 155 | + static func read(_ fd: Int32, _ buffer: UnsafeMutableRawPointer!, _ nbyte: Int) -> Int { |
| 156 | + Android.read(fd, buffer, nbyte) |
| 157 | + } |
| 158 | + |
| 159 | + static func write(_ fd: Int32, _ buffer: UnsafeRawPointer!, _ nbyte: Int) -> Int { |
| 160 | + Android.send(fd, buffer, nbyte, Int32(MSG_NOSIGNAL)) |
| 161 | + } |
| 162 | + |
| 163 | + static func close(_ fd: Int32) -> Int32 { |
| 164 | + Android.close(fd) |
| 165 | + } |
| 166 | + |
| 167 | + static func unlink(_ addr: UnsafePointer<CChar>!) -> Int32 { |
| 168 | + Android.unlink(addr) |
| 169 | + } |
| 170 | + |
| 171 | + static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: UInt32, _ tmo_p: Int32) -> Int32 { |
| 172 | + Android.poll(fds, nfds_t(nfds), tmo_p) |
| 173 | + } |
| 174 | + |
| 175 | + static func pollfd(fd: FileDescriptorType, events: Int16, revents: Int16) -> Android.pollfd { |
| 176 | + Android.pollfd(fd: fd, events: events, revents: revents) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +#endif |
0 commit comments