1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel (at) holtmann.org> 6 * 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #ifdef HAVE_CONFIG_H 25 #include <config.h> 26 #endif 27 28 #include <stdio.h> 29 #include <errno.h> 30 #include <fcntl.h> 31 #include <unistd.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <sys/stat.h> 35 #include <sys/mman.h> 36 37 #include "oui.h" 38 39 /* http://standards.ieee.org/regauth/oui/oui.txt */ 40 41 char *ouitocomp(const char *oui) 42 { 43 struct stat st; 44 char *str, *map, *off, *end; 45 int fd; 46 47 fd = open(OUIFILE, O_RDONLY); 48 if (fd < 0) 49 return NULL; 50 51 if (fstat(fd, &st) < 0) { 52 close(fd); 53 return NULL; 54 } 55 56 str = malloc(128); 57 if (!str) { 58 close(fd); 59 return NULL; 60 } 61 62 memset(str, 0, 128); 63 64 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 65 if (!map || map == MAP_FAILED) { 66 free(str); 67 close(fd); 68 return NULL; 69 } 70 71 off = strstr(map, oui); 72 if (off) { 73 off += 18; 74 end = strpbrk(off, "\r\n"); 75 strncpy(str, off, end - off); 76 } else { 77 free(str); 78 str = NULL; 79 } 80 81 munmap(map, st.st_size); 82 83 close(fd); 84 85 return str; 86 } 87 88 int oui2comp(const char *oui, char *comp, size_t size) 89 { 90 char *tmp; 91 92 tmp = ouitocomp(oui); 93 if (!tmp) 94 return -1; 95 96 snprintf(comp, size, "%s", tmp); 97 98 free(tmp); 99 100 return 0; 101 } 102