I have the following code to get information about the tcp ports:
var length = 0
if (sysctlbyname("net.inet.tcp.pcblist", nil, &length, nil, 0) < 0)
{
perror("sysctlbyname")
}
else
{
var buffer: [UInt8] = [UInt8](repeating: 0, count: Int(length))
sysctlbyname("net.inet.tcp.pcblist", &buffer, &length, nil, 0)
}
I now want to convert the buffer into something more "useful". I read that the return value is a struct called "xinpgen". How can I convert the buffer into that struct?
I tried the following code to directly write the result into the struct variable:
var length = 0
if (sysctlbyname("net.inet.tcp.pcblist", nil, &length, nil, 0) < 0)
{
perror("sysctlbyname")
}
else
{
var input = xinpgen()
sysctlbyname("net.inet.tcp.pcblist", &input, &length, nil, 0)
}
The call itself doesn't fail and seems to be successful. The variable contains some data that isn't zero. But shortly after the call is finished and the program continues, the app crashes:
error: memory read failed for 0x0
How can I use the buffer to fill the struct variable? And why does the second call fail?