new tester

This commit is contained in:
asus
2023-02-17 15:03:41 +01:00
parent 356267e288
commit 101d7b7581
24 changed files with 87 additions and 12648 deletions

16
utils/commands.txt Normal file
View File

@@ -0,0 +1,16 @@
clang -Wall -Wextra -Werror mini_serv.c
./a.out <port>
nc localhost <port>
echo "test1" | nc localhost <port>
bash tester.sh 2>/dev/null
bash tester.sh 2>/dev/null | sort 1>log1.txt
rm mini_serv.c; cp mini_serv_03.c mini_serv.c; bash tester.sh 2>/dev/null | sort 1>log1.txt

95
utils/main.c Normal file
View File

@@ -0,0 +1,95 @@
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
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);
}
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 main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(8081);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
if (listen(sockfd, 10) != 0) {
printf("cannot listen\n");
exit(0);
}
len = sizeof(cli);
connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
}

143
utils/mini_serv_eric.c Normal file
View File

@@ -0,0 +1,143 @@
// #include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct s_clients {
int id;
char msg[1024];
} t_clients;
// so we just have a table of clients, no need to worry about linked lists, yea sounds good to me
t_clients clients[1024]; // 1024 otherwise would have to change FD_SETSIZE and why bother
fd_set readfds, writefds, active;
int fd_Max = 0;
int id_Next = 0;
char bufread[120000];
char bufwrite[120000];
void ft_error(char *str)
{
// 2 is stderr i guess
if (str)
write(2, str, strlen(str));
else
write(2, "Fatal error", strlen("Fatal error"));
write(2, "\n", 1);
exit(1);
}
// not is the fd that send the message meaning you should not send that message to that fd
void send_to_all(int not)
{
for(int i = 0; i <= fd_Max; i++)
{
// have to check cuz we use an array not a linked list
if (FD_ISSET(i, &writefds) && i != not)
send(i, bufwrite, strlen(bufwrite), 0); // man send explains things well
}
}
int main(int ac, char **av)
{
if (ac != 2)
ft_error("Wrong number of arguments");
// just like in main.c
// all this is explained in the man socket, cool
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
ft_error(NULL);
// like bzeroing i guess but for the struct fd_set, yea it clears the set, none are active
FD_ZERO(&active);
// i think this gets all the contents of the structs as well ? yes cuz it's an array not a linked list so easy to bzero
bzero(&clients, sizeof(clients));
fd_Max = sockfd; // not sure what that does, none of this is in main.c
FD_SET(sockfd, &active); // we add sockfd to active, ok cool
struct sockaddr_in servaddr; // like in main.c
socklen_t len; // not in main.c, i mean it kinda is but i prefer doing it this way, not even that important
bzero(&servaddr, sizeof(servaddr)); // in main.c
servaddr.sin_family = AF_INET; // like main.c
servaddr.sin_addr.s_addr = htonl(2130706433); // like main.c
servaddr.sin_port = htons(atoi(av[1])); // slightly different than main.c cuz have to get the right port
// just like main.c, we bind and listen, normal stuff that requres specific numbers
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) < 0)
ft_error(NULL);
if (listen(sockfd, 10) < 0)
ft_error(NULL);
// no longer like main.c
while(1)
{
readfds = active;
writefds = active;
// so like if it didn't add a new one? it continues? yea idk something about making sure select has happened
if (select(fd_Max + 1, &readfds, &writefds, NULL, NULL) < 0)
continue;
// we cycle through all the FDs we've assigned at one point so far (they may not all be active)
for (int fd_i = 0; fd_i <= fd_Max; fd_i++)
{
// for each one we check if it's set, and check if is or isn't sockfd
// ohhh this is if a client just arrived
if (FD_ISSET(fd_i, &readfds) && fd_i == sockfd)
{
// try to accept a new connection
int connfd = accept(sockfd, (struct sockaddr*)&servaddr, &len); // more or less in main.c
if (connfd < 0)
continue; // non blocking but this one didn't work
// reset fd_max
fd_Max = connfd > fd_Max ? connfd : fd_Max; // whichever is larger is the new max
clients[connfd].id = id_Next++; // we add the new client
FD_SET(connfd, &active); // add it to the list
sprintf(bufwrite, "server: client %d just arrived\n", clients[connfd].id); // create message to send out
send_to_all(connfd);
break;
}
// things other than a new connection i guess
if (FD_ISSET(fd_i, &readfds) && fd_i != sockfd)
{
// 65536 is just a big number i think
int res = recv(fd_i, bufread, 65536, 0); // similar to send, also see man recv, it's good
// something happened to a client so we tell the others this one is gone and handle clears and prevent leaks and such
if (res <= 0) // -1 is error and 0 is end of file
{
sprintf(bufwrite, "server: client %d just left\n", clients[fd_i].id);
send_to_all(fd_i);
FD_CLR(fd_i, &active);
close(fd_i);
break;
}
else
{
// we read the whole message we just received and then send it out
for(int i = 0, j = strlen(clients[fd_i].msg); i < res; i++, j++)
{
clients[fd_i].msg[j] = bufread[i];
if (clients[fd_i].msg[j] == '\n')
{
clients[fd_i].msg[j] = '\0';
sprintf(bufwrite, "client %d: %s\n", clients[fd_i].id, clients[fd_i].msg);
send_to_all(fd_i);
bzero(clients[fd_i].msg, strlen(clients[fd_i].msg));
j = -1; // becomes 0 right after this at the end of the loop, i think
}
}
break;
}
}
}
}
return (0);
}

