summaryrefslogtreecommitdiff
path: root/src/com-core.c
blob: ceec3a74e199771d47ddc8d6d90520332ea73c16 (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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <glib.h>

#include <dlog.h>

#include "dlist.h"
#include "secure_socket.h"
#include "debug.h"
#include "com-core.h"
#include "com-core_internal.h"
#include "util.h"

static struct {
    struct dlist *watch_list;
    struct dlist *conn_cb_list;
    struct dlist *disconn_cb_list;
    enum processing_event_callback {
	PROCESSING_NONE = 0x0,
	PROCESSING_DISCONNECTION = 0x01,
	PROCESSING_CONNECTION = 0x02,
    } processing_event_callback;
} s_info = {
    .watch_list = NULL,
    .conn_cb_list = NULL,
    .disconn_cb_list = NULL,
    .processing_event_callback = PROCESSING_NONE,
};

struct watch_item {
    int server_fd;
    int fd;
    guint id;
    void *cbdata;
};

struct cbdata {
    int (*service_cb)(int fd, void *data);
    void *data;
};

struct evtdata {
    int deleted;
    int (*evt_cb)(int fd, void *data);
    void *data;
};

static int watch_item_create(int server_fd, int handle, guint id, void *cbdata)
{
    struct watch_item *item;

    item = malloc(sizeof(*item));
    if (!item) {
	return -ENOMEM;
    }

    item->server_fd = server_fd;
    item->fd = handle;
    item->id = id;
    item->cbdata = cbdata;

    DbgPrint("Watch Item is created for %d/%d\n", server_fd, handle);
    s_info.watch_list = dlist_append(s_info.watch_list, item);
    return 0;
}

static int watch_item_destroy(int handle, int remove_id, int remove_cbdata)
{
    struct dlist *l;
    struct dlist *n;
    struct watch_item *item;

    dlist_foreach_safe(s_info.watch_list, l, n, item) {
	if (item->fd == handle) {
	    s_info.watch_list = dlist_remove(s_info.watch_list, l);

	    DbgPrint("Watch item is destroyed for %d/%d\n", item->server_fd, item->fd);

	    if (remove_id && item->id) {
		g_source_remove(item->id);
	    }

	    if (remove_cbdata && item->cbdata) {
		free(item->cbdata);
	    }

	    free(item);
	    return 0;
	}
    }

    DbgPrint("No entry found\n");
    return -ENOENT;
}

static void watch_item_destroy_all(int socket_fd)
{
    struct dlist *l;
    struct dlist *n;
    struct watch_item *item;

    dlist_foreach_safe(s_info.watch_list, l, n, item) {
	if (item->server_fd == socket_fd) {
	    DbgPrint("Watch item removed: %d/%d\n", item->server_fd, item->fd);
	    /*!
	     * \WARN
	     * If the watch_list item is removed from disconnected
	     * callback, this list loop can be broken.
	     * Please check it again.
	     */
	    invoke_disconn_cb_list(item->fd, 0, 0, 0);

	    s_info.watch_list = dlist_remove(s_info.watch_list, l);
	    if (item->id > 0) {
		g_source_remove(item->id);
	    }
	    free(item->cbdata);
	    free(item);
	}
    }
}

HAPI void invoke_con_cb_list(int server_fd, int handle, guint id, void *data, int watch)
{
    struct dlist *l;
    struct dlist *n;
    struct evtdata *cbdata;

    if (watch) {
	if (watch_item_create(server_fd, handle, id, data) < 0) {
	    ErrPrint("Failed to create a watch item\n");
	}
    }

    s_info.processing_event_callback |= PROCESSING_CONNECTION;
    dlist_foreach_safe(s_info.conn_cb_list, l, n, cbdata) {
	/*!
	 * \NOTE
	 * cbdata->deleted must has to be checked before call the function and
	 * return from the function call.
	 */
	if (cbdata->deleted || cbdata->evt_cb(handle, cbdata->data) < 0 || cbdata->deleted) {
	    s_info.conn_cb_list = dlist_remove(s_info.conn_cb_list, l);
	    free(cbdata);
	}
    }
    s_info.processing_event_callback &= ~PROCESSING_CONNECTION;
}

HAPI void invoke_disconn_cb_list(int handle, int remove_id, int remove_data, int watch)
{
    struct dlist *l;
    struct dlist *n;
    struct evtdata *cbdata;

    s_info.processing_event_callback |= PROCESSING_DISCONNECTION;
    dlist_foreach_safe(s_info.disconn_cb_list, l, n, cbdata) {
	/*!
	 * \NOTE
	 * cbdata->deleted must has to be checked before call the function and
	 * return from the function call.
	 */
	if (cbdata->deleted || cbdata->evt_cb(handle, cbdata->data) < 0 || cbdata->deleted) {
	    s_info.disconn_cb_list = dlist_remove(s_info.disconn_cb_list, l);
	    free(cbdata);
	}
    }
    s_info.processing_event_callback &= ~PROCESSING_DISCONNECTION;

    if (watch) {
	if (watch_item_destroy(handle, remove_id, remove_data) < 0) {
	    ErrPrint("Failed to destroy watch item\n");
	}
    }
}

static int validate_handle(int fd)
{
    int error;
    socklen_t len;

    len = sizeof(error);
    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
	ErrPrint("getsockopt: %s\n", strerror(errno));
	return 0;
    }

    return !(error == EBADF);
}

