Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions | Variables
firmware.c File Reference

IAX Firmware Support. More...

#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"
Include dependency graph for firmware.c:

Go to the source code of this file.

Data Structures

struct  firmwares
 
struct  iax_firmware
 

Macros

#define IAX_FIRMWARE_SUBDIR   "/firmware/iax"
 

Functions

static void destroy_firmware (struct iax_firmware *cur)
 
int iax_firmware_append (struct iax_ie_data *ied, const char *dev, unsigned int desc)
 
int iax_firmware_get_version (const char *dev, uint16_t *version)
 
void iax_firmware_reload (void)
 
void iax_firmware_traverse (const char *filter, int(*callback)(struct ast_iax2_firmware_header *header, void *data), void *data)
 
void iax_firmware_unload (void)
 
static int try_firmware (char *s)
 

Variables

static struct firmwares firmwares = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 

Detailed Description

IAX Firmware Support.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file firmware.c.

Macro Definition Documentation

◆ IAX_FIRMWARE_SUBDIR

#define IAX_FIRMWARE_SUBDIR   "/firmware/iax"

Definition at line 47 of file firmware.c.

Function Documentation

◆ destroy_firmware()

static void destroy_firmware ( struct iax_firmware cur)
static

Definition at line 196 of file firmware.c.

197{
198 /* Close firmware */
199 if (cur->fwh) {
200 munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
201 }
202 close(cur->fd);
203 ast_free(cur);
204}
#define ast_free(a)
Definition: astmm.h:180
unsigned int datalen
Definition: iax2.h:299
struct ast_iax2_firmware_header * fwh
Definition: firmware.c:54

References ast_free, ast_iax2_firmware_header::datalen, iax_firmware::fd, and iax_firmware::fwh.

Referenced by iax_firmware_reload(), and iax_firmware_unload().

◆ iax_firmware_append()

int iax_firmware_append ( struct iax_ie_data ied,
const char *  dev,
unsigned int  desc 
)

Definition at line 283 of file firmware.c.

284{
285 int res = -1;
286 unsigned int bs = desc & 0xff;
287 unsigned int start = (desc >> 8) & 0xffffff;
288 unsigned int bytes;
289 struct iax_firmware *cur;
290
291 if (ast_strlen_zero((char *)dev) || !bs)
292 return -1;
293
294 start *= bs;
295
298 if (strcmp(dev, (const char *) cur->fwh->devname))
299 continue;
301 if (start < ntohl(cur->fwh->datalen)) {
302 bytes = ntohl(cur->fwh->datalen) - start;
303 if (bytes > bs)
304 bytes = bs;
305 iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
306 } else {
307 bytes = 0;
309 }
310 if (bytes == bs)
311 res = 0;
312 else
313 res = 1;
314 break;
315 }
317
318 return res;
319}
static const char desc[]
Definition: cdr_radius.c:84
char * bs
Definition: eagi_proxy.c:73
#define IAX_IE_FWBLOCKDATA
Definition: iax2.h:166
#define IAX_IE_FWBLOCKDESC
Definition: iax2.h:165
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
int iax_ie_append(struct iax_ie_data *ied, unsigned char ie)
Definition: parser.c:780
int iax_ie_append_raw(struct iax_ie_data *ied, unsigned char ie, const void *data, int datalen)
Definition: parser.c:726
int iax_ie_append_int(struct iax_ie_data *ied, unsigned char ie, unsigned int value)
Definition: parser.c:756
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
unsigned char devname[16]
Definition: iax2.h:298
unsigned char data[0]
Definition: iax2.h:301
struct iax_firmware::@138 list

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), bs, ast_iax2_firmware_header::data, ast_iax2_firmware_header::datalen, desc, ast_iax2_firmware_header::devname, iax_firmware::fwh, iax_ie_append(), iax_ie_append_int(), iax_ie_append_raw(), IAX_IE_FWBLOCKDATA, IAX_IE_FWBLOCKDESC, and iax_firmware::list.

Referenced by socket_process_helper().

◆ iax_firmware_get_version()

int iax_firmware_get_version ( const char *  dev,
uint16_t *  version 
)

Definition at line 263 of file firmware.c.

