Theldus / wsServer

wsServer - a tiny WebSocket server library written in C

Home Page:https://theldus.github.io/wsServer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ws_getaddress() do not work in onmessage() on localhost

opened this issue · comments

When using the wsServer on localhost the IP can be retrieved in open and close, but not in onmessage():

Connection opened, addr: 127.0.0.1
I receive a message: {"data": ... } (451), from: (null) <-------- PROBLEM HERE
Connection closed, addr: 127.0.0.1

The code used in this test is based on the demo example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ws.h>

/**
 * @brief This function is called whenever a new connection is opened.
 * @param client Client connection.
 */
void onopen(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection opened, addr: %s\n", cli);
}

/**
 * @brief This function is called whenever a connection is closed.
 * @param client Client connection.
 */
void onclose(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection closed, addr: %s\n", cli);
}

/**
 * @brief Message events goes here.
 * @param client Client connection.
 * @param msg    Message content.
 * @param size   Message size.
 * @param type   Message type.
 */
void onmessage(ws_cli_conn_t *client,
    const unsigned char *msg, uint64_t size, int type)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("I receive a message: %s (%zu), from: %s\n", msg, size, cli);

    ws_sendframe_txt(client, "OK");
}

int main(void)
{
    /* Register events. */
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;

    /*
     * Main loop, this function never* returns.
     *
     * *If the third argument is != 0, a new thread is created
     * to handle new connections.
     */
    ws_socket(&evs, 8181, 0, 1000);

    return (0);
}

Hi @dennisnoerprevas,
The example you provided works just fine here:

$ ./examples/echo/echo 
Waiting for incoming connections...
Connection opened, addr: 127.0.0.1
I receive a message: abc (3), from: 127.0.0.1
Connection closed, addr: 127.0.0.1

Could you tell me more about the environment you're running? like OS (and version of it), whether Glibc, Musl (and version), which commit version are you using from this project and etc?

so I can try to reproduce here...

I do not really know what is going on x) I just tested again, and this time I got a secfault.

Waiting for incoming connections...
Connection opened, addr: 127.0.0.1
Segmentation fault

Then, I used GDB to generate a backtrace, but then everything worked as expected.

And now when I run the program without GDB, it works as expected...

Sooo the problem is 'solved' / no longer observable.

Nice project^ Thanks for your work.