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