summaryrefslogtreecommitdiffstats
path: root/pceplib/pcep_socket_comm_loop.c
blob: ed8037d074e10c87153ca3771f532d697d32642c (plain)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "pcep_socket_comm_internals.h"
#include "pcep_socket_comm_loop.h"
#include "pcep_utils_logging.h"
#include "pcep_utils_ordered_list.h"
#include "pcep_utils_logging.h"
#include "pcep_utils_memory.h"

void write_message(int socket_fd, const char *message, unsigned int msg_length);
unsigned int read_message(int socket_fd, char *received_message,
			  unsigned int max_message_size);
int build_fd_sets(pcep_socket_comm_handle *socket_comm_handle);
void handle_writes(pcep_socket_comm_handle *socket_comm_handle);
void handle_excepts(pcep_socket_comm_handle *socket_comm_handle);

bool comm_session_exists(pcep_socket_comm_handle *socket_comm_handle,
			 pcep_socket_comm_session *socket_comm_session)
{
	if (socket_comm_handle == NULL) {
		return false;
	}

	return (ordered_list_find(socket_comm_handle->session_list,
				  socket_comm_session)
		!= NULL);
}


bool comm_session_exists_locking(pcep_socket_comm_handle *socket_comm_handle,
				 pcep_socket_comm_session *socket_comm_session)
{
	if (socket_comm_handle == NULL) {
		return false;
	}

	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
	bool exists =
		comm_session_exists(socket_comm_handle, socket_comm_session);
	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	return exists;
}


void write_message(int socket_fd, const char *message, unsigned int msg_length)
{
	ssize_t bytes_sent = 0;
	unsigned int total_bytes_sent = 0;

	while ((uint32_t)bytes_sent < msg_length) {
		bytes_sent = write(socket_fd, message + total_bytes_sent,
				   msg_length);

		pcep_log(
			LOG_INFO,
			"%s: [%ld-%ld] socket_comm writing on socket fd [%d] msg_lenth [%u] bytes sent [%d]",
			__func__, time(NULL), pthread_self(), socket_fd,
			msg_length, bytes_sent);

		if (bytes_sent < 0) {
			if (errno != EAGAIN && errno != EWOULDBLOCK) {
				pcep_log(LOG_WARNING, "%s: send() failure",
					 __func__);

				return;
			}
		} else {
			total_bytes_sent += bytes_sent;
		}
	}
}


unsigned int read_message(int socket_fd, char *received_message,
			  unsigned int max_message_size)
{
	/* TODO what if bytes_read == max_message_size? there could be more to
	 * read */
	unsigned int bytes_read =
		read(socket_fd, received_message, max_message_size);
	pcep_log(
		LOG_INFO,
		"%s: [%ld-%ld] socket_comm read message bytes_read [%u] on socket fd [%d]",
		__func__, time(NULL), pthread_self(), bytes_read, socket_fd);

	return bytes_read;
}


int build_fd_sets(pcep_socket_comm_handle *socket_comm_handle)
{
	int max_fd = 0;

	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));

	FD_ZERO(&socket_comm_handle->except_master_set);
	FD_ZERO(&socket_comm_handle->read_master_set);
	ordered_list_node *node = socket_comm_handle->read_list->head;
	pcep_socket_comm_session *comm_session;
	while (node != NULL) {
		comm_session = (pcep_socket_comm_session *)node->data;
		if (comm_session->socket_fd > max_fd) {
			max_fd = comm_session->socket_fd;
		} else if (comm_session->socket_fd < 0) {
			pcep_log(LOG_ERR, "%s: Negative fd", __func__);
			assert(comm_session->socket_fd > 0);
		}

		/*pcep_log(LOG_DEBUG, ld] socket_comm::build_fdSets set
		   ready_toRead
		   [%d]", __func__, time(NULL), comm_session->socket_fd);*/
		FD_SET(comm_session->socket_fd,
		       &socket_comm_handle->read_master_set);
		FD_SET(comm_session->socket_fd,
		       &socket_comm_handle->except_master_set);
		node = node->next_node;
	}

	FD_ZERO(&socket_comm_handle->write_master_set);
	node = socket_comm_handle->write_list->head;
	while (node != NULL) {
		comm_session = (pcep_socket_comm_session *)node->data;
		if (comm_session->socket_fd > max_fd) {
			max_fd = comm_session->socket_fd;
		} else if (comm_session->socket_fd < 0) {
			pcep_log(LOG_ERR, "%s: Negative fd", __func__);
			assert(comm_session->socket_fd > 0);
		}

		/*pcep_log(LOG_DEBUG, "%s: [%ld] socket_comm::build_fdSets set
		   ready_toWrite [%d]", __func__, time(NULL),
		   comm_session->socket_fd);*/
		FD_SET(comm_session->socket_fd,
		       &socket_comm_handle->write_master_set);
		FD_SET(comm_session->socket_fd,
		       &socket_comm_handle->except_master_set);
		node = node->next_node;
	}

	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	return max_fd + 1;
}


