forked from Joao-Tiago-Almeida/Name-Based-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol_support.c
147 lines (129 loc) · 3.64 KB
/
protocol_support.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
#include "protocol_support.h"
int Accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len)
{
int newfd;
if ((newfd = accept(socket, address, address_len)) == -1)
{
fprintf(stderr, "Error in Accept(): %s\n", strerror(errno));
Close(socket);
exit(1);
}
return newfd;
}
// sudo lsof -i :port - https://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac : 1. kill -15 <PID>, 2. kill -9 <PID>
void Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int n = bind(sockfd, addr, addrlen);
if (n == -1)
{
fprintf(stderr, "Error in Bind(): %s\n", strerror(errno));
Close(sockfd);
exit(1);
}
}
void Close(int fildes)
{
int n = close(fildes);
if (n == -1)
{
fprintf(stderr, "Error in Close(%d): %s\n", fildes, strerror(errno));
exit(1);
}
if (DEBUG)
printf("Closed(%d) successfully\n", fildes);
}
int Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int n = connect(sockfd, addr, addrlen);
if (n == -1)
if(DEBUG) fprintf(stderr, "Error in Connect(%d): %s\n", sockfd, strerror(errno));
return n;
}
void Getaddrinfo(const char *hostname, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
int errcode = getaddrinfo(hostname, servname, hints, res);
if (errcode != 0)
{
fprintf(stderr, "Error in Getaddrinfo(): %s\n", gai_strerror(errcode));
freeaddrinfo(*res);
exit(1);
}
}
int Listen(int sockfd)
{
int n = listen(sockfd, 5);
if (n == -1)
{
fprintf(stderr, "Error in Listen(%d): %s\n", sockfd, strerror(errno));
Close(sockfd);
exit(1);
}
return n;
}
ssize_t Read(int fd, char *buf)
{
char character[2] = {'\0'};
int n = 0, n_total = 0;
memset(buf, '\0', MESSAGE_SIZE);
do
{
n = read(fd, character, 1);
if (n == -1)
{
fprintf(stderr, "Error in Read(): %s\n", strerror(errno));
Close(fd);
exit(1);
}
n_total = n_total + n;
buf[n_total - 1] = character[0];
} while (character[0] != '\n' && n != 0);
if (DEBUG)
printf("You received from %d a %d:%d Bytes message: %s\n", fd, n_total, (int)strlen(buf), (char *)buf);
return n_total;
}
int Recvfrom(int socket, void *restrict buffer, size_t length, int flags,
struct sockaddr *restrict address, socklen_t *restrict address_len)
{
int n = recvfrom(socket, buffer, length, flags, address, address_len);
if (n == -1)
{
fprintf(stderr, "Error in Recvfrom(): %s\n", strerror(errno));
Close(socket);
exit(1);
}
return n;
}
int Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
int n = select(nfds, readfds, writefds, exceptfds, timeout);
if (n == -1)
{
fprintf(stderr, "Error in Select(%d): %s\n", n, strerror(errno));
// Close(nfds); TODO
exit(1);
}
return n;
}
int Socket(int domain, int type, int protocol)
{
int fd = socket(domain, type, protocol); //UDP socket
if (fd == -1)
{
fprintf(stderr, "Error in Socket(): %s\n", strerror(errno));
exit(1);
}
return fd;
}
ssize_t Write(int fd, const void *buf, size_t count)
{
int n = write(fd, buf, count);
if (n == -1)
{
fprintf(stderr, "Error in Write(%d): %s\n", fd, strerror(errno));
exit(1);
}
if (DEBUG)
printf("You wrote to %d a %d:%d Bytes message: %s\n", fd, n, (int)strlen(buf), (char *)buf);
return n;
}