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)
Mon, Apr 8, 5:48 PM
Unknown Object (File)
Dec 21 2023, 11:09 PM
Unknown Object (File)
Dec 20 2023, 7:29 AM
Unknown Object (File)
Nov 26 2023, 10:14 PM
Unknown Object (File)
Nov 11 2023, 2:12 PM
Unknown Object (File)
Nov 10 2023, 8:29 AM
Unknown Object (File)
Nov 9 2023, 2:15 PM
Unknown Object (File)
Nov 8 2023, 10:23 PM
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.