-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.c
214 lines (188 loc) · 5.45 KB
/
client.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
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
bool client_same(client* a, client* b){
struct sockaddr_in6* a6 = (struct sockaddr_in6*)&(a->peer);
struct sockaddr_in6* b6 = (struct sockaddr_in6*)&(b->peer);
struct sockaddr_in* a4 = (struct sockaddr_in*)&(a->peer);
struct sockaddr_in* b4 = (struct sockaddr_in*)&(b->peer);
if(a->peer.ss_family == b->peer.ss_family){
switch(a->peer.ss_family){
case AF_INET:
return memcmp(&a4->sin_addr, &b4->sin_addr, sizeof(a4->sin_addr)) == 0;
case AF_INET6:
return memcmp(&a6->sin6_addr, &b6->sin6_addr, sizeof(a6->sin6_addr)) == 0;
default:
//unknown protocol type, disconnect
return true;
}
}
return false;
}
void clients_add(fd_set* fds, int* maxfd){
size_t u;
for(u = 0; u < clients.length; u++){
if(clients.entries[u].fd >= 0){
FD_SET(clients.entries[u].fd, fds);
*maxfd = (clients.entries[u].fd > *maxfd) ? clients.entries[u].fd:*maxfd;
}
}
}
void clients_relimit(){
size_t u, c = 0;
for(u = 0; u < clients.length; u++){
c += clients.entries[u].submits;
clients.entries[u].submits = 0;
}
config.pixels += c;
}
int client_disconnect(client* disc){
client empty_client = {
.fd = -1
};
config.clients--;
close(disc->fd);
*disc = empty_client;
if(!config.quiet){
fprintf(stderr, "Disconnected client in slot %zi\n", disc - clients.entries);
}
return 0;
}
int client_accept(int listen_fd){
size_t u;
size_t position;
client empty_client = {
.fd = -1
};
for(position = 0; position < clients.length; position++){
if(clients.entries[position].fd < 0){
break;
}
}
if(position == clients.length){
clients.entries = realloc(clients.entries, (position + 1) * sizeof(client));
if(!clients.entries){
fprintf(stderr, "Failed to allocate memory");
//harsh but well
exit(EXIT_FAILURE);
}
clients.length = position + 1;
}
clients.entries[position] = empty_client;
clients.entries[position].fd = accept(listen_fd,(struct sockaddr*) &clients.entries[position].peer, &clients.entries[position].peer_len);
config.clients++;
if(clients.entries[position].fd < 0){
perror("accept");
return -1;
}
if(!config.quiet){
fprintf(stderr, "Client in slot %zu\n", position);
}
if(!config.unsafe){
//check for multiple connections
for(u = 0; u < clients.length; u++){
if(u != position && clients.entries[u].fd >= 0 && client_same(clients.entries + position, clients.entries + u)){
fprintf(stderr, "Disconnecting duplicate client\n");
client_disconnect(clients.entries + position);
return -1;
}
}
}
return 0;
}
int client_process(client* client, bool recv_data){
ssize_t bytes = 0;
char send_buffer[DATA_BUFFER_LEN];
unsigned pixel_x = 0, pixel_y = 0;
char pixel_raw[10] = "";
char* offset = NULL, *base = client->data;
XRenderColor pixel_color = {
.red = 0x0000,
.green = 0xFF00,
.blue = 0x00FF,
.alpha = 0xFFFF
};
if(recv_data && (sizeof(client->data) - client->data_offset) > 0){
//read data
bytes = recv(client->fd, client->data + client->data_offset, sizeof(client->data) - client->data_offset, 0);
//check for errors
if(bytes < 0){
perror("recv");
return client_disconnect(client);
}
//check if closed
else if(bytes == 0){
return client_disconnect(client);
}
client->data_offset += bytes;
}
while(memchr(base, '\n', client->data_offset)){
if(!strncmp(base, "PX ", 3)){
//check pixel limit
if(config.limit_handling != none && !config.unsafe && client->submits >= config.frame_limit){
if(config.limit_handling == ignore){
goto line_handled;
}
break;
}
//draw pixel
//cant use strtok here to separate arguments because the separator needs to be kept
offset = base + 3;
pixel_x = strtoul(offset, &offset, 0);
if(*offset == ' '){
pixel_y = strtoul(offset, &offset, 0);
if(*offset == ' '){
offset++;
bytes = strchr(offset, '\n') - offset;
if(bytes == 6 || bytes == 8){
//parse color
strncpy(pixel_raw, offset, bytes);
pixel_color = args_color(pixel_raw);
//draw pixel
XRenderFillRectangle(x11.display, PictOpOver, x11.canvas_handle, &pixel_color, min(pixel_x, config.width), min(pixel_y, config.height), 1, 1);
}
}
}
client->submits++;
}
else if(!strncmp(base, "SIZE", 4)){
//slight deviation from original protocol, terminate size response with \n
bytes = snprintf(send_buffer, sizeof(send_buffer), "SIZE %d %d\n", config.width, config.height);
//unsafe send, but hey
send(client->fd, send_buffer, bytes, 0);
}
line_handled:
//skip to next sentence
bytes = (char*)memchr(base, '\n', client->data_offset) + 1 - base;
client->data_offset -= bytes;
base += bytes;
}
memmove(client->data, base, client->data_offset);
//check if client hit the limits
if(sizeof(client->data) - client->data_offset < 2){
if(config.limit_handling == disconnect || client->submits < config.frame_limit){
fprintf(stderr, "Client %zu disconnected: Limit exceeded\n", client - clients.entries);
return client_disconnect(client);
}
}
return 0;
}
void clients_cleanup(){
size_t u;
for(u = 0; u < clients.length; u++){
if(clients.entries[u].fd >= 0){
close(clients.entries[u].fd);
clients.entries[u].fd = -1;
}
}
free(clients.entries);
clients.length = 0;
}
int clients_handle(fd_set* fds){
size_t u;
int rv = 0;
for(u = 0; u < clients.length; u++){
if(clients.entries[u].fd >= 0 && (FD_ISSET(clients.entries[u].fd, fds)
|| clients.entries[u].data_offset)){
rv += client_process(clients.entries + u, FD_ISSET(clients.entries[u].fd, fds));
}
}
return rv;
}