Code reformat and a few fixes

- Merge 'active' into connection states
- Reformated commented CAP code
- Removed (now useless) l_ping and l_pong
- Remove CONN_ACTIVE toggles on FIFO
This commit is contained in:
Alex D. 2021-01-09 18:52:38 +00:00
parent 845066176d
commit fd2821f444
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
4 changed files with 56 additions and 40 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)
project(
uIRCd
VERSION 2021.01.04
VERSION 2021.01.09
DESCRIPTION "High performance IRC daemon based on uIRC"
LANGUAGES C
)

View File

@ -156,10 +156,7 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
break;
}
case (PONG): {
if (mesg->trailing && mesg->args[1] != NULL) {
LOG(LOG_DEBUG, "Got PONG back with mesg \"%s\".", mesg->args[1]);
conn->info.l_pong = ctime;
}
if (mesg->trailing && mesg->args[1] != NULL) { LOG(LOG_DEBUG, "Got PONG back with mesg \"%s\".", mesg->args[1]); }
break;
}
/* Autojoin channels from current conn on first response from the server */
@ -169,25 +166,41 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
if (!commit_channelist(buf, conn)) return 0;
break;
}
/*
/* TODO: IRCv3 capabilities
case (CAP): {
if (mesg->args[1] != NULL && strcmp(mesg->args[1], "LS") == 0) {
LOG(LOG_VERBOSE, "Requesting capabilities \"%s\" on " ADDRFMT ".", mesg->args[2], conn->data.address,
conn->data.port);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_REQ(mesg->args[2]), sizeof(buf->buffer))) > 0) {
if (flush_buffer(buf->buffer, (size_t)len, buf->fd) == -1) {
LOG(LOG_WARN, "Couldn't request capabilities \"%s\" on " ADDRFMT ". " ERRNOFMT, mesg->args[2],
conn->data.address, conn->data.port, strerror(errno), errno);
conn->state = CONN_RECONNECTING;
return 0;
if (mesg->args[1] != NULL) {
if (strcmp(mesg->args[1], "LS") == 0) {
LOG(LOG_VERBOSE,
"Requesting capabilities \"%s\" on " ADDRFMT ".",
mesg->args[2],
conn->data.address,
conn->data.service);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_REQ(mesg->args[2]), sizeof(buf->buffer))) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
"Couldn't request capabilities \"%s\" on " ADDRFMT ". " ERRNOFMT,
mesg->args[2],
conn->data.address,
conn->data.service,
strerror(errno),
errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
}
}
} else if (mesg->args[1] != NULL && strcmp(mesg->args[1], "ACK") == 0) {
LOG(LOG_VERBOSE, "Ending capability negotiation on " ADDRFMT ".", conn->data.address, conn->data.port);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_END(), sizeof(buf->buffer))) > 0) {
if (flush_buffer(buf->buffer, (size_t)len, buf->fd) == -1) {
LOG(LOG_WARN, "Couldn't end capability negotiation on " ADDRFMT ". " ERRNOFMT,
conn->data.address, conn->data.port, strerror(errno), errno); conn->state = CONN_RECONNECTING; return 0;
} else if (strcmp(mesg->args[1], "ACK") == 0) {
LOG(LOG_VERBOSE, "Ending capability negotiation on " ADDRFMT ".", conn->data.address, conn->data.service);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_END(), sizeof(buf->buffer))) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
"Couldn't end capability negotiation on " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
}
}
}

View File

@ -32,7 +32,8 @@
#define INIT_HARDFAIL -1
#define INIT_SOFTFAIL -2
#define CONN_ACTIVE 2
#define CONN_ACTIVE 3
#define CONN_IDLE 2
#define CONN_REGISTERED 1
#define CONN_PENDING 0
#define CONN_RECONNECTING -1
@ -51,11 +52,10 @@ typedef struct {
} Connection_User; // User information [persistent]
typedef struct {
time_t l_ping, l_pong, l_connect, l_message;
time_t l_connect, l_message;
Channel* channels;
signed short state;
signed int reconinter;
bool active;
} Connection_Info; // Connection information [temporary]
typedef struct {

View File

@ -153,8 +153,8 @@ main(int argc, char* argv[])
setup_signals();
for (;;) {
ctime = time(NULL);
if (!connection.info.active) nanosleep(&sleep, NULL);
connection.info.active = false;
if (connection.info.state == CONN_IDLE) nanosleep(&sleep, NULL);
connection.info.state = CONN_IDLE;
if (!run || connection.info.state == CONN_CLOSING) {
signed long temp;
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_QUIT(connection.data.quitmsg), sizeof(buf.send.buffer))) > 0) {
@ -192,21 +192,26 @@ main(int argc, char* argv[])
/* Reset all state-dependent values to empty */
memset(&buf, '\0', sizeof(buf));
buf.recv.fd = buf.send.fd = buf.fifo.fd = -1;
connection.info.l_ping = connection.info.l_pong = connection.info.l_message = 0;
connection.info.l_message = 0;
if ((buf.send.fd = init_connection(&connection)) > 0) {
buf.recv.fd = buf.send.fd;
add_socket_flags(buf.send.fd, O_NONBLOCK);
/* Registration process and CAP negotiation (TODO) */
signed long temp;
/*
/* Registration process and CAP negotiation (TODO)
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_CAP_LS("302"), sizeof(buf.send.buffer))) > 0) {
buf.send.append_pos = (size_t)temp;
LOG(LOG_VERBOSE, "Sending a CAP LS to " ADDRFMT ".", connection.data.address,
connection.data.service); if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1)
{ LOG(LOG_WARN, "Couldn't send CAP LS to " ADDRFMT ". " ERRNOFMT, connection.data.address,
connection.data.service, strerror(errno), errno); connection.info.state = CONN_RECONNECTING; continue;
buf.send.append_pos = (size_t) temp;
LOG(LOG_VERBOSE, "Sending a CAP LS to " ADDRFMT ".", connection.data.address, connection.data.service);
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1) {
LOG(LOG_WARN,
"Couldn't send CAP LS to " ADDRFMT ". " ERRNOFMT,
connection.data.address,
connection.data.service,
strerror(errno),
errno);
connection.info.state = CONN_RECONNECTING;
continue;
}
}
*/
@ -272,7 +277,7 @@ main(int argc, char* argv[])
connection.info.state = CONN_CLOSED;
continue;
}
} else if (connection.info.state == CONN_ACTIVE) {
} else if (connection.info.state == CONN_ACTIVE || connection.info.state == CONN_IDLE) {
connection.info.reconinter = 0;
if (connection.data.timeout > 0 && connection.info.l_message < ctime - connection.data.timeout) {
LOG(LOG_WARN, "Timed out because no message was received since %lu.", connection.info.l_message);
@ -287,7 +292,6 @@ main(int argc, char* argv[])
> 0) {
*(buf.fifo.buffer + (buf.fifo.append_pos += (size_t) brd)) = '\0';
LOG(LOG_DEBUG, "Read %li bytes from FIFO. Now appending at %li", brd, buf.fifo.append_pos);
connection.info.active = true;
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
LOG(LOG_ERROR, "Failed to read FIFO fifo. " ERRNOFMT, strerror(errno), errno);
connection.info.state = CONN_RECONNECTING;
@ -316,7 +320,6 @@ main(int argc, char* argv[])
*(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len);
buf.fifo.append_pos -= (unsigned long) len;
*(buf.fifo.buffer + buf.fifo.append_pos) = '\0';
connection.info.active = true;
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
} else if (len == -1) {
@ -343,7 +346,7 @@ main(int argc, char* argv[])
brd,
buf.recv.append_pos,
buf.recv.buffer);
connection.info.active = true;
connection.info.state = CONN_ACTIVE;
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
LOG(LOG_ERROR, "Failed to read inbound traffic. " ERRNOFMT, strerror(errno), errno);
connection.info.state = CONN_RECONNECTING;
@ -376,7 +379,7 @@ main(int argc, char* argv[])
*(buf.recv.buffer + x) = *(buf.recv.buffer + x + len);
buf.recv.append_pos -= (unsigned long) len;
*(buf.recv.buffer + buf.recv.append_pos) = '\0';
connection.info.active = true;
connection.info.state = CONN_ACTIVE;
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
} else if (len == -1) {