Building an IRC Server 6

2021-04-28
2 min read

by Arthur

[ipv6, unregister connection, buffer packet, bug fixing]

At the time i am writting this, a ticket has been made to have the following change in the subject:
Server will listen on a port n for Client connection and on port K for server connection (lots of ircd does this) it’s much easier to handle new connection and also make a lot more sense.

Anyway, we started off by refactoring how we handle new connection, to either decide if it’s a Client connection or a Server connection.
We had a lot of trouble but manage to do something clean in the end (it got delete because now we already now wether it’s a server or a client depending on the socket we receive the connection on).

We also did IPV6, it’s not pretty hard, just replace AF_INET by AF_INET6, but in our case it got a little bit complicated, we wanted to do logs and so we needed to get the ip address of the connection, thing which used to work pretty well with AF_INET but broke completly when implementing IPV6.

I had to do a memory print of the structure passed to accept() to know where the ip address now belonged too, we then made a custom inet_ntoa which can be found here:

std::string	custom_ntoa(uint32_t in)
{
	std::string		buffer;
	unsigned char	*bytes = (unsigned char *)∈

	buffer = ft_to_string((int)bytes[0]) + "." + ft_to_string((int)bytes[1]) + "." + ft_to_string((int)bytes[2]) + "." + ft_to_string((int)bytes[3]);
	return buffer;
}
/*
** On mac, send:	sockaddr_in6.sin6_addr.__u6_addr.__u6_addr32[3]
** On Linux, send:	sockaddr_in6.sin6_addr.__in6_u.__u6_addr32[3]
*/

Sadly, we havent managed to make it work for ipv6 so it does weird things with ipv6 but osef.

In the meantime, we changed how we send packet to client, we used to write() every time we had to tell him something, we now store everything in a string which is send either after the server rotation or when he is disconnected.

We also added a limit to how many clients could connect to the server.

This week we started trying to destroy our server by doing the weirdest thing we could think off, we fixed whitespace problem in most response, added a destructor to client (which broke many times) etc.

Friday 30 i was connected trough 4g to our server when suddenly i received a call, which made my phone automatically goes into 3g, it broked the entire server with segfaults popping out of nowhere, want to know what happened ? Just read next week post :D.