Create your own IRC server in C++, fully compatible with an official client.
The keyword in IRC is "Relay." While "Internet" and "Chat" have obvious
meanings, "Relay" sounds a bit more mysterious. Let's have a look at the
basic concept of IRC in order to discover the meaning behind the term.
IRC, in its simplest form, is made up of two programs—a server
program that accepts connections and a client program that connects
to the server.
Of course, it isn't absolutely necessary to use a special program—
the server would view a simple network connection between you and
the server as a client. However, a client program handles some necessary
procedures automatically and provides a better and simpler user interface
than the more technical messages the client and server exchange.
IRC servers connect to each other via an IRC network of servers. Let's
use a very simple model of an IRC network for our example: two servers
and two clients. The servers are connected to each other, and each has a
client (a user) connected to it. The structure would look like this:
![Screen Shot 2024-02-29 at 11 21 02 AM](https://private-user-images.githubusercontent.com/79755743/308888422-d18a804a-4ce2-4e42-b337-041b86fdad1f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzk3MzgsIm5iZiI6MTcyNDAzOTQzOCwicGF0aCI6Ii83OTc1NTc0My8zMDg4ODg0MjItZDE4YTgwNGEtNGNlMi00ZTQyLWIzMzctMDQxYjg2ZmRhZDFmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDAzNTAzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdjMThkZjViNmI3YjU4MmI0MDNjM2IwYzYwZTUzOWYyNzZkMjBhY2M3OTJjYjI5Nzc3MWU1MmI0NWNjNzVhMTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pmc8ijVf5NePiKTZsysvZW7SX8lZT6JrUSCsbgGMXj0)
![Screen Shot 2024-02-29 at 11 26 58 AM](https://private-user-images.githubusercontent.com/79755743/308888319-1c2e7cf0-ae22-40b5-a296-1527ab857a94.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzk3MzgsIm5iZiI6MTcyNDAzOTQzOCwicGF0aCI6Ii83OTc1NTc0My8zMDg4ODgzMTktMWMyZTdjZjAtYWUyMi00MGI1LWEyOTYtMTUyN2FiODU3YTk0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDAzNTAzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNkOTIwNmNmOTg5YzFjYjYzZmMxZDM0NDljYzc2YTVhM2RiZWJjZDJkMDc5NWZjMDgxMTY2N2RjMDQxYTcwNmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.oKG7uGY0z-7t_-RzVpJaXyszVrZYyT2hMSOdAvRE2RA)
All networking API function are considered equivalent to system calls
such as read
{.verbatim} or write
{.verbatim}, they lay in man(2).
#define INADDR_ANY (u_int32_t)0x00000000
in_addr_t inet_addr(const char *cp);
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
int socket(int domain, int type, int protocol);
int bind(int sock_fd, struct sockaddr *addr, socklen_t addr_size);
int listen(int socket, int backlog);
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
ssize_t send(int socket, const void *buffer, size_t length, int flags);
ssize_t recv(int socket, void *buffer, size_t length, int flags);
When socket()
{.verbatim} is created, which is just allocating a file
resource in the OS, by specifiying the protocol family and type, and
which mode of transmission i.e. UDP or TCP. It requires additional
information on the address itself, namelty the host and the port, which
both should be in machine encoding i.e. same behaviour as
hton()
{.verbatim}.
- Except that
inet_addr()
{.verbatim} returns the address in machine encoding already
As a legacy, there are two types
struct sockaddr
{.verbatim} andstruct sockaddr_in
{.verbatim}, they represent the same information, but the later is more convenient to work with.
At last, both the socket and the address are entangeled using
bind()
{.verbatim}
When getting a socket for the server, we may listen()
{.verbatim} for
incoming connections. Each new connection is a new socket()
{.verbatim}
associated with a client, that is created after calling
accept()
{.verbatim}.
However, listen()
{.verbatim} runs asynchronously,
accept()
{.verbatim} should be called for as many clients that ahve
sent the request. Yet, it blocks e.g. same as waiting for IO. Thus if no
new request has been sent, accept()
{.verbatim} will await and blocks
the process.
This can be resolved using multitasking, by telling the OS to not block
using fcntl()
{.verbatim} to alter the flags of the socket file. And
then use poll()
{.verbatim} to fetch only the ones that are ready to be
fetched. By setting event POLLIN
{.verbatim} on creation, the OS would
set flags in return when it is unblocking. Now instead of blocking the
process, an EWOULDBLOCK
{.verbatim} (or EAGAIN
{.verbatim}) would be
raised, resolving the block.
For each client that has sent, we may recv()
{.verbatim} some data to
be put in a buffer, the length of the data is returned, and a closed
connection would return 0. However, the total length of the incoming
data may overfit the size of the buffer, thus it may require multiple
reads until it blocks by raising errno
{.verbatim}.
Internet Relay Chat (RFC1459)
- The UChicago χ-Projects Internet Relay Chat
- Beej’s Guide to Network Programming Using Internet Sockets
- https://ubuntu.com/tutorials/irc-server#5-build-the-server
- https://www.inspircd.org
- https://linuxhint.com/irc_ubuntu/
- https://ircgod.com/docs/irc/to_know/
- Projet Reseau Warm Up Exercice — Building an IRC Client and Server Under guidance of Benoˆıt Barbot & C´esar Rodr´ıguez
- Service Name and Transport Protocol Port Number Registry
- Check if a string starts with a certain string in C++
- Modern IRC Client Protocol
- Socket Programming Tutorial In C For Beginners | Part 1 | Eduonix
- Creating a connection-oriented socket
- What is the difference between a port and a socket?
- The Definition of a Socket Joel M. Winett Lincoln Laboratory 360/677 May 1971
- Chapter 4. Elementary TCP Sockets
- TCP Server-Client implementation in C
- ARM BSD Socket
- sockets docs.oracle.com
- IRC Standard
- Using Freenode IRC Server
- Beej's Guide to Network Programming
- DWise1's Sockets Programming Pages
- Handling multiple users
![Screen Shot 2024-02-29 at 12 28 10 PM](https://private-user-images.githubusercontent.com/79755743/308906173-12b7fa9c-d9a8-4498-87f4-5ab9d906eeca.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzk3MzgsIm5iZiI6MTcyNDAzOTQzOCwicGF0aCI6Ii83OTc1NTc0My8zMDg5MDYxNzMtMTJiN2ZhOWMtZDlhOC00NDk4LTg3ZjQtNWFiOWQ5MDZlZWNhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDAzNTAzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ2Yjg4NmY3YjdmM2E2NDViNmViNmMzNTAzZWU0OGU3ODQ5MzJiZWNiZWUzNzZmZDA1ZmRjZTFhM2E3MGY2NDEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.yuUiU6A_os4Gu2MslCJXTeWF9TjyELu4_OiP01frwtU)
![Screen Shot 2024-02-29 at 11 50 41 AM](https://private-user-images.githubusercontent.com/79755743/308906189-19cbfe85-a0d3-4684-801c-6ab4574d7206.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwMzk3MzgsIm5iZiI6MTcyNDAzOTQzOCwicGF0aCI6Ii83OTc1NTc0My8zMDg5MDYxODktMTljYmZlODUtYTBkMy00Njg0LTgwMWMtNmFiNDU3NGQ3MjA2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDAzNTAzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM2NWMwOTU0MmNmMjIwNmE4ZWNlMThhYzFiYjI4OGIzNmE5YzNjOTVmY2U1OGJlNjUxOGUwMjFlZGVjNDBlN2YmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.WPGzwynfLIH18_0TYBuig3FbkUfnLRexAPXrPuhxR4U)