I'm trying to convert the code of objective C mentioned here - https://gist.github.com/Coeur/1409855/f6df10c79f8cdd0fcb2a0735b99f4b3a74b9f954 to swift
The code I've wrote till now in swift -
class func getMacAddress(_ ifName: String?) -> String? {
var mgmtInfoBase = [Int32](repeating: 0, count: 6)
var msgBuffer: Int8? = nil
var length: size_t
var macAddress = [UInt8](repeating: 0, count: 6)
var interfaceMsgStruct: if_msghdr?
var socketStruct: sockaddr_dl?
var errorFlag: String? = nil
// Setup the management Information Base (mib)
mgmtInfoBase[0] = Int32(Int(CTL_NET)) // Request network subsystem
mgmtInfoBase[1] = Int32(Int(AF_ROUTE)) // Routing table info
mgmtInfoBase[2] = 0
mgmtInfoBase[3] = Int32(Int(AF_LINK)) // Request link layer information
mgmtInfoBase[4] = Int32(Int(NET_RT_IFLIST)) // Request all configured interfaces
mgmtInfoBase[5] = Int32(if_nametoindex(ifName?.utf8CString)) //ERROR: Type of expression is ambiguous without more context
// With all configured interfaces requested, get handle index
if ( mgmtInfoBase[5] == 0) {
errorFlag = "if_nametoindex failure"
} else {
// Get the size of the data available (store in len)
if sysctl(&mgmtInfoBase, 6, nil, &length, nil, 0) < 0 {
errorFlag = "sysctl mgmtInfoBase failure"
} else {
// Alloc memory based on above call
if (msgBuffer = Int8((length))) == nil {
errorFlag = "buffer allocation failure"
} else {
// Get system information, store in buffer
if sysctl(&mgmtInfoBase, 6, &msgBuffer, &length, nil, 0) < 0 {
errorFlag = "sysctl msgBuffer failure"
}
}
}
}
// Before going any further...
if errorFlag != nil {
// Release the buffer memory
if (msgBuffer != nil) {
free(&msgBuffer)
}
return nil
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = msgBuffer as? if_msghdr
// Map to link-level socket structure
socketStruct = (interfaceMsgStruct + 1) as? sockaddr_dl // ERROR: Cannot convert value of type 'if_msghdr?' to expected argument type 'Int'
// Copy link layer address data in socket structure to an array
if socketStruct == nil {
return nil
}
memcpy(&macAddress, socketStruct.sdl_data + socketStruct.sdl_nlen, 6) // ERROR: Type of expression is ambiguous without more context
// Read from char array into a string object, into traditional Mac address format
let macAddressString = String(format: "%02X:%02X:%02X:%02X:%02X:%02X", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5])
// Release the buffer memory
free(&msgBuffer)
return macAddressString
}
I'm getting the errors that I've mentioned. I searched and tried every possible thing and read articles from the documentation but still I couldn't get away with these errors. Please help.
The definitions of the functions for which I'm getting error as mentioned in Darwin.posix.net.if -
public func if_nametoindex(_: UnsafePointer<Int8>!) -> UInt32
public struct if_msghdr {
public var ifm_msglen: UInt16 /* to skip non-understood messages */
public var ifm_version: UInt8 /* future binary compatability */
public var ifm_type: UInt8 /* message type */
public var ifm_addrs: Int32 /* like rtm_addrs */
public var ifm_flags: Int32 /* value of if_flags */
public var ifm_index: UInt16 /* index for associated ifp */
public var ifm_data: if_data /* statistics and other data about if */
public init()
public init(ifm_msglen: UInt16, ifm_version: UInt8, ifm_type: UInt8, ifm_addrs: Int32, ifm_flags: Int32, ifm_index: UInt16, ifm_data: if_data)
}
public func memcpy(_ __dst: UnsafeMutableRawPointer!, _ __src: UnsafeRawPointer!, _ __n: Int) -> UnsafeMutableRawPointer!````