4 * This file was part of the NYS Library.
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.
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.
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.
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
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 ********************************************************************/
38 #include <sys/types.h>
39 #include <sys/socket.h>
44 #include <netinet/ether.h>
45 #include <net/ethernet.h>
47 #include "ethernetdb.h"
51 static FILE *etherf
= NULL
;
52 static char line
[BUFSIZ
+ 1];
53 static struct ethertypeent et_ent
;
54 static char *ethertype_aliases
[MAXALIASES
];
55 static int ethertype_stayopen
;
57 void setethertypeent(int f
)
60 etherf
= fopen(_PATH_ETHERTYPES
, "r");
63 ethertype_stayopen
|= f
;
66 void endethertypeent(void)
72 ethertype_stayopen
= 0;
75 struct ethertypeent
*getethertypeent(void)
79 register char *cp
, **q
;
82 && (etherf
= fopen(_PATH_ETHERTYPES
, "r")) == NULL
) {
87 if ((e
= fgets(line
, BUFSIZ
, etherf
)) == NULL
) {
92 cp
= strpbrk(e
, "#\n");
97 cp
= strpbrk(e
, " \t");
101 while (*cp
== ' ' || *cp
== '\t')
103 e
= strpbrk(cp
, " \t");
107 et_ent
.e_ethertype
= strtol(cp
, &endptr
, 16);
109 || (et_ent
.e_ethertype
< ETH_ZLEN
110 || et_ent
.e_ethertype
> 0xFFFF))
111 goto again
; // Skip invalid etherproto type entry
112 q
= et_ent
.e_aliases
= ethertype_aliases
;
116 if (*cp
== ' ' || *cp
== '\t') {
120 if (q
< ðertype_aliases
[MAXALIASES
- 1])
122 cp
= strpbrk(cp
, " \t");
132 struct ethertypeent
*getethertypebyname(const char *name
)
134 register struct ethertypeent
*e
;
137 setethertypeent(ethertype_stayopen
);
138 while ((e
= getethertypeent()) != NULL
) {
139 if (strcasecmp(e
->e_name
, name
) == 0)
141 for (cp
= e
->e_aliases
; *cp
!= 0; cp
++)
142 if (strcasecmp(*cp
, name
) == 0)
146 if (!ethertype_stayopen
)
151 struct ethertypeent
*getethertypebynumber(int type
)
153 register struct ethertypeent
*e
;
155 setethertypeent(ethertype_stayopen
);
156 while ((e
= getethertypeent()) != NULL
)
157 if (e
->e_ethertype
== type
)
159 if (!ethertype_stayopen
)