Report connection errors back to main and add more refined filtering, add more connection states to reflect between connection established and acknowledged by the server

This commit is contained in:
Alex 2020-08-20 19:51:43 +02:00
parent 94a11c328e
commit 905afcc485
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
4 changed files with 35 additions and 40 deletions

View File

@ -147,7 +147,6 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
else if (connstate == CONN_PENDING) {
if (ctime - lastconnect < recon_inter)
continue;
connstate = CONN_CLOSED;
int tmp;
if ((tmp = init_conn(conn)) > 0) {
fds[0] = tmp;
@ -198,11 +197,12 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
continue;
}
}
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port);
connstate = CONN_ACTIVE;
connstate = CONN_REGISTERED;
lastconnect = ctime;
} else
break;
} else if (tmp == INIT_SOFTFAIL)
connstate = CONN_PENDING;
else if (tmp == INIT_HARDFAIL)
connstate = CONN_CLOSED;
} else if (connstate == CONN_ACTIVE) {
if (ctime - lastping >= ping_inter) {
char* mesg = "UIRC PONG";
@ -240,7 +240,7 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
LOG(LOG_DEBUG, "Recieved line: %s", recvbuf);
memset((void*)&buffer, '\0', sizeof(IRC_Message));
if (Tok_mesg(recvbuf, &buffer) == 1) {
LOG(LOG_DEBUG, "Tokenized message successfully.", NULL);
LOG(LOG_DEBUG, "Tokenized message successfully.");
int categ = get_category(&buffer);
path[1] = category[categ];
path[2] = NULL;
@ -278,6 +278,8 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
}
/* Autojoin channels from current connection on first response from the server */
case (RPL_WELCOME): {
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port);
connstate = CONN_ACTIVE;
LOG(LOG_INFO, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port);
IRC_Message* mesg = Assm_cmd_JOIN(conn->data.chans, NULL);
if ((sendbufpos = Assm_mesg(sendbuf, mesg, sizeof(sendbuf))) > 0)
@ -290,6 +292,9 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter
case (RPL_BOUNCE): {
break; // TODO: Make bounces work
}
case (ERROR): {
LOG(LOG_ERROR, "Received error on connection " ADDRFMT " with the message \"%s\".", conn->data.addr, conn->data.port, buffer.args[0]);
}
}
} else
LOG(LOG_WARN, "Log directory couldn't be created. " ERRNOFMT, strerror(errno), errno);

View File

@ -53,6 +53,10 @@ int get_category(IRC_Message* mesg)
return CAT_CHAN;
else if (mesg->name.nick != NULL)
return CAT_USER;
} else if (mesg->cmd == RPL_TOPIC || mesg->cmd == RPL_NOTOPIC || mesg->cmd == MODE || mesg->cmd == TOPIC || mesg->cmd == RPL_BANLIST || mesg->cmd == RPL_ENDOFBANLIST) {
return CAT_CHAN;
} else if (mesg->cmd == KILL || mesg->cmd == RPL_AWAY) {
return CAT_USER;
} else if (mesg->cmd == JOIN || mesg->cmd == PART) {
return CAT_CHAN;
}

View File

@ -21,47 +21,29 @@ signed int init_conn(Connection* info)
{
const struct timespec retry_timeout = {3, 0.0L};
int sockfd, getaddrres, connectres;
if (info->data.addr == NULL) return -1;
if (info->data.addr == NULL) return INIT_HARDFAIL;
struct addrinfo* conn;
for (unsigned int i = 0; i < 3; i++) {
if ((getaddrres = getaddrinfo(info->data.addr, info->data.port, NULL, &conn)) == 0) {
break;
} else {
LOG(LOG_ERROR, "Failed to get address info for %s:%s. %s (%i)", info->data.addr, info->data.port, strerror(errno), errno);
if (i == 2 || (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME)) {
freeaddrinfo(conn);
return -1;
} else {
nanosleep(&retry_timeout, NULL); /* Signals don't bother us */
}
}
}
if (getaddrres != 0) {
freeaddrinfo(conn);
return -1;
if ((getaddrres = getaddrinfo(info->data.addr, info->data.port, NULL, &conn)) == -1) {
LOG(LOG_ERROR, "Failed to get address info for %s:%s. %s (%i)", info->data.addr, info->data.port, strerror(errno), errno);
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) {
freeaddrinfo(conn);
return INIT_HARDFAIL;
} else
return INIT_SOFTFAIL;
}
if ((sockfd = socket(conn->ai_family, conn->ai_socktype, conn->ai_protocol)) < 0) {
LOG(LOG_ERROR, "Failed to open a socket for " ADDRFMT ". " ERRNOFMT, info->data.addr, info->data.port, strerror(errno), errno);
return -1;
return INIT_HARDFAIL;
}
for (unsigned int i = 0; i < 3; i++) {
connectres = connect(sockfd, conn->ai_addr, conn->ai_addrlen);
if (connectres == 0 || (connectres == -1 && errno == EINTR)) {
break;
if ((connectres = connect(sockfd, conn->ai_addr, conn->ai_addrlen)) == -1) {
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, info->data.addr, info->data.port, strerror(errno), errno);
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) {
close(sockfd);
return INIT_HARDFAIL;
} else {
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, info->data.addr, info->data.port ,strerror(errno), errno);
if (i == 2 || (connectres != EADDRNOTAVAIL && connectres != ETIMEDOUT && connectres != ECONNRESET && connectres != ECONNREFUSED)) {
close(sockfd);
return -1;
} else {
nanosleep(&retry_timeout, NULL);
}
return INIT_SOFTFAIL;
}
}
if (connectres == -1) {
close(sockfd);
return -1;
}
return sockfd;
}

View File

@ -39,11 +39,15 @@
#ifndef _UIRCD_INCLUDED_NETWORK
#define _UIRCD_INCLUDED_NETWORK
#define CONN_ACTIVE 1
#define CONN_ACTIVE 2
#define CONN_REGISTERED 1
#define CONN_PENDING 0
#define CONN_CLOSING -1
#define CONN_CLOSED -2
#define INIT_HARDFAIL -1
#define INIT_SOFTFAIL -2
signed int init_conn(Connection* info);
int flush_buffer(char* buf, int buflen, int fd);
#endif