SCNetworkReachability only works for domain names? - iphone

Given iOS's SCNetworkReachability API configured like this:
SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, "example.com");
SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context);
SCNetworkReachabilitySetDispatchQueue(reachabilityRef, dispatch_queue);
It works nice if I configure it with a domain name as in the example above. However, if I configure it with an IP address the callback is never called.
Anyone can confirm that this is the expected behavior of the SCNetworkReachability API? If so, any clues why?

Try using SCNetworkReachabilityCreateWithAddress instead of SCNetworkReachabilityCreateWithName.

Use eppz!reachability that works well with IP addresses, too. Simple usage like:
// Get status on-demand.
[EPPZReachability reachHost:#"eppz.eu"
completition:^(EPPZReachability *reachability)
{
if (reachability.reachable) [self postSomething];
}];
It is true. It won't work nor with SCNetworkReachabilityCreateWithAddress (It actually works on future reachability condition changes, but the first callback never get called).
If you design needs only on-demand reachability check, consider using SCNetworkReachabilityGetFlags.
A bit more at: Why asynchronous reachability with IP address doesn’t work? and Reachability with blocks for everyday use

Related

Milo: get IP of client

Is there a way to get a Clients IP in Context of a write?
I want to get the IP of an Client that writes to my Milo-OPCUA-Server, so I can handle these writes differently based on the Clients IP (local Clients should be able to write directly on the Server, whilst other writes should get forwarded to another Server)
Okay, this is not part of any official API right now, so it almost certainly will break in the future, but:
With the OperationContext you get when implementing AttributeManager#write(WriteContext, List<WriteValue>):
context.getSession().ifPresent(session -> {
UaStackServer stackServer = context.getServer().getServer();
if (stackServer instanceof UaTcpStackServer) {
ServerSecureChannel secureChannel = ((UaTcpStackServer) stackServer)
.getSecureChannel(session.getSecureChannelId());
Channel channel = secureChannel.attr(UaTcpStackServer.BoundChannelKey).get();
SocketAddress remoteAddress = channel.remoteAddress();
}
});
I'll have to add some official API to do this, probably something hanging off the Session object.

fiddler - can I output requesting client ip/name?

Using the code here shows how to add a column:
http://fiddler2.com/documentation/KnowledgeBase/FiddlerScript/AddColumns
What I'd like to know, though, is the ip (or name) of the client issuing the request. Is that possible to determine?
Thanks,
Ben
I believe you can grab this off Session object that is passed in. So in the code example in the article you link to you would set the value of you column to oS.clientIP.
For convenience the complete code you have to insert into the Handlers class:
public static BindUIColumn("ClientIP")
function ColClientIP(oS: Session){
return oS.clientIP;
}
This is now available from the UI using Customise Columns and the session flag X-clientIP. Now means V5.0.20211 of Fiddler Classic. Probably been there for some time.

Why was the state parameter removed from WebServerClient:RequestUserAuthorization in version 4.0.1.12097

I have been using DotNetOpenAuth v3.5.0.10357 for about a year now and finally decided to upgrade to v4.0.1.12097. In doing so, I noticed the RequestUserAuthorization method no longer accepts a state parameter.
//v3.5.0.10357
WebServerClient:RequestUserAuthorization(IEnumerable<string> scope = null, string state = null, Uri returnTo = null);
//v4.0.1.12097
WebServerClient:RequestUserAuthorization(IEnumerable<string> scope = null, Uri returnTo = null);
Facebook documentation, mentions this helps guard against Cross-site Request Forgery. What was the reasoning for the removal?
Well, after a bit more digging I found out why it was removed. DotNetOpenAuth does state checking internally (EndUserAuthorizationRequest.ClientState), leaving one less thing for us to worry about. Nicely done!

Unable to receive data sent using NSStream objects

