This repository has been archived on 2021-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
uIRCd/src/connection.c

238 lines
7.4 KiB
C

/*
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRCd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* uIRCd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
*/
#include "connection.h"
#include "buffer.h"
#include "channels.h"
#include "limits.h"
#include "logging.h"
#include "memory.h"
#include <errno.h> // errno
#include <limits.h> // ??
#include <netdb.h> // getaddrinfo() gai_strerror()
#include <stdbool.h> // bool
#include <stdio.h> // snprintf()
#include <string.h> // strerror()
#include <sys/socket.h> // sockaddr connect()
#include <sys/types.h> // size_t ssize_t socklen_t
#include <unistd.h> // ??
#define UIRC_HELPERS
#include <uirc/uirc.h> // Assm_mesg Assm_cmd...
signed int
init_connection(Connection* conn)
{
int sockfd, getaddrres, connectres;
struct addrinfo* addr_info;
if ((getaddrres = getaddrinfo(conn->data.address, conn->data.service, NULL, &addr_info)) != 0) {
freeaddrinfo(addr_info);
LOG(LOG_WARN,
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
gai_strerror(getaddrres),
getaddrres);
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
}
if ((sockfd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol)) < 0) {
LOG(LOG_ERROR, "Failed to open a socket for " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
freeaddrinfo(addr_info);
return INIT_HARDFAIL;
}
if ((connectres = connect(sockfd, addr_info->ai_addr, addr_info->ai_addrlen)) == -1) {
close(sockfd);
freeaddrinfo(addr_info);
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
}
freeaddrinfo(addr_info);
return sockfd;
}
signed int
get_msgchannel(IRC_Message* mesg)
{
// TODO: These message arguments might be parsed differently depending on what command they offer.
// Maybe offering a static context would allow reentrant calls
switch (mesg->cmd) {
case JOIN:
case PART:
case MODE:
case TOPIC:
case LIST:
case NAMES:
case INVITE:
case KICK:
case PRIVMSG:
case NOTICE: {
if (CHANNELMASK(mesg->args[0])) return 0;
break;
}
case RPL_TOPIC:
case RPL_NOTOPIC:
case RPL_ENDOFNAMES: {
if (CHANNELMASK(mesg->args[1])) return 1;
break;
}
case RPL_NAMREPLY: {
if (CHANNELMASK(mesg->args[2])) return 2;
break;
}
}
return -1;
}
const char*
get_categ(IRC_Message* mesg)
{
switch (mesg->cmd) {
case JOIN:
case PART:
case KICK: return "events";
case TOPIC:
case RPL_TOPIC:
case RPL_NOTOPIC: return "topic";
case PRIVMSG:
case NOTICE: return "msgs";
case RPL_MOTD:
case RPL_MOTDSTART:
case RPL_ENDOFMOTD:
case ERR_NOMOTD: return "motd";
case PING:
case PONG: return "pings";
case RPL_NAMREPLY:
case RPL_ENDOFNAMES: return "names";
default: return NULL;
}
}
int
auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
{
signed long len;
time_t ctime = time(NULL);
switch (mesg->cmd) {
case (PING): {
LOG(LOG_DEBUG, "Auto-replying to ping \"%s\".", mesg->args[0]);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_PONG(mesg->args[0], NULL), sizeof(buf->buffer))) > 0)
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
"Couldn't pong " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
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;
}
break;
}
/* Autojoin channels from current conn on first response from the server */
case (RPL_WELCOME): {
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.address, conn->data.service);
conn->info.state = CONN_ACTIVE;
if (!commit_channelist(buf, conn)) return 0;
break;
}
/*
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;
}
}
} 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;
}
}
}
break;
}
*/
case (ERROR): {
LOG(LOG_ERROR, "Received error %s.", mesg->args[0]);
break;
}
case (JOIN):
case (PART): {
set_channel(&conn->info.channels[get_channelindex(mesg->args[0], conn->info.channels)],
mesg->args[0],
mesg->args[1],
mesg->cmd == JOIN);
break;
}
case (ERR_NICKNAMEINUSE):
case (ERR_NICKCOLLISION): {
LOG(LOG_ERROR, "Nickname %s is already taken.", conn->user.nickname);
conn->info.state = CONN_RECONNECTING;
break;
}
}
return 1;
}
int
commit_channelist(Buffer_Info* buf, Connection* conn)
{
signed long len;
for (unsigned int i = 0; conn->info.channels != NULL && conn->info.channels[i].name != NULL; i++) {
IRC_Message msg = { .args = { conn->info.channels[i].name, conn->info.channels[i].key, NULL },
.trailing = true,
.cmd = (conn->info.channels[i].joined) ? JOIN : PART };
if ((len = Assm_mesg(buf->buffer, &msg, sizeof(buf->buffer))) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
"Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT,
conn->info.channels[i].name,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
}
}
return 1;
}