static gboolean client_cb(GIOChannel *src, GIOCondition cond, gpointer data)
{
    int client_fd;
    struct cbdata *cbdata = data;
    int ret;

    client_fd = g_io_channel_unix_get_fd(src);

    if (!(cond & G_IO_IN)) {
	DbgPrint("Client is disconencted\n");
	invoke_disconn_cb_list(client_fd, 0, 1, 1);
	secure_socket_destroy_handle(client_fd);
	return FALSE;
    }

    if ((cond & G_IO_ERR) || (cond & G_IO_HUP) || (cond & G_IO_NVAL)) {
	DbgPrint("Client connection is lost\n");
	invoke_disconn_cb_list(client_fd, 0, 1, 1);
	secure_socket_destroy_handle(client_fd);
	return FALSE;
    }

    ret = cbdata->service_cb(client_fd, cbdata->data);
    if (ret < 0) {
	DbgPrint("service callback returns %d < 0\n", ret);
	invoke_disconn_cb_list(client_fd, 0, 1, 1);
	secure_socket_destroy_handle(client_fd);
	return FALSE;
    }

    /* Check whether the socket FD is closed or not */
    if (!validate_handle(client_fd)) {
	invoke_disconn_cb_list(client_fd, 0, 1, 1);
	secure_socket_destroy_handle(client_fd);
	return FALSE;
    }

    return TRUE;
}

static gboolean accept_cb(GIOChannel *src, GIOCondition cond, gpointer cbdata)
{
    int socket_fd;
    int client_fd;
    GIOChannel *gio;
    guint id;

    socket_fd = g_io_channel_unix_get_fd(src);
    if (!(cond & G_IO_IN)) {
	ErrPrint("Accept socket closed\n");
	watch_item_destroy_all(socket_fd);
	secure_socket_destroy_handle(socket_fd);
	free(cbdata);
	return FALSE;
    }

    if ((cond & G_IO_ERR) || (cond & G_IO_HUP) || (cond & G_IO_NVAL)) {
	ErrPrint("Client connection is lost\n");
	watch_item_destroy_all(socket_fd);
	secure_socket_destroy_handle(socket_fd);
	free(cbdata);
	return FALSE;
    }

    client_fd = secure_socket_get_connection_handle(socket_fd);
    if (client_fd < 0) {
	/* Keep server running */
	return TRUE;
    }
    DbgPrint("New connectino arrived: server(%d), client(%d)\n", socket_fd, client_fd);

    if (fcntl(client_fd, F_SETFD, FD_CLOEXEC) < 0) {
	ErrPrint("Error: %s\n", strerror(errno));
    }

    if (fcntl(client_fd, F_SETFL, O_NONBLOCK) < 0) {
	ErrPrint("Error: %s\n", strerror(errno));
    }

    gio = g_io_channel_unix_new(client_fd);
    if (!gio) {
	ErrPrint("Failed to get gio\n");
	secure_socket_destroy_handle(client_fd);
	/* Keep server running */
	return TRUE;
    }

    g_io_channel_set_close_on_unref(gio, FALSE);

    id = g_io_add_watch(gio, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, (GIOFunc)client_cb, cbdata);
    if (id == 0) {
	GError *err = NULL;

	ErrPrint("Failed to add IO watch\n");
	g_io_channel_shutdown(gio, TRUE, &err);
	if (err) {
	    ErrPrint("Shutdown: %s\n", err->message);
	    g_error_free(err);
	}
	g_io_channel_unref(gio);
	secure_socket_destroy_handle(client_fd);
	/* Keep server running */
	return TRUE;
    }

    g_io_channel_unref(gio);

    invoke_con_cb_list(socket_fd, client_fd, id, NULL, 1);

    if (!validate_handle(socket_fd)) {
	watch_item_destroy_all(socket_fd);
	return FALSE;
    }

    /* Keep server running */
    return TRUE;
}

