aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/msg.c
blob: ba6c47403789a6a51890eeb9cef2b6650a92f261 (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
#include "msg.h"
#include "log.h"
#include "net.h"

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

int msg_from_argv(struct msg *msg, const char *argv[])
{
	int argc = 0;

	for (const char **s = argv; *s; ++s)
		++argc;

	msg->argc = argc;
	msg->argv = calloc(argc, sizeof(char *));

	if (!msg->argv) {
		print_errno("calloc");
		return -1;
	}

	for (int i = 0; i < argc; ++i) {
		msg->argv[i] = strdup(argv[i]);
		if (!msg->argv[i]) {
			print_errno("strdup");
			goto free;
		}
	}

	return 0;

free:
	for (int i = 0; i < argc; ++i)
		if (msg->argv[i])
			free(msg->argv[i]);
		else
			break;

	free(msg->argv);
	return -1;
}

static size_t calc_buf_len(int argc, char **argv)
{
	size_t len = 0;
	for (int i = 0; i < argc; ++i)
		len += strlen(argv[i]) + 1;
	return len;
}

static int calc_arr_len(const void *buf, size_t len)
{
	int argc = 0;
	for (const char *it = buf; it < (const char *)buf + len; it += strlen(it) + 1)
		++argc;
	return argc;
}

static void arr_pack(char *dest, int argc, char **argv)
{
	for (int i = 0; i < argc; ++i) {
		strcpy(dest, argv[i]);
		dest += strlen(argv[i]) + 1;
	}
}

static int arr_unpack(char **argv, int argc, const char *src)
{
	for (int i = 0; i < argc; ++i) {
		size_t len = strlen(src);

		argv[i] = malloc(len);
		if (!argv[i]) {
			print_errno("malloc");
			goto free;
		}

		strcpy(argv[i], src);
		src += len + 1;
	}

	return 0;

free:
	for (int i = 0; i < argc; ++i)
		if (argv[i])
			free(argv[i]);
		else
			break;

	return -1;
}

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

	size_t len = calc_buf_len(msg->argc, msg->argv);
	char *buf = malloc(len);
	if (!buf) {
		print_errno("malloc");
		return -1;
	}
	arr_pack(buf, msg->argc, msg->argv);

	ret = send_buf(fd, buf, len);
	if (ret < 0)
		goto free_buf;

free_buf:
	free(buf);

	return ret;
}

int msg_send_and_wait_for_result(int fd, const struct msg *msg, int *result)
{
	int ret = 0;

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

	ret = recv_static(fd, result, sizeof(*result));
	if (ret < 0)
		return ret;

	return ret;
}

int msg_recv(int fd, struct msg *msg)
{
	void *buf;
	size_t len;
	int ret = 0;

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

	msg->argc = calc_arr_len(buf, len);
	msg->argv = calloc(msg->argc, sizeof(char *));
	if (!msg->argv) {
		print_errno("calloc");
		ret = -1;
		goto free_buf;
	}

	ret = arr_unpack(msg->argv, msg->argc, buf);
	if (ret < 0)
		goto free_argv;

	goto free_buf;

free_argv:
	free(msg->argv);

free_buf:
	free(buf);

	return ret;
}

int msg_recv_and_send_result(int fd, msg_handler handler, void *arg)
{
	struct msg msg;
	int result;
	int ret = 0;

	ret = msg_recv(fd, &msg);
	if (ret < 0)
		return ret;

	result = handler(&msg, arg);

	ret = send_buf(fd, &result, sizeof(result));
	if (ret < 0)
		goto free_msg;

free_msg:
	msg_free(&msg);

	return ret;
}

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