In this task, you will write a simple group chat server program and a client program. The server will accept multiple clients and relay client messages back to the clients. Each client, on the other hand, will be a fuzzer generating messages and sending and receiving those messages to and from the server.
Group Chat Server
The group chat server accepts client connections, receives client messages, and echoes those messages back to all clients. The following are the requirements of the server.
The command-line interface should be able to start the server as follows. $ ./server <# of clients>
The executable of your server should be named server.
The server should bind to port port number.
<# of clients> is the expected number of clients.
A client can join any time and leave any time.
You do not need to handle cases where this number is just too big and our container cannot handle that many clients. We will not test with more than 100 clients.
The server should use AF_INET.
The server should bind to all available IP addresses for the local host.
The server should receive all messages from each and every client as they send them.
When the server receives a message from a client, it should send the message to all clients, including the sender.
Message ordering
When a client sends multiple messages, all clients should receive the messages in the original sending order.
In addition, all clients should receive all messages in the same order.
For example, suppose we have three clients, C0, C1, and C2.
Consider the following scenario.
C0 sends a message, M0, which is received by the server.
C1 then sends a message, M1, which is also received by the server.
Lastly, C2 sends a message, M2, which is again received by the server.
Based on the above scenario, C0, C1, and C2 should all receive M0 first, M1 next, and then M2.
This message ordering is not a trick requirement. It is just what you expect from a group chat room.
Note: the simplest way to test a server is to use telnet .
Suppose your local IP address is 192.168.68.6 and you run the server, which binds to port 8000.
telnet 192.168.68.6 8000 will act as a client connected to the server.
127.0.0.1 is a local IP address that refers to the local machine, so telnet 127.0.0.1 8000 will also work.
localhost is a host name that refers to the local machine, so telnet localhost 8000 works as well.
You can enter messages on your terminal as a standard input to telnet and they will be sent to the server.
Messaging Protocol
Assistant
The following is how the server and the clients should format and interpret messages they send and receive.
For clarity, we use two terms---a group chat message and a message.
A group chat message refers to what we would normally call a "message" in the context of a group chat app.
A message is a piece of data sent by a client or a server through a socket. As described below, this may or may not contain a group chat message.
The message size should be no more than 1024 bytes.
Both for the server side and the client side, a message should always start with a one-byte (uint8_t) "type."
0 means that it is a regular message that contains a group chat message.
We refer to this as a type 0 message.
1 means that it is a special "end of execution" message.
Both for the server and the clients, a new line character '\n' always marks the end of a message.
However, you need to be careful with this due to the requirement described next.
When the server receives a type 0 message from a client, the server should get the sender's address (IP and port) and include the address in the message sent to all clients.
Consider a typical group chat app. When a client sends a group chat message, everyone should know who sent it. We achieve this by having the server add the sender's address to each group chat message.
The IP address should be of type uint32_t. It should come after the first byte that indicates the type (0).
The port number should be of type uint16_t. It should come after the IP address.
Note that only the server performs this address inclusion. A client does not do this.
Also note that the address and the port number are numbers, uint32_t and uint16_t, respectively. They are not strings. This means that those numbers can contain a byte value that is '\n', which, in our protocol, indicates the end of a message. You need to be careful with this when you send and receive messages.
In summary, when the server receives a message from a client, it should interpret the first 1 byte as the type. If it is 0, then the server should interpret the rest of the message as a group chat message until it reads '\n'.
If it is 1, the server should follow the description in Two-Phase Commit Protocol below.
Similarly, when a client receives a message from the server, it should interpret the first 1 byte as the type. If it is 0, then the client should interpret the next 4 bytes as the original sender's IP address, the next 2 bytes as the original sender's port number, and the rest as a group chat message until it reads '\n'.
If it is 1, the client should follow the description in Two-Phase Commit Protocol below.
Fuzzing Clients
The client program is a simple custom fuzzer that generates messages and sends those messages to the server. It also receives messages sent by the server. It is a custom fuzzer in the sense that it does not use a fuzzing framework such as libFuzzer discussed in A5. The following are the requirements.
The command-line interface should be able to start a client as follows. $ ./client <# of messages>
The executable should be named client.
IP and port are the IP address and the port number of the server, respectively.
A client should follow the above message protocol when sending and receiving messages.
After it starts, a client should keep generating and sending random messages. <# of messages> should be the number of messages sent.
The random messages should be of type 0.
After sending <# of messages> messages, the client should stop sending type 0 messages, and prepare for termination as described below in Two-Phase Commit Protocol.
You can use getentropy() to generate random bytes. Read man getentropy to understand how to use it.
Since getentropy() gives you random bytes that you may not be able to print out, use the following function to convert it to a hex string. #include <stdint.h>
#include <stdio.h>
#include <unistd.h>
/*
buf should point to an array that contains random bytes to convert to a
hex string.
str should point to a buffer used to return the hex string of the random
bytes. The size of the buffer should be twice the size of the random bytes
(since a byte is two characters in hex) plus one for NULL.
size is the size of the str buffer.
For example,
uint8_t buf[10];
char str[10 * 2 + 1];
getentropy(buf, 10);
if (convert(buf, sizeof(buf), str, sizeof(str)) != 0) {
code
exit(EXIT_FAILURE);
}
*/
int convert(uint8_t *buf, ssize_t buf_size, char *str, ssize_t str_size) {
A client should print out each and every type 0 message it receives from the server and store them in a file named .
Use this format: "%-15s%-10u%s", e.g., printf("%-15s%-10u%s", ip, port, str);
For example, 192.267.128.205 9000 9391DE3E275ADB19637D
Make sure you print out one message per line. Adjust the format string accordingly regarding the new line character.
Note that a client should be able to send messages and receive messages concurrently.
Two-Phase Commit Protocol
To gracefully end the execution for both the server and the clients, the server and the clients should collectively implement a simplified two-phase commit protocol signaling the end of execution as follows.
After sending # of messages messages, each client should send a type 1 message to the server (as opposed to a type 0 message described in Message Protocol). This is to indicate that the client is done sending messages.
After the server receives a type 1 message from each and every client, it should send a type 1 message to all clients. It should then terminate itself.
Each client should terminate after receiving a type 1 message from the server.
Code Structure and CMake
You need to use the same code structure as previous tasks with src/ and include/.
You also need to write CMakeLists.txt that produces two executables, server and client.A Simple Group Chat Server with Fuzzing Clients
In this task, you will write a simple group chat server program and a client program. The server will accept multiple clients and relay client messages back to the clients. Each client, on the other hand, will be a fuzzer generating messages and sending and receiving those messages to and from the server.
Group Chat Server
The group chat server accepts client connections, receives client messages, and echoes those messages back to all clients. The following are the requirements of the server.
The command-line interface should be able to start the server as follows. $ ./server <# of clients>
The executable of your server should be named server.
The server should bind to port port number.
<# of clients> is the expected number of clients.
A client can join any time and leave any time.
You do not need to handle cases where this number is just too big and our container cannot handle that many clients. We will not test with more than 100 clients.
The server should use AF_INET.
The server should bind to all available IP addresses for the local host.
The server should receive all messages from each and every client as they send them.
When the server receives a message from a client, it should send the message to all clients, including the sender.
Message ordering
When a client sends multiple messages, all clients should receive the messages in the original sending order.
In addition, all clients should receive all messages in the same order.
For example, suppose we have three clients, C0, C1, and C2.
Consider the following scenario.
C0 sends a message, M0, which is received by the server.
C1 then sends a message, M1, which is also received by the server.
Lastly, C2 sends a message, M2, which is again received by the server.
Based on the above scenario, C0, C1, and C2 should all receive M0 first, M1 next, and then M2.
This message ordering is not a trick requirement. It is just what you expect from a group chat room.
Note: the simplest way to test a server is to use telnet .
Suppose your local IP address is 192.168.68.6 and you run the server, which binds to port 8000.
telnet 192.168.68.6 8000 will act as a client connected to the server.
127.0.0.1 is a local IP address that refers to the local machine, so telnet 127.0.0.1 8000 will also work.
localhost is a host name that refers to the local machine, so telnet localhost 8000 works as well.
You can enter messages on your terminal as a standard input to telnet and they will be sent to the server.
Messaging Protocol
The following is how the server and the clients should format and interpret messages they send and receive.
For clarity, we use two terms---a group chat message and a message.
A group chat message refers to what we would normally call a "message" in the context of a group chat app.
A message is a piece of data sent by a client or a server through a socket. As described below, this may or may not contain a group chat message.
The message size should be no more than 1024 bytes.
Both for the server side and the client side, a message should always start with a one-byte (uint8_t) "type."
0 means that it is a regular message that contains a group chat message.
We refer to this as a type 0 message.
1 means that it is a special "end of execution" message.
Both for the server and the clients, a new line character '\n' always marks the end of a message.
However, you need to be careful with this due to the requirement described next.
When the server receives a type 0 message from a client, the server should get the sender's address (IP and port) and include the address in the message sent to all clients.
Consider a typical group chat app. When a client sends a group chat message, everyone should know who sent it. We achieve this by having the server add the sender's address to each group chat message.
The IP address should be of type uint32_t. It should come after the first byte that indicates the type (0).
The port number should be of type uint16_t. It should come after the IP address.
Note that only the server performs this address inclusion. A client does not do this.
Also note that the address and the port number are numbers, uint32_t and uint16_t, respectively. They are not strings. This means that those numbers can contain a byte value that is '\n', which, in our protocol, indicates the end of a message. You need to be careful with this when you send and receive messages.
In summary, when the server receives a message from a client, it should interpret the first 1 byte as the type. If it is 0, then the server should interpret the rest of the message as a group chat message until it reads '\n'.
If it is 1, the server should follow the description in Two-Phase Commit Protocol below.
Similarly, when a client receives a message from the server, it should interpret the first 1 byte as the type. If it is 0, then the client should interpret the next 4 bytes as the original sender's IP address, the next 2 bytes as the original sender's port number, and the rest as a group chat message until it reads '\n'.
If it is 1, the client should follow the description in Two-Phase Commit Protocol below.
Fuzzing Clients
The client program is a simple custom fuzzer that generates messages and sends those messages to the server. It also receives messages sent by the server. It is a custom fuzzer in the sense that it does not use a fuzzing framework such as libFuzzer discussed in A5. The following are the requirements.
The command-line interface should be able to start a client as follows. $ ./client <# of messages>
The executable should be named client.
IP and port are the IP address and the port number of the server, respectively.
A client should follow the above message protocol when sending and receiving messages.
After it starts, a client should keep generating and sending random messages. <# of messages> should be the number of messages sent.
The random messages should be of type 0.
After sending <# of messages> messages, the client should stop sending type 0 messages, and prepare for termination as described below in Two-Phase Commit Protocol.
You can use getentropy() to generate random bytes. Read man getentropy to understand how to use it.
Since getentropy() gives you random bytes that you may not be able to print out, use the following function to convert it to a hex string. #include <stdint.h>
#include <stdio.h>
#include <unistd.h>
/*
buf should point to an array that contains random bytes to convert to a
hex string.
str should point to a buffer used to return the hex string of the random
bytes. The size of the buffer should be twice the size of the random bytes
(since a byte is two characters in hex) plus one for NULL.
size is the size of the str buffer.
For example,
uint8_t buf[10];
char str[10 * 2 + 1];
getentropy(buf, 10);
if (convert(buf, sizeof(buf), str, sizeof(str)) != 0) {
code
exit(EXIT_FAILURE);
}
*/
int convert(uint8_t *buf, ssize_t buf_size, char *str, ssize_t str_size) {
A client should print out each and every type 0 message it receives from the server and store them in a file named .
Use this format: "%-15s%-10u%s", e.g., printf("%-15s%-10u%s", ip, port, str);
For example, 192.267.128.205 9000 9391DE3E275ADB19637D
Make sure you print out one message per line. Adjust the format string accordingly regarding the new line character.
Note that a client should be able to send messages and receive messages concurrently.
Two-Phase Commit Protocol
To gracefully end the execution for both the server and the clients, the server and the clients should collectively implement a simplified two-phase commit protocol signaling the end of execution as follows.
After sending # of messages messages, each client should send a type 1 message to the server (as opposed to a type 0 message described in Message Protocol). This is to indicate that the client is done sending messages.
After the server receives a type 1 message from each and every client, it should send a type 1 message to all clients. It should then terminate itself.
Each client should terminate after receiving a type 1 message from the server.
Code Structure and CMake
You need to use the same code structure as previous tasks with src/ and include/.
• You also need to write CMakeLists.txt that produces two executables, server and client.
Certainly! I'd be happy to help you understand how to approach implementing the simple group chat server and client as described. Let's break down the requirements and discuss how you might go about coding each part. I'll provide guidance on key aspects and include code snippets to illustrate important points.
Overview
You'll be implementing:
Server (server executable): Accepts multiple clients, receives messages from them, and echoes those messages back to all clients, including the sender, following specific messaging protocols.
Client (client executable): Connects to the server, sends a specified number of random messages (acting as a fuzzer), receives messages from the server, and logs them.
Both programs should be structured with a src/ directory for source code and an include/ directory for headers, and build with CMake.
Server Implementation
Command-Line Interface
bash
$ ./server <port number> <# of clients>
<port number>: Port on which the server listens.
<# of clients>: Expected number of clients to handle.
Key Requirements
Networking Basics:
Use AF_INET (IPv4 addresses).
Bind to INADDR_ANY to accept connections on all local IP addresses.
Handling Multiple Clients:
Since multiple clients can connect and send messages concurrently, you'll need to handle concurrency.
Use select(), multi-threading, or non-blocking sockets to manage multiple clients.
Message Receiving and Broadcasting:
When a message is received from a client, broadcast it to all connected clients, including the sender.
Maintain message ordering:
Messages from a single client should be sent to others in the order received.
All clients should receive messages in the same order.
Two-Phase Commit Protocol:
Each client sends a type 1 message when done sending messages.
After the server receives a type 1 message from all clients, it sends a type 1 message to all clients and terminates.
Messaging Protocol
Message Format:
First Byte: uint8_t type indicator (0 or 1).
Type 0 Message:
From Client to Server: [Type (0)] [Group Chat Message] '\n'
From Server to Clients:
[Type (0)] [Sender's IP (uint32_t)] [Sender's Port (uint16_t)] [Group Chat Message] '\n'
Type 1 Message:
Indicates end of execution.
Sent without additional data.
Steps to Implement the Server
Set Up the Socket:
c
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // Bind to all interfaces
address.sin_port = htons(port);
bind(server_fd, (struct sockaddr *)&address, sizeof(address));
listen(server_fd, SOMAXCONN);
Handle Client Connections:
Use accept() to accept incoming client connections.
Consider storing client sockets in a list/array for broadcasting messages.
Manage Multiple Clients:
Option 1: select()
Monitor multiple sockets for readability.
Efficient for a relatively small number of clients.
Option 2: Multi-threading
Spawn a new thread for each client connection.
Be careful with shared resources (e.g., the list of client sockets). Use mutexes to prevent race conditions.
Option 3: Non-blocking Sockets and Event Loop
Set sockets to non-blocking mode and use an event loop.
Receiving Messages:
Read the first byte to determine the message type.
For type 0 messages:
Read until you encounter '\n'.
When broadcasting to clients, prepend the sender's IP and port.
For type 1 messages:
Mark that the client has completed sending messages.
Once all clients have sent type 1, proceed to send type 1 messages to all clients.
Broadcasting Messages:
Iterate over all connected clients and send the message.
Ensure that you handle partial sends and socket errors.
Graceful Shutdown:
After sending type messages to all clients, close all client sockets and then the server socket.
Example Code Snippets
Accepting Clients with select()
c
fd_set master_set, read_fds;
int fdmax = server_fd;
FD_ZERO(&master_set);
FD_SET(server_fd, &master_set);
while (1) {
read_fds = master_set;
if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
for (int i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == server_fd) {
// Handle new connections
int new_fd = accept(server_fd, NULL, NULL);
FD_SET(new_fd, &master_set);
if (new_fd > fdmax) {
fdmax = new_fd;
}
} else {
// Handle data from a client
}
}
}
}
Place your source code in src/ and headers in include/.
Adjust source files and add any additional libraries or dependencies as needed.
Additional Tips
Error Handling:
Always check the return values of socket functions (socket(), bind(), listen(), accept(), recv(), send()) for errors.
Use perror() or strerror() to output informative error messages.
Byte Order:
Remember to handle network byte order when sending/receiving multi-byte integers.
Use htonl(), htons(), ntohl(), ntohs() as appropriate.
Concurrency Control:
If using threads, protect shared data structures with mutexes to prevent race conditions.
Be mindful of deadlocks and try to keep the locking granularity as fine as necessary.
Resource Management:
Ensure all sockets and files are properly closed when done.
Handle client disconnects gracefully on the server side.
Testing:
Test your server using telnet as suggested to verify basic functionality.
Run multiple clients to test concurrent message handling and ordering.
Verify that messages are received in the correct order by all clients.
Debugging:
Use logging statements to track the flow of your program.
Consider using tools like gdb for debugging segmentation faults or other issues.
By breaking down the problem into smaller, manageable parts and carefully implementing each component while adhering to the specified protocols, you'll be able to develop the group chat server and client applications successfully.
If you encounter specific issues or have questions about particular parts of the implementation, feel free to ask!