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