EAPI int com_core_server_create(const char *addr, int is_sync, int (*service_cb)(int fd, void *data), void *data)
{
    GIOChannel *gio;
    guint id;
    int fd;
    struct cbdata *cbdata;

    cbdata = malloc(sizeof(*cbdata));
    if (!cbdata) {
	ErrPrint("Heap: %s\n", strerror(errno));
	return -ENOMEM;
    }

    cbdata->service_cb = service_cb;
    cbdata->data = data;

    fd = secure_socket_create_server(addr);
    if (fd < 0) {
	free(cbdata);
	return fd;
    }

    if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
	ErrPrint("fcntl: %s\n", strerror(errno));
    }

    if (!is_sync && fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
	ErrPrint("fcntl: %s\n", strerror(errno));
    }

    DbgPrint("Create new IO channel for server FD: %d\n", fd);
    gio = g_io_channel_unix_new(fd);
    if (!gio) {
	ErrPrint("Failed to create new io channel\n");
	free(cbdata);
	secure_socket_destroy_handle(fd);
	return -EIO;
    }

    g_io_channel_set_close_on_unref(gio, FALSE);

    id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, (GIOFunc)accept_cb, cbdata);
    if (id == 0) {
	GError *err = NULL;
	ErrPrint("Failed to add IO watch\n");
	free(cbdata);
	g_io_channel_shutdown(gio, TRUE, &err);
	if (err) {
	    ErrPrint("Shutdown: %s\n", err->message);
	    g_error_free(err);
	}
	g_io_channel_unref(gio);
	secure_socket_destroy_handle(fd);
	return -EIO;
    }

    if (watch_item_create(fd, fd, id, cbdata) < 0) {
	GError *err = NULL;

	ErrPrint("Failed to create a watch item\n");
	g_source_remove(id);

	free(cbdata);
	g_io_channel_shutdown(gio, TRUE, &err);
	if (err) {
	    ErrPrint("Shutdown: %s\n", err->message);
	    g_error_free(err);
	}
	g_io_channel_unref(gio);
	secure_socket_destroy_handle(fd);
	return -ENOMEM;
    }

    g_io_channel_unref(gio);
    return fd;
}

