Page MenuHomeFreeBSD

Update example in man page to use cap_getnameinfo function.
ClosedPublic

Authored by oshogbo on Aug 28 2018, 10:05 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Dec 15, 4:45 PM
Unknown Object (File)
Sat, Dec 14, 8:04 PM
Unknown Object (File)
Fri, Dec 13, 2:24 PM
Unknown Object (File)
Oct 28 2024, 12:28 PM
Unknown Object (File)
Oct 25 2024, 1:24 AM
Unknown Object (File)
Oct 25 2024, 1:24 AM
Unknown Object (File)
Oct 25 2024, 1:24 AM
Unknown Object (File)
Oct 25 2024, 1:24 AM
Subscribers

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

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

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
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.