From e3b6a2c0f5e34af51850c46b6773c0887fa43d7a Mon Sep 17 00:00:00 2001 From: Tobias Girstmair Date: Mon, 20 May 2024 18:04:18 +0200 Subject: [PATCH] improve PONG sending this gets rid of the double-send(2) (which in theory allows us to enable Nagle's TCP_NODELAY). we need to restore the line terminator that strtok removed. while the spec says we should send \r\n, it would probably suffice to non-compliantly just do line[n] = '\n' (and rely on postel's law to save us). but strtok only overwrites the first delimeter; transforming "...\n..." into "...\0..." and "...\r\n..." into "...\0\n...". so by looking ahead one byte we can determine which terminator(s) were sent by the server and send back the same one(s). of course, this doesn't hold for \n\r, just \r or \n\n (neither of which we expect), but it would fall back to a sensible form regardless. --- ircpipe.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ircpipe.c b/ircpipe.c index 89baa76..45b5821 100644 --- a/ircpipe.c +++ b/ircpipe.c @@ -165,9 +165,11 @@ int irc_answer(const sock_t sock, char *buf, const unsigned int command) { /* reply to pings: */ if (strncmp(line, "PING ", 5) == 0) { + int n = strlen(line); + int crlf = line[n+1] == '\n'; /* strtok only removes first delimeter */ line[1] = 'O'; /* PING :foo -> PONG :foo */ - WRITE(sock, line, strlen(line)); - WRITE(sock, "\r\n", 2); + line[n] = crlf ? '\r' : '\n'; /* re-terminate after strtok */ + WRITE(sock, line, n+crlf+1); } } while ((line = strtok_r(NULL, "\r\n", &saveptr))); -- 2.39.3