316
utils/mini_serv_luke.c Normal file
View File

@@ -0,0 +1,316 @@
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/select.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h> // debug
///////////
struct s_client
{
int fd;
int id;
struct s_client *next;
} typedef t_client;
t_client *g_clients = NULL;
int g_id = 0;
int lsocket = -1;
fd_set rfds;
fd_set wfds;
////////////
int extract_message(char **buf, char **msg);
char *str_join(char *buf, char *add);
void init(int port);
t_client *new_client(int fd);
t_client *close_client(t_client *client_to_close);
void broadcast(t_client *client, char *str);
int read_client(t_client *client);
void free_all();
void fatal_error();
ssize_t ft_print(int fd, char *buf);
////////////
int main(int argc, char** argv)
{
// int count = 0; // force exit debug
int fd_max;
t_client *clients;
int new_fd;
int client_id;
t_client *last_connected;
char announce_msg[420];
int ret;
if (argc != 2) {
ft_print(STDERR_FILENO, "Wrong number of arguments\n");
exit(1);
}
init(atoi(argv[1]));
fcntl(lsocket, F_SETFL, O_NONBLOCK); // debug
while (1)
{
// reset rfds and wfds
fd_max = lsocket;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(lsocket, &rfds);
clients = g_clients;
while (clients)
{
if (clients->fd > fd_max)
fd_max = clients->fd;
FD_SET(clients->fd, &rfds);
FD_SET(clients->fd, &wfds);
clients = clients->next;
}
// multiplexing (select)
select(fd_max + 1, &rfds, &wfds, NULL, NULL);
// accept new connection
if (FD_ISSET(lsocket, &rfds))
{
new_fd = accept(lsocket, NULL, NULL);
fcntl(new_fd, F_SETFL, O_NONBLOCK); // debug
last_connected = new_client(new_fd);
sprintf(announce_msg, "server: client %d just arrived\n", last_connected->id);
broadcast(last_connected, announce_msg);
// debug
// ++count;
// printf("count: %d\n", count);
// if (count == 7)
// break;
}
// read incoming clients messages
clients = g_clients;
while (clients)
{
ret = 1;
if (FD_ISSET(clients->fd, &rfds))
ret = read_client(clients);
if (ret == 0)
{
client_id = clients->id;
clients = close_client(clients);
sprintf(announce_msg, "server: client %d just left\n", client_id);
broadcast(NULL, announce_msg);
}
else
clients = clients->next;
}
}
free_all();
return (0);
}
void init(int port)
{
struct sockaddr_in servaddr;
lsocket = socket(AF_INET, SOCK_STREAM, 0);
if (lsocket == -1)
fatal_error();
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(port);
if (bind(lsocket, (const struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
fatal_error();
if (listen(lsocket, 10) != 0)
fatal_error();
}
t_client *new_client(int fd)
{
t_client *new;
new = calloc(1, sizeof (t_client));
if (!new)
fatal_error();
new->fd = fd;
new->id = g_id++;
if (!g_clients)
g_clients = new;
else
{
t_client *last = g_clients;
while (last->next)
last = last->next;
last->next = new;
}
return (new);
}
t_client *close_client(t_client *client_to_close)
{
t_client *next_client = client_to_close->next;
t_client *clients = g_clients;
if (g_clients == client_to_close)
{
g_clients = next_client;
}
else
{
while (clients->next != client_to_close)
clients = clients->next;
clients->next = next_client;
}
close(client_to_close->fd);
free(client_to_close);
return (next_client);
}
void broadcast(t_client *client, char *str)
{
t_client *clients = g_clients;
while (clients)
{
if (clients != client && FD_ISSET(clients->fd, &wfds))
{
send(clients->fd, str, strlen(str), 0);
}
clients = clients->next;
}
}
int read_client(t_client *client)
{
char *buf;
int recv_ret;
buf = malloc(420000);
if (!buf)
fatal_error();
recv_ret = recv(client->fd, buf, 420000, 0);
if (recv_ret > 0)
{
buf[recv_ret] = '\0';
if (recv_ret == 420000) // debug
ft_print(STDERR_FILENO, "Oupsi, ca depasse. Il faut malloc mon petit\n");
char buf_sprintf[420];
char *msg;
char *final_msg;
int ret = 1;
while (ret == 1)
{
msg = NULL;
ret = extract_message(&buf, &msg);
if (ret == -1)
{
free(buf);
fatal_error();
}
if (!msg)
msg = buf;
if (msg && msg[0] != 0)
{
sprintf(buf_sprintf, "client %d: ", client->id);
final_msg = str_join(buf_sprintf, msg);
free(msg);
if (!final_msg)
{
free(buf);
fatal_error();
}
broadcast(client, final_msg);
free(final_msg);
}
else
free(msg);
}
}
else
{
free(buf);
}
return (recv_ret);
}
void free_all()
{
if (lsocket != -1)
close(lsocket);
while (g_clients)
close_client(g_clients);
}
void fatal_error()
{
ft_print(STDERR_FILENO, "Fatal error\n");
free_all();
exit(1);
}
ssize_t ft_print(int fd, char *buf)
{
return write(fd, buf, strlen(buf));
}
// Base example function
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);
}
// Base example function, minus the commented free()
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);
}

