How to get a remote MAC address via IPv6 in iOS programmatically - ios

I need to find a solution how to get the MAC address from the other device in the WiFi network. There is a good method how to do this for IPv4 (How does iOS app Fing get MAC Address?), but how to do this for IPv6? Since ARP was replaced by the NDP (Neighbour Discovery Protocol), the latter method doesn't work. I would greatly appreciate if anyone could help me.

Layer and Encapsulation
The network architecture is layered, the upper layer encapsulate the different implementations of lower layer and provide higher abstraction relative to lower layer.
The network layer which use IP encapsulate different link layers protocols, like Ethernet, WiFi, PPP (which may run on serial cable which not use MAC address) etc.
So, the first problem is what do you mean by remote?
If you mean other hosts in WAN, it is impossible unless both device implement a specific protocol: you send a request to those device, they reply with his mac address.
If you mean other host in the same LAN, you can use ARP protocol in IPv4 and NDP (which ) in IPv6.
Arp & NDP
ARP send broadcast in LAN when it knows the IP address of a host but not MAC address, then the hosts which find someone is calling for him reply its MAC address.
NDP provides two main part of functionality, the first is the same with ARP: mapping between network- and link-layer addresses. (The difference is NDP using a multicast address: prefix f02::1:f/104, combined with the low-order 24 bits of the solicited IPv6 address)
So what you need is to send ICMPv6 Neighbor Solicitation message.
Address Assignment in IPv6
Link-local IPv6 addresses (and some global IPv6 addresses) use interface identifiers (IIDs) as a basis for unicast IPv6 address assignment. ... IIDs are ordinarily 64 bits long and are formed either directly from the underlyinglink-layer MAC address of a network interface using a modified EUI-64 format. or by another
process that randomizes the value in hopes of providing some degree of privacy against address tracking.
So in the most common cases, you can get the MAC address of a device directly from their IPv6 link local address.
Conclusion:
implement your protocol in both devices
send NDP message to solicited node if it is in same LAN
extract MAC address from link local IPv6 address
Ref
Wikipedia
TCP/IP Illustrated, volume 1

I'm using MMLanScan for that purpose. It's very simple to get working to find IP/MAC addresses of devices on your LAN.
I use it some time ago but I think it's works with IPv6 also.

Related

Can grpc communicate with mac address insted of ip

Currently my program use the IP of the other device in order to communicate over grpc.
Unfortunately the IP is changing all the time and I cannot continue the connection between the devices without updating it manually.
The connection between the devices is only on LAN and should blocked access from WAN.
Locking the IP of the device in the rowter settings is not an option this time.
Does grpc have a way to communicate over LAN (inside my network) using identification number or mac address that does not change over time?.
Related questions I found:
grpc: Identify clients from same IP address
Reason for both a MAC and an IP address
In order to locate a machine based on its MAC address, you are talking about implementing ARP. This is something that is not supported in dart nor am I sure it would be advisable to go this route anyway. If setting a DHCP reservation on your router is not an option, are you able to skip DHCP and set a static IP on the receiving machine?
You could look into dynamic DNS which your router may support, or you may need to set up separately, but that will also require a static IP (for your DDNS server). If your machines can connect to the internet there are many DDNS services you can sign up for, but it sounds like that is not the case.

Scanning for network devices

I want to scan for all devices in the local network. Then I want to further check if the discovered devices respond on a specific port, say #4000 for example. All that should happen on an iPad preferably using Swift (version 3).
Should I use a library for that job?
This document didn't help me and sadly I cannot find useful information on the internet.
Edit: I want to know how can you implement this in Swift/iOS libraries. Are there any examples, libraries, core classes where I should start?
I think I may know what you are trying to ask. Your device sees the router, but doesn't know who is connected.
Read about multicast IP and broadcast IP, which is usually the highest IP address in your subnet.
Example: 192.168.1.255
As example, you make all devices listen on broadcast/multicast IP. To discover, you send a UDP message to that IP. Your router, if not configured otherwise, will forward that to the other devices.
This message can be something like "I am Mr. John, Reply to me at this Port".
That is the general idea

Programmatically find if the iPad is on IPv4 or an IPv6 network

