Xamarin NetworkReachability reports reachability incorrectly - ios

Calling 'GetFlags' on NetworkReachability always returns 'Reachable' even if
the domain doesn't exist. I'll attach a full test solution but the following
code should result in "Failed..." but results in "Success = True".
The code is based on the Reachability sample.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
string host = "http://nonsense.test12345679.com";
using (var r = new SystemConfiguration.NetworkReachability (host)) {
NetworkReachabilityFlags flags;
if (r.TryGetFlags (out flags)) {
_label.Text = "Success = " + IsReachableWithoutRequiringConnection (flags);
} else {
_label.Text = "Failed to get flags ";
}
}
}
public bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags)
{
// Is it reachable with the current network configuration?
bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
// Do we need a connection to reach it?
bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
|| (flags & NetworkReachabilityFlags.IsWWAN) != 0;
return isReachable && noConnectionRequired;
}
If you switch the device into airplane mode then reachability is correctly
returned as false.
I'm testing on an iPad Air, iOS 9.0.2 and the latest stable Xamarin.

iOS System Configuration framework's Reachability does not actually check to see if the host is alive (i.e. it is not a ping/icmp/http/... based test).
A remote host is considered reachable when a data packet, sent by an
application into the network stack, can leave the local device.
Reachability does not guarantee that the data packet will actually be
received by the host.
iOS SCNetworkReachability Reference
Also a good SO A/Q on about the Reachability flags : How to interpret NetworkReachabilityFlags in Xamarin.iOS?

I haven't used that functionality directly; but have you tried the nuget package for connectivity? https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Connectivity. I have tried it and it seems to work pretty well. It has methods for testing if a specific endpoint is reachable.
bool canConnect = CrossConnectivity.Current.IsConnected
&& await CrossConnectivity.Current.IsRemoteReachable(host, portInt));

Related

GCDWebServer via Hotspot network

I'm developing an application where devices can connect and interact with each other via common wi-fi network and for the purpose of file exchange I'm using GCDWebServer.
Everything is working great when I use usual wi-fi network or devices are connected to hotspot network with 3rd party host. But I encounter a strange issue when one of devices with the launched app is actually a host of a Hotspot.
I have this code:
- (void)startStreamHLSServer
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!_webServer.isRunning)
{
_webServer = [GCDWebServer new];
[_webServer addGETHandlerForBasePath:#"/" directoryPath:[_fileManager videosURL].path indexFilename:nil cacheAge:3600 allowRangeRequests:YES];
[_webServer startWithPort:1000 bonjourName:nil];
NSLog(#"URL: %#", _webServer.serverURL.absoluteString);
}
});
}
The problem is that serverURL is nil. Which actually seems logical because I checked a function GCDWebServerGetPrimaryIPAddress which is supposed to tell the address and this function is only looking for addresses in the en0 interface when Hotspot network is actually bridge100.
So question is - Is there a "normal" way to make GCDWebServer work with bridge100?
SECOND PART:
Although serverURL is nil, method startWithPort returns true. So I thought maybe server is running, it just can not tell me its address. So I got device's address with my custom method (if you're interested, I can attach it here, but I'm 100% sure it gives a correct address) and tried to use it in order to "speak" with web server, but no luck with that - server doesn't respond. So maybe startWithPort returns a false result after all.
Very interesting observation - when I change primaryInterface to bridge100 in GCDWebServerGetPrimaryIPAddress method, it fixes the issue. GCDWebServer shows a correct address and it is definitely running since I can have an access to the device folder.
Any help would be appreciated!
So question is - Is there a "normal" way to make GCDWebServer work with bridge100?
No. You would need to fork GCDWebServer and patch this function.
Although serverURL is nil, method startWithPort returns true.
The server is certainly running if this method returns true. The ports are open and listening (and by default are bound to all interfaces). The problem is that you need to figure out what IP to use to reach the server from outside the iPhone.
In order to summarize:
GCDWebServer can be used in the hotspot network although serverURL is nil.
What you need to do is next:
Define IP address of your device on your own. Here's a method you can use:
- (void)getDeviceAddress
{
NSString *address = #"";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
NSString *interfaceName = [NSString stringWithUTF8String:temp_addr->ifa_name];
if([interfaceName isEqualToString:#"bridge100"] || [interfaceName isEqualToString:#"en0"])
{
//fetch ip address
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
break;
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}
2) Remember the port you used in order to start GCDWebServer
3) Build your serverURL:
- (NSString *)serverURL {
NSString *serverURL = [NSString stringWithFormat:#"http:/%#:%d", [self getDeviceAddress], serverPort]; //serverPort is the port your GCDWebServer is running on
return serverURL;
}

Programmatically set wifi hotspot password

I want to set my wifi hotspot password programmatically for my application so that user don't have to go to the setting menu to check their password.
I am already using NEHotspotNetwork, where it set the password, but here, we need to set the password which is already there in the setting menu for connecting to the network.
It's also helpful if I can get my wifi hotspot password, from the application without jailbreak my device.
You just need to use the following code:
WifiConfiguration netConfig = new WifiConfiguration();
netConfig .preSharedKey = "yourpassword";
Using NEHotspotNetwork function register you can set the password
NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
//Get all available hotspots
var list: [NEHotspotNetwork] = cmd.networkList!
//Figure out the hotspot you wish to connect to
// let desiredNetwork : NEHotspotNetwork? = getBestScanResult(list)
var hotspot = [NEHotspotNetwork]()
for network in cmd.networkList!
{//check for your network ssid and set password
network.setConfidence(.high)
network.setPassword("yourpassword") //Set the WIFI password
hotspot.append(network)
}
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetworkList(hotspot)
response.deliver() } else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
if let network = cmd.network {
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetwork(network)
response.deliver() //Respond back }
} else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
//Perform custom authentication and respond back with success
// if all is OK
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.deliver() //Respond back
}
Also you can use network configuration profile with the help of Apple Configurator 2 tool for your known network. There you need to setup your wi-fi and then after installing the NCP on your device, It will automatically connect with the mentioned network. But you have to host that file on server cause we can't download profile locally and using local server like GCDServer(tried already.)

