-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
74 lines (68 loc) · 2.56 KB
/
common.h
File metadata and controls
74 lines (68 loc) · 2.56 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#define ACK 1
#define NACK 2
#define CORRUPT 3
#define NOT_CORRUPT 4
#define TIMEOUT 5
//the number of times the client will re-send a packet for which it hasn't received an ack
#define MAX_RETRY_COUNT 1000 //basically infinity, for the sake of this assignment
//TODO: get rid fo magic numbers and define maxes in terms of a single parameter, eg sizeof(struct Packet)
#define PKT_DATA_MAX_LEN 65535
#define RXTX_BUFFER_SIZE PKT_DATA_MAX_LEN + 64
#define PKT_HEADER_SIZE 21
#define U16_PRIME 65497
#define TRUE 1
#define FALSE 0
#define SERVER_PORT 5432
#define MAX_RX_LINE 256
#define MAX_LINE 80
#define DBG 1
typedef unsigned char byte;
//Let all U16's, etc, be represented by byte buffers of length mod 2; this makes it easy to htons/htonl, etc.
//Read the 4-byte buffers from left to right: so 3 == [0,0,0,0011]
struct Packet{
//Let zero represent ACK
byte ack;
//This packet's sequence number (may not be used)
byte seqnum[4];
//Length of the data in this packet
byte dataLen[4];
//the header checksum
byte hdrChecksum[4];
//purely for id purposes, eg with wireshark
byte name[4];
//checksum over packet data
byte dataChecksum[4];
//Bytes allocated for this packet's data buffer. Its easier to statically allocate the data for now, instead of managing a ptr to dynamic mem.
byte data[PKT_DATA_MAX_LEN];
};
void testByteConversion();
void serializePacket(struct Packet* pkt, byte buf[RXTX_BUFFER_SIZE]);
void deserializePacket(const char buf[RXTX_BUFFER_SIZE], struct Packet* pkt);
int isAck(struct Packet* pkt);
int isSequentialAck(struct Packet* pkt, int seqnum);
int getHeaderChecksum(struct Packet* pkt);
int getDataChecksum(struct Packet* pkt);
int isCorruptPacket(struct Packet* pkt);
int bytesToLint(const byte buf[4]);
void lintToBytes(const int i, byte obuf[4]);
void setDataChecksum(struct Packet* pkt);
void setSocketTimeout(int sockfd, int timeout_s, int timeout_us);
void printPacket(const struct Packet* pkt);
void printRawPacket(const struct Packet* pkt);
void makePacket(int seqnum, int ack, byte* data, struct Packet* pkt);
void SendFile(FILE* fptr, int sock, struct sockaddr_in* sin);
int SendData(int sock, struct sockaddr_in* addr, int seqnum, byte* data);
int awaitAck(int sock, struct sockaddr_in* addr, int seqnum, struct Packet* ackPkt);
void cleanPacket(struct Packet* pkt);
void sendPacket(struct Packet* pkt, int sock, struct sockaddr_in * sin);