Files
42-exam06/reference/mini_serv.c
T

224 lines
4.0 KiB
C

/* ============================================================================
* 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);
}