EAPI int com_core_client_create(const char *addr, int is_sync, int (*service_cb)(int fd, void *data), void *data)
{
    GIOChannel *gio;
    guint id;
    int client_fd;
    struct cbdata *cbdata;

    cbdata = malloc(sizeof(*cbdata));
    if (!cbdata) {
	ErrPrint("Heap: %s\n", strerror(errno));
	return -ENOMEM;
    }

    cbdata->service_cb = service_cb;
    cbdata->data = data;

    client_fd = secure_socket_create_client(addr);
    if (client_fd < 0) {
	free(cbdata);
	return client_fd;
    }

    if (fcntl(client_fd, F_SETFD, FD_CLOEXEC) < 0) {
	ErrPrint("Error: %s\n", strerror(errno));
    }

    if (!is_sync && fcntl(client_fd, F_SETFL, O_NONBLOCK) < 0) {
	ErrPrint("Error: %s\n", strerror(errno));
    }

    gio = g_io_channel_unix_new(client_fd);
    if (!gio) {
	ErrPrint("Failed to create a new IO channel\n");
	free(cbdata);
	secure_socket_destroy_handle(client_fd);
	return -EIO;
    }

    g_io_channel_set_close_on_unref(gio, FALSE);

    id = g_io_add_watch(gio, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, (GIOFunc)client_cb, cbdata);
    if (id == 0) {
	GError *err = NULL;
	ErrPrint("Failed to add IO watch\n");
	free(cbdata);
	g_io_channel_shutdown(gio, TRUE, &err);
	if (err) {
	    ErrPrint("Shutdown: %s\n", err->message);
	    g_error_free(err);
	}
	g_io_channel_unref(gio);
	secure_socket_destroy_handle(client_fd);
	return -EIO;
    }

    g_io_channel_unref(gio);

    invoke_con_cb_list(client_fd, client_fd, id, cbdata, 1);
    return client_fd;
}

EAPI int com_core_add_event_callback(enum com_core_event_type type, int (*evt_cb)(int handle, void *data), void *data)
{
    struct evtdata *cbdata;
    cbdata = malloc(sizeof(*cbdata));
    if (!cbdata) {
	ErrPrint("Heap: %s\n", strerror(errno));
	return -ENOMEM;
    }

    cbdata->evt_cb = evt_cb;
    cbdata->data = data;
    cbdata->deleted = 0;

    if (type == CONNECTOR_CONNECTED) {
	s_info.conn_cb_list = dlist_append(s_info.conn_cb_list, cbdata);
    } else {
	s_info.disconn_cb_list = dlist_append(s_info.disconn_cb_list, cbdata);
    }
    return 0;
}

EAPI int com_core_recv_with_fd(int handle, char *buffer, int size, int *sender_pid, double timeout, int *fd)
{
    int readsize;
    int ret;
    int *recv_fd;

    fd_set set;

    recv_fd = fd;
    readsize = 0;
    while (size > 0) {
	FD_ZERO(&set);
	FD_SET(handle, &set);

	if (timeout > 0.0f) {
	    struct timeval tv;

	    tv.tv_sec = (unsigned long)timeout;
	    tv.tv_usec = (timeout - (unsigned long)timeout) * 1000000u;
	    ret = select(handle + 1, &set, NULL, NULL, &tv);
	} else if (timeout == 0.0f) {
	    ret = select(handle + 1, &set, NULL, NULL, NULL);
	} else {
	    ErrPrint("Invalid timeout: %lf (it must be greater than 0.0)\n", timeout);
	    return -EINVAL;
	}

	if (ret < 0) {
	    ret = -errno;
	    if (errno == EINTR) {
		DbgPrint("Select receives INTR\n");
		continue;
	    }
	    ErrPrint("Error: %s\n", strerror(errno));
	    return ret;
	} else if (ret == 0) {
	    ErrPrint("Timeout expired\n");
	    break;
	}

	if (!FD_ISSET(handle, &set)) {
	    ErrPrint("Unexpected handle is toggled\n");
	    return -EINVAL;
	}

	ret = secure_socket_recv_with_fd(handle, buffer + readsize, size, sender_pid, recv_fd);
	if (ret < 0) {
	    if (ret == -EAGAIN) {
		DbgPrint("Retry to get data (%d:%d)\n", readsize, size);
		continue;
	    }
	    DbgPrint("[%d] recv returns: %d\n", handle, ret);
	    return ret;
	} else if (ret == 0) {
	    DbgPrint("Disconnected(req.size: %d)\n", size);
	    return 0;
	}

	recv_fd = NULL; /** Get it only for the first time */
	size -= ret;
	readsize += ret;
    }

    return readsize;
}