264{
265 struct iax_firmware *cur = NULL;
266
267 if (ast_strlen_zero(dev))
268 return 0;
269
272 if (!strcmp(dev, (const char *) cur->fwh->devname)) {
273 *version = ntohs(cur->fwh->version);
275 return 1;
276 }
277 }
279
280 return 0;
281}
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
#define NULL
Definition: resample.c:96
unsigned short version
Definition: iax2.h:297

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_iax2_firmware_header::devname, iax_firmware::fwh, iax_firmware::list, NULL, version, and ast_iax2_firmware_header::version.

Referenced by update_registry().

◆ iax_firmware_reload()

void iax_firmware_reload ( void  )

Definition at line 206 of file firmware.c.

207{
208 struct iax_firmware *cur = NULL;
209 DIR *fwd;
210 struct dirent *de;
211 char fn[PATH_MAX + sizeof(IAX_FIRMWARE_SUBDIR) + sizeof(de->d_name)];
212
214
215 /* Mark all as dead */
216 AST_LIST_TRAVERSE(&firmwares, cur, list) {
217 cur->dead = 1;
218 }
219
220 /* Now that we have marked them dead... load new ones */
221 snprintf(fn, sizeof(fn), "%s%s", ast_config_AST_DATA_DIR, IAX_FIRMWARE_SUBDIR);
222 fwd = opendir(fn);
223 if (fwd) {
224 while((de = readdir(fwd))) {
225 if (de->d_name[0] != '.') {
226 snprintf(fn, sizeof(fn), "%s%s/%s",
228 if (!try_firmware(fn)) {
229 ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
230 }
231 }
232 }
233 closedir(fwd);
234 } else {
235 ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", fn, strerror(errno));
236 }
237
238 /* Clean up leftovers */
240 if (!cur->dead)
241 continue;
243 destroy_firmware(cur);
244 }
246
248}
#define PATH_MAX
Definition: asterisk.h:40
#define ast_log
Definition: astobj2.c:42
static int try_firmware(char *s)
Definition: firmware.c:60
#define IAX_FIRMWARE_SUBDIR
Definition: firmware.c:47
static void destroy_firmware(struct iax_firmware *cur)
Definition: firmware.c:196
#define ast_verb(level,...)
#define LOG_WARNING
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
int errno
const char * ast_config_AST_DATA_DIR
Definition: options.c:158

References ast_config_AST_DATA_DIR, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log, ast_verb, iax_firmware::dead, destroy_firmware(), errno, IAX_FIRMWARE_SUBDIR, LOG_WARNING, NULL, PATH_MAX, and try_firmware().

Referenced by load_module(), and reload_config().

◆ iax_firmware_traverse()

void iax_firmware_traverse ( const char *  filter,
int(*)(struct ast_iax2_firmware_header *header, void *data)  callback,
void *  data 
)

Definition at line 321 of file firmware.c.

325{
326 struct iax_firmware *cur = NULL;
327
328 if (!callback) {
329 return;
330 }
331
334 if (!filter || !strcasecmp(filter, (const char *) cur->fwh->devname)) {
335 if (callback(cur->fwh, data)) {
336 break;
337 }
338 }
339 }
341}
static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:807

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_iax2_firmware_header::devname, filter(), iax_firmware::fwh, iax_firmware::list, and NULL.

Referenced by handle_cli_iax2_show_firmware().

◆ iax_firmware_unload()

void iax_firmware_unload ( void  )

◆ try_firmware()

static int try_firmware ( char *  s)
static

Definition at line 60 of file firmware.c.