void handle_reads(pcep_socket_comm_handle *socket_comm_handle)
{

	/*
	 * iterate all the socket_fd's in the read_list. it may be that not
	 * all of them have something to read. dont remove the socket_fd
	 * from the read_list since messages could come at any time.
	 */

	/* Notice: Only locking the mutex when accessing the read_list,
	 * since the read callbacks may end up calling back into the socket
	 * comm module to write messages which could be a deadlock. */
	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
	ordered_list_node *node = socket_comm_handle->read_list->head;
	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	while (node != NULL) {
		pcep_socket_comm_session *comm_session =
			(pcep_socket_comm_session *)node->data;

		pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
		node = node->next_node;
		if (!comm_session_exists(socket_comm_handle, comm_session)) {
			/* This comm_session has been deleted, move on to the
			 * next one */
			pthread_mutex_unlock(
				&(socket_comm_handle->socket_comm_mutex));
			continue;
		}

		int is_set = FD_ISSET(comm_session->socket_fd,
				      &(socket_comm_handle->read_master_set));
		/* Upon read failure, the comm_session might be free'd, so we
		 * cant store the received_bytes in the comm_session, until we
		 * know the read was successful. */
		int received_bytes = 0;
		pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

		if (is_set) {
			FD_CLR(comm_session->socket_fd,
			       &(socket_comm_handle->read_master_set));

			/* either read the message locally, or call the
			 * message_ready_handler to read it */
			if (comm_session->message_handler != NULL) {
				received_bytes = read_message(
					comm_session->socket_fd,
					comm_session->received_message,
					MAX_RECVD_MSG_SIZE);
				if (received_bytes > 0) {
					/* Send the received message to the
					 * handler */
					comm_session->received_bytes =
						received_bytes;
					comm_session->message_handler(
						comm_session->session_data,
						comm_session->received_message,
						comm_session->received_bytes);
				}
			} else {
				/* Tell the handler a message is ready to be
				 * read. The comm_session may be destroyed in
				 * this call, if
				 * there is an error reading or if the socket is
				 * closed. */
				received_bytes =
					comm_session
						->message_ready_to_read_handler(
							comm_session
								->session_data,
							comm_session
								->socket_fd);
			}

			/* handle the read results */
			if (received_bytes == 0) {
				if (comm_session_exists_locking(
					    socket_comm_handle, comm_session)) {
					comm_session->received_bytes = 0;
					/* the socket was closed */
					/* TODO should we define a socket except
					 * enum? or will the only time we call
					 * this is when the socket is closed??
					 */
					if (comm_session->conn_except_notifier
					    != NULL) {
						comm_session->conn_except_notifier(
							comm_session
								->session_data,
							comm_session
								->socket_fd);
					}

					/* stop reading from the socket if its
					 * closed */
					pthread_mutex_lock(
						&(socket_comm_handle
							  ->socket_comm_mutex));
					ordered_list_remove_first_node_equals(
						socket_comm_handle->read_list,
						comm_session);
					pthread_mutex_unlock(
						&(socket_comm_handle
							  ->socket_comm_mutex));
				}
			} else if (received_bytes < 0) {
				/* TODO should we call conn_except_notifier()
				 * here ? */
				pcep_log(
					LOG_WARNING,
					"%s: Error on socket fd [%d] : errno [%d][%s]",
					__func__, comm_session->socket_fd,
					errno, strerror(errno));
			} else {
				comm_session->received_bytes = received_bytes;
			}
		}
	}
}