I have a functionality in iOS which I need to disable if the user's device network in IPv6. Is there a way we can get the device (iPhone/iPad) IP address as a string programmatically, and then check if it's IPv4 or IPv6 network (or simply tell whether it is on IPv4 or IPv6 network without fetching the actual address)?
The closest I could get to was iPhone/iPad/OSX: How to get my IP address programmatically? (by David H.), which again wants us to specify the network beforehand only, hence doesn't solve my purpose...
By his solution either I always get an IPv4 or always an IPv6.
For testing on IPv6 I'm using the Apple's recommended NAT64 tool (https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html).
As a workaround I'm using the DNS to check (https://hirooka.pro/?p=6169) currently, which I feel is not a proper way as it should be IP address based.
Also, quite weirdly, the iPad always shows an IPv4 address (even when connected to IPv6 network) but only varies in the DNS style... Anybody knows why that happens ?
Thanks in advance.

How does the iOS Reachability class work, and why the IP Address 169.254.0.0?

How does the iOS Reachability class work, and what's so special about the IP Address 169.254.0.0?
I'm trying to figure out whether Wi-Fi is on, independent of whether it is connected to an actual Wi-Fi network or not.
I haven't found a satisfactory answer for this question on SO, and even the answers that try to answer this don't work for me.
When you fail to get an IP address from DHCP, it will automatically assign you a random address in the 169.254.x.x range. So if you have an IP address like this, it means you're connected to a network, and set to use DHCP to get an address, but it is unable to obtain an IP address from DHCP.
There doesn't appear to be any way to simply detect if wifi is enabled and not connected to a network, so I think you're out of luck there. The best you can do is detect if you're connected to a wifi network, but often if you're out and about, you won't be connected to a wifi network even though it is enabled.

any way to detect iPads or iPhones on wireless network? bonjour?

I am trying to detect Apple devices connected to a wireless network. This is relatively simple using Bonjour, however I am also trying to detect what kind of device it is. Like, a MacBook Air, a MacBook Pro, a MacPro, an iPhone, iPod, or an iPad.
I have found that Bonjour requests to MacBook's and MacPros include an "ADDITIONAL SECTION" response to the query which includes the model:
;; ADDITIONAL SECTION:
Q9550._device-info._tcp.local. 10 IN TXT "model=MacPro3,1"
and
;; ADDITIONAL SECTION:
Air._device-info._tcp.local. 10 IN TXT "model=MacBookAir4,2"
From testing an iPhone (3GS and 4), an iPod touch, and an iPad2, all of the iDevices only respond with their name:
;; ANSWER SECTION:
111.1.168.192.in-addr.arpa. 10 IN PTR gmPad2.local.
Clearly, the name may not reflect the device. So, I would not like to try to extrapolate the type of device from the name. Does anyone know any other ways to detect iDevice types?
Edit: just to be clear, the command I am using is: dig #224.0.0.251 -p5353 -x 192.168.1.111 ... substituting the IP address of the Apple device
Use port 62078
The most reliable indicator I have seen is whether you can connect to IP port 62078.
Port 62078 is used for the "iphone-sync" service, and I don't think MacBooks use it. This port always appears to be open for the iPhones and iPads on our (very small) network.
Possibly (but not probably) there are messages you can send to the port to sniff out more details...
I think the official xml list of port assignements is here, although it wasn't working for me just now:
http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
MAC address
In theory the MAC addresses might help - but probably not much use unless you can find somewhere that maintains a reliable list of ranges (e.g. a network security firm, or hardware provider). MAC addresses do depend on the actual chips used (or a flashed MAC). The database is at the organisation level (although organisations sometimes choose to use specific ranges for specific devices).
http://standards.ieee.org/develop/regauth/oui/public.html allows you to download the database of "Organizationally Unique Identifiers", or you can look up "Apple", or the first three bytes of a MAC address e.g. 00264A.
Anecdotally, the MAC lookup doesn't work... First three digits of my iPad MAC are 28-68-BA and that comes up with nothing.
User agent
Probably not useful, but if you can watch the network traffic or have an http proxy, then the user-agent string could help (see http://developer.apple.com/library/IOS/documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html#//apple_ref/doc/uid/TP40006517-SW3).
Edit (added):
Appleā€™s Bonjour protocol relies on Multicast DNS (mDNS) operating at UDP port 5353 and sends to these reserved group addresses: IPv4 Group Address - 224.0.0.251, IPv6 Group Address - FF02::FB - reference.
This would help get push notification when Apple devices connect to a local network (link-local) by listening for multicast messages on 5353 UDP. Perhaps sniff the packet and see if it has any extra information in it :)
Although I presume that Bonjour API also allows for seeing this...
You can also use the airport utility to do this manually :
1) open AirportUtility
2) Go to "Wireless Clients" (hover mouse by the arrow and click it)
3) Go to DHCP Clients, and you will see iPad,iPhone, computer name, etc.... as the Client ID column.

Resources