File: getethertype.c

package info (click to toggle)
ebtables 2.0.11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,144 kB
  • sloc: ansic: 9,451; sh: 4,647; makefile: 61; perl: 45
file content (161 lines) | stat: -rw-r--r-- 3,919 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* getethertype.c
*
* This file was part of the NYS Library.
*
** The NYS Library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public License as
** published by the Free Software Foundation; either version 2 of the
** License, or (at your option) any later version.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/********************************************************************
* Description: Ethertype name service switch and the ethertypes 
* database access functions
* Author: Nick Fedchik <fnm@ukrsat.com>
* Checker: Bart De Schuymer <bdschuym@pandora.be>
* Origin: uClibc-0.9.16/libc/inet/getproto.c
* Created at: Mon Nov 11 12:20:11 EET 2002
********************************************************************/

#include <ctype.h>
#include <features.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ether.h>
#include <net/ethernet.h>

#include "ethernetdb.h"

#define	MAXALIASES	35

static FILE *etherf = NULL;
static char line[BUFSIZ + 1];
static struct ethertypeent et_ent;
static char *ethertype_aliases[MAXALIASES];
static int ethertype_stayopen;

void setethertypeent(int f)
{
	if (etherf == NULL)
		etherf = fopen(_PATH_ETHERTYPES, "r");
	else
		rewind(etherf);
	ethertype_stayopen |= f;
}

void endethertypeent(void)
{
	if (etherf) {
		fclose(etherf);
		etherf = NULL;
	}
	ethertype_stayopen = 0;
}

struct ethertypeent *getethertypeent(void)
{
	char *e;
	char *endptr;
	register char *cp, **q;

	if (etherf == NULL
	    && (etherf = fopen(_PATH_ETHERTYPES, "r")) == NULL) {
		return (NULL);
	}

again:
	if ((e = fgets(line, BUFSIZ, etherf)) == NULL) {
		return (NULL);
	}
	if (*e == '#')
		goto again;
	cp = strpbrk(e, "#\n");
	if (cp == NULL)
		goto again;
	*cp = '\0';
	et_ent.e_name = e;
	cp = strpbrk(e, " \t");
	if (cp == NULL)
		goto again;
	*cp++ = '\0';
	while (*cp == ' ' || *cp == '\t')
		cp++;
	e = strpbrk(cp, " \t");
	if (e != NULL)
		*e++ = '\0';
// Check point
	et_ent.e_ethertype = strtol(cp, &endptr, 16);
	if (*endptr != '\0'
	    || (et_ent.e_ethertype < ETH_ZLEN
		|| et_ent.e_ethertype > 0xFFFF))
		goto again;	// Skip invalid etherproto type entry
	q = et_ent.e_aliases = ethertype_aliases;
	if (e != NULL) {
		cp = e;
		while (cp && *cp) {
			if (*cp == ' ' || *cp == '\t') {
				cp++;
				continue;
			}
			if (q < &ethertype_aliases[MAXALIASES - 1])
				*q++ = cp;
			cp = strpbrk(cp, " \t");
			if (cp != NULL)
				*cp++ = '\0';
		}
	}
	*q = NULL;
	return (&et_ent);
}


struct ethertypeent *getethertypebyname(const char *name)
{
	register struct ethertypeent *e;
	register char **cp;

	setethertypeent(ethertype_stayopen);
	while ((e = getethertypeent()) != NULL) {
		if (strcasecmp(e->e_name, name) == 0)
			break;
		for (cp = e->e_aliases; *cp != 0; cp++)
			if (strcasecmp(*cp, name) == 0)
				goto found;
	}
found:
	if (!ethertype_stayopen)
		endethertypeent();
	return (e);
}

struct ethertypeent *getethertypebynumber(int type)
{
	register struct ethertypeent *e;

	setethertypeent(ethertype_stayopen);
	while ((e = getethertypeent()) != NULL)
		if (e->e_ethertype == type)
			break;
	if (!ethertype_stayopen)
		endethertypeent();
	return (e);
}