1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2004-2009 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 #define OUIFILE "/var/lib/misc/oui.txt" 42 43 char *ouitocomp(const char *oui) 44 { 45 struct stat st; 46 char *str, *map, *off, *end; 47 int fd; 48 49 fd = open("oui.txt", O_RDONLY); 50 if (fd < 0) { 51 fd = open(OUIFILE, O_RDONLY); 52 if (fd < 0) { 53 fd = open("/usr/share/misc/oui.txt", O_RDONLY); 54 if (fd < 0) 55 return NULL; 56 } 57 } 58 59 if (fstat(fd, &st) < 0) { 60 close(fd); 61 return NULL; 62 } 63 64 str = malloc(128); 65 if (!str) { 66 close(fd); 67 return NULL; 68 } 69 70 memset(str, 0, 128); 71 72 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 73 if (!map || map == MAP_FAILED) { 74 free(str); 75 close(fd); 76 return NULL; 77 } 78 79 off = strstr(map, oui); 80 if (off) { 81 off += 18; 82 end = strpbrk(off, "\r\n"); 83 strncpy(str, off, end - off); 84 } else { 85 free(str); 86 str = NULL; 87 } 88 89 munmap(map, st.st_size); 90 91 close(fd); 92 93 return str; 94 } 95 96 int oui2comp(const char *oui, char *comp, size_t size) 97 { 98 char *tmp; 99 100 tmp = ouitocomp(oui); 101 if (!tmp) 102 return -1; 103 104 snprintf(comp, size, "%s", tmp); 105 106 free(tmp); 107 108 return 0; 109 } 110