This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomProtocol.h
executable file
·157 lines (139 loc) · 4.48 KB
/
customProtocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @file customProtocol.h
* @author Benjamin Wang (bwang4@scu.edu, ID: 1179478)
* @brief Client using customized protocol on top of UDP protocol for sending
* information to the server. Header file defining the CUSTOM PROTOCOL
* @date 2022-03-02
*
* @copyright Copyright (c) 2022
*
*/
#ifndef CUSTOMPROTOCOL_H /* include guard */
#define CUSTOMPROTOCOL_H
// Debug flag
// #define DEBUGGING 0
// library includes
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
// Port and Hostname:
#define PORT 8080
#define HOSTNAME "localhost"
// Custom Protocol Meta-Settings
#define PACKET_GROUP_SIZE 5
#define ACK_TIMER_WAIT_TIME_MS 3000
#define ACK_TIMER_RETRY_COUNT 3
#define MAXLINE 1024
#define LINE_LENGTH 256
#define DEFAULT_VALUE 0
#define PACKET_OK 0
// Error Handling Testing:
#define ERROR_SPECIAL_CLIENT 123
#define ERR_OOS_MSG "Case 1: Out of Sequence segment number"
#define ERR_LEN_MSG "Case 2: Length Mismatch"
#define ERR_END_MSG "Case 3: End of Packet Missing"
#define ERR_DUP_MSG "Case 4: Duplicate Packet check done by comparing sequence number as specified in instruction"
// Custom Protocol Primitives
#define START_PACKET 0xFFFF
#define END_PACKET 0xFFFF
#define MAX_CLIENT_ID 0XFF
#define MAX_PACKET_SIZE 0xFF
// Custom Protocol Packet Types
typedef enum
{
PACKET_DATA = 0xFFF1,
PACKET_ACK,
PACKET_REJECT
} PACKET_TYPE;
// Custom Protocol Reject Sub-Codes
typedef enum
{
REJECT_OUT_OF_SEQUENCE = 0xFFF4,
REJECT_LENGTH_MISMATCH,
REJECT_END_OF_PACKET_MISSING,
REJECT_DUPLICATE_PACKET
} REJECT_SUB_CODE;
// Custom Protocol Packet Structure Sizes
#define PACKET_DATA_PAYLOAD_SIZE 255
// Custom Protocol Packet Structures:
typedef struct
{
uint16_t start_packet;
uint8_t client_id;
PACKET_TYPE packet_type;
uint8_t segment_no;
uint8_t length;
char payload[PACKET_DATA_PAYLOAD_SIZE];
uint16_t end_packet;
} __attribute__((packed)) data_packet_t; // Size 266
typedef struct
{
uint16_t start_packet;
uint8_t client_id;
PACKET_TYPE packet_type;
uint8_t received_segment_no;
uint16_t end_packet;
} __attribute__((packed)) ack_packet_t; // Size 8
typedef struct
{
uint16_t start_packet;
uint8_t client_id;
PACKET_TYPE packet_type;
REJECT_SUB_CODE sub_code;
uint8_t received_segment_no;
uint16_t end_packet;
} __attribute__((packed)) reject_packet_t; // Size 10
/**
* @brief Error function
*
* @param msg Error message
*/
void error(const char *msg);
// Validating that packet is correct
bool is_valid_data_packet(data_packet_t *packet);
bool is_valid_ack_packet(ack_packet_t *packet);
bool is_valid_reject_packet(reject_packet_t *packet);
// Packet reset to default values. Clear payload array if it exists.
void reset_data_packet(data_packet_t *packet);
void reset_ack_packet(ack_packet_t *packet);
void reset_reject_packet(reject_packet_t *packet);
// Packet setters. Note: Should call reset() before setting values.
void update_data_packet(data_packet_t *packet, uint8_t client_id, uint8_t segment_no, uint8_t length, char *payload);
void update_ack_packet(ack_packet_t *packet, uint8_t client_id, uint8_t received_segment_no);
void update_reject_packet(reject_packet_t *packet, uint8_t client_id, REJECT_SUB_CODE sub_code, uint8_t received_segment_no);
// Data Packet Comparison Equal method: For checking duplicate packets
bool data_packet_equals(data_packet_t *packet1, data_packet_t *packet2);
// ToString Sizes, gathered from compiler warnings:
#define DATA_PACKET_STRING_SIZE 355
#define ACK_PACKET_STRING_SIZE 88
#define REJECT_PACKET_STRING_SIZE 103
/**
* @brief Convert DATA packet to string. NOTE: MUST BE FOLLOWED BY A FREE() CALL TO PREVENT MEMORY LEAKS
*
* @param packet data_packet_t packet to convert
* @return char* string representation of packet
*/
char *data_packet_to_string(data_packet_t *packet);
/**
* @brief Convert ACK packet to string. NOTE: MUST BE FOLLOWED BY A FREE() CALL TO PREVENT MEMORY LEAKS
*
* @param packet ack_packet_t packet to convert
* @return char* string representation of packet
*/
char *ack_packet_to_string(ack_packet_t *packet);
/**
* @brief Convert REJECT packet to string. NOTE: MUST BE FOLLOWED BY A FREE() CALL TO PREVENT MEMORY LEAKS
*
* @param packet reject_packet_t packet to convert
* @return char* string representation of packet
*/
char *reject_packet_to_string(reject_packet_t *packet);
#endif