Home | History | Annotate | Download | only in iptables
      1 /*
      2 * getethertype.c
      3 *
      4 * This file was part of the NYS Library.
      5 *
      6 ** The NYS Library is free software; you can redistribute it and/or
      7 ** modify it under the terms of the GNU Library General Public License as
      8 ** published by the Free Software Foundation; either version 2 of the
      9 ** License, or (at your option) any later version.
     10 *
     11 * This program is free software; you can redistribute it and/or modify
     12 * it under the terms of the GNU General Public License as published by
     13 * the Free Software Foundation; either version 2 of the License, or
     14 * (at your option) any later version.
     15 *
     16 * This program is distributed in the hope that it will be useful,
     17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 * GNU General Public License for more details.
     20 *
     21 * You should have received a copy of the GNU General Public License
     22 * along with this program; if not, write to the Free Software
     23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     24 */
     25 
     26 /********************************************************************
     27 * Description: Ethertype name service switch and the ethertypes
     28 * database access functions
     29 * Author: Nick Fedchik <fnm (at) ukrsat.com>
     30 * Checker: Bart De Schuymer <bdschuym (at) pandora.be>
     31 * Origin: uClibc-0.9.16/libc/inet/getproto.c
     32 * Created at: Mon Nov 11 12:20:11 EET 2002
     33 ********************************************************************/
     34 
     35 #include <ctype.h>
     36 #include <features.h>
     37 #include <sys/types.h>
     38 #include <sys/socket.h>
     39 #include <netdb.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <netinet/ether.h>
     44 #include <net/ethernet.h>
     45 
     46 #include <ebtables/ethernetdb.h>
     47 
     48 #define	MAXALIASES	35
     49 
     50 static FILE *etherf = NULL;
     51 static char line[BUFSIZ + 1];
     52 static struct ethertypeent et_ent;
     53 static char *ethertype_aliases[MAXALIASES];
     54 static int ethertype_stayopen;
     55 
     56 void setethertypeent(int f)
     57 {
     58 	if (etherf == NULL)
     59 		etherf = fopen(_PATH_ETHERTYPES, "r");
     60 	else
     61 		rewind(etherf);
     62 	ethertype_stayopen |= f;
     63 }
     64 
     65 void endethertypeent(void)
     66 {
     67 	if (etherf) {
     68 		fclose(etherf);
     69 		etherf = NULL;
     70 	}
     71 	ethertype_stayopen = 0;
     72 }
     73 
     74 struct ethertypeent *getethertypeent(void)
     75 {
     76 	char *e;
     77 	char *endptr;
     78 	register char *cp, **q;
     79 
     80 	if (etherf == NULL
     81 	    && (etherf = fopen(_PATH_ETHERTYPES, "r")) == NULL) {
     82 		return (NULL);
     83 	}
     84 
     85 again:
     86 	if ((e = fgets(line, BUFSIZ, etherf)) == NULL) {
     87 		return (NULL);
     88 	}
     89 	if (*e == '#')
     90 		goto again;
     91 	cp = strpbrk(e, "#\n");
     92 	if (cp == NULL)
     93 		goto again;
     94 	*cp = '\0';
     95 	et_ent.e_name = e;
     96 	cp = strpbrk(e, " \t");
     97 	if (cp == NULL)
     98 		goto again;
     99 	*cp++ = '\0';
    100 	while (*cp == ' ' || *cp == '\t')
    101 		cp++;
    102 	e = strpbrk(cp, " \t");
    103 	if (e != NULL)
    104 		*e++ = '\0';
    105 // Check point
    106 	et_ent.e_ethertype = strtol(cp, &endptr, 16);
    107 	if (*endptr != '\0'
    108 	    || (et_ent.e_ethertype < ETH_ZLEN
    109 		|| et_ent.e_ethertype > 0xFFFF))
    110 		goto again;	// Skip invalid etherproto type entry
    111 	q = et_ent.e_aliases = ethertype_aliases;
    112 	if (e != NULL) {
    113 		cp = e;
    114 		while (cp && *cp) {
    115 			if (*cp == ' ' || *cp == '\t') {
    116 				cp++;
    117 				continue;
    118 			}
    119 			if (q < &ethertype_aliases[MAXALIASES - 1])
    120 				*q++ = cp;
    121 			cp = strpbrk(cp, " \t");
    122 			if (cp != NULL)
    123 				*cp++ = '\0';
    124 		}
    125 	}
    126 	*q = NULL;
    127 	return (&et_ent);
    128 }
    129 
    130 
    131 struct ethertypeent *getethertypebyname(const char *name)
    132 {
    133 	register struct ethertypeent *e;
    134 	register char **cp;
    135 
    136 	setethertypeent(ethertype_stayopen);
    137 	while ((e = getethertypeent()) != NULL) {
    138 		if (strcasecmp(e->e_name, name) == 0)
    139 			break;
    140 		for (cp = e->e_aliases; *cp != 0; cp++)
    141 			if (strcasecmp(*cp, name) == 0)
    142 				goto found;
    143 	}
    144 found:
    145 	if (!ethertype_stayopen)
    146 		endethertypeent();
    147 	return (e);
    148 }
    149 
    150 struct ethertypeent *getethertypebynumber(int type)
    151 {
    152 	register struct ethertypeent *e;
    153 
    154 	setethertypeent(ethertype_stayopen);
    155 	while ((e = getethertypeent()) != NULL)
    156 		if (e->e_ethertype == type)
    157 			break;
    158 	if (!ethertype_stayopen)
    159 		endethertypeent();
    160 	return (e);
    161 }
    162