1 /* $NetBSD: etherent.c,v 1.1.1.3 2013/04/06 15:57:49 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993, 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #ifndef lint 25 static const char rcsid[] _U_ = 26 "@(#) Header: /tcpdump/master/libpcap/etherent.c,v 1.23 2006-10-04 18:09:22 guy Exp (LBL)"; 27 #endif 28 29 #ifdef HAVE_CONFIG_H 30 #include "config.h" 31 #endif 32 33 #ifdef WIN32 34 #include <pcap-stdinc.h> 35 #else /* WIN32 */ 36 #if HAVE_INTTYPES_H 37 #include <inttypes.h> 38 #elif HAVE_STDINT_H 39 #include <stdint.h> 40 #endif 41 #ifdef HAVE_SYS_BITYPES_H 42 #include <sys/bitypes.h> 43 #endif 44 #include <sys/types.h> 45 #endif /* WIN32 */ 46 47 #include <ctype.h> 48 #include <memory.h> 49 #include <stdio.h> 50 #include <string.h> 51 52 #include "pcap-int.h" 53 54 #include <pcap/namedb.h> 55 56 #ifdef HAVE_OS_PROTO_H 57 #include "os-proto.h" 58 #endif 59 60 static inline int xdtoi(int); 61 static inline int skip_space(FILE *); 62 static inline int skip_line(FILE *); 63 64 /* Hex digit to integer. */ 65 static inline int 66 xdtoi(c) 67 register int c; 68 { 69 if (isdigit(c)) 70 return c - '0'; 71 else if (islower(c)) 72 return c - 'a' + 10; 73 else 74 return c - 'A' + 10; 75 } 76 77 static inline int 78 skip_space(f) 79 FILE *f; 80 { 81 int c; 82 83 do { 84 c = getc(f); 85 } while (isspace(c) && c != '\n'); 86 87 return c; 88 } 89 90 static inline int 91 skip_line(f) 92 FILE *f; 93 { 94 int c; 95 96 do 97 c = getc(f); 98 while (c != '\n' && c != EOF); 99 100 return c; 101 } 102 103 struct pcap_etherent * 104 pcap_next_etherent(FILE *fp) 105 { 106 register int c, d, i; 107 char *bp; 108 static struct pcap_etherent e; 109 110 memset((char *)&e, 0, sizeof(e)); 111 do { 112 /* Find addr */ 113 c = skip_space(fp); 114 if (c == '\n') 115 continue; 116 117 /* If this is a comment, or first thing on line 118 cannot be etehrnet address, skip the line. */ 119 if (!isxdigit(c)) { 120 c = skip_line(fp); 121 continue; 122 } 123 124 /* must be the start of an address */ 125 for (i = 0; i < 6; i += 1) { 126 d = xdtoi(c); 127 c = getc(fp); 128 if (isxdigit(c)) { 129 d <<= 4; 130 d |= xdtoi(c); 131 c = getc(fp); 132 } 133 e.addr[i] = d; 134 if (c != ':') 135 break; 136 c = getc(fp); 137 } 138 if (c == EOF) 139 break; 140 141 /* Must be whitespace */ 142 if (!isspace(c)) { 143 c = skip_line(fp); 144 continue; 145 } 146 c = skip_space(fp); 147 148 /* hit end of line... */ 149 if (c == '\n') 150 continue; 151 152 if (c == '#') { 153 c = skip_line(fp); 154 continue; 155 } 156 157 /* pick up name */ 158 bp = e.name; 159 /* Use 'd' to prevent buffer overflow. */ 160 d = sizeof(e.name) - 1; 161 do { 162 *bp++ = c; 163 c = getc(fp); 164 } while (!isspace(c) && c != EOF && --d > 0); 165 *bp = '\0'; 166 167 /* Eat trailing junk */ 168 if (c != '\n') 169 (void)skip_line(fp); 170 171 return &e; 172 173 } while (c != EOF); 174 175 return (NULL); 176 } 177