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
+162
View File
@@ -0,0 +1,162 @@
/* ============================================================================
* THE EXAM'S main.c — ANNOTATED STUDY COPY
*
* Code below is verbatim from the exam attachment (subject/main.c), comments
* added. This is what you receive in the exam, and what you must triage in the
* first 2 minutes.
*
* It does NOT compile as-is on a modern gcc (verified): calloc, malloc, free,
* exit, printf, bzero are used without <stdlib.h>/<stdio.h>/<strings.h>, and
* `int len` is passed where accept() wants socklen_t *. It is not supposed to
* be compiled — the subject warns it "uses forbidden functions or writes
* things that must not be there in your final program".
*
* HOW TO USE IT IN THE EXAM:
* 1. KEEP extract_message() and str_join() — copy them verbatim into your file.
* 2. THROW AWAY main() — it is a one-client demo with a hardcoded port.
* 3. Reuse the socket setup LINES (socket/sockaddr/bind/listen) from main().
* 4. Never ship printf/exit(0): errors are "Wrong number of arguments\n" /
* "Fatal error\n" on stderr + exit status 1.
*
* Stage map:
* B <- the socket setup lines in main()
* C <- you add: slots, the select loop, accept, arrival broadcast
* D <- you add: recv + str_join + extract_message + relay
* E <- you add: recv<=0 -> "just left", close, free
* ==========================================================================*/
#include <errno.h> /* unused here; not needed in your file */
#include <string.h> /* strlen, strcpy, strcat, bzero */
#include <unistd.h> /* write, close */
#include <netdb.h> /* unused here; not needed in your file */
#include <sys/socket.h> /* socket, bind, listen, accept, send, recv */
#include <netinet/in.h> /* sockaddr_in, htonl, htons */
/* MISSING (you add in your file): <stdlib.h> for malloc/calloc/free/exit/atoi,
* <stdio.h> for sprintf, <sys/select.h> for select + FD_* macros. */
/* ------------------------------------------------------------------------- *
* extract_message — pull ONE complete \n-terminated line out of *buf.
*
* CONTRACT (memorize the three return values, not the body):
* 1 -> a line was found: *msg points at it (you must free(*msg)),
* *buf now holds the remainder
* 0 -> no '\n' yet: keep waiting, nothing to send
* -1 -> allocation failure: treat as "Fatal error\n" + exit 1
*
* This is THE function that makes TCP's arbitrary chunking irrelevant.
* Call it in a loop after every append — a single recv may contain several
* lines, and a line may span several recv calls.
* ------------------------------------------------------------------------- */
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);
}
/* ------------------------------------------------------------------------- *
* str_join — append `add` to `buf`, return a NEW heap string, free old `buf`.
* Returns NULL on allocation failure. Handles buf == NULL (new buffer).
*
* This is how a per-client pending buffer grows across recv calls. The exam
* gives it to you; copy it verbatim. Leak rule: the function frees the old
* buffer itself; you free the final buffer when the client disconnects.
* ------------------------------------------------------------------------- */
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);
}
/* ------------------------------------------------------------------------- *
* main — A DEMO, NOT A SOLUTION. Replace it entirely.
*
* What is wrong with it (why you rewrite main):
* - no argc check, no "Wrong number of arguments\n";
* - hardcoded port 8081 instead of atoi(argv[1]);
* - printf() everywhere (forbidden) and exit(0) on errors (must be exit 1
* with "Fatal error\n" on stderr);
* - accepts ONE client and then does nothing with it: no select loop, no
* ids, no broadcast, no disconnect handling;
* - `int len` is passed to accept() which wants socklen_t *.
*
* What is worth keeping: the exact socket/sockaddr/bind/listen lines.
* ------------------------------------------------------------------------- */
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) { /* in your file: fatal() */
printf("socket creation failed...\n"); /* forbidden */
exit(0); /* wrong status, wrong text */
}
else
printf("Socket successfully created..\n"); /* forbidden; grader ignores stdout */
bzero(&servaddr, sizeof(servaddr)); /* KEEP: must zero the struct */
// assign IP, PORT
servaddr.sin_family = AF_INET; /* KEEP: IPv4 */
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1 /* KEEP: localhost only */
servaddr.sin_port = htons(8081); /* in your file: htons(atoi(argv[1])) */
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n"); /* replace with fatal() */
exit(0);
}
else
printf("Socket successfully binded..\n");
if (listen(sockfd, 10) != 0) { /* KEEP: backlog 10 */
printf("cannot listen\n"); /* replace with fatal() */
exit(0);
}
len = sizeof(cli); /* should be socklen_t len */
connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0) { /* in your loop: do NOT exit */
printf("server acccept failed...\n"); /* forbidden */
exit(0);
}
else
printf("server acccept the client...\n");
/* ... and then the demo ends. Your program now needs:
* - slots init, g_maxfd = g_server;
* - while (1): rebuild fd_set, select, accept / recv, broadcast,
* disconnect, cleanup.
* Follow drills/typing-script.md stages C-E. */
}
+94
View File
@@ -0,0 +1,94 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.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");
}
+42
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)