MCPcopy Create free account
hub / github.com/F-Stack/f-stack / realhostname

Function realhostname

tools/libutil/realhostname.c:50–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48};
49
50int
51realhostname(char *host, size_t hsize, const struct in_addr *ip)
52{
53 char trimmed[MAXHOSTNAMELEN];
54 int result;
55 struct hostent *hp;
56
57 result = HOSTNAME_INVALIDADDR;
58 hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
59
60 if (hp != NULL) {
61 strlcpy(trimmed, hp->h_name, sizeof(trimmed));
62 trimdomain(trimmed, strlen(trimmed));
63 if (strlen(trimmed) <= hsize) {
64 char lookup[MAXHOSTNAMELEN];
65
66 strlcpy(lookup, hp->h_name, sizeof(lookup));
67 hp = gethostbyname(lookup);
68 if (hp == NULL)
69 result = HOSTNAME_INVALIDNAME;
70 else for (; ; hp->h_addr_list++) {
71 if (*hp->h_addr_list == NULL) {
72 result = HOSTNAME_INCORRECTNAME;
73 break;
74 }
75 if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
76 strncpy(host, trimmed, hsize);
77 return HOSTNAME_FOUND;
78 }
79 }
80 }
81 }
82
83 strncpy(host, inet_ntoa(*ip), hsize);
84
85 return result;
86}
87
88/*
89 * struct sockaddr has very lax alignment requirements, since all its

Callers

nothing calls this directly

Calls 3

trimdomainFunction · 0.85
strncpyFunction · 0.85
strlcpyFunction · 0.50

Tested by

no test coverage detected