aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/msg.c
blob: 0de8a135969171e824acade20a25d6325d3a63b3 (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
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "msg.h"
#include "log.h"
#include "net.h"

#include <stdlib.h>
#include <string.h>

struct msg {
	size_t argc;
	const char **argv;
};

size_t msg_get_length(const struct msg *msg)
{
	return msg->argc;
}

const char **msg_get_strings(const struct msg *msg)
{
	return msg->argv;
}

const char *msg_get_first_string(const struct msg *msg)
{
	return msg->argv[0];
}

int msg_success(struct msg **msg)
{
	static const char *argv[] = {"success", NULL};
	return msg_from_argv(msg, argv);
}

int msg_error(struct msg **msg)
{
	static const char *argv[] = {"error", NULL};
	return msg_from_argv(msg, argv);
}

int msg_is_success(const struct msg *msg)
{
	return msg->argc == 1 && !strcmp(msg->argv[0], "success");
}

int msg_is_error(const struct msg *msg)
{
	return msg->argc == 1 && !strcmp(msg->argv[0], "error");
}

static int msg_copy_argv(struct msg *msg, const char **argv)
{
	size_t copied = 0;

	msg->argv = calloc(msg->argc + 1, sizeof(const char *));
	if (!msg->argv) {
		log_errno("calloc");
		return -1;
	}

	for (copied = 0; copied < msg->argc; ++copied) {
		msg->argv[copied] = strdup(argv[copied]);
		if (!msg->argv[copied]) {
			log_errno("strdup");
			goto free_copied;
		}
	}

	return 0;

free_copied:
	for (size_t i = 0; i < copied; ++i) {
		free((char *)msg->argv[i]);
	}

	free(msg->argv);

	return -1;
}

int msg_copy(struct msg **_dest, const struct msg *src)
{
	int ret = 0;

	struct msg *dest = malloc(sizeof(struct msg));
	if (!dest) {
		log_errno("malloc");
		return -1;
	}

	dest->argc = src->argc;

	ret = msg_copy_argv(dest, (const char **)src->argv);
	if (ret < 0)
		goto free;

	*_dest = dest;
	return 0;

free:
	free(dest);

	return -1;
}

void msg_free(struct msg *msg)
{
	for (size_t i = 0; i < msg->argc; ++i)
		free((char *)msg->argv[i]);
	free(msg->argv);
	free(msg);
}

int msg_from_argv(struct msg **_msg, const char **argv)
{
	int ret = 0;

	struct msg *msg = malloc(sizeof(struct msg));
	if (!msg) {
		log_errno("malloc");
		return -1;
	}

	msg->argc = 0;
	for (const char **s = argv; *s; ++s)
		++msg->argc;

	if (!msg->argc) {
		log_err("A message must contain at least one string\n");
		goto free;
	}

	ret = msg_copy_argv(msg, argv);
	if (ret < 0)
		goto free;

	*_msg = msg;
	return 0;

free:
	free(msg);

	return -1;
}

int msg_send(int fd, const struct msg *msg)
{
	struct buf *buf = NULL;
	int ret = 0;

	ret = buf_pack_strings(&buf, msg->argc, msg->argv);
	if (ret < 0)
		return ret;

	ret = net_send_buf(fd, buf);
	if (ret < 0)
		goto destroy_buf;

destroy_buf:
	buf_destroy(buf);

	return ret;
}

int msg_send_argv(int fd, const char **argv)
{
	struct msg *msg = NULL;
	int ret = 0;

	ret = msg_from_argv(&msg, argv);
	if (ret < 0)
		return ret;

	ret = msg_send(fd, msg);
	msg_free(msg);
	if (ret < 0)
		return ret;

	return ret;
}

int msg_recv(int fd, struct msg **_msg)
{
	struct buf *buf = NULL;
	int ret = 0;

	ret = net_recv_buf(fd, &buf);
	if (ret < 0)
		return ret;

	struct msg *msg = malloc(sizeof(struct msg));
	if (!msg) {
		log_errno("malloc");
		ret = -1;
		goto destroy_buf;
	}

	ret = buf_unpack_strings(buf, &msg->argc, &msg->argv);
	if (ret < 0)
		goto free_msg;

	if (!msg->argc) {
		log_err("A message must contain at least one string\n");
		goto free_msg;
	}

	*_msg = msg;
	goto destroy_buf;

free_msg:
	free(msg);

destroy_buf:
	buf_destroy(buf);

	return ret;
}

int msg_talk(int fd, const struct msg *request, struct msg **response)
{
	int ret = 0;

	if (!request && !response) {
		log_err("For communication, there must be at least a request and/or response\n");
		return -1;
	}

	if (request) {
		ret = msg_send(fd, request);
		if (ret < 0)
			return ret;
	}

	if (response) {
		ret = msg_recv(fd, response);
		if (ret < 0)
			return ret;
	}

	return ret;
}

int msg_talk_argv(int fd, const char **argv, struct msg **response)
{
	struct msg *request = NULL;
	int ret = 0;

	ret = msg_from_argv(&request, argv);
	if (ret < 0)
		return ret;

	ret = msg_talk(fd, request, response);
	msg_free(request);
	if (ret < 0)
		return ret;

	return ret;
}

int msg_connect_and_talk(const char *host, const char *port, const struct msg *request,
                         struct msg **response)
{
	int fd = -1, ret = 0;

	fd = net_connect(host, port);
	if (fd < 0)
		return fd;

	ret = msg_talk(fd, request, response);
	if (ret < 0)
		goto close;

close:
	net_close(fd);

	return ret;
}

int msg_connect_and_talk_argv(const char *host, const char *port, const char **argv,
                              struct msg **response)
{
	struct msg *request = NULL;
	int ret = 0;

	ret = msg_from_argv(&request, argv);
	if (ret < 0)
		return ret;

	ret = msg_connect_and_talk(host, port, request, response);
	msg_free(request);
	if (ret < 0)
		return ret;

	return ret;
}

void msg_dump(const struct msg *msg)
{
	log("Message[%zu]:\n", msg->argc);
	for (size_t i = 0; i < msg->argc; ++i)
		log("\t%s\n", msg->argv[i]);
}