aws / aws-iot-device-sdk-cpp

SDK for connecting to AWS IoT from a device using C++
http://aws-iot-device-sdk-cpp-docs.s3-website-us-east-1.amazonaws.com
Apache License 2.0
123 stars 111 forks source link

gethostbyname() returns NULL on GCC-7.4 #184

Closed trahman-khan closed 4 years ago

trahman-khan commented 4 years ago

Hi, I have cross-compiled the SDK (1.4.0 version from https://github.com/aws/aws-iot-device-sdk-cpp/tree/release) with an armv5 gcc-7.4 toolchain. After setting up the certificates and SampleConfig.json file, I tried to run the sample application pub-sub-sample but, found that the gethostbyname() function that is called inside the network/OpenSSL/OpenSSLConnection.cpp file always returns NULL. The glibc version is 2.29 on my machine. The man page http://man7.org/linux/man-pages/man3/gethostbyname.3.html says this function is obsolete, use other functions like getaddrinfo(). I have modified the OpenSSLConnection.cpp with getaddrinfo() for IPv4 and it seems to be working fine:

struct addrinfo hints, *res;
            memset (&hints, 0, sizeof (hints));

            hints.ai_family = AF_INET;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_flags |= AI_CANONNAME;

            int getaddrinfo_result = getaddrinfo (endpoint_char, NULL, &hints, &res);
            if (getaddrinfo_result != 0)
        {
            return ResponseCode::NETWORK_TCP_NO_ENDPOINT_SPECIFIED;
        }

            sockaddr_in dest_addr;

            dest_addr.sin_family = AF_INET;
            dest_addr.sin_port = htons(endpoint_port_);
        dest_addr.sin_addr.s_addr = ((struct sockaddr_in *) res->ai_addr)->sin_addr.s_addr;
            memset(&(dest_addr.sin_zero), '\0', 8);

            AWS_LOG_INFO(OPENSSL_WRAPPER_LOG_TAG,
                         "resolved %s to %s",
                         endpoint_.c_str(),
                         inet_ntoa(dest_addr.sin_addr));

            int connect_status = connect(server_tcp_socket_fd_, (sockaddr *) &dest_addr, sizeof(sockaddr));

Please let me know if anybody has any feedback or question. I want to create a PR for this issue.

bretambrose commented 4 years ago

Thanks for tracking this down. If you'd like to make a PR, we can take a look. But if we're transitioning to getaddrinfo it might be nice to add IPv6 support as well.

trahman-khan commented 4 years ago

Thanks for the feedback. I was about to create the PR but then realized that the master branch has the fix already.