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;
}
Related
What I have so far is:
void startQueryIPv4(const char *hostName){
printf("startQueryIPv4");
DNSServiceRef serviceRef;
DNSServiceGetAddrInfo(&serviceRef, kDNSServiceFlagsForceMulticast, 0, kDNSServiceProtocol_IPv4, hostName, queryIPv4Callback, NULL);
DNSServiceProcessResult(serviceRef);
DNSServiceRefDeallocate(serviceRef);
}
static void queryIPv4Callback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *hostname, const struct sockaddr *address, uint32_t ttl, void *context){
printf("queryIPv4Callback");
if (errorCode == kDNSServiceErr_NoError) {
printf("no error");
char *theAddress = NULL;
switch(address->sa_family) {
case AF_INET: {
struct sockaddr_in *addr_in = (struct sockaddr_in *)address;
theAddress = malloc(INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(addr_in->sin_addr), theAddress, INET_ADDRSTRLEN);
break;
}
case AF_INET6: {
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)address;
theAddress = malloc(INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &(addr_in6->sin6_addr), theAddress, INET6_ADDRSTRLEN);
break;
}
default:
break;
}
printf("IP address: %s\n", theAddress);
free(theAddress);
} else {
printf("%d", errorCode);
}
}
But the callback is never called.
In the console I get this error: TIC Read Status [9:0x0]: 1:57
ObjectiveC is not my power but I had to mess with it. Any help will be appreciated.
Inasmuch as Objective-C is a true superset of C and Darwin is certified compatible with SUS 3, your Objective-C program for iOS should be able to use the C interface to the system's name resolver: getaddrinfo(). You can use the third argument to this function to specify that you want only IPv4 results (or only IPv6 results).
Things of which you should be aware:
this is of course a synchronous interface; if you want asynchronous then you'll need to arrange for that yourself.
getaddrinfo() allocates and returns a linked list of addresses, so
in principle, you might need to check more than one
you need to free the list after you're done with it via freeaddrinfo()
The reason you aren't getting a callback is that you aren't using a dispatch queue. The DNSServiceGetAddrInfo API is asynchronous. If you are doing this on an Apple device, you want something more like this:
void startQueryIPv4(const char *hostName) {
printf("startQueryIPv4");
DNSServiceRef serviceRef;
DNSServiceGetAddrInfo(&serviceRef, kDNSServiceFlagsForceMulticast, 0, kDNSServiceProtocol_IPv4, hostName, queryIPv4Callback, NULL);
main_queue = dispatch_get_main_queue();
DNSServiceSetDispatchQueue(sdref, main_queue);
dispatch_main();
}
Note that dispatch_main() is the main event loop for libdispatch: if you want to do other stuff, you need to schedule it in the dispatch loop, because dispatch_main() will not return.
In your original code you called DNSServiceRefDeallocate(), but you can't do that until you want to stop the query. If you call it right after you start the query, it will cancel the query. So e.g. you could call it from the callback.
However, a better flow would be to do a long-lived query (kDNSServiceFlagsLongLivedQuery) so that you get an update whenever the information changes. Of course you'd then need to change the host you're connecting to, so only do this if your application will be connected for an extended period.
Additionally, you may get more than one answer. If you do, it may be that some answers work to connect, and others don't. So you might like to accumulate answers and try each one, rather than giving up if the first answer you get doesn't work. The callback will include the kDNSServiceFlagsMoreComing flag if there is more data coming immediately. Each time the callback is called, it will get one answer (or an indication that the query has failed in some way).
Of course, this is a fairly low-level API. If you want to make your life a bit easier, you should use Network Framework. Network Framework does the "happy eyeballs" part for you—trying each response until it gets a connection, and returning you the connection it gets, canceling the others.
But you didn't ask about that, so I won't go into details here.
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.
I am using reachability class in my application to monitor networkstate.
Apple has mentioned in their docs to provide support for IPv6 types.
I found that some iPV4 types are using in reachability class.
I have searched for new reachability class and did not find..
is there any new class for check iPv6 network reachability state.?
+ (Reachability*) reachabilityForLocalWiFi;
{
struct sockaddr_in localWifiAddress;
bzero(&localWifiAddress, sizeof(localWifiAddress));
localWifiAddress.sin_len = sizeof(localWifiAddress);
localWifiAddress.sin_family = AF_INET;
// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress];
if(retVal!= NULL)
{
retVal->localWiFiRef = YES;
}
return retVal;
}
Found this on Apple's forums. It describe the best the current status of this issue:
Q: "We are using reachabilityForLocalWiFi from reachability class? I
notice reachabilityForLocalWiFi method is using
reachabilityWithAddress (local ip address)? I am wondering how will it
work for the ivp6 address? Currently it works for ipv4 address."
A: (By "the Eskimo"): "I don’t think that will be a problem. Even if
the device only has IPv6 connectivity to the outside world, it should
still be able to get to link-local IPv4 addresses (169.254/16), which
is what +reachabilityForLocalWiFi uses."
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));
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.