In my app I doing something very similar to what is done in the WiTap project. I use Bonjour to discover peers and then send data over the socket to perform an initial handshake.
I'm able to see the data being sent OTA using Cocoa Packet Analyzer. But the stream: handleEvent: function is never called on the receiving peer side.
What I am able to see is:
Sometimes, when the peer that sent the data exits, the receiver peer seems to get the data.
Sometimes I am able to see an NSStreamEventErrorOccurred error in the handler function.
I'm unable to see any noticeable pattern on when the above behavior occurs.
Here is a bit of the code that might be helpful.
PacketSender and PacketReceiver objects are singletons.
I have verified multiple times that the correct (and the only) instance of these objects are set as delegates while debugging:
if (![netService getInputStream:&_inStream outputStream:&_outStream])
{
[Utilities showAlert:#"Failed connecting to server"];
return BM_ERROR_NETSERVICE_STREAM_FAIL;
}
if(!sharedProtocolManager.mPacketSender)
{
sharedProtocolManager.mPacketSender = [PacketSender sharedSender];
}
if(!sharedProtocolManager.mPacketReceiver)
{
sharedProtocolManager.mPacketReceiver = [PacketReceiver sharedReceiver];
}
if(!sharedProtocolManager.mPacketSender || !sharedProtocolManager.mPacketReceiver)
{
return BM_ERROR_FAIL;
}
[PacketReceiver setupInStream:_inStream];
[PacketSender setupOutStream:_outStream];
}
Inside the PacketReceiver setupInStream: function I have:
if (sharedPacketReceiver->mInStream != inStream)
{
[sharedPacketReceiver->mInStream release];
sharedPacketReceiver->mInStream = [inStream retain];
}
sharedPacketReceiver->mInStream.delegate = sharedPacketReceiver;
Any answers or even suggestions on ways to debug this further would be greatly appreciated.
Thanks!

Can OCUnit rely on code in background threads? (in order to test asynchronous requests)

I've been trying to automate tests on asynchronous requests but I haven't been able to run anything in a different thread while the test function was waiting.
Here is the test function:
- (void) testBoxManagerConnexionStatus
{
ControlSender* cs = [ControlSender get];
requestShouldSucceed = YES;
[cs startCheckingReachabilityWithDelegate:self];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:TIMEOUT_INTERVAL+1.0]];
STAssertTrue(downloadComplete, #"Download should be over by now");
}
My test class implements the callback methods this way:
- (void)controlSender:(ControlSender *)controlSender sentSuccessfullyCode:(FreeboxControl)code
{
if (requestShouldFail) {
STAssertTrue(NO, #"Request should have failed");
}
downloadComplete = YES;
}
- (void)controlSender:(ControlSender *)controlSender couldntSendCode:(FreeboxControl)code details:(NSHTTPURLResponse*)details
{
if (requestShouldSucceed) {
STAssertTrue(NO, #"Request should have succeded");
}
downloadComplete = YES;
}
But whenever my usual code try to run something in a different thread nothing happens. For example the NSURLConnection never call its delegate methods when allocated:
m_connexion = [[NSURLConnection alloc] initWithRequest:m_networkRequest delegate:self];
Neither the -connectionDidFinishLoading: nor the -connection:didFailWithError:
Same thing for calls like this one:
[self performSelectorInBackground:#selector(BG_startCheckingReachabilityWithDelegate:) withObject:delegate];
Nothings gets called in background when running the test.
The same code works fine outside of the test though.
Is there any way to test asynchronous url request with OCUnit?
Thanks for the help.
you could look at https://github.com/danielpunkass/RSTestingKit which has a way to wait on the run loop in unit tests you can see his slides at http://www.red-sweater.com/talks/UnitTesting.pdf for some background. It may have some info to help you get started.
You can try running requests like this in a background thread, but you don't want to. For your sanity, you don't want your tests dependent on external services. What if the remote service is down? Your test fails. What if the remote service returns an error? Your test fails? What if you want to what your code does when the remote service responds with an error to a valid request? You can't (consistently). Even if your tests pass they'll run slowly, depending on how long the remote service takes to respond.
Your life will be easier if you stub out the dependency on any remote services for the purpose of your tests. See this answer (and the associated question) for more detailed reasoning.