1*19fef815Sderaadt /* $OpenBSD: etherent.c,v 1.10 2024/04/05 18:01:56 deraadt Exp $ */
2df930be7Sderaadt
3df930be7Sderaadt /*
401efc7efSderaadt * Copyright (c) 1990, 1993, 1994, 1995, 1996
5df930be7Sderaadt * The Regents of the University of California. All rights reserved.
6df930be7Sderaadt *
7df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt * modification, are permitted provided that: (1) source code distributions
9df930be7Sderaadt * retain the above copyright notice and this paragraph in its entirety, (2)
10df930be7Sderaadt * distributions including binary code include the above copyright notice and
11df930be7Sderaadt * this paragraph in its entirety in the documentation or other materials
12df930be7Sderaadt * provided with the distribution, and (3) all advertising materials mentioning
13df930be7Sderaadt * features or use of this software display the following acknowledgement:
14df930be7Sderaadt * ``This product includes software developed by the University of California,
15df930be7Sderaadt * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16df930be7Sderaadt * the University nor the names of its contributors may be used to endorse
17df930be7Sderaadt * or promote products derived from this software without specific prior
18df930be7Sderaadt * written permission.
19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20df930be7Sderaadt * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21df930be7Sderaadt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22df930be7Sderaadt */
2301efc7efSderaadt
24df930be7Sderaadt #include <sys/types.h>
25df930be7Sderaadt
26df930be7Sderaadt #include <ctype.h>
27df930be7Sderaadt #include <stdio.h>
289b113833Smickey #include <string.h>
29df930be7Sderaadt
3001efc7efSderaadt #include "pcap-int.h"
3101efc7efSderaadt
3201efc7efSderaadt #include <pcap-namedb.h>
339b113833Smickey #ifdef HAVE_OS_PROTO_H
349b113833Smickey #include "os-proto.h"
35df930be7Sderaadt #endif
36df930be7Sderaadt
378df16311Stholo static __inline int xdtoi(int);
388df16311Stholo static __inline int skip_space(FILE *);
398df16311Stholo static __inline int skip_line(FILE *);
40df930be7Sderaadt
41df930be7Sderaadt /* Hex digit to integer. */
428df16311Stholo static __inline int
xdtoi(int c)43*19fef815Sderaadt xdtoi(int c)
44df930be7Sderaadt {
45df930be7Sderaadt if (isdigit(c))
46df930be7Sderaadt return c - '0';
47df930be7Sderaadt else if (islower(c))
48df930be7Sderaadt return c - 'a' + 10;
49df930be7Sderaadt else
50df930be7Sderaadt return c - 'A' + 10;
51df930be7Sderaadt }
52df930be7Sderaadt
538df16311Stholo static __inline int
skip_space(FILE * f)54*19fef815Sderaadt skip_space(FILE *f)
55df930be7Sderaadt {
56df930be7Sderaadt int c;
57df930be7Sderaadt
58df930be7Sderaadt do {
59df930be7Sderaadt c = getc(f);
60df930be7Sderaadt } while (isspace(c) && c != '\n');
61df930be7Sderaadt
62df930be7Sderaadt return c;
63df930be7Sderaadt }
64df930be7Sderaadt
658df16311Stholo static __inline int
skip_line(FILE * f)66*19fef815Sderaadt skip_line(FILE *f)
67df930be7Sderaadt {
68df930be7Sderaadt int c;
69df930be7Sderaadt
70df930be7Sderaadt do
71df930be7Sderaadt c = getc(f);
72df930be7Sderaadt while (c != '\n' && c != EOF);
73df930be7Sderaadt
74df930be7Sderaadt return c;
75df930be7Sderaadt }
76df930be7Sderaadt
77df930be7Sderaadt struct pcap_etherent *
pcap_next_etherent(FILE * fp)78df930be7Sderaadt pcap_next_etherent(FILE *fp)
79df930be7Sderaadt {
80d0438536Smmcc int c, d, i;
81df930be7Sderaadt char *bp;
82df930be7Sderaadt static struct pcap_etherent e;
839b113833Smickey
849b113833Smickey memset((char *)&e, 0, sizeof(e));
859b113833Smickey do {
86df930be7Sderaadt /* Find addr */
87df930be7Sderaadt c = skip_space(fp);
88df930be7Sderaadt if (c == '\n')
89df930be7Sderaadt continue;
909b113833Smickey
91df930be7Sderaadt /* If this is a comment, or first thing on line
929b113833Smickey cannot be etehrnet address, skip the line. */
939b113833Smickey if (!isxdigit(c)) {
94df930be7Sderaadt c = skip_line(fp);
959b113833Smickey continue;
969b113833Smickey }
979b113833Smickey
98df930be7Sderaadt /* must be the start of an address */
99df930be7Sderaadt for (i = 0; i < 6; i += 1) {
100df930be7Sderaadt d = xdtoi(c);
101df930be7Sderaadt c = getc(fp);
1029b113833Smickey if (isxdigit(c)) {
103df930be7Sderaadt d <<= 4;
104df930be7Sderaadt d |= xdtoi(c);
105df930be7Sderaadt c = getc(fp);
106df930be7Sderaadt }
107df930be7Sderaadt e.addr[i] = d;
108df930be7Sderaadt if (c != ':')
109df930be7Sderaadt break;
110df930be7Sderaadt c = getc(fp);
111df930be7Sderaadt }
112df930be7Sderaadt if (c == EOF)
1139b113833Smickey break;
1149b113833Smickey
1159b113833Smickey /* Must be whitespace */
1169b113833Smickey if (!isspace(c)) {
1179b113833Smickey c = skip_line(fp);
1189b113833Smickey continue;
119df930be7Sderaadt }
120df930be7Sderaadt c = skip_space(fp);
121df930be7Sderaadt
1229b113833Smickey /* hit end of line... */
1239b113833Smickey if (c == '\n')
1249b113833Smickey continue;
1259b113833Smickey
1269b113833Smickey if (c == '#') {
1279b113833Smickey c = skip_line(fp);
1289b113833Smickey continue;
1299b113833Smickey }
1309b113833Smickey
1319b113833Smickey /* pick up name */
132df930be7Sderaadt bp = e.name;
133df930be7Sderaadt /* Use 'd' to prevent buffer overflow. */
134df930be7Sderaadt d = sizeof(e.name) - 1;
135df930be7Sderaadt do {
136df930be7Sderaadt *bp++ = c;
137df930be7Sderaadt c = getc(fp);
138df930be7Sderaadt } while (!isspace(c) && c != EOF && --d > 0);
139df930be7Sderaadt *bp = '\0';
1409b113833Smickey
1419b113833Smickey /* Eat trailing junk */
1429b113833Smickey if (c != '\n')
1439b113833Smickey (void)skip_line(fp);
144df930be7Sderaadt
145df930be7Sderaadt return &e;
1469b113833Smickey
1479b113833Smickey } while (c != EOF);
1489b113833Smickey
1499b113833Smickey return (NULL);
150df930be7Sderaadt }
151