So I’m trying to convert some C code for an IRC server to Zig, and right now I’m not really sure where some stuff can be found.
bool server_init (Server_t *const server, i32 port, const char *const password) {
if (!server or port < 1024 or port >= 65536 or !password) {
return (false);
}
signal (SIGINT, server_handle_signal);
signal (SIGQUIT, server_handle_signal);
log_info ("server : signal_handler setup.");
memset (&server->address, 0, sizeof (struct sockaddr_in));
server->address.sin_family = AF_INET;
server->address.sin_addr.s_addr = INADDR_ANY;
server->address.sin_port = htons (port);
log_info ("server : port resolution done.");
server->password = strdup (password);
if (!server->password) {
return (false);
}
log_info ("server : password setup.");
server->socket = socket (AF_INET, SOCK_STREAM, 0);
if (server->socket == -1) {
return (false);
}
log_info ("server : socket openned.");
isize error_or_value = 0;
isize optval = 0;
error_or_value = setsockopt (server->socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
if (error_or_value == -1) {
return (false);
}
log_info ("server : socket options setup.");
error_or_value = fcntl (server->socket, F_SETFL, O_NONBLOCK);
if (error_or_value == -1) {
return (false);
}
log_info ("server : socket switched to O_NONBLOCK.");
error_or_value = bind (server->socket, (const struct sockaddr *) &server->address, sizeof (struct sockaddr_in));
if (error_or_value == -1) {
return (false);
}
log_info ("server : socket binded.");
error_or_value = listen (server->socket, SOMAXCONN);
if (error_or_value == -1) {
return (false);
}
log_info ("server : socket is listenning.");
server->pollfds.push_back ((struct pollfd){
.fd = server->socket,
.events = POLLIN,
.revents = 0,
});
log_info ("server : initialization is done. waiting for new connections...");
return (true);
}
So first of all I’m not a network/web person, I’m very ignorant about that kind of programming, so this is why I’m asking because since I’m not very familiar with this kind of code, the different options in the std weren’t very clear to me.
The first thing that I need is about the signal handling, I’ve looked in a few namespace for the signal function to map a handler to a signal but I can’t seem to find one in the different namespaces of the std.
Secondly I need htons which If my understanding is correct is simply a macro that reverse the bits of the port to be in Big endian ?.
also I’ve noticed that there exist the std.net namespace, which I think is a different API which can achieve the same thing, if anyone could indicate me what would be the equivalent code using the std.net, or if anyone knows some code that I can look at to understand it would be appreciated.