Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
422 stars 80 forks source link

suggest for some conditions. #64

Closed confused-ddk-888 closed 1 year ago

confused-ddk-888 commented 1 year ago

In function ws_accept():

/* Accept. */
new_sock = accept(sock, (struct sockaddr *)&client, (socklen_t *)&len);

if (timeout)
{
    time.tv_sec = timeout / 1000;
    time.tv_usec = (timeout % 1000) * 1000;

    /*
     * Socket timeout
     * This feature seems to be supported on Linux, Windows,
     * macOS and FreeBSD.
     *
     * See:
     *   https://linux.die.net/man/3/setsockopt
     */
    setsockopt(new_sock, SOL_SOCKET, SO_SNDTIMEO, &time,
        sizeof(struct timeval));
}

if (new_sock < 0)
    panic("Error on accepting connections..");

condition if (new_sock < 0) may move up before if (timeout) ?

Theldus commented 1 year ago

Hi @confused-ddk-888, Yes, you're right.

Checking whether new_sock is valid or not should be done as soon as possible, preferably right after accepting the connection. Furthermore, allowing setsockopt() to occur for an invalid socket is an error.


Commit eb45cd5d2fcf748a06f50a7e0a5d781c198185b8 fixes this.