aboutsummaryrefslogtreecommitdiff
path: root/networks/plaintext_buffered.c
blob: 437c8fad61b678e37e33b36b1f16e8653cd24c3c (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// One of the code files for HaxServ
//
// Written by: Test_User <hax@andrewyu.org>
//
// This is free and unencumbered software released into the public
// domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <arpa/inet.h>
#include <errno.h>
#include <pthread.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef USE_FUTEX
#else
#include <semaphore.h>
#endif

#include "../config.h"
#include "../general_network.h"
#include "../haxstring.h"
#include "../main.h"
#include "../mutex.h"
#include "plaintext_buffered.h"

struct plaintext_buffered_handle {
	MUTEX_TYPE mutex;
#ifdef USE_FUTEX
	MUTEX_TYPE release_read;
	MUTEX_TYPE release_write;
#else
	sem_t release_read;
	sem_t release_write;
#endif
	int fd;
	char valid;
	char close;
	char *buffer;
	size_t write_buffer_index;
	size_t buffer_len;
};

int init_plaintext_buffered_network(void) {
	return 0;
}

void * plaintext_buffered_send_thread(void *handle) {
	struct plaintext_buffered_handle *info = handle;

	size_t read_buffer_index = 0;
	ssize_t res = 0;
	while (1) {
#ifdef USE_FUTEX
#else
		mutex_lock(&(info->mutex));
#endif

		size_t len;
#ifdef USE_FUTEX
		len = __sync_sub_and_fetch(&(info->buffer_len), res);
#else
		info->buffer_len -= (size_t)res;
		len = info->buffer_len;
#endif

#ifdef USE_FUTEX
		mutex_unlock(&(info->release_write));
#else
		sem_trywait(&(info->release_write));
		sem_post(&(info->release_write));
#endif

#ifdef USE_FUTEX
		if (!__sync_fetch_and_or(&(info->valid), 0)) { // TODO: Clean up mutexes in exit code too
			mutex_lock(&(info->mutex));
			goto plaintext_buffered_send_thread_error_unlock;
		}
#else
		if (!info->valid)
			goto plaintext_buffered_send_thread_error_unlock;
#endif

#ifdef USE_FUTEX
#else
		mutex_unlock(&(info->mutex));
#endif

#ifdef USE_FUTEX
		if (len == 0) {
			res = 0;
			mutex_lock(&(info->release_read));
			continue;
		}
#else
		if (len == 0) {
			res = 0;
			sem_wait(&(info->release_read));
			continue;
		}
#endif

		if (read_buffer_index + len > PLAINTEXT_BUFFERED_LEN)
			len = PLAINTEXT_BUFFERED_LEN - read_buffer_index;
		if (len > PLAINTEXT_BUFFERED_LEN/2 && PLAINTEXT_BUFFERED_LEN > 1)
			len = PLAINTEXT_BUFFERED_LEN/2;

		do {
			res = send(info->fd, &(info->buffer[read_buffer_index]), len, 0);
		} while (res == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
		if (res < 0)
			goto plaintext_buffered_send_thread_error;

		read_buffer_index += (size_t)res;
		if (read_buffer_index >= PLAINTEXT_BUFFERED_LEN)
			read_buffer_index = 0;
	}

	plaintext_buffered_send_thread_error:
	mutex_lock(&(info->mutex));
	plaintext_buffered_send_thread_error_unlock:
	info->valid = 0;
	mutex_unlock(&(info->mutex));
#ifdef USE_FUTEX
	mutex_unlock(&(info->release_write));
#else
	sem_trywait(&(info->release_write));
	sem_post(&(info->release_write));
#endif
	while (1) {
		mutex_lock(&(info->mutex));
		if (info->close) {
			close(info->fd);
			free(info->buffer);
			mutex_unlock(&(info->mutex));
			mutex_destroy(&(info->mutex));
#ifdef USE_FUTEX
#else
			sem_destroy(&(info->release_read));
			sem_destroy(&(info->release_write));
#endif
			free(info);
			return 0;
		} else {
#ifdef USE_FUTEX
			info->release_read = 1;
			mutex_unlock(&(info->mutex));
			mutex_lock(&(info->release_read));
			continue;
#else
			mutex_unlock(&(info->mutex));
			sem_wait(&(info->release_read));
			continue;
#endif
		}
		mutex_unlock(&(info->mutex));
	}

	return 0;
}

int plaintext_buffered_send(void *handle, struct string msg) {
	struct plaintext_buffered_handle *plaintext_handle = handle;
	while (msg.len > 0) {
#ifdef USE_FUTEX
#else
		mutex_lock(&(plaintext_handle->mutex));
#endif

#ifdef USE_FUTEX
		size_t len = PLAINTEXT_BUFFERED_LEN - __sync_fetch_and_or(&(plaintext_handle->buffer_len), 0); // There's no fetch-only for __sync
#else
		size_t len = PLAINTEXT_BUFFERED_LEN - plaintext_handle->buffer_len;
#endif

		if (len > msg.len)
			len = msg.len;

		if (len > PLAINTEXT_BUFFERED_LEN - plaintext_handle->write_buffer_index)
			len = PLAINTEXT_BUFFERED_LEN - plaintext_handle->write_buffer_index;

#ifdef USE_FUTEX
		if (!__sync_fetch_and_or(&(plaintext_handle->valid), 0)) {
			return 1;
		}
#else
		if (!plaintext_handle->valid) {
			mutex_unlock(&(plaintext_handle->mutex));
			return 1;
		}
#endif

#ifdef USE_FUTEX
		if (len == 0) {
			mutex_lock(&(plaintext_handle->release_write));
			continue;
		}
#else
		if (len == 0) {
			mutex_unlock(&(plaintext_handle->mutex));
			sem_wait(&(plaintext_handle->release_write));
			continue;
		}
#endif
		memcpy(&(plaintext_handle->buffer[plaintext_handle->write_buffer_index]), msg.data, len);

#ifdef USE_FUTEX
		__sync_fetch_and_add(&(plaintext_handle->buffer_len), len); // No __sync add-only either
#else
		plaintext_handle->buffer_len += len;
#endif

#ifdef USE_FUTEX
		mutex_unlock(&(plaintext_handle->release_read));
#else
		mutex_unlock(&(plaintext_handle->mutex));

		sem_trywait(&(plaintext_handle->release_read));
		sem_post(&(plaintext_handle->release_read));
#endif

		plaintext_handle->write_buffer_index += len;
		if (plaintext_handle->write_buffer_index >= PLAINTEXT_BUFFERED_LEN)
			plaintext_handle->write_buffer_index = 0;
		msg.len -= len;
		msg.data += len;
	}

	return 0;
}

size_t plaintext_buffered_recv(void *handle, char *data, size_t len, char *err) {
	struct plaintext_buffered_handle *plaintext_handle = handle;
	ssize_t res;
	do {
		res = recv(plaintext_handle->fd, data, len, 0);
	} while (res == -1 && (errno == EINTR));

	if (res == -1) {
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			*err = 1;
		} else {
			*err = 3;
		}
		return 0;
	} else if (res == 0) {
		*err = 2;
		return 0;
	}
	*err = 0;

	return (size_t)res;
}

int plaintext_buffered_connect(void **handle, struct string address, struct string port, struct string *addr_out) {
	struct sockaddr sockaddr;
	if (resolve(address, port, &sockaddr) != 0)
		return -1;

	int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (fd == -1)
		return -1;

	{
		struct timeval timeout = {
			.tv_sec = PING_INTERVAL,
			.tv_usec = 0,
		};

		setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
	}

	int res;
	do {
		res = connect(fd, &sockaddr, sizeof(sockaddr));
	} while (res < 0 && errno == EINTR);
	if (res < 0)
		goto plaintext_buffered_connect_close;

	struct plaintext_buffered_handle *plaintext_handle;
	plaintext_handle = malloc(sizeof(*plaintext_handle));
	if (!handle)
		goto plaintext_buffered_connect_close;
	*handle = plaintext_handle;
	plaintext_handle->valid = 1;
	plaintext_handle->close = 0;
	plaintext_handle->fd = fd;

	addr_out->data = malloc(sizeof(sockaddr));
	if (!addr_out->data)
		goto plaintext_buffered_connect_free_handle;
	memcpy(addr_out->data, &sockaddr, sizeof(sockaddr));
	addr_out->len = sizeof(sockaddr);

	plaintext_handle->buffer = malloc(PLAINTEXT_BUFFERED_LEN);
	if (!plaintext_handle->buffer)
		goto plaintext_buffered_connect_free_addr;
	plaintext_handle->write_buffer_index = 0;
	plaintext_handle->buffer_len = 0;

	if (mutex_init(&(plaintext_handle->mutex)) != 0)
		goto plaintext_buffered_connect_free_buffer;

#ifdef USE_FUTEX
	plaintext_handle->release_read = 0;
	plaintext_handle->release_write = 0;
#else
	sem_init(&(plaintext_handle->release_read), 0, 0);
	sem_init(&(plaintext_handle->release_write), 0, 0);
#endif

	pthread_t trash;
	if (pthread_create(&trash, &pthread_attr, plaintext_buffered_send_thread, plaintext_handle) != 0)
		goto plaintext_buffered_connect_destroy_mutex;

	return fd;

	plaintext_buffered_connect_destroy_mutex:
	mutex_destroy(&(plaintext_handle->mutex));
#ifdef USE_FUTEX
#else
	sem_destroy(&(plaintext_handle->release_read));
	sem_destroy(&(plaintext_handle->release_write));
#endif
	plaintext_buffered_connect_free_buffer:
	free(plaintext_handle->buffer);
	plaintext_buffered_connect_free_addr:
	free(addr_out->data);
	plaintext_buffered_connect_free_handle:
	free(plaintext_handle);
	plaintext_buffered_connect_close:
	close(fd);

	return -1;
}

int plaintext_buffered_accept(int listen_fd, void **handle, struct string *addr) {
	struct sockaddr address;
	socklen_t address_len = sizeof(address);

	int con_fd;
	do {
		con_fd = accept(listen_fd, &address, &address_len);
	} while (con_fd == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK || errno == ENETDOWN || errno == EPROTO || errno == ENOPROTOOPT || errno == EHOSTDOWN || errno == ENONET || errno == EHOSTUNREACH || errno == EOPNOTSUPP || errno == ENETUNREACH));

	if (con_fd == -1)
		return -1;

	{
		struct timeval timeout = {
			.tv_sec = PING_INTERVAL,
			.tv_usec = 0,
		};

		setsockopt(con_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
	}

	struct plaintext_buffered_handle *plaintext_handle = malloc(sizeof(*plaintext_handle));
	if (!plaintext_handle)
		goto plaintext_buffered_accept_close;
	*handle = plaintext_handle;
	plaintext_handle->valid = 1;
	plaintext_handle->close = 0;
	plaintext_handle->fd = con_fd;

	addr->data = malloc(address_len);
	if (addr->data == 0 && address_len != 0)
		goto plaintext_buffered_accept_free_handle;
	memcpy(addr->data, &address, address_len);
	addr->len = address_len;


	plaintext_handle->buffer = malloc(PLAINTEXT_BUFFERED_LEN);
	if (!plaintext_handle->buffer)
		goto plaintext_buffered_accept_free_addr;
	plaintext_handle->write_buffer_index = 0;
	plaintext_handle->buffer_len = 0;

	if (mutex_init(&(plaintext_handle->mutex)) != 0)
		goto plaintext_buffered_accept_free_buffer;

#ifdef USE_FUTEX
	plaintext_handle->release_read = 0;
	plaintext_handle->release_write = 0;
#else
	sem_init(&(plaintext_handle->release_read), 0, 0);
	sem_init(&(plaintext_handle->release_write), 0, 0);
#endif

	pthread_t trash;
	if (pthread_create(&trash, &pthread_attr, plaintext_buffered_send_thread, plaintext_handle) != 0)
		goto plaintext_buffered_accept_destroy_mutex;

	return con_fd;

	plaintext_buffered_accept_destroy_mutex:
	mutex_destroy(&(plaintext_handle->mutex));
#ifdef USE_FUTEX
#else
	sem_destroy(&(plaintext_handle->release_read));
	sem_destroy(&(plaintext_handle->release_write));
#endif
	plaintext_buffered_accept_free_buffer:
	free(plaintext_handle->buffer);
	plaintext_buffered_accept_free_addr:
	free(addr->data);
	plaintext_buffered_accept_free_handle:
	free(plaintext_handle);
	plaintext_buffered_accept_close:
	close(con_fd);

	return -1;
}

void plaintext_buffered_shutdown(void *handle) {
	struct plaintext_buffered_handle *plaintext_handle = handle;
	mutex_lock(&(plaintext_handle->mutex));
	plaintext_handle->valid = 0;
#ifdef USE_FUTEX
	mutex_unlock(&(plaintext_handle->release_read));
#else
	sem_trywait(&(plaintext_handle->release_read));
	sem_post(&(plaintext_handle->release_read));
#endif
	mutex_unlock(&(plaintext_handle->mutex));
	shutdown(plaintext_handle->fd, SHUT_RDWR);
}

void plaintext_buffered_close(int fd, void *handle) {
	struct plaintext_buffered_handle *plaintext_handle = handle;
	mutex_lock(&(plaintext_handle->mutex));
	plaintext_handle->valid = 0;
#ifdef USE_FUTEX
	mutex_unlock(&(plaintext_handle->release_read));
#else
	sem_trywait(&(plaintext_handle->release_read));
	sem_post(&(plaintext_handle->release_read));
#endif
	plaintext_handle->close = 1;
	mutex_unlock(&(plaintext_handle->mutex));
}