42
utils/subject.en.txt Normal file
View File

@@ -0,0 +1,42 @@
Assignment name : mini_serv
Expected files : mini_serv.c
Allowed functions: write, close, select, socket, accept, listen, send, recv, bind, strstr, malloc, realloc, free, calloc, bzero, atoi, sprintf, strlen, exit, strcpy, strcat, memset
--------------------------------------------------------------------------------
Write a program that will listen for client to connect on a certain port on 127.0.0.1 and will let clients to speak with each other
This program will take as first argument the port to bind to
If no argument is given, it should write in stderr "Wrong number of arguments" followed by a \n and exit with status 1
If a System Calls returns an error before the program start accepting connection, it should write in stderr "Fatal error" followed by a \n and exit with status 1
If you cant allocate memory it should write in stderr "Fatal error" followed by a \n and exit with status 1
Your program must be non-blocking but client can be lazy and if they don't read your message you must NOT disconnect them...
Your program must not contains #define preproc
Your program must only listen to 127.0.0.1
The fd that you will receive will already be set to make 'recv' or 'send' to block if select hasn't be called before calling them, but will not block otherwise.
When a client connect to the server:
- the client will be given an id. the first client will receive the id 0 and each new client will received the last client id + 1
- %d will be replace by this number
- a message is sent to all the client that was connected to the server: "server: client %d just arrived\n"
clients must be able to send messages to your program.
- message will only be printable characters, no need to check
- a single message can contains multiple \n
- when the server receive a message, it must resend it to all the other client with "client %d: " before every line!
When a client disconnect from the server:
- a message is sent to all the client that was connected to the server: "server: client %d just left\n"
Memory or fd leaks are forbidden
To help you, you will find the file main.c with the beginning of a server and maybe some useful functions. (Beware this file use forbidden functions or write things that must not be there in your final program)
Warning our tester is expecting that you send the messages as fast as you can. Don't do un-necessary buffer.
Evaluation can be a bit longer than usual...
Hint: you can use nc to test your program
Hint: you should use nc to test your program
Hint: To test you can use fcntl(fd, F_SETFL, O_NONBLOCK) but use select and NEVER check EAGAIN (man 2 send)