Skip to content

Instantly share code, notes, and snippets.

@darach
Created February 23, 2016 23:25
Show Gist options
  • Save darach/182a70189de60379f42a to your computer and use it in GitHub Desktop.
Save darach/182a70189de60379f42a to your computer and use it in GitHub Desktop.
A minimal example of mapping a native C structure to pony struct using FFI - Shows usage of Maybe, FFI and structs working together
// Use Pony struct's, FFI, and Maybe to map native structures to C
// Thanks to sclebsch for a little help with the embedded sa_data
// structure for which details are well hidden in sockaddr_in and sockaddr_in6
//
struct SockAddr
var sa_len: U8 = 0
var sa_family: U8 = 0
var sa_port: U16 = 0
var sa_data_1: U32 = 0
var sa_data_2: U32 = 0
var sa_data_3: U32 = 0
var sa_data_4: U32 = 0
struct IfAddr
var next : Maybe[IfAddr] = Maybe[IfAddr].none()
var name: Pointer[U8] = Pointer[U8]
var ifa_flags: U32 = 0
var ifa_addr : Maybe[SockAddr] = Maybe[SockAddr].none()
var ifa_netmask : Maybe[SockAddr] = Maybe[SockAddr].none()
var ifa_dstaddr : Maybe[SockAddr] = Maybe[SockAddr].none()
var ifa_data: Pointer[U8] = Pointer[U8]
actor Main
new create(env : Env) =>
var ifaddrs = Maybe[IfAddr].none()
if @getifaddrs[I32](addressof ifaddrs) != 0 then
env.out.print("error")
return
end
try
while not ifaddrs.is_none() do
var addrs = ifaddrs()
var s = String.copy_cstring(addrs.name)
env.out.print(s + " (" + addrs.ifa_flags.string() + ")")
ifaddrs = addrs.next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment