Blame libxtables/getethertype.c

Packit 7b22a4
/*
Packit 7b22a4
* getethertype.c
Packit 7b22a4
*
Packit 7b22a4
* This file was part of the NYS Library.
Packit 7b22a4
*
Packit 7b22a4
** The NYS Library is free software; you can redistribute it and/or
Packit 7b22a4
** modify it under the terms of the GNU Library General Public License as
Packit 7b22a4
** published by the Free Software Foundation; either version 2 of the
Packit 7b22a4
** License, or (at your option) any later version.
Packit 7b22a4
*
Packit 7b22a4
* This program is free software; you can redistribute it and/or modify
Packit 7b22a4
* it under the terms of the GNU General Public License as published by
Packit 7b22a4
* the Free Software Foundation; either version 2 of the License, or
Packit 7b22a4
* (at your option) any later version.
Packit 7b22a4
*
Packit 7b22a4
* This program is distributed in the hope that it will be useful,
Packit 7b22a4
* but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7b22a4
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7b22a4
* GNU General Public License for more details.
Packit 7b22a4
*
Packit 7b22a4
* You should have received a copy of the GNU General Public License
Packit 7b22a4
* along with this program; if not, write to the Free Software
Packit 7b22a4
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 7b22a4
*/
Packit 7b22a4
Packit 7b22a4
/********************************************************************
Packit 7b22a4
* Description: Ethertype name service switch and the ethertypes 
Packit 7b22a4
* database access functions
Packit 7b22a4
* Author: Nick Fedchik <fnm@ukrsat.com>
Packit 7b22a4
* Checker: Bart De Schuymer <bdschuym@pandora.be>
Packit 7b22a4
* Origin: uClibc-0.9.16/libc/inet/getproto.c
Packit 7b22a4
* Created at: Mon Nov 11 12:20:11 EET 2002
Packit 7b22a4
********************************************************************/
Packit 7b22a4
Packit 7b22a4
#include <ctype.h>
Packit 7b22a4
#include <features.h>
Packit 7b22a4
#include <sys/types.h>
Packit 7b22a4
#include <sys/socket.h>
Packit 7b22a4
#include <netdb.h>
Packit 7b22a4
#include <stdio.h>
Packit 7b22a4
#include <stdlib.h>
Packit 7b22a4
#include <string.h>
Packit 7b22a4
#include <netinet/ether.h>
Packit 7b22a4
#include <net/ethernet.h>
Packit 7b22a4
#include <xtables.h>
Packit 7b22a4
Packit 7b22a4
#define	MAXALIASES	35
Packit 7b22a4
Packit 7b22a4
static FILE *etherf = NULL;
Packit 7b22a4
static char line[BUFSIZ + 1];
Packit 7b22a4
static struct xt_ethertypeent et_ent;
Packit 7b22a4
static char *ethertype_aliases[MAXALIASES];
Packit 7b22a4
static int ethertype_stayopen;
Packit 7b22a4
Packit 7b22a4
static void setethertypeent(int f)
Packit 7b22a4
{
Packit 7b22a4
	if (etherf == NULL)
Packit 7b22a4
		etherf = fopen(XT_PATH_ETHERTYPES, "r");
Packit 7b22a4
	else
Packit 7b22a4
		rewind(etherf);
Packit 7b22a4
	ethertype_stayopen |= f;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static void endethertypeent(void)
Packit 7b22a4
{
Packit 7b22a4
	if (etherf) {
Packit 7b22a4
		fclose(etherf);
Packit 7b22a4
		etherf = NULL;
Packit 7b22a4
	}
Packit 7b22a4
	ethertype_stayopen = 0;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
Packit 7b22a4
static struct xt_ethertypeent *getethertypeent(void)
Packit 7b22a4
{
Packit 7b22a4
	char *e;
Packit 7b22a4
	char *endptr;
Packit 7b22a4
	register char *cp, **q;
Packit 7b22a4
Packit 7b22a4
	if (etherf == NULL
Packit 7b22a4
	    && (etherf = fopen(XT_PATH_ETHERTYPES, "r")) == NULL) {
Packit 7b22a4
		return (NULL);
Packit 7b22a4
	}
Packit 7b22a4
Packit 7b22a4
again:
Packit 7b22a4
	if ((e = fgets(line, BUFSIZ, etherf)) == NULL) {
Packit 7b22a4
		return (NULL);
Packit 7b22a4
	}
Packit 7b22a4
	if (*e == '#')
Packit 7b22a4
		goto again;
Packit 7b22a4
	cp = strpbrk(e, "#\n");
Packit 7b22a4
	if (cp == NULL)
Packit 7b22a4
		goto again;
Packit 7b22a4
	*cp = '\0';
Packit 7b22a4
	et_ent.e_name = e;
Packit 7b22a4
	cp = strpbrk(e, " \t");
Packit 7b22a4
	if (cp == NULL)
Packit 7b22a4
		goto again;
Packit 7b22a4
	*cp++ = '\0';
Packit 7b22a4
	while (*cp == ' ' || *cp == '\t')
Packit 7b22a4
		cp++;
Packit 7b22a4
	e = strpbrk(cp, " \t");
Packit 7b22a4
	if (e != NULL)
Packit 7b22a4
		*e++ = '\0';
Packit 7b22a4
// Check point
Packit 7b22a4
	et_ent.e_ethertype = strtol(cp, &endptr, 16);
Packit 7b22a4
	if (*endptr != '\0'
Packit 7b22a4
	    || (et_ent.e_ethertype < ETH_ZLEN
Packit 7b22a4
		|| et_ent.e_ethertype > 0xFFFF))
Packit 7b22a4
		goto again;	// Skip invalid etherproto type entry
Packit 7b22a4
	q = et_ent.e_aliases = ethertype_aliases;
Packit 7b22a4
	if (e != NULL) {
Packit 7b22a4
		cp = e;
Packit 7b22a4
		while (cp && *cp) {
Packit 7b22a4
			if (*cp == ' ' || *cp == '\t') {
Packit 7b22a4
				cp++;
Packit 7b22a4
				continue;
Packit 7b22a4
			}
Packit 7b22a4
			if (q < &ethertype_aliases[MAXALIASES - 1])
Packit 7b22a4
				*q++ = cp;
Packit 7b22a4
			cp = strpbrk(cp, " \t");
Packit 7b22a4
			if (cp != NULL)
Packit 7b22a4
				*cp++ = '\0';
Packit 7b22a4
		}
Packit 7b22a4
	}
Packit 7b22a4
	*q = NULL;
Packit 7b22a4
	return (&et_ent);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
struct xt_ethertypeent *xtables_getethertypebyname(const char *name)
Packit 7b22a4
{
Packit 7b22a4
	register struct xt_ethertypeent *e;
Packit 7b22a4
	register char **cp;
Packit 7b22a4
Packit 7b22a4
	setethertypeent(ethertype_stayopen);
Packit 7b22a4
	while ((e = getethertypeent()) != NULL) {
Packit 7b22a4
		if (strcasecmp(e->e_name, name) == 0)
Packit 7b22a4
			break;
Packit 7b22a4
		for (cp = e->e_aliases; *cp != 0; cp++)
Packit 7b22a4
			if (strcasecmp(*cp, name) == 0)
Packit 7b22a4
				goto found;
Packit 7b22a4
	}
Packit 7b22a4
found:
Packit 7b22a4
	if (!ethertype_stayopen)
Packit 7b22a4
		endethertypeent();
Packit 7b22a4
	return (e);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
struct xt_ethertypeent *xtables_getethertypebynumber(int type)
Packit 7b22a4
{
Packit 7b22a4
	register struct xt_ethertypeent *e;
Packit 7b22a4
Packit 7b22a4
	setethertypeent(ethertype_stayopen);
Packit 7b22a4
	while ((e = getethertypeent()) != NULL)
Packit 7b22a4
		if (e->e_ethertype == type)
Packit 7b22a4
			break;
Packit 7b22a4
	if (!ethertype_stayopen)
Packit 7b22a4
		endethertypeent();
Packit 7b22a4
	return (e);
Packit 7b22a4
}