Page Background Animation
213 words
1 minute
ft_irc — Non-Blocking C++98 IRC Server

ft_irc: Internet Relay Chat Server from Scratch#

ft_irc is a 1337 / 42 Rank 5 project requiring an RFC 2812-compliant Internet Relay Chat server in C++98, capable of multiplexing concurrent client connections without blocking.

Architectural Design#

  • I/O Multiplexing with poll(): A single-threaded event loop manages listener sockets, incoming client events, and write queues without polling loops or CPU starvation.
  • Non-Blocking Sockets: Sockets are configured with O_NONBLOCK via fcntl to prevent slow or malicious clients from blocking server dispatching.
  • Protocol Parsing: Strict parsing of IRC commands including PASS, NICK, USER, JOIN, PRIVMSG, MODE, KICK, INVITE, TOPIC, PART, and QUIT.
  • Channel Modes: Support for channel flags (+i invite-only, +t topic lock, +k password key, +o channel operator status, +l user count limit).
int pollCount = poll(&pollFds[0], pollFds.size(), -1);
if (pollCount < 0 && !Server::signalReceived) {
throw std::runtime_error("poll() error encountered");
}
for (size_t i = 0; i < pollFds.size(); ++i) {
if (pollFds[i].revents & POLLIN) {
if (pollFds[i].fd == serverSocketFd) {
acceptNewClient();
} else {
handleClientData(pollFds[i].fd);
}
}
}

TCP Stream and State Handling#

  1. Partial Reads and Framing: Accumulates fragmented TCP packets into client-specific buffers until the standard \r\n boundary is reached.
  2. Buffer Bounds: Implements bounded buffers to protect against resource exhaustion.
  3. Graceful Teardown: Captures SIGINT and SIGTERM to close socket descriptors cleanly and release resources.

Client Compatibility#

Tested against standard IRC clients:

  • Irssi
  • HexChat
  • Raw socket debugging via nc / telnet