Home | History | Annotate | Line # | Download | only in dist
etherent.c revision 1.2
      1  1.2  christos /*	$NetBSD: etherent.c,v 1.2 2014/11/19 19:33:30 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1990, 1993, 1994, 1995, 1996
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      9  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  1.1  christos  * distributions including binary code include the above copyright notice and
     11  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     12  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     13  1.1  christos  * features or use of this software display the following acknowledgement:
     14  1.1  christos  * ``This product includes software developed by the University of California,
     15  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  1.1  christos  * the University nor the names of its contributors may be used to endorse
     17  1.1  christos  * or promote products derived from this software without specific prior
     18  1.1  christos  * written permission.
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  1.1  christos  */
     23  1.1  christos 
     24  1.2  christos #include <sys/cdefs.h>
     25  1.2  christos __RCSID("$NetBSD: etherent.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
     26  1.1  christos 
     27  1.1  christos #ifdef HAVE_CONFIG_H
     28  1.1  christos #include "config.h"
     29  1.1  christos #endif
     30  1.1  christos 
     31  1.1  christos #ifdef WIN32
     32  1.1  christos #include <pcap-stdinc.h>
     33  1.1  christos #else /* WIN32 */
     34  1.1  christos #if HAVE_INTTYPES_H
     35  1.1  christos #include <inttypes.h>
     36  1.1  christos #elif HAVE_STDINT_H
     37  1.1  christos #include <stdint.h>
     38  1.1  christos #endif
     39  1.1  christos #ifdef HAVE_SYS_BITYPES_H
     40  1.1  christos #include <sys/bitypes.h>
     41  1.1  christos #endif
     42  1.1  christos #include <sys/types.h>
     43  1.1  christos #endif /* WIN32 */
     44  1.1  christos 
     45  1.1  christos #include <ctype.h>
     46  1.1  christos #include <memory.h>
     47  1.1  christos #include <stdio.h>
     48  1.1  christos #include <string.h>
     49  1.1  christos 
     50  1.1  christos #include "pcap-int.h"
     51  1.1  christos 
     52  1.1  christos #include <pcap/namedb.h>
     53  1.1  christos 
     54  1.1  christos #ifdef HAVE_OS_PROTO_H
     55  1.1  christos #include "os-proto.h"
     56  1.1  christos #endif
     57  1.1  christos 
     58  1.1  christos static inline int xdtoi(int);
     59  1.1  christos static inline int skip_space(FILE *);
     60  1.1  christos static inline int skip_line(FILE *);
     61  1.1  christos 
     62  1.1  christos /* Hex digit to integer. */
     63  1.1  christos static inline int
     64  1.1  christos xdtoi(c)
     65  1.1  christos 	register int c;
     66  1.1  christos {
     67  1.1  christos 	if (isdigit(c))
     68  1.1  christos 		return c - '0';
     69  1.1  christos 	else if (islower(c))
     70  1.1  christos 		return c - 'a' + 10;
     71  1.1  christos 	else
     72  1.1  christos 		return c - 'A' + 10;
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos static inline int
     76  1.1  christos skip_space(f)
     77  1.1  christos 	FILE *f;
     78  1.1  christos {
     79  1.1  christos 	int c;
     80  1.1  christos 
     81  1.1  christos 	do {
     82  1.1  christos 		c = getc(f);
     83  1.1  christos 	} while (isspace(c) && c != '\n');
     84  1.1  christos 
     85  1.1  christos 	return c;
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos static inline int
     89  1.1  christos skip_line(f)
     90  1.1  christos 	FILE *f;
     91  1.1  christos {
     92  1.1  christos 	int c;
     93  1.1  christos 
     94  1.1  christos 	do
     95  1.1  christos 		c = getc(f);
     96  1.1  christos 	while (c != '\n' && c != EOF);
     97  1.1  christos 
     98  1.1  christos 	return c;
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos struct pcap_etherent *
    102  1.1  christos pcap_next_etherent(FILE *fp)
    103  1.1  christos {
    104  1.1  christos 	register int c, d, i;
    105  1.1  christos 	char *bp;
    106  1.1  christos 	static struct pcap_etherent e;
    107  1.1  christos 
    108  1.1  christos 	memset((char *)&e, 0, sizeof(e));
    109  1.1  christos 	do {
    110  1.1  christos 		/* Find addr */
    111  1.1  christos 		c = skip_space(fp);
    112  1.1  christos 		if (c == '\n')
    113  1.1  christos 			continue;
    114  1.1  christos 
    115  1.1  christos 		/* If this is a comment, or first thing on line
    116  1.1  christos 		   cannot be etehrnet address, skip the line. */
    117  1.1  christos 		if (!isxdigit(c)) {
    118  1.1  christos 			c = skip_line(fp);
    119  1.1  christos 			continue;
    120  1.1  christos 		}
    121  1.1  christos 
    122  1.1  christos 		/* must be the start of an address */
    123  1.1  christos 		for (i = 0; i < 6; i += 1) {
    124  1.1  christos 			d = xdtoi(c);
    125  1.1  christos 			c = getc(fp);
    126  1.1  christos 			if (isxdigit(c)) {
    127  1.1  christos 				d <<= 4;
    128  1.1  christos 				d |= xdtoi(c);
    129  1.1  christos 				c = getc(fp);
    130  1.1  christos 			}
    131  1.1  christos 			e.addr[i] = d;
    132  1.1  christos 			if (c != ':')
    133  1.1  christos 				break;
    134  1.1  christos 			c = getc(fp);
    135  1.1  christos 		}
    136  1.1  christos 		if (c == EOF)
    137  1.1  christos 			break;
    138  1.1  christos 
    139  1.1  christos 		/* Must be whitespace */
    140  1.1  christos 		if (!isspace(c)) {
    141  1.1  christos 			c = skip_line(fp);
    142  1.1  christos 			continue;
    143  1.1  christos 		}
    144  1.1  christos 		c = skip_space(fp);
    145  1.1  christos 
    146  1.1  christos 		/* hit end of line... */
    147  1.1  christos 		if (c == '\n')
    148  1.1  christos 			continue;
    149  1.1  christos 
    150  1.1  christos 		if (c == '#') {
    151  1.1  christos 			c = skip_line(fp);
    152  1.1  christos 			continue;
    153  1.1  christos 		}
    154  1.1  christos 
    155  1.1  christos 		/* pick up name */
    156  1.1  christos 		bp = e.name;
    157  1.1  christos 		/* Use 'd' to prevent buffer overflow. */
    158  1.1  christos 		d = sizeof(e.name) - 1;
    159  1.1  christos 		do {
    160  1.1  christos 			*bp++ = c;
    161  1.1  christos 			c = getc(fp);
    162  1.1  christos 		} while (!isspace(c) && c != EOF && --d > 0);
    163  1.1  christos 		*bp = '\0';
    164  1.1  christos 
    165  1.1  christos 		/* Eat trailing junk */
    166  1.1  christos 		if (c != '\n')
    167  1.1  christos 			(void)skip_line(fp);
    168  1.1  christos 
    169  1.1  christos 		return &e;
    170  1.1  christos 
    171  1.1  christos 	} while (c != EOF);
    172  1.1  christos 
    173  1.1  christos 	return (NULL);
    174  1.1  christos }
    175