Details
Details
- Reviewers
bcr hrs - Group Reviewers
manpages - Commits
- rS340140: libcasper: Update example in man page to use cap_getnameinfo function.
Diff Detail
Diff Detail
- Repository
- rS FreeBSD src repository - subversion
- Lint
Lint Not Applicable - Unit
Tests Not Applicable
Event Timeline
Comment Actions
I would suggest the following changes using getaddrinfo() instead of inet_aton() to avoid address-family dependency. Is cap_getaddrinfo() allowed by capdns?
@@ -147,8 +163,9 @@
const char *typelimit = "ADDR";
int familylimit;
const char *ipstr = "127.0.0.1";
-struct in_addr ip;
-struct hostent *hp;
+char hname[NI_MAXHOST];
+struct addrinfo hints, *res;
+int error;
/* Open capability to Casper. */
capcas = cap_init();
@@ -176,16 +193,21 @@
if (cap_dns_family_limit(capdns, &familylimit, 1) < 0)
err(1, "Unable to limit access to the system.dns service");
-/* Convert IP address in C-string to in_addr. */
-if (!inet_aton(ipstr, &ip))
- errx(1, "Unable to parse IP address %s.", ipstr);
+/* Convert IP address in C-string to struct sockaddr. */
+hints = (struct addrinfo){
+ .ai_family = familylimit,
+ .ai_flags = AI_NUMERICHOST
+};
+error = cap_getaddrinfo(ipstr, NULL, &hints, &res);
+if (error)
+ errx(1, "cap_getaddrinfo(): %s: %s", ipstr, gai_strerror(error));
/* Find hostname for the given IP address. */
-hp = cap_gethostbyaddr(capdns, (const void *)&ip, sizeof(ip), AF_INET);
-if (hp == NULL)
- errx(1, "No name associated with %s.", ipstr);
+error = cap_getnameinfo(capdns, res->ai_addr, res->ai_addrlen, hname,
+ sizeof(hname), NULL, 0, 0);
+if (error)
+ errx(1, "cap_getnameinfo(): %s: %s", ipstr, gai_strerror(error));
-printf("Name associated with %s is %s.\\n", ipstr, hp->h_name);
+printf("Name associated with %s is %s.\\n", ipstr, hname);
.Ed
.Sh SEE ALSO
.Xr cap_enter 2 ,