wolfSSL / wolfMQTT

wolfMQTT is a small, fast, portable MQTT client implementation, including support for TLS 1.3.
https://www.wolfssl.com
GNU General Public License v2.0
518 stars 156 forks source link

Simple Publish & Subscribe Code to Test (Without TLS) #337

Closed nitol-saha closed 1 year ago

nitol-saha commented 1 year ago

Hello I am trying to test this library for a project but I am facing difficulties. There is an example code in the repo: https://github.com/wolfSSL/wolfMQTT/blob/master/examples/mqttclient/mqttclient.c but I am not able to find the proper definition of each function so that I can modify the code. Can you provide me with simple publish and subscriber code to test the library without TLS? I am not currently using any hardware to test. I am running code on the local machine and the broker is installed in the localhost.

embhorn commented 1 year ago

Hello @nitol-saha

I moved this issue from the wolfSSH repository to the wolfMQTT repository.

There are several simple examples. https://github.com/wolfSSL/wolfMQTT/tree/master/examples/mqttsimple

What host system are you developing with? Let me know if you are building in linux, macOS, or Win and I can help recommend some instructions.

Thanks,

nitol-saha commented 1 year ago

I am getting error in the following code block. struct addrinfo hints; is showing error incomplete type is not allowedand also res variable is showing error pointer to incomplete class type "struct addrinfo" is not allowed. How to solve this? I am running my code on Ubuntu 22.04.2 LTS.

static int mqtt_net_connect(void *context, const char* host, word16 port,
    int timeout_ms)
{
    int rc;
    int sockFd, *pSockFd = (int*)context;
    struct sockaddr_in addr;
    struct addrinfo *result = NULL;
    struct addrinfo hints;

    if (pSockFd == NULL) {
        return MQTT_CODE_ERROR_BAD_ARG;
    }

    (void)timeout_ms;

    /* get address */
    XMEMSET(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    XMEMSET(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;

    rc = getaddrinfo(host, NULL, &hints, &result);
    if (rc >= 0 && result != NULL) {
        struct addrinfo* res = result;

        /* prefer ip4 addresses */
        while (res) {
            if (res->ai_family == AF_INET) {
                result = res;
                break;
            }
            res = res->ai_next;
        }
        if (result->ai_family == AF_INET) {
            addr.sin_port = htons(port);
            addr.sin_family = AF_INET;
            addr.sin_addr =
                ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
        }
        else {
            rc = -1;
        }
        freeaddrinfo(result);
    }
    if (rc < 0) {
        return MQTT_CODE_ERROR_NETWORK;
    }

    sockFd = socket(addr.sin_family, SOCK_STREAM, 0);
    if (sockFd < 0) {
        return MQTT_CODE_ERROR_NETWORK;
    }

    /* Start connect */
    rc = connect(sockFd, (struct sockaddr*)&addr, sizeof(addr));
    if (rc < 0) {
        PRINTF("NetConnect: Error %d (Sock Err %d)",
            rc, socket_get_error(*pSockFd));
        close(sockFd);
        return MQTT_CODE_ERROR_NETWORK;
    }

    /* save socket number to context */
    *pSockFd = sockFd;

    return MQTT_CODE_SUCCESS;
}
embhorn commented 1 year ago

Hi @nitol-saha

Did you follow the build instructions here? https://github.com/wolfSSL/wolfMQTT/blob/master/README.md#maclinuxunix

Do you have the development tools installed? sudo apt install build-essential

Could you tell us a bit about the project you are working on that uses wolfMQTT?

Thanks, Eric - wolfSSL Support

nitol-saha commented 1 year ago

I have the development tools installed and also followed the build instruction. I am not working on a specific project. I am trying this library to work to print hello world so that I can use this library for future projects as it is a very light weight library. It seems like my VScode is not able to recognize the variable properly but when I build the code using gcc, it is not showing any error and I am able to connect to the broker. Thanks!

embhorn commented 1 year ago

Fantastic! VSCode can be finicky about building with CMake. We recommend using the command line CMake instructions: https://github.com/wolfSSL/wolfMQTT/blob/master/README.md#cmake

I'll go ahead and close this issue, but please feel free to reach out if you have any questions.

Thanks, Eric