summaryrefslogtreecommitdiffstats
path: root/src/utils.c
blob: e318fcb3d23bbd0a4d3a303f3396e0eb992dd349 (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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <libdect.h>
#include <utils.h>

#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif

void dect_hexdump(const char *prefix, const uint8_t *buf, size_t size)
{
	unsigned int i;

	for (i = 0; i < size; i++) {
		if (i % 16 == 0)
			dect_debug("%s%s: ", i ? "\n" : "", prefix);
		dect_debug("%.2x ", buf[i]);
	}
	dect_debug("\n\n");
}

void *dect_malloc(const struct dect_handle *dh, size_t size)
{
	return dh->ops->malloc(size);
}

void *dect_zalloc(const struct dect_handle *dh, size_t size)
{
	void *ptr;

	ptr = dect_malloc(dh, size);
	if (ptr != NULL)
		memset(ptr, 0, size);
	return ptr;
}

void dect_free(const struct dect_handle *dh, void *ptr)
{
	dh->ops->free(ptr);
}

struct dect_timer *dect_alloc_timer(const struct dect_handle *dh)
{
	return dect_malloc(dh, sizeof(struct dect_timer) +
			   dh->ops->event_ops->timer_priv_size);
}

void dect_start_timer(const struct dect_handle *dh,
		      struct dect_timer *timer, unsigned int timeout)
{
	struct timeval tv = {
		.tv_sec = timeout,
	};

	dh->ops->event_ops->start_timer(dh, timer, &tv);
}

void dect_stop_timer(const struct dect_handle *dh, struct dect_timer *timer)
{
	dh->ops->event_ops->stop_timer(dh, timer);
}

struct dect_fd *dect_alloc_fd(const struct dect_handle *dh)
{
	struct dect_fd *dfd;

	dfd = dect_malloc(dh, sizeof(struct dect_fd) +
			  dh->ops->event_ops->fd_priv_size);
	if (dfd == NULL)
		return NULL;
	dfd->fd = -1;
	return dfd;
}

int dect_register_fd(const struct dect_handle *dh, struct dect_fd *dfd,
		     uint32_t events)
{
	return dh->ops->event_ops->register_fd(dh, dfd, events);
}

void dect_unregister_fd(const struct dect_handle *dh, struct dect_fd *dfd)
{
	dh->ops->event_ops->unregister_fd(dh, dfd);
}

void dect_close(const struct dect_handle *dh, struct dect_fd *dfd)
{
	if (dfd->fd >= 0)
		close(dfd->fd);
	dect_free(dh, dfd);
}

struct dect_fd *dect_socket(const struct dect_handle *dh, int type, int protocol)
{
	struct dect_fd *dfd;

	dfd = dect_alloc_fd(dh);
	if (dfd == NULL)
		goto err1;

	dfd->fd = socket(AF_DECT, type | SOCK_NONBLOCK, protocol);
	if (dfd->fd < 0)
		goto err2;

	return dfd;

err2:
	dect_close(dh, dfd);
err1:
	return NULL;
}

struct dect_fd *dect_accept(const struct dect_handle *dh,
			    const struct dect_fd *dfd,
			    struct sockaddr *addr, socklen_t len)
{
	struct dect_fd *nfd;

	nfd = dect_alloc_fd(dh);
	if (nfd == NULL)
		goto err1;

	nfd->fd = accept(dfd->fd, addr, &len);
	if (nfd->fd < 0)
		goto err2;
	if (fcntl(nfd->fd, F_SETFL, O_NONBLOCK) < 0)
		goto err2;

	return nfd;

err2:
	dect_close(dh, nfd);
err1:
	return NULL;
}