Is gethostbyname safe?

I'm trying to convert the url to IP address during app startup by using the method gethostbyname, and in this way, let the system cache the IP address, so that in later webview and NSURLSession network requests, can skip the DNS stage and send requests directly to the IP address.

My question is, first of all, is gethostbyname safe? I see that gethostbyname is a synchronous method and may cause crash, so I put it in a background current queue and use try-catch to protect it, will this prevent crashes from happening?

Secondly, is gethostbyname the best solution to this problem? I know CFHost also has API to convert url to ip, but CFHostStartInfoResolution and CFHostGetAddressing are already marked as deprecate, is there any other solution?

Answered by DTS Engineer in 738402022

I did some automation tests

I’m surprised by that result.

I have not encountered such crashes myself, but only saw other person's cases in the forum

I wouldn’t put too much stock in that thread, given the lack of details and the fact that they never responded to Matt’s request for more info.

Regardless, gethostbyname is never the right choice. If you want to play this game — and, like endecotp, I’m skeptical about its value — you should use getaddrinfo. It is the canonical BSD Sockets level DNS resolution API.

You will still need to call it on a secondary thread. It’s a synchronous API and synchronous networking on the main thread is never safe.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The problem is mostly that the IP address may not be valid for certain access : web site may be addressed on the same IP address with different hostname which are redirected to different web pages (that is part of apache configuration possibilities for example). SO it may work work but be careful.

I'm surprised that you feel it is worth trying this. Have you actually measured a delay that you believe would be eliminated by this?

I see that gethostbyname is a synchronous method and may cause crash

Err, it's a synchronous method which will cause a (short?) hang if used on the main thread. It won't cause a crash, AFAIK - what makes you think it will?

so I put it in a background current queue

Right.

and use try-catch to protect it, will this prevent crashes from happening?

It's a C API, so try-catch will do nothing useful.

I did some automation tests, and in the 480 tests, the first request after the app starts, using gethostbyname can be about 20ms faster on average than without gethostbyname.

I have not encountered such crashes myself, but only saw other person's cases in the forum: https://developer.apple.com/forums/thread/711054

Accepted Answer

I did some automation tests

I’m surprised by that result.

I have not encountered such crashes myself, but only saw other person's cases in the forum

I wouldn’t put too much stock in that thread, given the lack of details and the fact that they never responded to Matt’s request for more info.

Regardless, gethostbyname is never the right choice. If you want to play this game — and, like endecotp, I’m skeptical about its value — you should use getaddrinfo. It is the canonical BSD Sockets level DNS resolution API.

You will still need to call it on a secondary thread. It’s a synchronous API and synchronous networking on the main thread is never safe.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi @eskimo,

Could you please explain why gethostbyname doesn't work, I thought gethostbyname and getaddrinfo were pretty much the same in my case.

Also I would like to confirm that by calling getaddrinfo or CFHostGetAddressing, the system will cache the IP address and use it for the next WKWebView or NSURLRequest to speed up the network request, is that right?

Could you please explain why gethostbyname doesn't work

gethostbyname is a problem even on non-Apple platforms because it doesn’t support support IPv6. There are various other resolver APIs that you can mess around with but ultimately you’ll end up at getaddrinfo. This has the features you want and is widely support across platforms. So, rather than mess around with legacy stuff, I recommend that you forget all the legacy resolver APIs and start with getaddrinfo.

That’s assuming your case, where you’re not actually connecting to the server. If you are connecting to the server, there are a whole bunch of other things to consider. See this post for the details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is gethostbyname safe?
 
 
Q