A comprehensive client-server competitive programming platform built in C. The platform supports user authentication, problem solving, real time 1v1 duels, an Elo rating system, and a robust backend. The project heavily utilizes core Operating System concepts such as socket programming, multi threading, concurrency control, inter process communication, file locking, and secure sandboxing to execute user submitted code safely.
- Concurrency & Multithreading: Extensive use of Pthreads (One thread between server and each client, and one incase of duel between 2 users), Mutexes (
activeUserList,activeChallengesList), and Semaphores (job_queue) to handle multiple concurrent clients and restrict the maximum number of simultaneous execution jobs. - Inter-Process Communication (IPC): Usage of Anonymous Pipes (
pipe()) to redirect standard streams (STDERR,STDOUT) andfork()to separate the compilation and execution of arbitrary code from the main server process. - System Calls & Sandboxing: Implements
seccomp-bpfto filter system calls dynamically, ensuring malicious users cannot access or modify the server. Limits CPU time, memory space, and output file sizes usingsetrlimit. - Synchronization: Resolves race conditions using condition variables (
pthread_cond_t) for waking threads upon 1v1 duel state changes. Usesselect()for non-blocking I/O multiplexing when monitoring multiple duel participants simultaneously. - File System & Integrity: Deploys
fcntl()advisory locks (F_RDLCK,F_WRLCK,F_SETLKW) to manage concurrent reads and writes safely in a custom file based generic database.
The repository is modularly divided into three main components: Server, Client, and Shared Utilities.
server.c: The core backend application. It opens a TCP socket and delegates incoming connections to worker threads (handleClient()).- Security & Evaluation: In
submitProblem(), it forks child processes to rungccto compile the user's C code. Another process is forked to execute the compiled binary inside a strict sandbox defined by BPF filters. - Duels: Implements real-time 1v1 duels via
startDuel(). It employsselect()on both participants' sockets to avoid busy-waiting, reacting instantly when a user views a problem statement, submits code, or forfeits.
- Security & Evaluation: In
genericDatabase.c / .h: An generic file based database. It avoids raw reads/writes by establishingfcntl()advisory locks on database files to prevent corruption during concurrent accesses. It uses function pointers (MatchFunction) for searching records, and supports pagination, updating, inserting, and deleting records of arbitrary sizes, effectively making it a highly modular code.queryDatabase.c / .h: The concrete data access layer built overgenericDatabase.c. It managesusers.dat(user auth and roles),problems.dat(challenges), andleaderboard.dat(Elo ratings), Also provides the specificmathFunctionandcompareFunctionfor comparing and sorting records.
client.c: A command-line interface application that connects to the server via TCP sockets.- Menu Management: Displays distinct menus based on the server authenticated Role Based Access Control (Admin vs Regular User). Admins can add or delete problem data dynamically.
- Data Encoding: Transmits user commands and data structures securely inside JSON packets.
- Remote Execution Interface: Collects the absolute path to a user's local
.csolution file, efficiently streams it to the server using thesendFile()utility, and waits for the server's judgment (Accepted, Wrong Answer, Compilation Error, Runtime Error).
fileTransfer.c / .h: Implements chunked data transmission protocols over standard sockets. To prevent memory overflow during massive payload transfers (like large I/O.in/.outfiles),sendFile()andreceiveFile()first transmit the total byte size, followed by the content streamed iteratively in manageable 1024-byte chunks. Also handlesreadPipe()for reliable IPC stdout scraping.elo.c / .h: Calculates rating adjustments after duels. Uses the formal Elo mathematical formula taking akFactor,playerElo,opponentElo, andscoreto return expected gains/losses dynamically.seccomp-bpf.h: A third party (external) header file packed with BPF (Berkeley Packet Filter) macro definitions. It is critical for defining the secure execution wrapper policy used by the server's sandbox (whitelisting calls likeread,write,exit, and droppingexecveor unauthorized filesystem accesses).cJSON.c / .h&jsonUtility.c / .h: A lightweight third party JSON parser, utilized heavily by both Server and Client to serialize structured requests and responses.structs.h&codes.h: Standardizes shared entities likeUser,UserRecord,LeaderboardEntry, andProblem.codes.hdefines numeric constants (LOGIN_ATTEMPT = 1,SUBMIT_SOLUTION = 5) for protocol standardizing.
This project utilizes the following excellent open source resources:
- cJSON: A lightweight JSON parser used extensively for serializing and parsing communication packets between the client and server.
- seccomp-bpf.h: A highly useful standalone header file borrowed from the SUNET
pkcs11-proxyproject. It provides the essential BPF macro definitions required to construct the secure execution sandbox.
To compile the platform:
make clean
makeTo run the backend server:
./serverTo launch a client terminal:
./clientNote: The platform connects to INADDR_ANY by default when run locally. Use #define ONLINE_VM within client.c to bind to the Azure remote server configuration if needed (So that different devices across the world can connect).