inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
protocols.h
Go to the documentation of this file.
1#ifndef PROTOCOLS_H
2#define PROTOCOLS_H
3
4#include <arpa/inet.h>
5#include <chrono>
6#include <cstdlib>
7#include <cstring>
8#include <filesystem>
9#include <fstream>
10#include <iomanip>
11#include <iostream>
12#include <openssl/err.h>
13#include <openssl/sha.h>
14#include <openssl/ssl.h>
15#include <random>
16#include <sstream>
17#include <string>
18#include <thread>
19#include <unistd.h>
20#include <unordered_map>
21
22/* Protocol status codes for FTP server */
23
24/* AUTH */
25#define AUTH_REQUEST "AUTH"
26#define STATUS_AUTH_FAILED 430
27#define STATUS_AUTH_SUCCESS 230
28
29/* FILES */
30#define LIST_REQUEST "LIST"
31#define GET_REQUEST "GET"
32#define STATUS_MISSING_FILE_NAME 400
33#define STATUS_LISTING_FILES 150
34#define STATUS_FILE_TRANSFER 160
35#define STATUS_TRANSFER_COMPLETE 226
36
37/* CHECKSUM */
38#define STATUS_CHECKSUM_VERIFY 151
39
40/* ERROR HANDLING */
41#define STATUS_FILE_NOT_FOUND 550
42#define STATUS_UNKNOWN_COMMAND 500
43
44/* TERMINATION */
45#define QUIT_REQUEST "QUIT"
46#define STATUS_GOODBYE 221
47
48#define PORT 12345
49#define BUFFER_SIZE 1024
50#define MAX_ATTEMPTS 3
51#define ATTEMPT_DELAY 2000 // in ms
52
53#define SERVER_CERT "server_cert.pem"
54#define SERVER_KEY "server_key.pem"
55
56/* Loopback addr just to test client and server on same system, ideally you should give the IP addr
57 * of the server */
58#define SERVER_IP "127.0.0.1"
59#define CA_CERT "server_cert.pem" // Certificate Authority certificate (for the client)
60
61const char* get_error_message(int status_code)
62{
63 switch (status_code)
64 {
66 return "--> 430: Too many failed attempts\n";
68 return "--> 230: Authentication successful!";
70 return "--> 550: File not found or invalid file path\n";
72 return "--> 500: Unknown command\n";
74 return "--> 400: Missing file name\n";
76 return "--> 150: Listing files:\n";
78 return "--> 226: Transfer complete\n";
79 case STATUS_GOODBYE:
80 return "--> 221: Goodbye\n";
82 return "--> 160: Starting File Transfer:\n";
84 return "--> 151: Verifying checksum..\n";
85 default:
86 return "--> 500: Unknown status code\n"; // Default error for unknown status codes
87 }
88}
89
90void send_protocol_message(SSL* ssl, int status_code)
91{
92 const char* error_msg = get_error_message(status_code);
93 SSL_write(ssl, error_msg, strlen(error_msg));
94}
95
96#endif
#define STATUS_FILE_NOT_FOUND
Definition protocols.h:41
#define STATUS_AUTH_SUCCESS
Definition protocols.h:27
void send_protocol_message(SSL *ssl, int status_code)
Definition protocols.h:90
#define STATUS_LISTING_FILES
Definition protocols.h:33
#define STATUS_FILE_TRANSFER
Definition protocols.h:34
#define STATUS_CHECKSUM_VERIFY
Definition protocols.h:38
#define STATUS_GOODBYE
Definition protocols.h:46
const char * get_error_message(int status_code)
Definition protocols.h:61
#define STATUS_MISSING_FILE_NAME
Definition protocols.h:32
#define STATUS_AUTH_FAILED
Definition protocols.h:26
#define STATUS_TRANSFER_COMPLETE
Definition protocols.h:35
#define STATUS_UNKNOWN_COMMAND
Definition protocols.h:42