I working on ssh protocol based app.
I wrote code to get hostname:
struct addrinfo hints;
int rv;
struct in6_addr serveraddr;
memset(&hints, 0, sizeof(hints));
struct addrinfo *res, *res0;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV;
int result = inet_pton(PF_INET, [server.hostname UTF8String], &serveraddr);
if (result == 1)
{
hints.ai_family = PF_INET;
hints.ai_flags |= AI_NUMERICHOST;
}
else
{
result = inet_pton(PF_INET6, [server.hostname UTF8String], &serveraddr);
if (result == 1)
{
hints.ai_family = PF_INET6;
hints.ai_flags |= AI_NUMERICHOST;
}
}
rv = getaddrinfo([server.hostname UTF8String], [[[server getPort] stringValue] UTF8String], &hints, &res0) ;
if (rv) {
dispatch_async(dispatch_get_main_queue(), ^{
server.error = true ;
server.details = [NSString stringWithUTF8String:gai_strerror(rv)] ;
[[NSNotificationCenter defaultCenter] postNotificationName:@"serverInfo"
object:self
userInfo:[NSDictionary dictionaryWithObject:server
forKey:@"server"]];
}) ;
[Helpers debugWithString:[NSString stringWithFormat:@"getaddrinfo: %s\n", gai_strerror(rv)]] ;
return 1;
}
Many times I get "nodename nor servname provided" error message (randomly) .. If I refreshing connections I'm not getting this error.
How to write reliable code for dns resolution ? And fix this problem ?