~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/channels/iax2/firmware.c -rw-r--r-- 8.0 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 10 months ago
                                                                                
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief IAX Firmware Support
 *
 * \author Mark Spencer <markster@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/mman.h>
#include <arpa/inet.h>

#include "asterisk/linkedlists.h"
#include "asterisk/md5.h"
#include "asterisk/paths.h"
#include "asterisk/utils.h"

#include "include/firmware.h"

#define IAX_FIRMWARE_SUBDIR "/firmware/iax"

struct iax_firmware {
	AST_LIST_ENTRY(iax_firmware) list;
	int fd;
	int mmaplen;
	int dead;
	struct ast_iax2_firmware_header *fwh;
	unsigned char *buf;
};

static AST_LIST_HEAD_STATIC(firmwares, iax_firmware);

static int try_firmware(char *s)
{
	struct stat stbuf;
	struct iax_firmware *cur = NULL;
	int ifd, fd, res, len, chunk;
	struct ast_iax2_firmware_header *fwh, fwh2;
	struct MD5Context md5;
	unsigned char sum[16], buf[1024];
	char *s2, *last;

	s2 = ast_alloca(strlen(s) + 100);

	last = strrchr(s, '/');
	if (last)
		last++;
	else
		last = s;

	snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, ast_random());

	if (stat(s, &stbuf) < 0) {
		ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
		return -1;
	}

	/* Make sure it's not a directory */
	if (S_ISDIR(stbuf.st_mode))
		return -1;
	ifd = open(s, O_RDONLY);
	if (ifd < 0) {
		ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
		return -1;
	}
	fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
	if (fd < 0) {
		ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
		close(ifd);
		return -1;
	}
	/* Unlink our newly created file */
	unlink(s2);

	/* Now copy the firmware into it */
	len = stbuf.st_size;
	while(len) {
		chunk = len;
		if (chunk > sizeof(buf))
			chunk = sizeof(buf);
		res = read(ifd, buf, chunk);
		if (res != chunk) {
			ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
			close(ifd);
			close(fd);
			return -1;
		}
		res = write(fd, buf, chunk);
		if (res != chunk) {
			ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
			close(ifd);
			close(fd);
			return -1;
		}
		len -= chunk;
	}
	close(ifd);
	/* Return to the beginning */
	lseek(fd, 0, SEEK_SET);
	if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
		ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
		close(fd);
		return -1;
	}
	if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
		ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
		close(fd);
		return -1;
	}
	if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
		ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
		close(fd);
		return -1;
	}
	if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
		ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
		close(fd);
		return -1;
	}
	fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (fwh == MAP_FAILED) {
		ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
		close(fd);
		return -1;
	}
	MD5Init(&md5);
	MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
	MD5Final(sum, &md5);
	if (memcmp(sum, fwh->chksum, sizeof(sum))) {
		ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
		munmap((void*)fwh, stbuf.st_size);
		close(fd);
		return -1;
	}

	AST_LIST_TRAVERSE(&firmwares, cur, list) {
		if (!strcmp((const char *) cur->fwh->devname, (const char *) fwh->devname)) {
			/* Found a candidate */
			if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
				/* The version we have on loaded is older, load this one instead */
				break;
			/* This version is no newer than what we have.  Don't worry about it.
			   We'll consider it a proper load anyhow though */
			munmap((void*)fwh, stbuf.st_size);
			close(fd);
			return 0;
		}
	}

	if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
		cur->fd = -1;
		AST_LIST_INSERT_TAIL(&firmwares, cur, list);
	}

	if (cur) {
		if (cur->fwh)
			munmap((void*)cur->fwh, cur->mmaplen);
		if (cur->fd > -1)
			close(cur->fd);
		cur->fwh = fwh;
		cur->fd = fd;
		cur->mmaplen = stbuf.st_size;
		cur->dead = 0;
	}

	return 0;
}

static void destroy_firmware(struct iax_firmware *cur)
{
	/* Close firmware */
	if (cur->fwh) {
		munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
	}
	close(cur->fd);
	ast_free(cur);
}