void handle_writes(pcep_socket_comm_handle *socket_comm_handle)
{
	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));

	/*
	 * iterate all the socket_fd's in the write_list. it may be that not
	 * all of them are ready to be written to. only remove the socket_fd
	 * from the list if it is ready to be written to.
	 */

	ordered_list_node *node = socket_comm_handle->write_list->head;
	pcep_socket_comm_session *comm_session;
	bool msg_written;
	while (node != NULL) {
		comm_session = (pcep_socket_comm_session *)node->data;
		node = node->next_node;
		msg_written = false;

		if (!comm_session_exists(socket_comm_handle, comm_session)) {
			/* This comm_session has been deleted, move on to the
			 * next one */
			continue;
		}

		if (FD_ISSET(comm_session->socket_fd,
			     &(socket_comm_handle->write_master_set))) {
			/* only remove the entry from the list, if it is written
			 * to */
			ordered_list_remove_first_node_equals(
				socket_comm_handle->write_list, comm_session);
			FD_CLR(comm_session->socket_fd,
			       &(socket_comm_handle->write_master_set));

			/* dequeue all the comm_session messages and send them
			 */
			pcep_socket_comm_queued_message *queued_message =
				queue_dequeue(comm_session->message_queue);
			while (queued_message != NULL) {
				msg_written = true;
				write_message(comm_session->socket_fd,
					      queued_message->encoded_message,
					      queued_message->msg_length);
				if (queued_message->free_after_send) {
					pceplib_free(PCEPLIB_MESSAGES,
						     (void *)queued_message
							     ->encoded_message);
				}
				pceplib_free(PCEPLIB_MESSAGES, queued_message);
				queued_message = queue_dequeue(
					comm_session->message_queue);
			}
		}

		/* check if the socket should be closed after writing */
		if (comm_session->close_after_write == true) {
			if (comm_session->message_queue->num_entries == 0) {
				/* TODO check to make sure modifying the
				 * write_list while iterating it doesn't cause
				 * problems. */
				pcep_log(
					LOG_DEBUG,
					"%s: handle_writes close() socket fd [%d]",
					__func__, comm_session->socket_fd);
				ordered_list_remove_first_node_equals(
					socket_comm_handle->read_list,
					comm_session);
				ordered_list_remove_first_node_equals(
					socket_comm_handle->write_list,
					comm_session);
				close(comm_session->socket_fd);
				comm_session->socket_fd = -1;
			}
		}

		if (comm_session->message_sent_handler != NULL
		    && msg_written == true) {
			/* Unlocking to allow the message_sent_handler to
			 * make calls like destroy_socket_comm_session */
			pthread_mutex_unlock(
				&(socket_comm_handle->socket_comm_mutex));
			comm_session->message_sent_handler(
				comm_session->session_data,
				comm_session->socket_fd);
			pthread_mutex_lock(
				&(socket_comm_handle->socket_comm_mutex));
		}
	}

	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));
}


void handle_excepts(pcep_socket_comm_handle *socket_comm_handle)
{
	/* TODO finish this */
	(void)socket_comm_handle;
}


/* pcep_socket_comm::initialize_socket_comm_loop() will create a thread and
 * invoke this method */
void *socket_comm_loop(void *data)
{
	if (data == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: Cannot start socket_comm_loop with NULL pcep_socketcomm_handle",
			__func__);
		return NULL;
	}

	pcep_log(LOG_NOTICE, "%s: [%ld-%ld] Starting socket_comm_loop thread",
		 __func__, time(NULL), pthread_self());

	pcep_socket_comm_handle *socket_comm_handle =
		(pcep_socket_comm_handle *)data;
	struct timeval timer;
	int max_fd;

	while (socket_comm_handle->active) {
		/* check the FD's every 1/4 sec, 250 milliseconds */
		timer.tv_sec = 0;
		timer.tv_usec = 250000;
		max_fd = build_fd_sets(socket_comm_handle);

		if (select(max_fd, &(socket_comm_handle->read_master_set),
			   &(socket_comm_handle->write_master_set),
			   &(socket_comm_handle->except_master_set), &timer)
		    < 0) {
			/* TODO handle the error */
			pcep_log(
				LOG_WARNING,
				"%s: ERROR socket_comm_loop on select : errno [%d][%s]",
				__func__, errno, strerror(errno));
		}

		handle_reads(socket_comm_handle);
		handle_writes(socket_comm_handle);
		handle_excepts(socket_comm_handle);
	}

	pcep_log(LOG_NOTICE, "%s: [%ld-%ld] Finished socket_comm_loop thread",
		 __func__, time(NULL), pthread_self());

	return NULL;
}

int pceplib_external_socket_read(int fd, void *payload)
{
	pcep_socket_comm_handle *socket_comm_handle =
		(pcep_socket_comm_handle *)payload;
	if (socket_comm_handle == NULL) {
		return -1;
	}

	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
	FD_SET(fd, &(socket_comm_handle->read_master_set));
	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	handle_reads(socket_comm_handle);

	/* Get the socket_comm_session */
	pcep_socket_comm_session find_session = {.socket_fd = fd};
	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
	ordered_list_node *node =
		ordered_list_find(socket_comm_handle->read_list, &find_session);

	/* read again */
	if (node != NULL) {
		socket_comm_handle->socket_read_func(
			socket_comm_handle->external_infra_data,
			&((pcep_socket_comm_session *)node)
				 ->external_socket_data,
			fd, socket_comm_handle);
	}
	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	return 0;
}

int pceplib_external_socket_write(int fd, void *payload)
{
	pcep_socket_comm_handle *socket_comm_handle =
		(pcep_socket_comm_handle *)payload;
	if (socket_comm_handle == NULL) {
		return -1;
	}

	pthread_mutex_lock(&(socket_comm_handle->socket_comm_mutex));
	FD_SET(fd, &(socket_comm_handle->write_master_set));
	pthread_mutex_unlock(&(socket_comm_handle->socket_comm_mutex));

	handle_writes(socket_comm_handle);

	/* TODO do we need to cancel this FD from writing?? */

	return 0;
}