Cannot Connect to IPv6 with CocoaAsyncSocket

Having issues connecting to iPv6 hosts with the CocoaAsyncSocket library
I successfully had GCDUDPAsyncSocket working but realized TCP was more appropriate for my use case.
Unfortunately - I can never successfully connect with a bonjour published and discovered NSNetService. The service is discovered and the address is discovered as well. A connection attempt without failure happens but the connection is never secured.
I can connect using "connectWithHost" and passing in the IP address assigned to my mac but this the only way i can get that ip is by hard coding it. Is there a way to obtain this IP through NSNetService?
I'm using swift, Xcode 7.1.1 and iOS 9.1. I am connecting between an iPhone and a Mac running an Apple TV Simulator. This works fine with UDP.
No matter what - the connection attempt times out even though an appropriate address is supplied!
Socket is Disconnecting - Error Domain=NSPOSIXErrorDomain Code=60 "Operation timed out" UserInfo={NSLocalizedDescription=Operation timed out, NSLocalizedFailureReason=Error in connect() function}
Anyone run into this before? Here is my connection code:
func connectToAddress(sender: NSNetService) {
if let addresses = sender.addresses {
for address in addresses {
print(address)
}
self.serverAddresses = addresses
var done = false
while !done && (self.serverAddresses.count > 0) {
let address = self.serverAddresses[0]
self.socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
do {
try self.socket.connectToAddress(address)
done = true
} catch {
print("Unable to Connect")
}
}
if !done {
print("Could Not Connect To Address")
}
}
}
Please update your CocoaAsyncSocket library. The issue was fixed in May 2nd commit. So this should work with the following flag set to false
socket.IPv4PreferredOverIPv6 = NO;
This will allow your app / game to connect in both IPv4 and IPv6. As of June 1 Apple is rejecting the apps which are not compliant with IPv6 networks. You app should work without any problems in an IPv6 network.
You can setting
socket.IPv4PreferredOverIPv6 = NO;
as #Kumar C mentioned, it will works well in IPv6, but if you still need work with IP address(use IP address as Host), you can update the code as below in GCDAsyncSocket.m(IPv4PreferredOverIPv6 should be set to NO first):
+ (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr
{
......
for (res = res0; res; res = res->ai_next)
{
if (res->ai_family == AF_INET)
{
// Found IPv4 address.
// Wrap the native address structure, and add to results.
if (((struct sockaddr_in *)res->ai_addr)->sin_port == 0)
((struct sockaddr_in *)res->ai_addr)->sin_port = htons(port);
NSData *address4 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
[addresses addObject:address4];
}
else if (res->ai_family == AF_INET6)
{
// Found IPv6 address.
// Wrap the native address structure, and add to results.
if (((struct sockaddr_in6 *)res->ai_addr)->sin6_port == 0)
((struct sockaddr_in6 *)res->ai_addr)->sin6_port = htons(port);
NSData *address6 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
[addresses addObject:address6];
}
}
.......
}
This will allows you app work well in IPv4/IPv6 whatever the HOST is IP address or domain.

How to check Blackberry device is connected to internet connection?

In Android, checking internet connection is like that.
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
How about in Blackberry RIM?
p/s*: I am not asking for checking the type of connection or connection available.
For instance, to check only for BIS coverage:
boolean hasConnectivity = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_BIS_B) && TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_BIS_B);
You have more flags available in TransportInfo class for Wi-Fi, BES (MDS) and direct TCP. So the method to check for internet connectivity would check for all the available transport types.

Check if a host is up on iOS SDK

I'm making an iOS app where I'd need to check if a host, for example, google.com, is available (or is up/down). I've been using this code:
NSString *host = theTextField.text;
bool success = false;
const char *host_name = [host
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
if (isAvailable) {
status.text = #"The host is up! :)";
} else {
status.text = #"The host is down! :(";
}
}
But I've a felling it doesn't works like it should. For example if I enter google.com it says it's up, okay, but if I enter the url of my webpage (which is down ATM) says it's up as well (the address is zad0xsis.net). If I enter a fake address it says it's down, so it may work right and I may be thinking odd things IDK.
zad0xsis.net is up for me.
The Reachability use a the same principle as the ping command line (DNS Resolution + Ping in fact).
I think you should use NSURLConnection to see if the webserver is up.
Have you tried ping zad0xsis.net from terminal? If you receive a successful response, then i believe it is about some networking stuff which i am not good at all.

Resources