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@ukrsat.com>
30 * Checker: Bart De Schuymer <bdschuym@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 #include <xtables.h>
46
47 #define MAXALIASES 35
48
49 static FILE *etherf = NULL;
50 static char line[BUFSIZ + 1];
51 static struct xt_ethertypeent et_ent;
52 static char *ethertype_aliases[MAXALIASES];
53 static int ethertype_stayopen;
54
setethertypeent(int f)55 static void setethertypeent(int f)
56 {
57 if (etherf == NULL)
58 etherf = fopen(XT_PATH_ETHERTYPES, "r");
59 else
60 rewind(etherf);
61 ethertype_stayopen |= f;
62 }
63
endethertypeent(void)64 static void endethertypeent(void)
65 {
66 if (etherf) {
67 fclose(etherf);
68 etherf = NULL;
69 }
70 ethertype_stayopen = 0;
71 }
72
73
getethertypeent(void)74 static struct xt_ethertypeent *getethertypeent(void)
75 {
76 char *e;
77 char *endptr;
78 register char *cp, **q;
79
80 if (etherf == NULL
81 && (etherf = fopen(XT_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 < ðertype_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
xtables_getethertypebyname(const char * name)130 struct xt_ethertypeent *xtables_getethertypebyname(const char *name)
131 {
132 register struct xt_ethertypeent *e;
133 register char **cp;
134
135 setethertypeent(ethertype_stayopen);
136 while ((e = getethertypeent()) != NULL) {
137 if (strcasecmp(e->e_name, name) == 0)
138 break;
139 for (cp = e->e_aliases; *cp != 0; cp++)
140 if (strcasecmp(*cp, name) == 0)
141 goto found;
142 }
143 found:
144 if (!ethertype_stayopen)
145 endethertypeent();
146 return (e);
147 }
148
xtables_getethertypebynumber(int type)149 struct xt_ethertypeent *xtables_getethertypebynumber(int type)
150 {
151 register struct xt_ethertypeent *e;
152
153 setethertypeent(ethertype_stayopen);
154 while ((e = getethertypeent()) != NULL)
155 if (e->e_ethertype == type)
156 break;
157 if (!ethertype_stayopen)
158 endethertypeent();
159 return (e);
160 }
161