Related
In my application want to check port is open or not. Here refer this link
iOS SDK: How can I check if a port is open?
But dnt get any solution. And also refer these two github source,
https://github.com/swiftsocket/SwiftSocket
https://github.com/robbiehanson/CocoaAsyncSocket
But dnt any solution. Any one help how check port is open or not. Thanks Advance.
Please use the below method to port is open or not
func isPortOpen(port: in_port_t) -> Bool {
let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
if socketFileDescriptor == -1 {
return false
}
var addr = sockaddr_in()
let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
addr.sin_len = __uint8_t(sizeOfSockkAddr)
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
var bind_addr = sockaddr()
memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
return false
}
if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
return false
}
return true
}
Use like:
let port = UInt16(10000)
print(isPortOpen(port:port))
I cannot add comments, so I am adding this as a separate answer. I followed Surani's answer, but encountered errors when using the port later in the application. I found I had to manually close the socket before returning the response.
So I modified the last 4 lines to
let isOpen = listen(socketFileDescriptor, SOMAXCONN ) != -1
Darwin.close(socketFileDescriptor)
return isOpen
Here is the revised version of Surani's answer with the socket closing code
func isPortOpen(port: in_port_t) -> Bool {
let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
if socketFileDescriptor == -1 {
return false
}
var addr = sockaddr_in()
let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
addr.sin_len = __uint8_t(sizeOfSockkAddr)
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
var bind_addr = sockaddr()
memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
return false
}
let isOpen = listen(socketFileDescriptor, SOMAXCONN ) != -1
Darwin.close(socketFileDescriptor)
return isOpen
}
Use like:
let port = UInt16(10000)
print(isPortOpen(port:port))
I'm migrating my Swift code from version 2 to 3 and there is this issue:
Argument labels '(count:, repeatedValue:)' do not match any available overloads
My code
static func getWiFiAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }
let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
let name = String(cString: (interface?.ifa_name)!)
var addr = interface?.ifa_addr.pointee
// issue while assigning to hostname variable
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
getnameinfo(&addr, socklen_t(interface.ifa_addr.memory.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST)
address = String.fromCString(hostname)
}
}
freeifaddrs(ifaddr)
}
if address == nil {
address = ""
}
return address
}
In Swift 3 they changed the function for no good reason. You should have
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
Note that, not only are the arguments reversed but repeatedValue: -> repeating:
can someone tell me how to find out in Swift2 if Port is busy?
Because I write a mac app that has a self written Tcp server, but sometimes it wont start up because it "cant bind to port". So how can I check if the port is not used, to block the start button of the Tcp server until the port is free, again?
And I do not want a new framework.
Thanks
mainly code taken from Swifter: https://github.com/glock45/swifter
func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String){
let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
if socketFileDescriptor == -1 {
return (false, "SocketCreationFailed, \(descriptionOfLastError())")
}
var addr = sockaddr_in()
addr.sin_len = __uint8_t(sizeof(sockaddr_in))
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
var bind_addr = sockaddr()
memcpy(&bind_addr, &addr, Int(sizeof(sockaddr_in)))
if bind(socketFileDescriptor, &bind_addr, socklen_t(sizeof(sockaddr_in))) == -1 {
let details = descriptionOfLastError()
release(socketFileDescriptor)
return (false, "\(port), BindFailed, \(details)")
}
if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
let details = descriptionOfLastError()
release(socketFileDescriptor)
return (false, "\(port), ListenFailed, \(details)")
}
release(socketFileDescriptor)
return (true, "\(port) is free for use")
}
func release(socket: Int32) {
Darwin.shutdown(socket, SHUT_RDWR)
close(socket)
}
func descriptionOfLastError() -> String {
return String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)"
}
Update of the correct answer for Swift 4:
func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String) {
let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
if socketFileDescriptor == -1 {
return (false, "SocketCreationFailed, \(descriptionOfLastError())")
}
var addr = sockaddr_in()
let sizeOfSockkAddr = MemoryLayout<sockaddr_in>.size
addr.sin_len = __uint8_t(sizeOfSockkAddr)
addr.sin_family = sa_family_t(AF_INET)
addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
var bind_addr = sockaddr()
memcpy(&bind_addr, &addr, Int(sizeOfSockkAddr))
if Darwin.bind(socketFileDescriptor, &bind_addr, socklen_t(sizeOfSockkAddr)) == -1 {
let details = descriptionOfLastError()
release(socket: socketFileDescriptor)
return (false, "\(port), BindFailed, \(details)")
}
if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
let details = descriptionOfLastError()
release(socket: socketFileDescriptor)
return (false, "\(port), ListenFailed, \(details)")
}
release(socket: socketFileDescriptor)
return (true, "\(port) is free for use")
}
func release(socket: Int32) {
Darwin.shutdown(socket, SHUT_RDWR)
close(socket)
}
func descriptionOfLastError() -> String {
return String.init(cString: (UnsafePointer(strerror(errno))))
}
EDIT:
example for calling this function:
var portNum: UInt16 = 0
for i in 50000..<65000 {
let (isFree, _) = checkTcpPortForListen(port: UInt16(i))
if isFree == true {
portNum = UInt16(i)
break;
}
}
I have a AudioFileStream_PacketsProc callback set during an AudioFileStreamOpen which handles converting audio packets into PCM using AudioConverterFillComplexBuffer. The issue that I am having is that I am getting a -50 OSStatus (paramErr) after AudioConverterFillComplexBuffer is called. Below is a snippet of what parameters were used in AudioConverterFillComplexBuffer and how they were made:
audioConverterRef = AudioConverterRef()
// AudioConvertInfo is a struct that contains information
// for the converter regarding the number of packets and
// which audiobuffer is being allocated
convertInfo? = AudioConvertInfo(done: false, numberOfPackets: numberPackets, audioBuffer: buffer,
packetDescriptions: packetDescriptions)
var framesToDecode: UInt32 = pcmBufferTotalFrameCount! - end
var localPcmAudioBuffer = AudioBuffer()
localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
localPcmAudioBuffer = localPcmBufferList.mBuffers
localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels
var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
localPcmAudioBuffer = localPcmBufferList.mBuffers
AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)
Does what could possibly be causing the param error?
Here is the full method for the callback if needed:
func handleAudioPackets(inputData: UnsafePointer<Void>, numberBytes: UInt32, numberPackets: UInt32, packetDescriptions: UnsafeMutablePointer<AudioStreamPacketDescription>) {
if currentlyReadingEntry == nil {
print("currentlyReadingEntry = nil")
return
}
if currentlyReadingEntry.parsedHeader == false {
print("currentlyReadingEntry.parsedHeader == false")
return
}
if disposedWasRequested == true {
print("disposedWasRequested == true")
return
}
guard let audioConverterRef = audioConverterRef else {
return
}
if seekToTimeWasRequested == true && currentlyReadingEntry.calculatedBitRate() > 0.0 {
wakeupPlaybackThread()
print("seekToTimeWasRequested == true && currentlyReadingEntry.calculatedBitRate() > 0.0")
return
}
discontinuous = false
var buffer = AudioBuffer()
buffer.mNumberChannels = audioConverterAudioStreamBasicDescription.mChannelsPerFrame
buffer.mDataByteSize = numberBytes
buffer.mData = UnsafeMutablePointer<Void>(inputData)
convertInfo? = AudioConvertInfo(done: false, numberOfPackets: numberPackets, audioBuffer: buffer,
packetDescriptions: packetDescriptions)
if packetDescriptions != nil && currentlyReadingEntry.processedPacketsCount < maxCompressedBacketsForBitrateCalculation {
let count: Int = min(Int(numberPackets), Int(maxCompressedBacketsForBitrateCalculation - currentlyReadingEntry.processedPacketsCount!))
for var i = 0;i < count;++i{
let packetSize: Int32 = Int32(packetDescriptions[i].mDataByteSize)
OSAtomicAdd32(packetSize, ¤tlyReadingEntry.processedPacketsSizeTotal!)
OSAtomicIncrement32(¤tlyReadingEntry.processedPacketsCount!)
}
}
while true {
OSSpinLockLock(&pcmBufferSpinLock)
var used: UInt32 = pcmBufferUsedFrameCount!
var start: UInt32 = pcmBufferFrameStartIndex!
var end = (pcmBufferFrameStartIndex! + pcmBufferUsedFrameCount!) % pcmBufferTotalFrameCount!
var framesLeftInsideBuffer = pcmBufferTotalFrameCount! - used
OSSpinLockUnlock(&pcmBufferSpinLock)
if framesLeftInsideBuffer == 0 {
pthread_mutex_lock(&playerMutex)
while true {
OSSpinLockLock(&pcmBufferSpinLock)
used = pcmBufferUsedFrameCount!
start = pcmBufferFrameStartIndex!
end = (pcmBufferFrameStartIndex! + pcmBufferUsedFrameCount!) % pcmBufferTotalFrameCount!
framesLeftInsideBuffer = pcmBufferTotalFrameCount! - used
OSSpinLockUnlock(&pcmBufferSpinLock)
if framesLeftInsideBuffer > 0 {
break
}
if (disposedWasRequested == true
|| internalState == SSPlayerInternalState.Disposed) {
pthread_mutex_unlock(&playerMutex)
return
}
if (seekToTimeWasRequested == true && currentlyPlayingEntry.calculatedBitRate() > 0.0)
{
pthread_mutex_unlock(&playerMutex)
wakeupPlaybackThread()
return;
}
waiting = true
pthread_cond_wait(&playerThreadReadyCondition, &playerMutex)
waiting = false
}
pthread_mutex_unlock(&playerMutex)
}
var localPcmAudioBuffer = AudioBuffer()
var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
localPcmAudioBuffer = localPcmBufferList.mBuffers
if end >= start {
var framesAdded: UInt32 = 0
var framesToDecode: UInt32 = pcmBufferTotalFrameCount! - end
localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels
AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)
framesAdded = framesToDecode
if status == 100 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
return
} else if status != 0 {
print("error")
return
}
framesToDecode = start
if framesToDecode == 0 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
continue
}
localPcmAudioBuffer.mData = pcmAudioBuffer!.mData
localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!
localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels
AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)
let decodedFramesAdded = framesAdded + framesToDecode
framesAdded = decodedFramesAdded
if status == 100 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
return
} else if status == 0 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
continue
} else if status != 0 {
print("error")
return
} else {
var framesAdded: UInt32 = 0
var framesToDecode: UInt32 = start - end
localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels
var convertInfoo: UnsafePointer<Void> = unsafeBitCast(convertInfo, UnsafePointer<Void>.self)
status = AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfoo, &framesToDecode, &localPcmBufferList, nil)
framesAdded = framesToDecode
if status == 100 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
return
} else if status == 0 {
OSSpinLockLock(&pcmBufferSpinLock)
let newCount = pcmBufferUsedFrameCount! + framesAdded
pcmBufferUsedFrameCount = newCount
OSSpinLockUnlock(&pcmBufferSpinLock);
OSSpinLockLock(¤tlyReadingEntry!.spinLock!)
let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
currentlyReadingEntry!.framesQueued! = newFramesAddedCount
OSSpinLockUnlock(¤tlyReadingEntry!.spinLock!)
continue
} else if status != 0 {
print("error")
return
}
}
}
}
}
Hej #3254523, I have some answers with possible solutions for you. I hope to guide you in the right way in spite of I am not expert in this major. So, the problem is for sure the configuration of:
AudioBufferList
Here the links that probes the hints of this -50 OSStatus related to the AudioBufferList:
http://lists.apple.com/archives/coreaudio-api/2012/Apr/msg00041.html
https://forums.developer.apple.com/thread/6313
Now, we have to focus in a solutions. Looking through your AudioBufferList, you have no assigned any value but mNumberBuffers which is 1. Try to change the values in the following way(as it shown in the second link):
var localPcmBufferList = AudioBufferList(mNumberBuffers: 2, mBuffers: AudioBuffer(mNumberChannels: 2, mDataByteSize: UInt32(buffer.count), mData: &buffer))
If still is not working, we have to focus to correct it properly, hence here you can find the solution to the -50 OSStatus in AudioConverterFillComplexBuffer although not in swift:
AudioConverterFillComplexBuffer return -50 (paramErr)
iPhone: AudioBufferList init and release
a nice example taken from AudioKit
Audio File Services ( to read the MP3 format and write AIFF or WAV )
Audio File Conversion Services ( to convert the MP3 data to PCM, or to encode from PCM to some other codec if you were to write a file )
A given converter can't convert between two encoded formats.
you can do MP3-to-PCM or PCM-to-AAC
While to do MP3-to-AAC, needs two converters
Easy to do with tanersener/mobile-ffmpeg
let command = "-i input.mp3 -f s16le -acodec pcm_s16le -ac 1 -ar 44100 output.raw"
let result = MobileFFmpeg.execute(command)
switch result {
case RETURN_CODE_SUCCESS:
print("command exe completed successfully.\n")
case RETURN_CODE_CANCEL:
print("command exe cancelled by user.\n")
default:
print("command exe failed with rc=\(result) and output=\(String(describing: MobileFFmpegConfig.getLastCommandOutput())).\n")
}
I'm trying to use Secure Transport with BSD sockets using Swift. It seems like it should be simple enough, but I can't get it to work and documentation on the subject is scarce.
I've boiled my issue down to a simple "Socket" class, where I've (to the best of my knowledge) fulfilled the requirements of Secure Transport.
import Cocoa
class Socket: NSObject {
private let hello = "Hello!"
private var socketfd: Int32
private var sock_addr: sockaddr
private var sslContext: SSLContext?
var sslWriteCallbackFunc: SSLWriteFunc {
get {
let ump = UnsafeMutablePointer<((SSLConnectionRef, UnsafePointer<Void>,
UnsafeMutablePointer<Int>) -> OSStatus)>.alloc(1)
ump.initialize(sslWriteCallback)
return CFunctionPointer<((SSLConnectionRef, UnsafePointer<Void>,
UnsafeMutablePointer<Int>) -> OSStatus)>(COpaquePointer(ump))
}
}
var sslReadCallbackFunc: SSLReadFunc {
get {
let ump = UnsafeMutablePointer<((SSLConnectionRef, UnsafeMutablePointer<Void>,
UnsafeMutablePointer<Int>) -> OSStatus)>.alloc(1)
ump.initialize(sslReadCallback)
return CFunctionPointer<((SSLConnectionRef, UnsafeMutablePointer<Void>,
UnsafeMutablePointer<Int>) -> OSStatus)>(COpaquePointer(ump))
}
}
init(address: String, port: UInt16) {
socketfd = Darwin.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
var addr = Darwin.sockaddr_in(sin_len: __uint8_t(sizeof(sockaddr_in)), sin_family: sa_family_t(AF_INET), sin_port: CFSwapInt16(port), sin_addr: in_addr(s_addr: inet_addr(address)), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
sock_addr = Darwin.sockaddr(sa_len: 0, sa_family: 0, sa_data: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
Darwin.memcpy(&sock_addr, &addr, Int(sizeof(sockaddr_in)))
super.init()
}
func connect() -> Socket {
let err = Darwin.connect(socketfd, &sock_addr, socklen_t(sizeof(sockaddr_in)))
return self
}
func makeSecure() -> Socket {
if let umc = SSLCreateContext(nil, kSSLClientSide, kSSLStreamType) {
sslContext = umc.takeRetainedValue()
var status = SSLSetIOFuncs(sslContext!, sslReadCallbackFunc, sslWriteCallbackFunc)
status = SSLSetConnection(sslContext!, &socketfd)
SSLHandshake(sslContext!)
}
return self
}
func sendHello() -> Socket {
let bytes = [UInt8](hello.utf8)
let data = NSData(bytes: bytes, length: bytes.count)
let test = UnsafeMutablePointer<Int>.alloc(1)
test.initialize(bytes.count)
self.sslWriteCallback(&socketfd, data: data.bytes, dataLength: test)
return self
}
// MARK: - SSL Callback Methods
func sslReadCallback(connection: SSLConnectionRef,
data: UnsafeMutablePointer<Void>,
dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let bytesRead = read(socketfd, data, UnsafePointer<Int>(dataLength).memory)
return noErr
}
func sslWriteCallback(connection: SSLConnectionRef,
data: UnsafePointer<Void>,
dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let sent = Darwin.sendto(socketfd, data, UnsafePointer<Int>(dataLength).memory, 0, &sock_addr, socklen_t(sizeof(sockaddr_in)))
if (sent < 0) {
let error = NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
println(error.localizedDescription)
} else {
println("Sent \(sent) bytes (\(hello))")
}
return noErr
}
}
I've tested the non-TLS socket communication by making a simple instance:
let socket = Socket(address: "some-ip-address", port: 8080)
socket.connect().sendHello()
and running an echo server on the target machine using netcat. This works fine.
nc -l -p 8080
Trying to wrap the socket in Secure Transport's TLS (calling the makeSecure() method) crashes at the call to SSLHandshake(...) with a EXC_BAD_ADDRESS(code=2, address=...) error. Does anyone have any pointers as to what it is I'm missing here?
EDIT
I can see Console puts out:
04/06/15 09:20:48,000 kernel[0]: Data/Stack execution not permitted: TheProject[pid 29184] at virtual address 0x100602000, protections were read-write
EDIT 2
I got it working with Swift 2 in the Xcode 7 beta. See below.
Starting with Swift 2 included with the Xcode 7 beta, Function Pointers in Swift work and have been greatly simplified. I turned my example above into this, which works:
import Foundation
func sslReadCallback(connection: SSLConnectionRef,
data: UnsafeMutablePointer<Void>,
var dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let socketfd = UnsafePointer<Int32>(connection).memory
let bytesRequested = dataLength.memory
let bytesRead = read(socketfd, data, UnsafePointer<Int>(dataLength).memory)
if (bytesRead > 0) {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(bytesRead)
if bytesRequested > bytesRead {
return Int32(errSSLWouldBlock)
} else {
return noErr
}
} else if (bytesRead == 0) {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(0)
return Int32(errSSLClosedGraceful)
} else {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(0)
switch (errno) {
case ENOENT: return Int32(errSSLClosedGraceful)
case EAGAIN: return Int32(errSSLWouldBlock)
case ECONNRESET: return Int32(errSSLClosedAbort)
default: return Int32(errSecIO)
}
}
}
func sslWriteCallback(connection: SSLConnectionRef,
data: UnsafePointer<Void>,
var dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let socketfd = UnsafePointer<Int32>(connection).memory
let bytesToWrite = dataLength.memory
let bytesWritten = write(socketfd, data, UnsafePointer<Int>(dataLength).memory)
if (bytesWritten > 0) {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(bytesWritten)
if (bytesToWrite > bytesWritten) {
return Int32(errSSLWouldBlock)
} else {
return noErr
}
} else if (bytesWritten == 0) {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(0)
return Int32(errSSLClosedGraceful)
} else {
dataLength = UnsafeMutablePointer<Int>.alloc(1)
dataLength.initialize(0)
if (EAGAIN == errno) {
return Int32(errSSLWouldBlock)
} else {
return Int32(errSecIO)
}
}
}
var socketfd = Darwin.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
var addr = Darwin.sockaddr_in(sin_len: __uint8_t(sizeof(sockaddr_in)), sin_family: sa_family_t(AF_INET), sin_port: CFSwapInt16(8080), sin_addr: in_addr(s_addr: inet_addr("192.168.0.113")), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
var sock_addr = Darwin.sockaddr(sa_len: 0, sa_family: 0, sa_data: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
Darwin.memcpy(&sock_addr, &addr, Int(sizeof(sockaddr_in)))
var err = Darwin.connect(socketfd, &sock_addr, socklen_t(sizeof(sockaddr_in)))
if let umc = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType) {
var sslContext = umc.takeRetainedValue()
SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback)
SSLSetConnection(sslContext, &socketfd)
SSLSetSessionOption(sslContext, kSSLSessionOptionBreakOnClientAuth, Boolean(1))
SSLHandshake(sslContext)
}
The answer by Hans appears to allocate memory unnecessarily. The following is a Swift 3.1 version with more error checking and URL support and grabs the common name to return (rather than actually reading or writing data).
func getCNforSSL(at url:URL, port:UInt16) -> String? {
var socketfd = Darwin.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
guard let ip = urlToIP(url) else {
NSLog("Could not get IP from URL \(url)")
return nil
}
let inAddr = in_addr(s_addr: inet_addr(ip))
var addr = sockaddr_in(sin_len: __uint8_t(MemoryLayout<sockaddr_in>.size),
sin_family: sa_family_t(AF_INET),
sin_port: CFSwapInt16(port),
sin_addr: inAddr,
sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
var sock_addr = sockaddr(sa_len: 0,
sa_family: 0,
sa_data: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
_ = memcpy(&sock_addr, &addr, MemoryLayout<sockaddr_in>.size)
guard connect(socketfd, &sock_addr, socklen_t(MemoryLayout<sockaddr_in>.size)) == 0 else {
NSLog("Failed connection for \(url) port \(port) with error \(Darwin.errno)")
return nil
}
defer {
if close(socketfd) != 0 {
NSLog("Error closing socket for \(url) port \(port) with error \(Darwin.errno)")
}
}
guard let sslContext = SSLCreateContext(kCFAllocatorDefault, .clientSide, .streamType) else {
NSLog("Could not create SSL Context for \(url) port \(port)")
return nil
}
defer {
SSLClose(sslContext)
}
SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback)
SSLSetConnection(sslContext, &socketfd)
SSLSetSessionOption(sslContext, .breakOnServerAuth, true)
var secTrust:SecTrust? = nil
var status:OSStatus = 0
var subject:String? = nil
repeat {
status = SSLHandshake(sslContext)
if status == errSSLPeerAuthCompleted {
SSLCopyPeerTrust(sslContext, &secTrust)
if let trust = secTrust {
// 0 always garunteed to exist
let cert = SecTrustGetCertificateAtIndex(trust, 0)!
subject = SecCertificateCopySubjectSummary(cert) as String?
}
}
} while status == errSSLWouldBlock
guard status == errSSLPeerAuthCompleted else {
NSLog("SSL Handshake Error for \(url) port \(port) OSStatus \(status)")
return nil
}
return subject
}
func sslReadCallback(connection: SSLConnectionRef,
data: UnsafeMutableRawPointer,
dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let socketfd = connection.load(as: Int32.self)
let bytesRequested = dataLength.pointee
let bytesRead = read(socketfd, data, UnsafePointer<Int>(dataLength).pointee)
if (bytesRead > 0) {
dataLength.initialize(to: bytesRead)
if bytesRequested > bytesRead {
return Int32(errSSLWouldBlock)
} else {
return noErr
}
} else if (bytesRead == 0) {
dataLength.initialize(to: 0)
return Int32(errSSLClosedGraceful)
} else {
dataLength.initialize(to: 0)
switch (errno) {
case ENOENT: return Int32(errSSLClosedGraceful)
case EAGAIN: return Int32(errSSLWouldBlock)
case ECONNRESET: return Int32(errSSLClosedAbort)
default: return Int32(errSecIO)
}
}
}
func sslWriteCallback(connection: SSLConnectionRef,
data: UnsafeRawPointer,
dataLength: UnsafeMutablePointer<Int>) -> OSStatus {
let socketfd = connection.load(as: Int32.self)
let bytesToWrite = dataLength.pointee
let bytesWritten = write(socketfd, data, UnsafePointer<Int>(dataLength).pointee)
if (bytesWritten > 0) {
dataLength.initialize(to: bytesWritten)
if (bytesToWrite > bytesWritten) {
return Int32(errSSLWouldBlock)
} else {
return noErr
}
} else if (bytesWritten == 0) {
dataLength.initialize(to: 0)
return Int32(errSSLClosedGraceful)
} else {
dataLength.initialize(to: 0)
if (EAGAIN == errno) {
return Int32(errSSLWouldBlock)
} else {
return Int32(errSecIO)
}
}
}
private func urlToIP(_ url:URL) -> String? {
guard let hostname = url.host else {
return nil
}
guard let host = hostname.withCString({gethostbyname($0)}) else {
return nil
}
guard host.pointee.h_length > 0 else {
return nil
}
var addr = in_addr()
memcpy(&addr.s_addr, host.pointee.h_addr_list[0], Int(host.pointee.h_length))
guard let remoteIPAsC = inet_ntoa(addr) else {
return nil
}
return String.init(cString: remoteIPAsC)
}
I asked a network guru peer your question; this was his response:
This person is SOL because Secure Transport requires that you implement C function callbacks and Swift does not currently support that .
I recommend that developer use CFSocketStream, which takes care of TLS and is easily callable from Swift. See the TLSTool sample code.
https://developer.apple.com/library/mac/samplecode/SC1236/