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