EAPI int com_core_recv(int handle, char *buffer, int size, int *sender_pid, double timeout)
{
    return com_core_recv_with_fd(handle, buffer, size, sender_pid, timeout, NULL);
}

EAPI int com_core_send_with_fd(int handle, const char *buffer, int size, double timeout, int fd)
{
    int writesize;
    int ret;

    fd_set set;

    writesize = 0;
    while (size > 0) {

	FD_ZERO(&set);
	FD_SET(handle, &set);

	if (timeout > 0.0f) {
	    struct timeval tv;

	    tv.tv_sec = (unsigned long)timeout;
	    tv.tv_usec = (timeout - (unsigned long)timeout) * 1000000u;

	    ret = select(handle + 1, NULL, &set, NULL, &tv);
	} else if (timeout == 0.0f) {
	    ret = select(handle + 1, NULL, &set, NULL, NULL);
	} else {
	    ErrPrint("Invalid timeout: %lf (it must be greater than 0.0)\n", timeout);
	    return -EINVAL;
	}

	if (ret < 0) {
	    ret = -errno;
	    if (errno == EINTR) {
		DbgPrint("Select receives INTR\n");
		continue;
	    }
	    ErrPrint("Error: %s\n", strerror(errno));
	    return ret;
	} else if (ret == 0) {
	    ErrPrint("Timeout expired\n");
	    break;
	}

	if (!FD_ISSET(handle, &set)) {
	    ErrPrint("Unexpected handle is toggled\n");
	    return -EINVAL;
	}

	ret = secure_socket_send_with_fd(handle, buffer + writesize, size, fd);
	if (ret < 0) {
	    if (ret == -EAGAIN) {
		DbgPrint("Retry to send data (%d:%d)\n", writesize, size);
		continue;
	    }
	    DbgPrint("Failed to send: %d\n", ret);
	    return ret;
	} else if (ret == 0) {
	    DbgPrint("Disconnected? : Send bytes: 0\n");
	    return 0;
	}

	fd = -1; /** Send only once if it is fd */
	size -= ret;
	writesize += ret;
    }

    return writesize;
}

EAPI int com_core_send(int handle, const char *buffer, int size, double timeout)
{
    return com_core_send_with_fd(handle, buffer, size, timeout, -1);
}

EAPI void *com_core_del_event_callback(enum com_core_event_type type, int (*cb)(int handle, void *data), void *data)
{
    struct dlist *l;
    struct dlist *n;
    struct evtdata *cbdata;

    if (type == CONNECTOR_CONNECTED) {
	dlist_foreach_safe(s_info.conn_cb_list, l, n, cbdata) {
	    if (cbdata->evt_cb == cb && cbdata->data == data) {
		void *data;
		data = cbdata->data;

		if ((s_info.processing_event_callback & PROCESSING_CONNECTION) == PROCESSING_CONNECTION) {
		    cbdata->deleted = 1;
		} else {
		    dlist_remove_data(s_info.conn_cb_list, cbdata);
		    free(cbdata);
		}

		return data;
	    }
	}
    } else {
	dlist_foreach_safe(s_info.disconn_cb_list, l, n, cbdata) {
	    if (cbdata->evt_cb == cb && cbdata->data == data) {
		void *data;
		data = cbdata->data;

		if ((s_info.processing_event_callback & PROCESSING_DISCONNECTION) == PROCESSING_DISCONNECTION) {
		    cbdata->deleted = 1;
		} else {
		    dlist_remove_data(s_info.disconn_cb_list, cbdata);
		    free(cbdata);
		}
		return data;
	    }
	}
    }

    return NULL;
}

EAPI int com_core_server_destroy(int handle)
{
    DbgPrint("Close server handle[%d]\n", handle);
    invoke_disconn_cb_list(handle, 1, 1, 1);
    secure_socket_destroy_handle(handle);
    return 0;
}

EAPI int com_core_client_destroy(int handle)
{
    DbgPrint("Close client handle[%d]\n", handle);
    invoke_disconn_cb_list(handle, 1, 1, 1);
    secure_socket_destroy_handle(handle);
    return 0;
}

/* End of a file */