Main Page Modules Data Structures File List Data Fields Globals Related Pages

# etherent.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 1990, 1993, 1994, 1995, 1996
    00003  *  The Regents of the University of California.  All rights reserved.
    00004  *
    00005  * Redistribution and use in source and binary forms, with or without
    00006  * modification, are permitted provided that: (1) source code distributions
    00007  * retain the above copyright notice and this paragraph in its entirety, (2)
    00008  * distributions including binary code include the above copyright notice and
    00009  * this paragraph in its entirety in the documentation or other materials
    00010  * provided with the distribution, and (3) all advertising materials mentioning
    00011  * features or use of this software display the following acknowledgement:
    00012  * ``This product includes software developed by the University of California,
    00013  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
    00014  * the University nor the names of its contributors may be used to endorse
    00015  * or promote products derived from this software without specific prior
    00016  * written permission.
    00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
    00018  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
    00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00020  */
    00021
    00022 #ifndef lint
    00023 static const char rcsid[] =
    00024     "@(#) $Header: /tcpdump/master/libpcap/etherent.c,v 1.21 2000/07/11 00:37:04 assar Exp $ (LBL)";
    00025 #endif
    00026
    00027 #ifdef HAVE_CONFIG_H
    00028 #include "config.h"
    00029 #endif
    00030
    00031 #include <sys/types.h>
    00032
    00033 #include <ctype.h>
    00034 #include <memory.h>
    00035 #include <stdio.h>
    00036 #include <string.h>
    00037
    00038 #include "pcap-int.h"
    00039
    00040 #include <pcap-namedb.h>
    00041
    00042 #ifdef HAVE_OS_PROTO_H
    00043 #include "os-proto.h"
    00044 #endif
    00045
    00046 static inline int xdtoi(int);
    00047 static inline int skip_space(FILE *);
    00048 static inline int skip_line(FILE *);
    00049
    00050 /* Hex digit to integer. */
    00051 static inline int
    00052 xdtoi(c)
    00053     register int c;
    00054 {
    00055     if (isdigit(c))
    00056         return c - '0';
    00057     else if (islower(c))
    00058         return c - 'a' + 10;
    00059     else
    00060         return c - 'A' + 10;
    00061 }
    00062
    00063 static inline int
    00064 skip_space(f)
    00065     FILE *f;
    00066 {
    00067     int c;
    00068
    00069     do {
    00070         c = getc(f);
    00071     } while (isspace(c) && c != '\n');
    00072
    00073     return c;
    00074 }
    00075
    00076 static inline int
    00077 skip_line(f)
    00078     FILE *f;
    00079 {
    00080     int c;
    00081
    00082     do
    00083         c = getc(f);
    00084     while (c != '\n' && c != EOF);
    00085
    00086     return c;
    00087 }
    00088
    00089 struct pcap_etherent *
    00090 pcap_next_etherent(FILE *fp)
    00091 {
    00092     register int c, d, i;
    00093     char *bp;
    00094     static struct pcap_etherent e;
    00095
    00096     memset((char *)&e, 0, sizeof(e));
    00097     do {
    00098         /* Find addr */
    00099         c = skip_space(fp);
    00100         if (c == '\n')
    00101             continue;
    00102
    00103         /* If this is a comment, or first thing on line
    00104            cannot be etehrnet address, skip the line. */
    00105         if (!isxdigit(c)) {
    00106             c = skip_line(fp);
    00107             continue;
    00108         }
    00109
    00110         /* must be the start of an address */
    00111         for (i = 0; i < 6; i += 1) {
    00112             d = xdtoi(c);
    00113             c = getc(fp);
    00114             if (isxdigit(c)) {
    00115                 d <<= 4;
    00116                 d |= xdtoi(c);
    00117                 c = getc(fp);
    00118             }
    00119             e.addr[i] = d;
    00120             if (c != ':')
    00121                 break;
    00122             c = getc(fp);
    00123         }
    00124         if (c == EOF)
    00125             break;
    00126
    00127         /* Must be whitespace */
    00128         if (!isspace(c)) {
    00129             c = skip_line(fp);
    00130             continue;
    00131         }
    00132         c = skip_space(fp);
    00133
    00134         /* hit end of line... */
    00135         if (c == '\n')
    00136             continue;
    00137
    00138         if (c == '#') {
    00139             c = skip_line(fp);
    00140             continue;
    00141         }
    00142
    00143         /* pick up name */
    00144         bp = e.name;
    00145         /* Use 'd' to prevent buffer overflow. */
    00146         d = sizeof(e.name) - 1;
    00147         do {
    00148             *bp++ = c;
    00149             c = getc(fp);
    00150         } while (!isspace(c) && c != EOF && --d > 0);
    00151         *bp = '\0';
    00152
    00153         /* Eat trailing junk */
    00154         if (c != '\n')
    00155             (void)skip_line(fp);
    00156
    00157         return &e;
    00158
    00159     } while (c != EOF);
    00160
    00161     return (NULL);
    00162 }

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.