]> git.saurik.com Git - apple/network_cmds.git/blob - pcap/etherent.c
3b2347e97976d3d63070ad69a95f007c95edcbaa
[apple/network_cmds.git] / pcap / etherent.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* $OpenBSD: etherent.c,v 1.5 1996/09/16 02:33:04 tholo Exp $ */
25
26 /*
27 * Copyright (c) 1990, 1993, 1994, 1995
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that: (1) source code distributions
32 * retain the above copyright notice and this paragraph in its entirety, (2)
33 * distributions including binary code include the above copyright notice and
34 * this paragraph in its entirety in the documentation or other materials
35 * provided with the distribution, and (3) all advertising materials mentioning
36 * features or use of this software display the following acknowledgement:
37 * ``This product includes software developed by the University of California,
38 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
39 * the University nor the names of its contributors may be used to endorse
40 * or promote products derived from this software without specific prior
41 * written permission.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
43 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
45 */
46 #ifndef lint
47 static char rcsid[] =
48 "@(#) Header: etherent.c,v 1.18 95/10/07 03:08:12 leres Exp (LBL)";
49 #endif
50
51 #include <sys/types.h>
52
53 #include <ctype.h>
54 #include <memory.h>
55 #include <pcap.h>
56 #include <pcap-namedb.h>
57 #include <stdio.h>
58 #include <string.h>
59
60 #ifdef HAVE_OS_PROTO_H
61 #include "os-proto.h"
62 #endif
63
64 #include "pcap-int.h"
65
66 static __inline int xdtoi(int);
67 static __inline int skip_space(FILE *);
68 static __inline int skip_line(FILE *);
69
70 /* Hex digit to integer. */
71 static __inline int
72 xdtoi(c)
73 register int c;
74 {
75 if (isdigit(c))
76 return c - '0';
77 else if (islower(c))
78 return c - 'a' + 10;
79 else
80 return c - 'A' + 10;
81 }
82
83 static __inline int
84 skip_space(f)
85 FILE *f;
86 {
87 int c;
88
89 do {
90 c = getc(f);
91 } while (isspace(c) && c != '\n');
92
93 return c;
94 }
95
96 static __inline int
97 skip_line(f)
98 FILE *f;
99 {
100 int c;
101
102 do
103 c = getc(f);
104 while (c != '\n' && c != EOF);
105
106 return c;
107 }
108
109 struct pcap_etherent *
110 pcap_next_etherent(FILE *fp)
111 {
112 register int c, d, i;
113 char *bp;
114 static struct pcap_etherent e;
115
116 memset((char *)&e, 0, sizeof(e));
117 do {
118 /* Find addr */
119 c = skip_space(fp);
120 if (c == '\n')
121 continue;
122
123 /* If this is a comment, or first thing on line
124 cannot be etehrnet address, skip the line. */
125 if (!isxdigit(c)) {
126 c = skip_line(fp);
127 continue;
128 }
129
130 /* must be the start of an address */
131 for (i = 0; i < 6; i += 1) {
132 d = xdtoi(c);
133 c = getc(fp);
134 if (isxdigit(c)) {
135 d <<= 4;
136 d |= xdtoi(c);
137 c = getc(fp);
138 }
139 e.addr[i] = d;
140 if (c != ':')
141 break;
142 c = getc(fp);
143 }
144 if (c == EOF)
145 break;
146
147 /* Must be whitespace */
148 if (!isspace(c)) {
149 c = skip_line(fp);
150 continue;
151 }
152 c = skip_space(fp);
153
154 /* hit end of line... */
155 if (c == '\n')
156 continue;
157
158 if (c == '#') {
159 c = skip_line(fp);
160 continue;
161 }
162
163 /* pick up name */
164 bp = e.name;
165 /* Use 'd' to prevent buffer overflow. */
166 d = sizeof(e.name) - 1;
167 do {
168 *bp++ = c;
169 c = getc(fp);
170 } while (!isspace(c) && c != EOF && --d > 0);
171 *bp = '\0';
172
173 /* Eat trailing junk */
174 if (c != '\n')
175 (void)skip_line(fp);
176
177 return &e;
178
179 } while (c != EOF);
180
181 return (NULL);
182 }