Using getnameinfo()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mariano

    Using getnameinfo()

    I have a socket-client application, on the server side I have a file
    descriptor for client connection, now I've to know what is the client
    name (otherwise IP address). I have tried to write a function, but ->
    operator doesn't work. Someone know the solution???

    void traccia_user(in t fd)
    {
    int tmp_len;
    struct sockaddr_in tmp;
    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
    unsigned int addrlen=sizeof( tmp);
    getpeername(fd, (struct sockaddr*)&tmp, &addrlen);
    printf("Client IP: %s\nClient port: %d
    \n",inet_ntoa(t mp.sin_addr),tm p.sin_port);
    tmp_len = sizeof(tmp);
    if (getnameinfo(tm p, tmp->tmp_len, hbuf, sizeof(hbuf), sbuf,
    sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
    printf("host=%s , serv=%s", hbuf, sbuf);
    }
  • santosh

    #2
    Re: Using getnameinfo()

    Mariano wrote:
    I have a socket-client application, on the server side I have a file
    descriptor for client connection, now I've to know what is the client
    name (otherwise IP address). I have tried to write a function, but ->
    operator doesn't work. Someone know the solution???
    >
    void traccia_user(in t fd)
    {
    int tmp_len;
    struct sockaddr_in tmp;
    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
    unsigned int addrlen=sizeof( tmp);
    getpeername(fd, (struct sockaddr*)&tmp, &addrlen);
    printf("Client IP: %s\nClient port: %d
    \n",inet_ntoa(t mp.sin_addr),tm p.sin_port);
    tmp_len = sizeof(tmp);
    if (getnameinfo(tm p, tmp->tmp_len, hbuf, sizeof(hbuf), sbuf,
    sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
    printf("host=%s , serv=%s", hbuf, sbuf);
    }
    This is off-topic here. You might want to try comp.unix.progr ammer, but
    you might want to use tmp_len instead of tmp->tmp_len in the call to
    getnameinfo.

    Also did you check the return value of getpeername? Did it actually
    succeed. Did you include all the necessary headers. Why are you using
    unsigned int instead of socklen_t for addrlen?

    Comment

    • Boon

      #3
      Re: Using getnameinfo()

      Mariano wrote:
      I have a socket-client application, on the server side I have a file
      descriptor for client connection, now I've to know what is the client
      name (otherwise IP address). I have tried to write a function, but ->
      operator doesn't work.
      p->f is equivalent to (*p).f
      p needs to be a pointer to a struct (or union)
      void traccia_user(in t fd)
      {
      int tmp_len;
      struct sockaddr_in tmp;
      char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
      unsigned int addrlen=sizeof( tmp);
      getpeername(fd, (struct sockaddr*)&tmp, &addrlen);
      printf("Client IP: %s\nClient port: %d
      \n",inet_ntoa(t mp.sin_addr),tm p.sin_port);
      tmp_len = sizeof(tmp);
      if (getnameinfo(tm p, tmp->tmp_len, hbuf, sizeof(hbuf), sbuf,
      sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
      printf("host=%s , serv=%s", hbuf, sbuf);
      }
      Try a different newsgroup : comp.unix.progr ammer
      They can help you with getnameinfo.

      Comment

      Working...