]> The Tcpdump Group git mirrors - libpcap/blob - etherent.c
Don't use old-style function definitions.
[libpcap] / etherent.c
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 #include <pcap-types.h>
27
28 #include <ctype.h>
29 #include <memory.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "pcap-int.h"
34
35 #include <pcap/namedb.h>
36
37 #ifdef HAVE_OS_PROTO_H
38 #include "os-proto.h"
39 #endif
40
41 static inline int xdtoi(int);
42 static inline int skip_space(FILE *);
43 static inline int skip_line(FILE *);
44
45 /* Hex digit to integer. */
46 static inline int
47 xdtoi(int c)
48 {
49 if (isdigit(c))
50 return c - '0';
51 else if (islower(c))
52 return c - 'a' + 10;
53 else
54 return c - 'A' + 10;
55 }
56
57 static inline int
58 skip_space(FILE *f)
59 {
60 int c;
61
62 do {
63 c = getc(f);
64 } while (isspace(c) && c != '\n');
65
66 return c;
67 }
68
69 static inline int
70 skip_line(FILE *f)
71 {
72 int c;
73
74 do
75 c = getc(f);
76 while (c != '\n' && c != EOF);
77
78 return c;
79 }
80
81 struct pcap_etherent *
82 pcap_next_etherent(FILE *fp)
83 {
84 register int c, d, i;
85 char *bp;
86 static struct pcap_etherent e;
87
88 memset((char *)&e, 0, sizeof(e));
89 for (;;) {
90 /* Find addr */
91 c = skip_space(fp);
92 if (c == EOF)
93 return (NULL);
94 if (c == '\n')
95 continue;
96
97 /* If this is a comment, or first thing on line
98 cannot be Ethernet address, skip the line. */
99 if (!isxdigit(c)) {
100 c = skip_line(fp);
101 if (c == EOF)
102 return (NULL);
103 continue;
104 }
105
106 /* must be the start of an address */
107 for (i = 0; i < 6; i += 1) {
108 d = xdtoi(c);
109 c = getc(fp);
110 if (c == EOF)
111 return (NULL);
112 if (isxdigit(c)) {
113 d <<= 4;
114 d |= xdtoi(c);
115 c = getc(fp);
116 if (c == EOF)
117 return (NULL);
118 }
119 e.addr[i] = d;
120 if (c != ':')
121 break;
122 c = getc(fp);
123 if (c == EOF)
124 return (NULL);
125 }
126
127 /* Must be whitespace */
128 if (!isspace(c)) {
129 c = skip_line(fp);
130 if (c == EOF)
131 return (NULL);
132 continue;
133 }
134 c = skip_space(fp);
135 if (c == EOF)
136 return (NULL);
137
138 /* hit end of line... */
139 if (c == '\n')
140 continue;
141
142 if (c == '#') {
143 c = skip_line(fp);
144 if (c == EOF)
145 return (NULL);
146 continue;
147 }
148
149 /* pick up name */
150 bp = e.name;
151 /* Use 'd' to prevent buffer overflow. */
152 d = sizeof(e.name) - 1;
153 do {
154 *bp++ = c;
155 c = getc(fp);
156 if (c == EOF)
157 return (NULL);
158 } while (!isspace(c) && --d > 0);
159 *bp = '\0';
160
161 /* Eat trailing junk */
162 if (c != '\n')
163 (void)skip_line(fp);
164
165 return &e;
166 }
167 }