core: add all the required files

This commit is contained in:
mkurkar
2026-11-04 16:42:12 +03:00 Unverified
commit 29324ac2ea
57 changed files with 57688 additions and 0 deletions
+223
View File
@@ -0,0 +1,223 @@
/* ============================================================================
* mini_serv — reference solution (known good).
*
* NOT an exam asset. Use it only to:
* 1) validate the test harness (tester/check.sh must be green on it), and
* 2) diff YOUR attempt against it afterwards to build your "what I missed" list.
*
* Written for: subject/subject.en.txt
* Compile: cc -Wall -Wextra -Werror mini_serv.c -o mini_serv
* ==========================================================================*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
typedef struct s_client
{
int fd;
int id;
char *buf;
} t_client;
static t_client g_c[FD_SETSIZE];
static int g_maxfd;
static int g_server;
static int g_next_id;
static void fatal(void)
{
write(2, "Fatal error\n", 12);
exit(1);
}
/* ---- helpers as given in the exam's main.c attachment ---- */
static char *str_join(char *buf, char *add)
{
char *newbuf;
int len;
if (buf == 0)
len = 0;
else
len = strlen(buf);
newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
if (newbuf == 0)
return (0);
newbuf[0] = 0;
if (buf != 0)
strcat(newbuf, buf);
free(buf);
strcat(newbuf, add);
return (newbuf);
}
static int extract_message(char **buf, char **msg)
{
char *newbuf;
int i;
*msg = 0;
if (*buf == 0)
return (0);
i = 0;
while ((*buf)[i])
{
if ((*buf)[i] == '\n')
{
newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
if (newbuf == 0)
return (-1);
strcpy(newbuf, *buf + i + 1);
*msg = *buf;
(*msg)[i + 1] = 0;
*buf = newbuf;
return (1);
}
i++;
}
return (0);
}
/* ---- server logic ---- */
static void to_all_except(int fd, char *s)
{
int i;
i = 0;
while (i <= g_maxfd)
{
if (i != fd && g_c[i].fd != -1)
send(g_c[i].fd, s, strlen(s), 0);
i++;
}
}
static void accept_new(void)
{
char msg[64];
int fd;
fd = accept(g_server, NULL, NULL);
if (fd < 0)
return ;
g_c[fd].fd = fd;
g_c[fd].id = g_next_id++;
g_c[fd].buf = NULL;
if (fd > g_maxfd)
g_maxfd = fd;
sprintf(msg, "server: client %d just arrived\n", g_c[fd].id);
to_all_except(fd, msg);
}
static void disconnect(int fd)
{
char msg[64];
int id;
id = g_c[fd].id;
close(fd);
g_c[fd].fd = -1;
g_c[fd].id = -1;
free(g_c[fd].buf);
g_c[fd].buf = NULL;
sprintf(msg, "server: client %d just left\n", id);
to_all_except(-1, msg);
while (g_maxfd > g_server && g_c[g_maxfd].fd == -1)
g_maxfd--;
}
static void handle_read(int fd)
{
char tmp[4096];
char *msg;
char *out;
int n;
n = recv(fd, tmp, sizeof(tmp) - 1, 0);
if (n <= 0)
{
disconnect(fd);
return ;
}
tmp[n] = 0;
g_c[fd].buf = str_join(g_c[fd].buf, tmp);
if (!g_c[fd].buf)
fatal();
while (extract_message(&g_c[fd].buf, &msg) == 1)
{
out = malloc(strlen(msg) + 64);
if (!out)
{
free(msg);
fatal();
}
sprintf(out, "client %d: %s", g_c[fd].id, msg);
to_all_except(fd, out);
free(out);
free(msg);
}
}
int main(int argc, char **argv)
{
struct sockaddr_in addr;
fd_set readfds;
int i;
if (argc != 2)
{
write(2, "Wrong number of arguments\n", 26);
return (1);
}
g_server = socket(AF_INET, SOCK_STREAM, 0);
if (g_server < 0)
fatal();
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(2130706433);
addr.sin_port = htons(atoi(argv[1]));
if (bind(g_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
fatal();
if (listen(g_server, 10) < 0)
fatal();
i = 0;
while (i < FD_SETSIZE)
{
g_c[i].fd = -1;
g_c[i].id = -1;
g_c[i].buf = NULL;
i++;
}
g_maxfd = g_server;
while (1)
{
FD_ZERO(&readfds);
FD_SET(g_server, &readfds);
i = 0;
while (i <= g_maxfd)
{
if (g_c[i].fd != -1)
FD_SET(i, &readfds);
i++;
}
if (select(g_maxfd + 1, &readfds, NULL, NULL, NULL) < 0)
continue ;
if (FD_ISSET(g_server, &readfds))
accept_new();
i = 0;
while (i <= g_maxfd)
{
if (i != g_server && g_c[i].fd != -1 && FD_ISSET(i, &readfds))
handle_read(i);
i++;
}
}
return (0);
}
+166
View File
@@ -0,0 +1,166 @@
/* ============================================================================
* mini_serv — community variant ("the sets + one-event-per-wakeup style").
*
* Source: the version the user's friend passed exam 06 with.
* Validated in this gym with:
* - cc -Wall -Wextra -Werror
* - tester/mini_serv_tester.py (13/13)
* - the real exam-style Grademe diff (PASSED)
*
* Style differences vs reference/mini_serv.c:
* - keeps a master fd_set (active_fds) and copies it into read_fds/write_fds
* before select, instead of rebuilding from a slot array;
* - processes ONE event per select() wakeup and `break`s (slower, but simpler);
* - sends arrival/departure messages via a shared writebuff.
*
* Known cosmetic wart: guards `cl_sock >= 10240` while `clients[1024]` — with
* select(2) fds are always < 1024, so the array cannot be overrun in practice.
* ==========================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
typedef struct s_client {
int id;
char *msg;
} t_client;
static t_client clients[1024];
static char readbuff[100000], writebuff[100000];
static int max_fd = 0, next_id = 0;
static fd_set active_fds, read_fds, write_fds;
void exitError(char *str) {
write(2, str, strlen(str));
exit(1);
}
void sendMsg(int sender_fd) {
for (int fd = 0; fd <= max_fd; fd++)
if (FD_ISSET(fd, &write_fds) && fd != sender_fd)
send(fd, writebuff, strlen(writebuff), 0);
}
/* str_join and extract_message are given in the exam's main.c */
char *str_join(char *buf, char *add)
{
char *newbuf;
int len;
if (buf == 0)
len = 0;
else
len = strlen(buf);
newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
if (newbuf == 0)
return (0);
newbuf[0] = 0;
if (buf != 0)
strcat(newbuf, buf);
free(buf);
strcat(newbuf, add);
return (newbuf);
}
int extract_message(char **buf, char **msg)
{
char *newbuf;
int i;
*msg = 0;
if (*buf == 0)
return (0);
i = 0;
while ((*buf)[i])
{
if ((*buf)[i] == '\n')
{
newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
if (newbuf == 0)
return (-1);
strcpy(newbuf, *buf + i + 1);
*msg = *buf;
(*msg)[i + 1] = 0;
*buf = newbuf;
return (1);
}
i++;
}
return (0);
}
int main(int ac, char **av) {
if (ac != 2)
exitError("Wrong number of arguments\n");
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
exitError("Fatal error\n");
max_fd = sockfd;
FD_ZERO(&active_fds);
FD_SET(sockfd, &active_fds);
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433);
servaddr.sin_port = htons(atoi(av[1]));
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
exitError("Fatal error\n");
if (listen(sockfd, 10) != 0)
exitError("Fatal error\n");
while (1) {
read_fds = write_fds = active_fds;
if (select(max_fd + 1, &read_fds, &write_fds, NULL, NULL) < 0)
continue;
for (int fd = 0; fd <= max_fd; fd++) {
if (FD_ISSET(fd, &read_fds) && fd == sockfd) {
int cl_sock = accept(sockfd, NULL, NULL);
if (cl_sock < 0)
continue;
if (cl_sock >= 10240) {
close(cl_sock);
continue;
}
if (cl_sock > max_fd)
max_fd = cl_sock;
clients[cl_sock].id = next_id++;
clients[cl_sock].msg = NULL;
FD_SET(cl_sock, &active_fds);
sprintf(writebuff, "server: client %d just arrived\n", clients[cl_sock].id);
sendMsg(cl_sock);
break;
}
else if (FD_ISSET(fd, &read_fds)) {
int read_bytes = recv(fd, readbuff, sizeof(readbuff) - 1, 0);
if (read_bytes <= 0) {
sprintf(writebuff, "server: client %d just left\n", clients[fd].id);
sendMsg(fd);
FD_CLR(fd, &active_fds);
close(fd);
free(clients[fd].msg);
break;
} else {
readbuff[read_bytes] = '\0';
clients[fd].msg = str_join(clients[fd].msg, readbuff);
char *line = NULL;
int res;
while ((res = extract_message(&clients[fd].msg, &line)) > 0)
{
sprintf(writebuff, "client %d: ", clients[fd].id);
char *str = str_join(NULL, writebuff);
str = str_join(str, line);
for (int cli_fd = 0; cli_fd <= max_fd; cli_fd++)
if (FD_ISSET(cli_fd, &write_fds) && fd != cli_fd)
send(cli_fd, str, strlen(str), 0);
free(str);
free(line);
}
}
}
}
}
}