| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
#include <sys/stat.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <fcntl.h> |
| 36 |
#include <dirent.h> |
| 37 |
#include <sys/mman.h> |
| 38 |
#include <arpa/inet.h> |
| 39 |
|
| 40 |
#include "asterisk/linkedlists.h" |
| 41 |
#include "asterisk/md5.h" |
| 42 |
#include "asterisk/paths.h" |
| 43 |
#include "asterisk/utils.h" |
| 44 |
|
| 45 |
#include "include/firmware.h" |
| 46 |
|
| 47 |
#define IAX_FIRMWARE_SUBDIR "/firmware/iax" |
| 48 |
|
| 49 |
struct iax_firmware { |
| 50 |
AST_LIST_ENTRY(iax_firmware) list; |
| 51 |
int fd; |
| 52 |
int mmaplen; |
| 53 |
int dead; |
| 54 |
struct ast_iax2_firmware_header *fwh; |
| 55 |
unsigned char *buf; |
| 56 |
}; |
| 57 |
|
| 58 |
static AST_LIST_HEAD_STATIC(firmwares, iax_firmware); |
| 59 |
|
| 60 |
static int try_firmware(char *s) |
| 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 |
|
| 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 |
|
| 100 |
unlink(s2); |
| 101 |
|
| 102 |
|
| 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 |
|
| 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 |
|
| 166 |
if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version))) |
| 167 |
|
| 168 |
break; |
| 169 |
|
| 170 |
|
| 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 |
} |
| 195 |
|
| 196 |
static void destroy_firmware(struct iax_firmware *cur) |
| 197 |
{ |
| 198 |
|
| 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 |
} |
| 205 |
|
| 206 |
void iax_firmware_reload(void) |
| 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 |
|
| 213 |
AST_LIST_LOCK(&firmwares); |
| 214 |
|
| 215 |
|
| 216 |
AST_LIST_TRAVERSE(&firmwares, cur, list) { |
| 217 |
cur->dead = 1; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 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", |
| 227 |
ast_config_AST_DATA_DIR, IAX_FIRMWARE_SUBDIR, de->d_name); |
| 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 |
|
| 239 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) { |
| 240 |
if (!cur->dead) |
| 241 |
continue; |
| 242 |
AST_LIST_REMOVE_CURRENT(list); |
| 243 |
destroy_firmware(cur); |
| 244 |
} |
| 245 |
AST_LIST_TRAVERSE_SAFE_END; |
| 246 |
|
| 247 |
AST_LIST_UNLOCK(&firmwares); |
| 248 |
} |
| 249 |
|
| 250 |
void iax_firmware_unload(void) |
| 251 |
{ |
| 252 |
struct iax_firmware *cur = NULL; |
| 253 |
|
| 254 |
AST_LIST_LOCK(&firmwares); |
| 255 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) { |
| 256 |
AST_LIST_REMOVE_CURRENT(list); |
| 257 |
destroy_firmware(cur); |
| 258 |
} |
| 259 |
AST_LIST_TRAVERSE_SAFE_END; |
| 260 |
AST_LIST_UNLOCK(&firmwares); |
| 261 |
} |
| 262 |
|
| 263 |
int iax_firmware_get_version(const char *dev, uint16_t *version) |
| 264 |
{ |
| 265 |
struct iax_firmware *cur = NULL; |
| 266 |
|
| 267 |
if (ast_strlen_zero(dev)) |
| 268 |
return 0; |
| 269 |
|
| 270 |
AST_LIST_LOCK(&firmwares); |
| 271 |
AST_LIST_TRAVERSE(&firmwares, cur, list) { |
| 272 |
if (!strcmp(dev, (const char *) cur->fwh->devname)) { |
| 273 |
*version = ntohs(cur->fwh->version); |
| 274 |
AST_LIST_UNLOCK(&firmwares); |
| 275 |
return 1; |
| 276 |
} |
| 277 |
} |
| 278 |
AST_LIST_UNLOCK(&firmwares); |
| 279 |
|
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
int iax_firmware_append(struct iax_ie_data *ied, const char *dev, unsigned int desc) |
| 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 |
|
| 296 |
AST_LIST_LOCK(&firmwares); |
| 297 |
AST_LIST_TRAVERSE(&firmwares, cur, list) { |
| 298 |
if (strcmp(dev, (const char *) cur->fwh->devname)) |
| 299 |
continue; |
| 300 |
iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc); |
| 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; |
| 308 |
iax_ie_append(ied, IAX_IE_FWBLOCKDATA); |
| 309 |
} |
| 310 |
if (bytes == bs) |
| 311 |
res = 0; |
| 312 |
else |
| 313 |
res = 1; |
| 314 |
break; |
| 315 |
} |
| 316 |
AST_LIST_UNLOCK(&firmwares); |
| 317 |
|
| 318 |
return res; |
| 319 |
} |
| 320 |
|
| 321 |
void iax_firmware_traverse( |
| 322 |
const char *filter, |
| 323 |
int (*callback)(struct ast_iax2_firmware_header *header, void *data), |
| 324 |
void *data) |
| 325 |
{ |
| 326 |
struct iax_firmware *cur = NULL; |
| 327 |
|
| 328 |
if (!callback) { |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
AST_LIST_LOCK(&firmwares); |
| 333 |
AST_LIST_TRAVERSE(&firmwares, cur, list) { |
| 334 |
if (!filter || !strcasecmp(filter, (const char *) cur->fwh->devname)) { |
| 335 |
if (callback(cur->fwh, data)) { |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
AST_LIST_UNLOCK(&firmwares); |
| 341 |
} |