delphi - IP address of iOS device -
i need ip address of ios device. gstack.getlocaladdresslist returns addresses including 1 cellular interface. how can address associated wifi interface (en0)?
@widi if similar question asked on different language platform, doesn't mean there answer 1 specified - read tags - says "delphi" "firemonkey". it's asking question in c# , rejoice getting code example in cobol.
you'd have implement own ip address enumeration returns adapter info. here's example:
uses ipaddresshelper, idstack, idglobal; procedure tform2.button1click(sender: tobject); var localips: tidstacklocaladdresslist; i: integer; begin memo1.lines.clear; tidstack.incusage; try localips := tidstacklocaladdresslist.create; try gstack.getlocaladdresslist(localips); := 0 localips.count - 1 begin if localips[i] tidstacklocaladdressipv4ex memo1.lines.add(tidstacklocaladdressipv4ex(localips[i]).ifaname + localips[i].ipaddress + ' ' + booltostr(tidstacklocaladdressipv4ex(localips[i]).iswifi, true)); end; localips.free; end; tidstack.decusage; end; end;
this unit implements advanced stack class includes adapter name , functions decide if network cellular or wifi.
unit ipaddresshelper; interface uses classes, idstack, idstackconsts, idglobal, idstackbsdbase, idstackvclposix; type tidstacklocaladdressipv4ex = class(tidstacklocaladdressipv4) protected fflags: cardinal; fifaname: string; public function iswifi: boolean; function isppp: boolean; property ifaname: string read fifaname; constructor create(acollection: tcollection; const aipaddress, asubnetmask: string; aname: marshaledastring; aflags: cardinal); reintroduce; end; tidstacklocaladdressipv6ex = class(tidstacklocaladdressipv6) protected fflags: cardinal; fifaname: string; public function iswifi: boolean; function isppp: boolean; property ifaname: string read fifaname; constructor create(acollection: tcollection; const aipaddress: string; aname: marshaledastring; aflags: cardinal); reintroduce; end; tidstackvclposixex = class(tidstackvclposix) public procedure getlocaladdresslist(aaddresses: tidstacklocaladdresslist); override; end; implementation uses posix.base, posix.netif, posix.netinetin, sysutils; function getifaddrs(ifap: pifaddrs): integer; cdecl; external libc name _pu + 'getifaddrs'; {do not localize} procedure freeifaddrs(ifap: pifaddrs); cdecl; external libc name _pu + 'freeifaddrs'; {do not localize} procedure tidstackvclposixex.getlocaladdresslist(aaddresses: tidstacklocaladdresslist); var laddrlist, laddrinfo: pifaddrs; lsubnetstr: string; begin if getifaddrs(@laddrlist) = 0 try aaddresses.beginupdate; try laddrinfo := laddrlist; repeat if (laddrinfo^.ifa_addr <> nil) , ((laddrinfo^.ifa_flags , iff_loopback) = 0) begin case laddrinfo^.ifa_addr^.sa_family of id_pf_inet4: begin if laddrinfo^.ifa_netmask <> nil begin lsubnetstr := translatetinaddrtostring( psockaddr_in(laddrinfo^.ifa_netmask)^.sin_addr, id_ipv4); end else begin lsubnetstr := ''; end; tidstacklocaladdressipv4ex.create(aaddresses, translatetinaddrtostring( psockaddr_in(laddrinfo^.ifa_addr)^.sin_addr, id_ipv4), lsubnetstr, laddrinfo^.ifa_name, laddrinfo^.ifa_flags); end; id_pf_inet6: begin tidstacklocaladdressipv6ex.create(aaddresses, translatetinaddrtostring( psockaddr_in6(laddrinfo^.ifa_addr)^.sin6_addr, id_ipv6), laddrinfo^.ifa_name, laddrinfo^.ifa_flags); end; end; end; laddrinfo := laddrinfo^.ifa_next; until laddrinfo = nil; aaddresses.endupdate; end; freeifaddrs(laddrlist); end; end; const iff_up = $1; iff_broadcast = $2; iff_loopback = $8; iff_pointopoint = $10; iff_multicast = $8000; { tidstacklocaladdressipv4ex } constructor tidstacklocaladdressipv4ex.create(acollection: tcollection; const aipaddress, asubnetmask: string; aname: marshaledastring; aflags: cardinal); begin inherited create(acollection, aipaddress, asubnetmask); fflags := aflags; if assigned(aname) fifaname := aname; end; function tidstacklocaladdressipv4ex.isppp: boolean; // network connection carrier established via ppp // gprs, edge, umts connections have flag iff_pointopoint set begin result := (fflags , (iff_up or iff_pointopoint) = (iff_up or iff_pointopoint)) , (fflags , (iff_loopback) = 0); end; function tidstacklocaladdressipv4ex.iswifi: boolean; // wlan connections support multicast // wlan connections not use ppp // filter out loopback interface (just completeness, in case // network enumeration changed loopback included) begin result := ((fflags , (iff_up or iff_multicast)) = (iff_up or iff_multicast)) , (fflags , (iff_loopback or iff_pointopoint) = 0); end; { tidstacklocaladdressipv6ex } constructor tidstacklocaladdressipv6ex.create(acollection: tcollection; const aipaddress: string; aname: marshaledastring; aflags: cardinal); begin inherited create(acollection, aipaddress); fflags := aflags; if assigned(aname) fifaname := aname; end; function tidstacklocaladdressipv6ex.isppp: boolean; begin result := (fflags , (iff_up or iff_pointopoint) = (iff_up or iff_pointopoint)) , (fflags , (iff_loopback) = 0); end; function tidstacklocaladdressipv6ex.iswifi: boolean; begin result := ((fflags , (iff_up or iff_multicast)) = (iff_up or iff_multicast)) , (fflags , (iff_loopback or iff_pointopoint) = 0); end; initialization setstackclass(tidstackvclposixex); end.
please keep in mind wifi interface might use metered connection if connects wireless hotspot.
Comments
Post a Comment