views:

137

answers:

2

i am getting the error ai_family not supported in call to getnameinfo.

  1 #include <iostream>
  2 #include <sys/types.h>
  3 #include <unistd.h>
  4 #include <sys/socket.h>
  5 #include <netdb.h>
  6 #include <arpa/inet.h>
  7 #include <iomanip>
  8 extern "C" {
  9 #include "../../pg/include/errhnd.h"
 10 }
 11 
 12 using namespace std;
 13 
 14 
 15 int main(int argc, char** argv)
 16 {
 17         if (argc != 2)
 18                 err_quit("Usage: %s <ip address>", argv[0]);
 19 
 20         struct sockaddr_in sa;
 21         if (inet_pton(AF_INET, argv[1], &sa) <= 0)
 22                 err_sys("inet_pton error");
 23 
 24         char hostname[1000], servname[1000];
 25 
 26         cout << hex << (unsigned int)sa.sin_addr.s_addr << endl;
 27 
 28         sa.sin_port = htons(80);
 29 
 30         int x;
 31         if ((x=getnameinfo((struct sockaddr*)&(sa.sin_addr),
 32                          16, hostname, 1000, servname, 1000, NI_NAMEREQD)) != 0) {
 33                 err_ret("getnameinfo error");
 34                 cout << gai_strerror(x) << endl;
 35         }
 36 
 37         cout << hostname << " " << servname << endl;
 38 
 39         return 0;
 40 }
A: 

First parameters of the function must be the struct sockaddr_in not the sin_addr member.

If you are using IPv6 you need to use struct sockaddr_in6 instead of struct sockaddr_in. This can be a reason of the EAI_FAMILY.

I think this may help you : http://linux.die.net/man/3/getnameinfo.

Patrice Bernassola
It is linux. You mght want to change it to this onehttp://linux.die.net/man/3/getnameinfo
Aviator
tried that argument as sa alsoi am using ipv4
iamrohitbanga
argument to inet_pton should be sa.sin_addr right?
iamrohitbanga
@Aviator: It is changed.
Patrice Bernassola
Patrice Bernassola
still the problem exists
iamrohitbanga
+1  A: 

Your problem is with the call to inet_pton. When AF_INET is the address family passed, the dst pointer must be a pointer to a struct in_addr, not a struct sockaddr_in.

Change line 21 to:

if (inet_pton(AF_INET, argv[1], &sa.sin_addr) <= 0)

Insert a line at line 23:

sa.sin_family = AF_INET;

Change lines 31-32 to:

if ((x=getnameinfo((struct sockaddr*)&sa, sizeof sa,
    hostname, sizeof hostname, servname, sizeof servname, NI_NAMEREQD)) != 0) {

then it should work.

caf