xref: /dflybsd-src/contrib/libpcap/etherent.c (revision e75ef36f1332e115895388cede9dfd24ca1a806c)
11077d0bdSPeter Avalos /*
21077d0bdSPeter Avalos  * Copyright (c) 1990, 1993, 1994, 1995, 1996
31077d0bdSPeter Avalos  *	The Regents of the University of California.  All rights reserved.
41077d0bdSPeter Avalos  *
51077d0bdSPeter Avalos  * Redistribution and use in source and binary forms, with or without
61077d0bdSPeter Avalos  * modification, are permitted provided that: (1) source code distributions
71077d0bdSPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
81077d0bdSPeter Avalos  * distributions including binary code include the above copyright notice and
91077d0bdSPeter Avalos  * this paragraph in its entirety in the documentation or other materials
101077d0bdSPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
111077d0bdSPeter Avalos  * features or use of this software display the following acknowledgement:
121077d0bdSPeter Avalos  * ``This product includes software developed by the University of California,
131077d0bdSPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
141077d0bdSPeter Avalos  * the University nor the names of its contributors may be used to endorse
151077d0bdSPeter Avalos  * or promote products derived from this software without specific prior
161077d0bdSPeter Avalos  * written permission.
171077d0bdSPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
181077d0bdSPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
191077d0bdSPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
201077d0bdSPeter Avalos  */
211077d0bdSPeter Avalos 
221077d0bdSPeter Avalos #ifdef HAVE_CONFIG_H
233a289941SAaron LI #include <config.h>
241077d0bdSPeter Avalos #endif
251077d0bdSPeter Avalos 
263a289941SAaron LI #include <pcap-types.h>
271077d0bdSPeter Avalos 
281077d0bdSPeter Avalos #include <memory.h>
291077d0bdSPeter Avalos #include <stdio.h>
301077d0bdSPeter Avalos #include <string.h>
311077d0bdSPeter Avalos 
321077d0bdSPeter Avalos #include "pcap-int.h"
331077d0bdSPeter Avalos 
34de0d3203SPeter Avalos #include <pcap/namedb.h>
351077d0bdSPeter Avalos 
361077d0bdSPeter Avalos #ifdef HAVE_OS_PROTO_H
371077d0bdSPeter Avalos #include "os-proto.h"
381077d0bdSPeter Avalos #endif
391077d0bdSPeter Avalos 
401077d0bdSPeter Avalos static inline int skip_space(FILE *);
411077d0bdSPeter Avalos static inline int skip_line(FILE *);
421077d0bdSPeter Avalos 
431077d0bdSPeter Avalos /* Hex digit to integer. */
443a289941SAaron LI static inline u_char
xdtoi(u_char c)453a289941SAaron LI xdtoi(u_char c)
461077d0bdSPeter Avalos {
47*ea16f64eSAntonio Huete Jimenez 	if (c >= '0' && c <= '9')
483a289941SAaron LI 		return (u_char)(c - '0');
49*ea16f64eSAntonio Huete Jimenez 	else if (c >= 'a' && c <= 'f')
503a289941SAaron LI 		return (u_char)(c - 'a' + 10);
511077d0bdSPeter Avalos 	else
523a289941SAaron LI 		return (u_char)(c - 'A' + 10);
531077d0bdSPeter Avalos }
541077d0bdSPeter Avalos 
55*ea16f64eSAntonio Huete Jimenez /*
56*ea16f64eSAntonio Huete Jimenez  * Skip linear white space (space and tab) and any CRs before LF.
57*ea16f64eSAntonio Huete Jimenez  * Stop when we hit a non-white-space character or an end-of-line LF.
58*ea16f64eSAntonio Huete Jimenez  */
591077d0bdSPeter Avalos static inline int
skip_space(FILE * f)603a289941SAaron LI skip_space(FILE *f)
611077d0bdSPeter Avalos {
621077d0bdSPeter Avalos 	int c;
631077d0bdSPeter Avalos 
641077d0bdSPeter Avalos 	do {
651077d0bdSPeter Avalos 		c = getc(f);
66*ea16f64eSAntonio Huete Jimenez 	} while (c == ' ' || c == '\t' || c == '\r');
671077d0bdSPeter Avalos 
681077d0bdSPeter Avalos 	return c;
691077d0bdSPeter Avalos }
701077d0bdSPeter Avalos 
711077d0bdSPeter Avalos static inline int
skip_line(FILE * f)723a289941SAaron LI skip_line(FILE *f)
731077d0bdSPeter Avalos {
741077d0bdSPeter Avalos 	int c;
751077d0bdSPeter Avalos 
761077d0bdSPeter Avalos 	do
771077d0bdSPeter Avalos 		c = getc(f);
781077d0bdSPeter Avalos 	while (c != '\n' && c != EOF);
791077d0bdSPeter Avalos 
801077d0bdSPeter Avalos 	return c;
811077d0bdSPeter Avalos }
821077d0bdSPeter Avalos 
831077d0bdSPeter Avalos struct pcap_etherent *
pcap_next_etherent(FILE * fp)841077d0bdSPeter Avalos pcap_next_etherent(FILE *fp)
851077d0bdSPeter Avalos {
863a289941SAaron LI 	register int c, i;
873a289941SAaron LI 	u_char d;
881077d0bdSPeter Avalos 	char *bp;
893a289941SAaron LI 	size_t namesize;
901077d0bdSPeter Avalos 	static struct pcap_etherent e;
911077d0bdSPeter Avalos 
921077d0bdSPeter Avalos 	memset((char *)&e, 0, sizeof(e));
933a289941SAaron LI 	for (;;) {
941077d0bdSPeter Avalos 		/* Find addr */
951077d0bdSPeter Avalos 		c = skip_space(fp);
963a289941SAaron LI 		if (c == EOF)
973a289941SAaron LI 			return (NULL);
981077d0bdSPeter Avalos 		if (c == '\n')
991077d0bdSPeter Avalos 			continue;
1001077d0bdSPeter Avalos 
1011077d0bdSPeter Avalos 		/* If this is a comment, or first thing on line
1023a289941SAaron LI 		   cannot be Ethernet address, skip the line. */
103*ea16f64eSAntonio Huete Jimenez 		if (!PCAP_ISXDIGIT(c)) {
1041077d0bdSPeter Avalos 			c = skip_line(fp);
1053a289941SAaron LI 			if (c == EOF)
1063a289941SAaron LI 				return (NULL);
1071077d0bdSPeter Avalos 			continue;
1081077d0bdSPeter Avalos 		}
1091077d0bdSPeter Avalos 
1101077d0bdSPeter Avalos 		/* must be the start of an address */
1111077d0bdSPeter Avalos 		for (i = 0; i < 6; i += 1) {
1123a289941SAaron LI 			d = xdtoi((u_char)c);
1131077d0bdSPeter Avalos 			c = getc(fp);
1143a289941SAaron LI 			if (c == EOF)
1153a289941SAaron LI 				return (NULL);
116*ea16f64eSAntonio Huete Jimenez 			if (PCAP_ISXDIGIT(c)) {
1171077d0bdSPeter Avalos 				d <<= 4;
1183a289941SAaron LI 				d |= xdtoi((u_char)c);
1191077d0bdSPeter Avalos 				c = getc(fp);
1203a289941SAaron LI 				if (c == EOF)
1213a289941SAaron LI 					return (NULL);
1221077d0bdSPeter Avalos 			}
1231077d0bdSPeter Avalos 			e.addr[i] = d;
1241077d0bdSPeter Avalos 			if (c != ':')
1251077d0bdSPeter Avalos 				break;
1261077d0bdSPeter Avalos 			c = getc(fp);
1271077d0bdSPeter Avalos 			if (c == EOF)
1283a289941SAaron LI 				return (NULL);
1293a289941SAaron LI 		}
1301077d0bdSPeter Avalos 
1311077d0bdSPeter Avalos 		/* Must be whitespace */
132*ea16f64eSAntonio Huete Jimenez 		if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
1331077d0bdSPeter Avalos 			c = skip_line(fp);
1343a289941SAaron LI 			if (c == EOF)
1353a289941SAaron LI 				return (NULL);
1361077d0bdSPeter Avalos 			continue;
1371077d0bdSPeter Avalos 		}
1381077d0bdSPeter Avalos 		c = skip_space(fp);
1393a289941SAaron LI 		if (c == EOF)
1403a289941SAaron LI 			return (NULL);
1411077d0bdSPeter Avalos 
1421077d0bdSPeter Avalos 		/* hit end of line... */
1431077d0bdSPeter Avalos 		if (c == '\n')
1441077d0bdSPeter Avalos 			continue;
1451077d0bdSPeter Avalos 
1461077d0bdSPeter Avalos 		if (c == '#') {
1471077d0bdSPeter Avalos 			c = skip_line(fp);
1483a289941SAaron LI 			if (c == EOF)
1493a289941SAaron LI 				return (NULL);
1501077d0bdSPeter Avalos 			continue;
1511077d0bdSPeter Avalos 		}
1521077d0bdSPeter Avalos 
1531077d0bdSPeter Avalos 		/* pick up name */
1541077d0bdSPeter Avalos 		bp = e.name;
1553a289941SAaron LI 		/* Use 'namesize' to prevent buffer overflow. */
1563a289941SAaron LI 		namesize = sizeof(e.name) - 1;
1571077d0bdSPeter Avalos 		do {
1583a289941SAaron LI 			*bp++ = (u_char)c;
1591077d0bdSPeter Avalos 			c = getc(fp);
1603a289941SAaron LI 			if (c == EOF)
1613a289941SAaron LI 				return (NULL);
162*ea16f64eSAntonio Huete Jimenez 		} while (c != ' ' && c != '\t' && c != '\r' && c != '\n'
163*ea16f64eSAntonio Huete Jimenez 		    && --namesize != 0);
1641077d0bdSPeter Avalos 		*bp = '\0';
1651077d0bdSPeter Avalos 
1661077d0bdSPeter Avalos 		/* Eat trailing junk */
1671077d0bdSPeter Avalos 		if (c != '\n')
1681077d0bdSPeter Avalos 			(void)skip_line(fp);
1691077d0bdSPeter Avalos 
1701077d0bdSPeter Avalos 		return &e;
1713a289941SAaron LI 	}
1721077d0bdSPeter Avalos }
173