Merge illumos-gate
[unleashed.git] / contrib / libpcap / etherent.c
blob5cfd1b4c3bbb689b9179cfbb474d5cd5ba1934e8
1 /*
2 * Copyright (c) 1990, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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.
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #ifdef _WIN32
27 #include <pcap-stdinc.h>
28 #else /* _WIN32 */
29 #if HAVE_INTTYPES_H
30 #include <inttypes.h>
31 #elif HAVE_STDINT_H
32 #include <stdint.h>
33 #endif
34 #ifdef HAVE_SYS_BITYPES_H
35 #include <sys/bitypes.h>
36 #endif
37 #include <sys/types.h>
38 #endif /* _WIN32 */
40 #include <ctype.h>
41 #include <memory.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "pcap-int.h"
47 #include <pcap/namedb.h>
49 #ifdef HAVE_OS_PROTO_H
50 #include "os-proto.h"
51 #endif
53 static inline int xdtoi(int);
54 static inline int skip_space(FILE *);
55 static inline int skip_line(FILE *);
57 /* Hex digit to integer. */
58 static inline int
59 xdtoi(c)
60 register int c;
62 if (isdigit(c))
63 return c - '0';
64 else if (islower(c))
65 return c - 'a' + 10;
66 else
67 return c - 'A' + 10;
70 static inline int
71 skip_space(f)
72 FILE *f;
74 int c;
76 do {
77 c = getc(f);
78 } while (isspace(c) && c != '\n');
80 return c;
83 static inline int
84 skip_line(f)
85 FILE *f;
87 int c;
90 c = getc(f);
91 while (c != '\n' && c != EOF);
93 return c;
96 struct pcap_etherent *
97 pcap_next_etherent(FILE *fp)
99 register int c, d, i;
100 char *bp;
101 static struct pcap_etherent e;
103 memset((char *)&e, 0, sizeof(e));
104 do {
105 /* Find addr */
106 c = skip_space(fp);
107 if (c == '\n')
108 continue;
110 /* If this is a comment, or first thing on line
111 cannot be etehrnet address, skip the line. */
112 if (!isxdigit(c)) {
113 c = skip_line(fp);
114 continue;
117 /* must be the start of an address */
118 for (i = 0; i < 6; i += 1) {
119 d = xdtoi(c);
120 c = getc(fp);
121 if (isxdigit(c)) {
122 d <<= 4;
123 d |= xdtoi(c);
124 c = getc(fp);
126 e.addr[i] = d;
127 if (c != ':')
128 break;
129 c = getc(fp);
131 if (c == EOF)
132 break;
134 /* Must be whitespace */
135 if (!isspace(c)) {
136 c = skip_line(fp);
137 continue;
139 c = skip_space(fp);
141 /* hit end of line... */
142 if (c == '\n')
143 continue;
145 if (c == '#') {
146 c = skip_line(fp);
147 continue;
150 /* pick up name */
151 bp = e.name;
152 /* Use 'd' to prevent buffer overflow. */
153 d = sizeof(e.name) - 1;
154 do {
155 *bp++ = c;
156 c = getc(fp);
157 } while (!isspace(c) && c != EOF && --d > 0);
158 *bp = '\0';
160 /* Eat trailing junk */
161 if (c != '\n')
162 (void)skip_line(fp);
164 return &e;
166 } while (c != EOF);
168 return (NULL);