I have very simple C program that gets the ip addresses for a host name as 
follows:

#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //for exit(0);
#include<sys/socket.h>
#include<errno.h> //For errno - the error number
#include<netdb.h> //hostent
#include<arpa/inet.h>

int hostname_to_ip(char *  , char *);

#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <netinet/in.h>

int main(int argc , char *argv[])
{
     printf("In main\n");
    char *hostname = "www.google.com";
    char ip[100];
     
    hostname_to_ip(hostname , ip);
    printf("%s x resolved to %s" , hostname , ip);
}
/*
    Get ip from domain name
 */
int hostname_to_ip(char * hostname , char* ip)
{
    printf("hostname %s\n", hostname);
    struct hostent *he;
    struct in_addr **addr_list;
    int i;
    he = gethostbyname( hostname );
    printf("name %s\n", he->h_name);
    printf("addrtype %d\n", he->h_addrtype);

    if ( (he = gethostbyname( hostname) ) == NULL) 
    { 
              printf("error");
              herror("gethostbyname");
            return 1;
    }
    addr_list = (struct in_addr **) he->h_addr_list;
    for(i = 0; addr_list[i] != NULL; i++) 
    {
            printf("address %s\n", inet_ntoa(*addr_list[i]));
    }
    return 1;
}

On Ubuntu when I compile it with gcc as: gcc socket.c 
-L/usr/lib/x86_64-linux-gnu  -o socket -lpthread and run it produces 
correct ip address. Output:
./socket
In main
hostname www.google.com
name www.google.com
addrtype 2
address 63.117.14.88
address 63.117.14.86
address 63.117.14.84
address 63.117.14.90
address 63.117.14.87
address 63.117.14.89
address 63.117.14.91
address 63.117.14.85

When I compile the same program using the emsdk and emcc as:
emcc socket.c -g4 -v -s "BINARYEN_METHOD='interpret-binary'" 
-L/usr/lib/x86_64-linux-gnu -o socket.html and run it with nodejs socket.js 
or in a browser with emrun and socket.html it produces the output:

In main
hostname www.google.com
name www.google.com
addrtype 2
address 172.29.1.0

Clearly the ip address in bogus. In debugging socket.js I found the 
__inet_pton4_raw (shown below) receives the correct host string 
(www.google.com) put generates address 172.29.1.0. This function generates 
the same ip address not matter what host name is supplied. I have tried the 
emsdk /test socket programs as well with the same results. They work when 
compiled with gcc but not with emcc. Any help will be much appreciated.

function __inet_pton4_raw(str) {
      var b = str.split('.');
      for (var i = 0; i < 4; i++) {
        var tmp = Number(b[i]);
        if (isNaN(tmp)) return null;
        b[i] = tmp;
      }
      return (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) >>> 0;
    }

  



-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to