void iax_firmware_reload(void)
{
	struct iax_firmware *cur = NULL;
	DIR *fwd;
	struct dirent *de;
	char fn[PATH_MAX + sizeof(IAX_FIRMWARE_SUBDIR) + sizeof(de->d_name)];

	AST_LIST_LOCK(&firmwares);

	/* Mark all as dead */
	AST_LIST_TRAVERSE(&firmwares, cur, list) {
		cur->dead = 1;
	}

	/* Now that we have marked them dead... load new ones */
	snprintf(fn, sizeof(fn), "%s%s", ast_config_AST_DATA_DIR, IAX_FIRMWARE_SUBDIR);
	fwd = opendir(fn);
	if (fwd) {
		while((de = readdir(fwd))) {
			if (de->d_name[0] != '.') {
				snprintf(fn, sizeof(fn), "%s%s/%s",
					ast_config_AST_DATA_DIR, IAX_FIRMWARE_SUBDIR, de->d_name);
				if (!try_firmware(fn)) {
					ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
				}
			}
		}
		closedir(fwd);
	} else {
		ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", fn, strerror(errno));
	}

	/* Clean up leftovers */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
		if (!cur->dead)
			continue;
		AST_LIST_REMOVE_CURRENT(list);
		destroy_firmware(cur);
	}
	AST_LIST_TRAVERSE_SAFE_END;

	AST_LIST_UNLOCK(&firmwares);
}

void iax_firmware_unload(void)
{
	struct iax_firmware *cur = NULL;

	AST_LIST_LOCK(&firmwares);
	AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
		AST_LIST_REMOVE_CURRENT(list);
		destroy_firmware(cur);
	}
	AST_LIST_TRAVERSE_SAFE_END;
	AST_LIST_UNLOCK(&firmwares);
}

int iax_firmware_get_version(const char *dev, uint16_t *version)
{
	struct iax_firmware *cur = NULL;

	if (ast_strlen_zero(dev))
		return 0;

	AST_LIST_LOCK(&firmwares);
	AST_LIST_TRAVERSE(&firmwares, cur, list) {
		if (!strcmp(dev, (const char *) cur->fwh->devname)) {
			*version = ntohs(cur->fwh->version);
			AST_LIST_UNLOCK(&firmwares);
			return 1;
		}
	}
	AST_LIST_UNLOCK(&firmwares);

	return 0;
}

int iax_firmware_append(struct iax_ie_data *ied, const char *dev, unsigned int desc)
{
	int res = -1;
	unsigned int bs = desc & 0xff;
	unsigned int start = (desc >> 8) & 0xffffff;
	unsigned int bytes;
	struct iax_firmware *cur;

	if (ast_strlen_zero((char *)dev) || !bs)
		return -1;

	start *= bs;

	AST_LIST_LOCK(&firmwares);
	AST_LIST_TRAVERSE(&firmwares, cur, list) {
		if (strcmp(dev, (const char *) cur->fwh->devname))
			continue;
		iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
		if (start < ntohl(cur->fwh->datalen)) {
			bytes = ntohl(cur->fwh->datalen) - start;
			if (bytes > bs)
				bytes = bs;
			iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
		} else {
			bytes = 0;
			iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
		}
		if (bytes == bs)
			res = 0;
		else
			res = 1;
		break;
	}
	AST_LIST_UNLOCK(&firmwares);

	return res;
}

void iax_firmware_traverse(
	const char *filter,
	int (*callback)(struct ast_iax2_firmware_header *header, void *data),
	void *data)
{
	struct iax_firmware *cur = NULL;

	if (!callback) {
		return;
	}

	AST_LIST_LOCK(&firmwares);
	AST_LIST_TRAVERSE(&firmwares, cur, list) {
		if (!filter || !strcasecmp(filter, (const char *) cur->fwh->devname)) {
			if (callback(cur->fwh, data)) {
				break;
			}
		}
	}
	AST_LIST_UNLOCK(&firmwares);
}