/getifaddrs.pl6 Secret
Last active
June 29, 2016 15:25
Star
You must be signed in to star a gist
getifaddrs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| use NativeCall; | |
| # https://opensource.apple.com/source/Libinfo/Libinfo-459.20.1/gen.subproj/ifaddrs.h | |
| #struct ifaddrs { | |
| # struct ifaddrs *ifa_next; | |
| # char *ifa_name; | |
| # unsigned int ifa_flags; | |
| # struct sockaddr *ifa_addr; | |
| # struct sockaddr *ifa_netmask; | |
| # struct sockaddr *ifa_dstaddr; | |
| # void *ifa_data; | |
| #}; | |
| class Ifaddrs is repr('CStruct') { | |
| has Pointer[void]$.ifa_next; | |
| has Str $.ifa_name; | |
| has int $.ifa_flags; | |
| has Str $.ifa_data; | |
| # has Pointer[Sockaddr] $.ifa_addr; | |
| # has Pointer[Sockaddr] $.ifa_netmask; | |
| # has Pointer[Sockaddr] $.dstaddr; | |
| # method init { | |
| # #$!ifa_next := Ifaddrs.new; | |
| # $!ifa_next.init; | |
| # } | |
| } | |
| # class MyStruct is repr('CStruct') { | |
| # HAS Ifaddrs $.point; | |
| # has Str $.ifa_name; | |
| # has int $.ifa_flags; | |
| # has Str $.ifa_data; | |
| # } | |
| sub getifaddrs(Ifaddrs $arg is rw) is native('info') { * } | |
| my Ifaddrs $ifaddrs .= new; | |
| getifaddrs($ifaddrs); | |
| # | |
| say $ifaddrs; | |
| # say $ifaddrs.point.ifa_next; | |
| # say $ifaddrs.ifa_name; | |
| # say $ifaddrs.ifa_next.WHAT; | |
| #include <arpa/inet.h> | |
| #include <sys/socket.h> | |
| #include <ifaddrs.h> | |
| #include <stdio.h> | |
| # int main () | |
| # { | |
| # struct ifaddrs *ifap, *ifa; | |
| # struct sockaddr_in *sa; | |
| # char *addr; | |
| # | |
| # getifaddrs (&ifap); | |
| # for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | |
| # if (ifa->ifa_addr->sa_family==AF_INET) { | |
| # sa = (struct sockaddr_in *) ifa->ifa_addr; | |
| # addr = inet_ntoa(sa->sin_addr); | |
| # printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr); | |
| # } | |
| # } | |
| # | |
| # freeifaddrs(ifap); | |
| # return 0; | |
| # } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment