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