-
Notifications
You must be signed in to change notification settings - Fork 1
/
gbn.h
102 lines (80 loc) · 3.35 KB
/
gbn.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
#ifndef _gbn_h
#define _gbn_h
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<signal.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<errno.h>
#include<netdb.h>
#include<time.h>
#include<assert.h>
/*----- Error variables -----*/
extern int h_errno;
extern int errno;
/*----- Protocol parameters -----*/
#define LOSS_PROB 1e-2 /* loss probability */
#define CORR_PROB 1e-3 /* corruption probability */
#define DATALEN 1024 /* length of the payload */
#define N 1024 /* Max number of packets a single call to gbn_send can process */
#define TIMEOUT 1 /* timeout to resend packets (1 second) */
/*----- Packet types -----*/
#define SYN 0 /* Opens a connection */
#define SYNACK 1 /* Acknowledgement of the SYN packet */
#define DATA 2 /* Data packets */
#define DATAACK 3 /* Acknowledgement of the DATA packet */
#define FIN 4 /* Ends a connection */
#define FINACK 5 /* Acknowledgement of the FIN packet */
#define RST 6 /* Reset packet used to reject new connections */
/*---- Actionable variables ----*/
#define MAX_ATTEMPTS 5
#define BACKLOG 1
#define TIMEOUT 1
/*----- Go-Back-n packet format -----*/
typedef struct {
uint8_t type; /* packet type (e.g. SYN, DATA, ACK, FIN) */
uint32_t seqnum; /* sequence number of the packet */
uint16_t checksum; /* header and payload checksum */
uint8_t data[DATALEN]; /* pointer to the payload */
uint16_t actual_len; /* actual data length in data field */
} __attribute__((packed)) gbnhdr;
typedef struct state_t{
int curr_state; /* ESTABLISHED, CLOSED, etc...*/
uint32_t seq_num; /* the current sequence number */
uint8_t window_size; /* the current window size which should be reset to 1 when congestion */
struct sockaddr address; /* the destination address */
socklen_t sock_len; /* the size of the destination address */
} state_t;
enum {
CLOSED=0,
SYN_SENT,
SYN_RCVD,
ESTABLISHED,
FIN_SENT,
FIN_RCVD
};
extern state_t s;
void gbn_init();
uint8_t validate_packet(gbnhdr *packet);
gbnhdr *create_rcv_pkt();
void create_DATA_packet(gbnhdr *DATA_packet, uint32_t pkt_seqnum, \
const void *buf_pointer, size_t data_len, int data_type);
void timeout_hdler(int);
int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen);
int gbn_listen(int sockfd, int backlog);
int gbn_bind(int sockfd, const struct sockaddr *server, socklen_t socklen);
int gbn_socket(int domain, int type, int protocol);
int gbn_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int gbn_close(int sockfd);
ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags);
ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags);
ssize_t maybe_recvfrom(int s, char *buf, size_t len, int flags, \
struct sockaddr *from, socklen_t *fromlen);
uint16_t checksum(uint16_t *buf, int nwords);
#define h_addr h_addr_list[0] /* for backward compatibility */
#endif