/
net.h
219 lines (189 loc) · 5.22 KB
/
net.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
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
212
213
214
215
216
217
218
219
// Virtual Choir Rehearsal Room Copyright (C) 2020 Lukas Ondracek <ondracek.lukas@gmail.com>, use under GNU GPLv3
// most of this file was taken from `man getaddrinfo`
#include <errno.h>
#ifdef __WIN32__
#include <winsock2.h>
#include <ws2tcpip.h>
#define close closesocket
#else
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
enum packetType {
PACKET_HELO,
PACKET_DATA,
PACKET_STATUS,
PACKET_KEY_PRESS,
PACKET_NOOP
};
struct packetClientHelo {
char type;
uint16_t version;
float aioLatency;
float dBAdj;
char name[100];
};
struct packetServerHelo {
char type;
uint8_t clientID;
bindex_t initBlockIndex;
char str[SHELO_STR_LEN]; // "keys\nhelp"
};
struct packetClientData {
char type;
uint8_t clientID;
bindex_t playBlockIndex; // server index to be played on the client side
bindex_t blockIndex;
sample_t block[MONO_BLOCK_SIZE];
};
struct packetServerData {
char type;
bindex_t blockIndex;
sample_t block[STEREO_BLOCK_SIZE];
};
struct packetStatusStr {
char type;
uint8_t packetIndex;
uint8_t packetsCnt;
bindex_t statusIndex;
char str[STATUS_LINES_PER_PACKET * (STATUS_WIDTH + 1)];
// (size of whole IP packet might be limited to 576 B; IP header is 20--60 B, UDP header is 8 B)
};
struct packetKeyPress {
char type;
uint8_t clientID;
bindex_t playBlockIndex;
bindex_t keyPressIndex;
int key;
};
struct packetClientNoop {
char type;
uint8_t clientID;
};
union packet {
struct packetClientHelo cHelo;
struct packetServerHelo sHelo;
struct packetClientData cData;
struct packetServerData sData;
struct packetStatusStr sStat;
struct packetKeyPress cKeyP;
};
bool netAddrsEqual(struct sockaddr_storage *addr1, struct sockaddr_storage *addr2) {
return memcmp(addr1, addr2, sizeof(struct sockaddr_storage)) == 0;
}
int netInit() {
#ifdef __WIN32__
{
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) {
return -1;
}
}
#endif
return 0;
}
void netCleanup() {
#ifdef __WIN32__
WSACleanup();
#endif
}
int netOpenPort(char *port) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, port, &hints, &result);
if (s != 0) {
printf("getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind(2).
If socket(2) (or bind(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == -1)
continue;
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
close(sfd);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
printf("Could not bind\n");
return -1;
}
return sfd;
}
int netOpenConn(char *addr, char *port) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
printf("Error while resolving the address: %s\n", gai_strerror(s));
return -1;
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sfd);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
printf("Error while creating socket: %s (%d)\n", strerror(errno), errno);
return -1;
}
{
uint32_t val;
socklen_t valsize = sizeof(val);
getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (char *)&val, &valsize);
if (val < CLIENT_SOCK_BUF_SIZE) {
val = CLIENT_SOCK_BUF_SIZE;
setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (char *)&val, sizeof(val));
}
valsize = sizeof(val);
getsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (char *)&val, &valsize);
if (val < CLIENT_SOCK_BUF_SIZE) {
val = CLIENT_SOCK_BUF_SIZE;
setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (char *)&val, sizeof(val));
}
}
#ifdef __WIN32__
DWORD tv = CONN_TIMEOUT_MSEC;
#else
struct timeval tv;
tv.tv_sec = CONN_TIMEOUT_MSEC / 1000;
tv.tv_usec = CONN_TIMEOUT_MSEC % 1000 * 1000;
#endif
if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv,sizeof(tv)) < 0) {
printf("Error while setting timeout of socket: %s (%d)\n", strerror(errno), errno);
}
return sfd;
}