aboutsummaryrefslogtreecommitdiff
path: root/networks/plaintext_buffered.c
blob: 2fc1e3a674b95b93088360897c60370586b63e71 (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
// 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>

#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;
#endif
	int fd;
	char valid;
	char close;
	char *buffer;
	size_t write_buffer_index;
	size_t buffer_len;
};

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

	size_t read_buffer_index = 0;
	mutex_lock(&(info->mutex));
	while (1) {
		size_t len;
		len = info->buffer_len;

		if (!info->valid)
			goto plaintext_buffered_send_thread_error_unlock;

#ifdef USE_FUTEX
		info->release_read = (len == 0);
#endif

		mutex_unlock(&(info->mutex));

#ifdef USE_FUTEX
		mutex_lock(&(info->release_read));
		if (len == 0)
			continue;
#endif

		if (read_buffer_index + len > PLAINTEXT_BUFFERED_LEN)
			len = PLAINTEXT_BUFFERED_LEN - read_buffer_index;

		ssize_t res;
		do {
			res = send(info->fd, &(info->buffer[read_buffer_index]), len, 0);
		} while (res == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK || errno == ETIMEDOUT));
		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;

		mutex_lock(&(info->mutex));

		info->buffer_len -= (size_t)res;
	}

	plaintext_buffered_send_thread_error_unlock:
	mutex_unlock(&(info->mutex));
	plaintext_buffered_send_thread_error:
	// TODO: Sane solution that works with ptread mutexes
	while (1) {
		mutex_lock(&(info->mutex));
		if (info->close) {
			close(info->fd);
			free(info->buffer);
			mutex_unlock(&(info->mutex));
			mutex_destroy(&(info->mutex));
			free(info);
			return 0;
		}
		mutex_unlock(&(info->mutex));
	}

	return 0;
}

int init_plaintext_buffered_network(void) {
	return 0;
}

int plaintext_buffered_send(void *handle, struct string msg) {
	struct plaintext_buffered_handle *plaintext_handle = handle;
	while (msg.len > 0) {
		size_t len = msg.len;
		if (len > PLAINTEXT_BUFFERED_LEN - plaintext_handle->write_buffer_index)
			len = PLAINTEXT_BUFFERED_LEN - plaintext_handle->write_buffer_index;
		mutex_lock(&(plaintext_handle->mutex));
		if (!plaintext_handle->valid)
			return 1;
		if (len > PLAINTEXT_BUFFERED_LEN - plaintext_handle->buffer_len)
			len = PLAINTEXT_BUFFERED_LEN - plaintext_handle->buffer_len;
		memcpy(&(plaintext_handle->buffer[plaintext_handle->write_buffer_index]), msg.data, len);
		plaintext_handle->write_buffer_index += len;
		if (plaintext_handle->write_buffer_index >= PLAINTEXT_BUFFERED_LEN)
			plaintext_handle->write_buffer_index = 0;
		plaintext_handle->buffer_len += len;
#ifdef USE_FUTEX
		mutex_unlock(&(plaintext_handle->release_read));
#endif
		mutex_unlock(&(plaintext_handle->mutex));
		msg.len -= 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->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;
#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));
	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 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 = 1;
	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;
#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));
	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));
#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));
#endif
	plaintext_handle->close = 1;
	mutex_unlock(&(plaintext_handle->mutex));
}