1

So gethostbyaddr() returns a hostent.

struct hostent {
    char  *h_name;            /* official name of host */
    char **h_aliases;         /* alias list */
    int    h_addrtype;        /* host address type */
    int    h_length;          /* length of address */
    char **h_addr_list;       /* list of addresses */

In theory, with memcpy we can't copy this struct to another hostent, because of h_aliases and h_addr_list.

So I tested doing a piece of code on C.

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main() {
    int i;
    unsigned char ip[] = {130, 206, 192, 10};
    char name[14]="www.apple.com";
    struct hostent *initial, *final;
    struct in_addr IP;

    if ((initial = gethostbyaddr(ip, sizeof (ip), AF_INET)) == NULL)
        exit(EXIT_FAILURE);

    memcpy(&final, &initial, sizeof(initial)); //memcpy(*dest, *initial, size)

    printf("HOST: %s\n", final -> h_name);

    for (i = 0; final->h_aliases[i] != NULL; i++)
        printf("ALIASES[%d]: %s\n", i, final -> h_aliases[i]);

    for (i = 0; final->h_addr_list[i] != NULL; i++) {
        IP.s_addr = *((uint32_t*) final->h_addr_list[i]);
        printf("IP[%d]: %s\n\n", i, inet_ntoa(IP));
    }


    if ((initial = gethostbyname(name)) == NULL)
        exit(EXIT_FAILURE);

    printf("HOST: %s\n", final -> h_name);

    for (i = 0; final->h_aliases[i] != NULL; i++)
        printf("ALIASES[%d]: %s\n", i, final -> h_aliases[i]);

    for (i = 0; final->h_addr_list[i] != NULL; i++) {
        IP.s_addr = *((uint32_t*) final->h_addr_list[i]);
        printf("IP[%d]: %s\n\n", i, inet_ntoa(IP));
    }
}

Te output of the code is:

HOST: a130-206-192-10.deploy.akamaitechnologies.com
IP[0]: 130.206.192.10

HOST: a130-206-192-10.deploy.akamaitechnologies.com
IP[0]: 130.206.192.10

As you can see, the same thing is printed twice. Shouldn't it print things related with Apple the second time? Because when I did memcpy I copied the pointers of h_aliases and h_addr_list on the dest struct.

Loperena
  • 11
  • 1
  • 2
  • 1
    `memcpy(&final, &initial, sizeof(initial));` is the same as `final = initial;`. – mch Oct 13 '16 at 11:00
  • Do you think `gethostbyaddr` and `gethostbyname` return a pointer to the same struct? Do you look if `final` and `initial` are equal after the call to `gethostbyname`? – mch Oct 13 '16 at 11:05
  • @mch They can't be equal. So why is the same output appearing? – Loperena Oct 13 '16 at 11:19
  • 1
    So you print the values of `final` twice, which does not change. Why should the output be different? You should print the values of `initial` if you want the other outputs. – mch Oct 13 '16 at 11:26
  • @mch so if I wanted to make `final` change in consequence to the changes I do in `initial`, how I should use `memcpy` – Loperena Oct 13 '16 at 11:30
  • Don't forget to call `freeaddrinfo()` at appropriate times. You're leaking memory as written — which doesn't matter much in the small scale you're working at, but does in general. – Jonathan Leffler Oct 13 '16 at 16:15

0 Answers0