61{
62 struct stat stbuf;
63 struct iax_firmware *cur = NULL;
64 int ifd, fd, res, len, chunk;
65 struct ast_iax2_firmware_header *fwh, fwh2;
66 struct MD5Context md5;
67 unsigned char sum[16], buf[1024];
68 char *s2, *last;
69
70 s2 = ast_alloca(strlen(s) + 100);
71
72 last = strrchr(s, '/');
73 if (last)
74 last++;
75 else
76 last = s;
77
78 snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, ast_random());
79
80 if (stat(s, &stbuf) < 0) {
81 ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
82 return -1;
83 }
84
85 /* Make sure it's not a directory */
86 if (S_ISDIR(stbuf.st_mode))
87 return -1;
88 ifd = open(s, O_RDONLY);
89 if (ifd < 0) {
90 ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
91 return -1;
92 }
93 fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
94 if (fd < 0) {
95 ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
96 close(ifd);
97 return -1;
98 }
99 /* Unlink our newly created file */
100 unlink(s2);
101
102 /* Now copy the firmware into it */
103 len = stbuf.st_size;
104 while(len) {
105 chunk = len;
106 if (chunk > sizeof(buf))
107 chunk = sizeof(buf);
108 res = read(ifd, buf, chunk);
109 if (res != chunk) {
110 ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
111 close(ifd);
112 close(fd);
113 return -1;
114 }
115 res = write(fd, buf, chunk);
116 if (res != chunk) {
117 ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
118 close(ifd);
119 close(fd);
120 return -1;
121 }
122 len -= chunk;
123 }
124 close(ifd);
125 /* Return to the beginning */
126 lseek(fd, 0, SEEK_SET);
127 if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
128 ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
129 close(fd);
130 return -1;
131 }
132 if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
133 ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
134 close(fd);
135 return -1;
136 }
137 if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
138 ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
139 close(fd);
140 return -1;
141 }
142 if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
143 ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
144 close(fd);
145 return -1;
146 }
147 fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
148 if (fwh == MAP_FAILED) {
149 ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
150 close(fd);
151 return -1;
152 }
153 MD5Init(&md5);
154 MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
155 MD5Final(sum, &md5);
156 if (memcmp(sum, fwh->chksum, sizeof(sum))) {
157 ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
158 munmap((void*)fwh, stbuf.st_size);
159 close(fd);
160 return -1;
161 }
162
163 AST_LIST_TRAVERSE(&firmwares, cur, list) {
164 if (!strcmp((const char *) cur->fwh->devname, (const char *) fwh->devname)) {
165 /* Found a candidate */
166 if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
167 /* The version we have on loaded is older, load this one instead */
168 break;
169 /* This version is no newer than what we have. Don't worry about it.
170 We'll consider it a proper load anyhow though */
171 munmap((void*)fwh, stbuf.st_size);
172 close(fd);
173 return 0;
174 }
175 }
176
177 if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
178 cur->fd = -1;
179 AST_LIST_INSERT_TAIL(&firmwares, cur, list);
180 }
181
182 if (cur) {
183 if (cur->fwh)
184 munmap((void*)cur->fwh, cur->mmaplen);
185 if (cur->fd > -1)
186 close(cur->fd);
187 cur->fwh = fwh;
188 cur->fd = fd;
189 cur->mmaplen = stbuf.st_size;
190 cur->dead = 0;
191 }
192
193 return 0;
194}
struct sla_ringing_trunk * last
Definition: app_sla.c:332
#define AST_FILE_MODE
Definition: asterisk.h:32
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int md5(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_md5.c:52
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define IAX_FIRMWARE_MAGIC
Definition: iax2.h:293
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len)
Definition: md5.c:72
void MD5Init(struct MD5Context *context)
Definition: md5.c:57
void MD5Final(unsigned char digest[16], struct MD5Context *context)
Definition: md5.c:120
Definition: md5.h:26
unsigned int magic
Definition: iax2.h:296
unsigned char chksum[16]
Definition: iax2.h:300
int mmaplen
Definition: firmware.c:52
long int ast_random(void)
Definition: utils.c:2312

References ast_alloca, ast_calloc, AST_FILE_MODE, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log, ast_random(), ast_strlen_zero(), buf, ast_iax2_firmware_header::chksum, ast_iax2_firmware_header::data, ast_iax2_firmware_header::datalen, iax_firmware::dead, ast_iax2_firmware_header::devname, errno, iax_firmware::fd, iax_firmware::fwh, IAX_FIRMWARE_MAGIC, last, len(), LOG_WARNING, ast_iax2_firmware_header::magic, md5(), MD5Final(), MD5Init(), MD5Update(), iax_firmware::mmaplen, NULL, and ast_iax2_firmware_header::version.

Referenced by iax_firmware_reload().

Variable Documentation

◆ firmwares

struct firmwares firmwares = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
static