NSURLSession and cfsocket/cfstream

Hi, Quinn

From figure 1-1 and 1-2 of the below link:

https://developer.apple.com/library/content/documentation/Networking/Conceptual/CFNetwork/Concepts/Concepts.html#//apple_ref/doc/uid/TP30001132-CH4-DontLinkElementID_17


Does it mean that the implementation of NSURLSession/NSURLConnection is based on the APIs from CFNetwork and CFNetwork APIs is based on those of CFStream/CFSocket? For example, for code below,

CFSocketConnectToAddress or CFSocketCreateConnectedToSocketSignature or some similar function will eventually be called underneath??


NSURLSessionDataTask *imageTask = [session dataTaskWithRequest:request completionHandler:

^(NSData *data, NSURLResponse *response, NSError *error) {

..

}];

[imageTask resume];


Or is it using raw socket and bypassing CFStream/CFSocket?


Just want to find out the concrete/exact relations between APIs from differenct network layers but could not google it out..


Thanks,

Rao

Replies

Or is it using raw socket and bypassing CFStream/CFSocket?

The answer here is “It depends.” Way back when NSURLConnection was first introduced the relationship was NSURLConnection > NSStream > CFSocketStream > CFSocket > BSD Sockets. However, that was just an implementation detail and not something we guaranteed would persist forever. Over the years there’s been a bunch of changes in this space:

  • We introduced CFURLConnection (a private CFNetwork-style API), that’s NSURLConnection’s entry to CFNetwork.

  • We introduced NSURLSession, with a corresponding CFURLSession.

  • We’ve reshuffled those four subsystems around multiple times, with the goal to unify stuff and implement the legacy mechanisms in terms of the new mechanisms.

  • The user space TCP abstraction — TLS, proxies, and so on — used to be implemented by CFSocketStream but much of that work has been pushed down into libnetwork, a private framework that’s now the primary point of responsibility for that job. CFNetwork calls that API through its own internal abstraction layer.

This stuff is not documented because, as a user of these APIs, you should not care how they work under the covers. OTOH, understanding these relationships can come in handy when debugging.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you, Quinn.


This is definitely very helpful, especially in debugging as you said.


Best regards,

Rao

Hi Quinn,


I have a vpn framework that use socket low level network api and

I wish to use nsurlsession that use this socket, is there a way to do that ?


Thank for you reply