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 pathmyserver.c
executable file
·211 lines (191 loc) · 6.98 KB
/
myserver.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @file myserver.c
* @author Benjamin Wang (bwang4@scu.edu, ID: 1179478)
* @brief Client using customized protocol on top of UDP protocol for sending
* information to the server. Server side implementation of UDP client-server.
* @version 0.1
* @date 2022-03-02
*
* @copyright Copyright (c) 2022
* @source: https://www.linuxhowtos.org/C_C++/socket.htm
* @source: https://www.geeksforgeeks.org/udp-server-client-implementation-c/
*
*/
#include "customProtocol.h"
/**
* @brief Verify the received client data-packet is within specification
*
* @param packet_seq_started flag for denoting whether the packet sequence has started
* @param packet_seq_num sequence number tracker of the packet
* @param curr_data current data packet
* @param prev_data previous data packet. Used for duplicate packet check, if at sequence number 0 this is not checked
* @return REJECT_SUB_CODE 0 if packet is valid, otherwise Reject sub-code
*/
uint16_t validate_client_data_packet(bool *packet_seq_started, uint8_t *packet_seq_num, data_packet_t *curr_data, data_packet_t *prev_data)
{
// if (*packet_seq_started && data_packet_equals(curr_data, prev_data))
if (*packet_seq_started && curr_data->segment_no == prev_data->segment_no)
{
printf("ERROR: Duplicate packet received!\n");
return REJECT_DUPLICATE_PACKET;
}
if (*packet_seq_started && curr_data->segment_no != (*packet_seq_num + 1))
{
#ifdef DEBUGGING
printf("Error: Expected sequence number %d\n", *packet_seq_num + 1);
#endif
printf("ERROR: Out of order packet!\n");
return REJECT_OUT_OF_SEQUENCE;
}
else if (!(*packet_seq_started) && curr_data->segment_no != 0)
{
#ifdef DEBUGGING
printf("Error: Expected sequence number %d\n", 0);
#endif
printf("ERROR: Out of order packet!\n");
return REJECT_OUT_OF_SEQUENCE;
}
if (strlen(curr_data->payload) != curr_data->length)
{
printf("ERROR: Length Mismatch!\n");
return REJECT_LENGTH_MISMATCH;
}
if (curr_data->end_packet != END_PACKET)
{
printf("ERROR: End of Packet Missing!\n");
return REJECT_END_OF_PACKET_MISSING;
}
return PACKET_OK;
}
/**
* @brief Main function (Driver code)
*
* @param argc number of arguments
* @param argv arguments
* @return int 0 if successful
*/
int main(int argc, char *argv[])
{
// Define variables
int sock, length, n, port;
socklen_t clientlen;
struct sockaddr_in server, client;
// Checking if port number is provided
if (argc == 1)
port = PORT;
else if (argc == 2)
port = atoi(argv[1]);
else
{
fprintf(stderr, "ERROR: no port provided\n");
exit(EXIT_FAILURE);
}
// Creating socket file descriptor
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
error("ERROR: Opening socket");
// Filling server information
length = sizeof(server);
bzero(&server, length); // memset(&servaddr, 0, length);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
// Bind the socket with the server address
if (bind(sock, (struct sockaddr *)&server, length) < 0)
error("ERROR: binding");
// Initializing client
clientlen = sizeof(client);
bzero(&client, clientlen);
// Custom protocol's Packets:
data_packet_t data_packet = {};
size_t data_packet_size = sizeof(data_packet);
ack_packet_t ack_packet = {};
size_t ack_packet_size = sizeof(ack_packet);
reject_packet_t reject_packet = {};
size_t reject_packet_size = sizeof(reject_packet);
data_packet_t prev_data_packet = {}; // For checking duplicates
// Packet Tracker:
bool packet_seq_started = false;
uint8_t packet_seq_num = 0;
uint16_t packet_status = 1; // 0 if OK, else REJECT sub code
// Server runs forever, I guess
while (1)
{
// Block until a packet is received
memset(&data_packet, 0, data_packet_size);
n = recvfrom(sock, &data_packet, data_packet_size, 0, (struct sockaddr *)&client, &clientlen);
if (n < 0)
error("ERROR: recvfrom");
printf("\nReceived packet!\n");
#ifdef DEBUGGING
char *data_str = data_packet_to_string(&data_packet);
printf("Received data packet: %s", data_str);
free(data_str);
#endif
// ACK or REJ logic here: All 4 Server Reject Sub-Code cases are handled here
packet_status = validate_client_data_packet(&packet_seq_started, &packet_seq_num, &data_packet, &prev_data_packet);
if (packet_status == PACKET_OK)
{
#ifdef DEBUGGING
printf("Packet OK!\n");
#endif
// Tracking the packet sequence: Since packet's valid
if (!packet_seq_started)
packet_seq_started = true;
else
packet_seq_num = data_packet.segment_no;
// Saving the current packet as next "prev_data_packet" for duplicate checking
memset(&prev_data_packet, 0, data_packet_size);
memcpy(&prev_data_packet, &data_packet, data_packet_size);
// ACK packet response:
reset_ack_packet(&ack_packet);
update_ack_packet(&ack_packet, data_packet.client_id, packet_seq_num);
if (!is_valid_ack_packet(&ack_packet))
error("ERROR: Invalid ACK packet");
#ifdef DEBUGGING
else
printf("ACK packet formatted properly!\n");
#endif
// Sending ACK response back to Client
n = sendto(sock, &ack_packet, ack_packet_size,
0, (const struct sockaddr *)&client, clientlen);
}
else
{
#ifdef DEBUGGING
printf("Packet Rejected with REJ sub code 0x%04X\n", packet_status);
#endif
// REJ packet response:
reset_reject_packet(&reject_packet);
update_reject_packet(&reject_packet, data_packet.client_id, packet_status, packet_seq_num);
if (!is_valid_reject_packet(&reject_packet))
error("ERROR: REJ packet formatted improperly!\n");
#ifdef DEBUGGING
else
printf("REJ packet formatted properly!\n");
char *rej_str = reject_packet_to_string(&reject_packet);
printf("Sending response REJ packet: %s\n", rej_str);
free(rej_str);
#endif
// Sending ACK response back to Client
n = sendto(sock, &reject_packet, reject_packet_size,
0, (const struct sockaddr *)&client, clientlen);
}
if (n < 0)
error("ERROR: sendto");
// Resetting the packet status after max of 5 packets
if (packet_seq_started && packet_seq_num >= (PACKET_GROUP_SIZE - 1))
{
#ifdef DEBUGGING
printf("SERVER RESETTING SEQ NUM AND SEQ STARTED\n");
#endif
packet_seq_num = 0;
packet_seq_started = false;
}
#ifdef DEBUGGING
printf("\n\n");
#endif
}
// Close the socket (Housekeeping)
close(sock);
return EXIT_SUCCESS;
}