Page MenuHomeFreeBSD

Update example in man page to use cap_getnameinfo function.
ClosedPublic

Authored by oshogbo on Aug 28 2018, 10:05 PM.

Diff Detail

Repository
rS FreeBSD src repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

oshogbo created this revision.Aug 28 2018, 10:05 PM
hrs requested changes to this revision.Aug 29 2018, 4:12 AM

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 ,
This revision now requires changes to proceed.Aug 29 2018, 4:12 AM
oshogbo updated this revision to Diff 47461.Aug 29 2018, 10:41 PM

I addressed yours suggestion @hrs - thank you!

oshogbo retitled this revision from date example in man page to use cap_getnameinfo function. to Update example in man page to use cap_getnameinfo function..Aug 30 2018, 5:23 PM
hrs accepted this revision.Sep 22 2018, 1:46 PM
This revision is now accepted and ready to land.Sep 22 2018, 1:46 PM
This revision was automatically updated to reflect the committed changes.