A comprehensive client-server application written in C that demonstrates secure, encrypted communication over both TCP (using TLS) and UDP (using DTLS) with the OpenSSL library.
This project was created to provide a clear and practical example of secure socket programming. Implementing TLS and DTLS from scratch can be a complex task, especially when handling different protocols and managing multiple clients. This repository breaks down the process into a well-structured, multi-threaded server that can handle both TCP and UDP connections simultaneously, along with corresponding clients to test the functionality.
The server uses pthreads
to manage concurrent TCP clients and implements a custom session management system to handle connectionless DTLS clients. The codebase is organized modularly with a clean Makefile
build system, making it an excellent learning resource for students, developers, or anyone interested in network security and C programming.
- Hybrid Server: A single server application listens for and handles both TCP and UDP connections on different ports.
- Secure Communication: Implements TLS v1.2+ for TCP and DTLS v1.2 for UDP to encrypt all data exchanged between the server and clients.
- Multi-Client Handling: Uses POSIX threads (
pthreads
) to handle multiple TCP clients concurrently without blocking. - DTLS Session Management: Includes a robust mechanism to find or create sessions for new DTLS clients, with automatic cleanup for timed-out sessions.
- Functional & Clean Code: The project is organized into
client
,server
, andcommon
modules with separate header files and a functional programming style for better readability and maintenance. - Robust Logging: Features a custom logging utility that writes timestamped events, errors, and debug messages to both the console and log files.
- Makefile Build System: A clean and efficient
Makefile
automates the entire compilation process. - Example Clients: Comes with three separate clients to demonstrate:
- Secure TCP (TLS) communication.
- Secure UDP (DTLS) communication.
- A basic (unencrypted) UDP broadcast.
The source code is organized into a clean and logical directory structure.
.
├── README.md
└── src
├── certs/
│ ├── cert.pem
│ └── key.pem
├── client/
│ ├── broadcast-client.c
│ ├── tcp-client.c
│ └── udp-client.c
├── common/
│ ├── logger.c
│ └── utils.c
├── include/
│ ├── broadcast-client.h
│ ├── logger.h
│ ├── protocol.h
│ ├── server.h
│ ├── tcp-client.h
│ └── udp-client.h
│ └── utils.h
├── Makefile
└── server/
└── server.c
Follow these instructions to get the project compiled and running on your system.
Before you begin, ensure you have the following installed on your Linux system:
-
GCC Compiler: Or any standard C compiler.
-
Make: To build the project using the Makefile.
-
OpenSSL Libraries: The development package is required.
# For Debian / Ubuntu sudo apt update sudo apt install build-essential libssl-dev
# For Arch Linux sudo pacman -Syu sudo pacman -S base-devel openssl
-
Clone the repository:
git clone "https://github.com/awmiriiw/c-socket.git" cd c-socket/src
-
Generate Self-Signed Certificates: The project requires SSL certificates to run. You can generate a self-signed pair for testing.
# Run this command from the src/ directory mkdir -p certs && openssl req -x509 -newkey rsa:4096 -nodes -keyout certs/key.pem -out certs/cert.pem -days 365
(You can skip the informational prompts by pressing Enter.)
-
Compile the Project: Run
make
to compile the server and all clients. The executables will be placed in thesrc/bin/
directory.make
-
Run the Server: Open a terminal and start the server. It will listen for connections on ports 8080 (TCP) and 8081 (UDP).
./bin/server
-
Run the Clients: Open new terminal windows for each client.
-
Secure TCP Client:
./bin/tcp-client
You can now type messages, and the server will echo them back.
-
Secure UDP Client:
./bin/udp-client
This client also functions as an echo client but over DTLS.
-
UDP Broadcast Client:
./bin/broadcast-client
(Note: The server does not handle this broadcast; it's a client-to-client example.)
-
-
Clean Up: To remove all compiled binaries, object files, and logs, run:
make clean
This project is open-source and available under the GNU AGPLv3 License.
Thanks for visiting! ☕