aboutsummaryrefslogtreecommitdiffstats
path: root/src/queue.c
blob: b02d7dadfb910f96ce16e40e333b0081a56e1018 (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
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * 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 <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "data.h"
#include "db.h"

#define DROP 0
#define SET  1

/**
 * Queue
 */
struct queue
{
	uint32_t read, write, capacity;
	void *queue;
};
typedef struct queue queue_t;

/** the queue */
static queue_t queue;

static
bool
qread(
	void *data,
	uint32_t length
) {
	if (queue.read + length > queue.write)
		return false;

	memcpy(data, queue.queue + queue.read, length);
	queue.read += length;
	return true;
}

static
bool
qget_time(
	time_t *value
) {
	return qread(value, sizeof *value);
}

static
bool
qget_string(
	const char **text
) {
	char *p = queue.queue + queue.read;
	uint32_t length = (uint32_t)strlen(p);
	if (queue.read + length >= queue.write)
		return false;
	*text = p;
	queue.read += length + 1;
	return true;
}

static
bool
qwrite(
	const void *data,
	uint32_t length
) {
	uint32_t c;
	void *b;

	c = queue.capacity;
	while (c < queue.write + length)
		c += 4096;
	if (c != queue.capacity) {
		b = realloc(queue.queue, c);
		if (b == NULL)
			return false;
		queue.queue = b;
		queue.capacity = c;
	}
	memcpy(queue.queue + queue.write, data, length);
	queue.write += length;
	return true;
}

static
bool
qput_time(
	time_t value
) {
	return qwrite(&value, sizeof value);
}

static
bool
qput_string(
	const char *text
) {
	size_t len;
	text = text ?: "";
	/* check length */
	len = strnlen(text, MAX_NAME_LENGTH + 1);
	if (len > MAX_NAME_LENGTH)
		return false;
	return qwrite(text, 1 + (uint32_t)len);
}

int
queue_drop(
	const data_key_t *key
) {
	return qput_string(key->client)
		&& qput_string(key->session)
		&& qput_string(key->user)
		&& qput_string(key->permission)
		&& qput_string(0)
			? 0 : -(errno = ENOMEM);
}

int
queue_set(
	const data_key_t *key,
	const data_value_t *value
) {
	return qput_string(key->client)
		&& qput_string(key->session)
		&& qput_string(key->user)
		&& qput_string(key->permission)
		&& qput_string(value->value)
		&& qput_time(value->expire)
			? 0 : -(errno = ENOMEM);
}


void
queue_clear(
) {
	queue.write = 0;
}

int
queue_play(
) {
	int rc, rc2;
	data_key_t key;
	data_value_t value;

	rc = 0;
	queue.read = 0;
	while (queue.read < queue.write) {
		rc2 = -EINVAL;
		if (qget_string(&key.client)
		 && qget_string(&key.session)
		 && qget_string(&key.user)
		 && qget_string(&key.permission)
		 && qget_string(&value.value)) {
			if (!value.value[0])
				rc2 = db_drop(&key);
			else {
				if (qget_time(&value.expire))
					rc2 = db_set(&key, &value);
			}
		}
		if (rc2 != 0 && rc == 0)
			rc = rc2;
	}
	return rc;
}