summaryrefslogtreecommitdiffstats
path: root/src/ctl.c
blob: f9473a26a18a3f627d303da5f57e25e1a128d608 (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
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

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

#include "ctl.h"
#include "marshal.h"
#include "log.h"
#include "compat/compat.h"

/**
 * Create a new listening Unix socket for control protocol.
 *
 * @param name The name of the Unix socket.
 * @return The socket when successful, -1 otherwise.
 */
int
ctl_create(const char *name)
{
	int s;
	struct sockaddr_un su;
	int rc;

	log_debug("control", "create control socket %s", name);

	if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) return -1;
	if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
		close(s);
		return -1;
	}
	su.sun_family = AF_UNIX;
	strlcpy(su.sun_path, name, sizeof(su.sun_path));
	if (bind(s, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
		rc = errno;
		close(s);
		errno = rc;
		return -1;
	}

	log_debug("control", "listen to control socket %s", name);
	if (listen(s, 5) == -1) {
		rc = errno;
		close(s);
		errno = rc;
		log_debug("control", "cannot listen to control socket %s", name);
		return -1;
	}
	return s;
}

/**
 * Connect to the control Unix socket.
 *
 * @param name The name of the Unix socket.
 * @return The socket when successful, -1 otherwise.
 */
int
ctl_connect(const char *name)
{
	int s;
	struct sockaddr_un su;
	int rc;

	log_debug("control", "connect to control socket %s", name);

	if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) return -1;
	su.sun_family = AF_UNIX;
	strlcpy(su.sun_path, name, sizeof(su.sun_path));
	if (connect(s, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
		rc = errno;
		log_warn("control", "unable to connect to socket %s", name);
		close(s);
		errno = rc;
		return -1;
	}
	return s;
}

/**
 * Remove the control Unix socket.
 *
 * @param name The name of the Unix socket.
 */
void
ctl_cleanup(const char *name)
{
	log_debug("control", "cleanup control socket");
	if (unlink(name) == -1) log_warn("control", "unable to unlink %s", name);
}

/**
 * Serialize and "send" a structure through the control protocol.
 *
 * This function does not really send the message but outputs it to a buffer.
 *
 * @param output_buffer A pointer to a buffer to which the message will be
 *                      appended. Can be @c NULL. In this case, the buffer will
 *                      be allocated.
 * @param[in,out] output_len The length of the provided buffer. Will be updated
 *                           with the new length
 * @param type  The type of message we want to send.
 * @param t     The structure to be serialized and sent.
 * @param mi    The appropriate marshal structure for serialization.
 * @return -1 in case of failure, 0 in case of success.
 *
 * Make sure this function logic matches the server-side one: @c levent_ctl_recv().
 */
int
ctl_msg_send_unserialized(uint8_t **output_buffer, size_t *output_len,
    enum hmsg_type type, void *t, struct marshal_info *mi)
{
	ssize_t len = 0, newlen;
	void *buffer = NULL;

	log_debug("control", "send a message through control socket");
	if (t) {
		len = marshal_serialize_(mi, t, &buffer, 0, NULL, 0);
		if (len <= 0) {
			log_warnx("control", "unable to serialize data");
			return -1;
		}
	}

	newlen = len + sizeof(struct hmsg_header);

	if (*output_buffer == NULL) {
		*output_len = 0;
		if ((*output_buffer = malloc(newlen)) == NULL) {
			log_warn("control", "no memory available");
			free(buffer);
			return -1;
		}
	} else {
		void *new = realloc(*output_buffer, *output_len + newlen);
		if (new == NULL) {
			log_warn("control", "no memory available");
			free(buffer);
			return -1;
		}
		*output_buffer = new;
	}

	struct hmsg_header hdr;
	memset(&hdr, 0, sizeof(struct hmsg_header));
	hdr.type = type;
	hdr.len = len;
	memcpy(*output_buffer + *output_len, &hdr, sizeof(struct hmsg_header));
	if (t)
		memcpy(*output_buffer + *output_len + sizeof(struct hmsg_header),
		    buffer, len);
	*output_len += newlen;
	free(buffer);
	return 0;
}

/**
 * "Receive" and unserialize a structure through the control protocol.
 *
 * Like @c ctl_msg_send_unserialized(), this function uses buffer to receive the
 * incoming message.
 *
 * @param[in,out] input_buffer The buffer with the incoming message. Will be
 *                             updated once the message has been unserialized to
 *                             point to the remaining of the message or will be
 *                             freed if all the buffer has been consumed. Can be
 *                             @c NULL.
 * @param[in,out] input_len    The length of the provided buffer. Will be updated
 *                             to the length of remaining data once the message
 *                             has been unserialized.
 * @param expected_type        The expected message type.
 * @param[out] t               Will contain a pointer to the unserialized structure.
 *                             Can be @c NULL if we don't want to store the
 *                             answer.
 * @param mi                   The appropriate marshal structure for unserialization.
 *
 * @return -1 in case of error, 0 in case of success and the number of bytes we
 *         request to complete unserialization.
 *
 * When requesting a notification, the input buffer is left untouched if we
 * don't get one and we fail silently.
 */
size_t
ctl_msg_recv_unserialized(uint8_t **input_buffer, size_t *input_len,
    enum hmsg_type expected_type, void **t, struct marshal_info *mi)
{
	struct hmsg_header hdr;
	int rc = -1;

	if (*input_buffer == NULL || *input_len < sizeof(struct hmsg_header)) {
		/* Not enough data. */
		return sizeof(struct hmsg_header) - *input_len;
	}

	log_debug("control", "receive a message through control socket");
	memcpy(&hdr, *input_buffer, sizeof(struct hmsg_header));
	if (hdr.len > HMSG_MAX_SIZE) {
		log_warnx("control", "message received is too large");
		/* We discard the whole buffer */
		free(*input_buffer);
		*input_buffer = NULL;
		*input_len = 0;
		return -1;
	}
	if (*input_len < sizeof(struct hmsg_header) + hdr.len) {
		/* Not enough data. */
		return sizeof(struct hmsg_header) + hdr.len - *input_len;
	}
	if (hdr.type != expected_type) {
		if (expected_type == NOTIFICATION) return -1;
		log_warnx("control",
		    "incorrect received message type (expected: %d, received: %d)",
		    expected_type, hdr.type);
		goto end;
	}

	if (t && !hdr.len) {
		log_warnx("control", "no payload available in answer");
		goto end;
	}
	if (t) {
		/* We have data to unserialize. */
		if (marshal_unserialize_(mi, *input_buffer + sizeof(struct hmsg_header),
			hdr.len, t, NULL, 0, 0) <= 0) {
			log_warnx("control", "unable to deserialize received data");
			goto end;
		}
	}

	rc = 0;
end:
	/* Discard input buffer */
	*input_len -= sizeof(struct hmsg_header) + hdr.len;
	if (*input_len == 0) {
		free(*input_buffer);
		*input_buffer = NULL;
	} else
		memmove(*input_buffer,
		    *input_buffer + sizeof(struct hmsg_header) + hdr.len, *input_len);
	return rc;
}