Repository navigation
Expand file tree
/
Copy pathksocket.h
More file actions
92 lines (77 loc) · 2.76 KB
/
Copy pathksocket.h
File metadata and controls
92 lines (77 loc) · 2.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef KSOCKET_H
#define KSOCKET_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <stdbool.h>
#include <time.h>
/* retransmission timeout (seconds) and simulated drop probability */
#define T 5
#define DROP_PROB 0.05f
#define SOCK_KTP 100
#define MAX_KTP_SOCKETS 25
#define MSG_SIZE 512
#define MAX_MESSAGES 10
/* custom errno values for KTP-specific failures */
#define ENOSPACE 1001
#define ENOTBOUND 1002
#define ENOMESSAGE 1003
/* 3-byte header prepended to every packet — data or ACK */
typedef struct
{
uint8_t seq_num; /* sequence number, wraps at 254 */
uint8_t is_ack; /* 1 = ACK, 0 = data */
uint8_t rwnd_size; /* receiver window size piggybacked on ACKs */
} KTP_header;
/* sender window — tracks what's been sent but not yet acknowledged */
typedef struct
{
int window_size;
uint8_t seq_num[MAX_MESSAGES]; /* seq nums of in-flight messages */
time_t send_time[MAX_MESSAGES]; /* when each was last sent, for timeout */
uint8_t next_seq_to_send;
} swnd_struct;
/* receiver window — tracks what's been received and what's still expected */
typedef struct
{
int window_size;
uint8_t expected_seq_num; /* next in-order seq we're waiting for */
uint8_t seq_num[MAX_MESSAGES]; /* seq nums of buffered messages */
uint8_t read_seq_num; /* next seq the app will read */
uint8_t last_acked_seq;
} rwnd_struct;
/* one slot in shared memory — everything about a single KTP socket */
typedef struct
{
bool is_free;
pid_t pid; /* owner process, used by garbage collector */
int udp_socket; /* the real UDP fd underneath */
int ktp_id; /* index in SM[], returned to user as sockfd */
/* flags to request socket ops from Thread R in the daemon */
int needs_init;
int domain;
int protocol;
int needs_bind;
int bind_result;
int needs_close;
struct sockaddr_in src_addr;
struct sockaddr_in dest_addr;
char send_buffer[MAX_MESSAGES * MSG_SIZE];
char recv_buffer[MAX_MESSAGES * MSG_SIZE];
swnd_struct swnd;
rwnd_struct rwnd;
int nospace; /* set when recv buffer is full; triggers dup-ACK on recovery */
} KTP_sockinfo;
int k_socket(int domain, int type, int protocol);
int k_bind(int sockfd, const struct sockaddr *src_addr, socklen_t src_addrlen, const struct sockaddr *dest_addr, socklen_t dst_addrlen);
ssize_t k_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t dest_addrlen);
ssize_t k_recvfrom(int sockfd, void *buff, size_t len, int flags, struct sockaddr *src_addr, socklen_t *src_addrlen);
int k_close(int sockfd);
int dropMessage(float p);
#endif