The VisAO Camera
getethertype.c
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 
36 #include <ctype.h>
37 #include <features.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <netinet/ether.h>
45 #include <net/ethernet.h>
46 
47 #include "ethernetdb.h"
48 
49 #define MAXALIASES 35
50 
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;
56 
57 void setethertypeent(int f)
58 {
59  if (etherf == NULL)
60  etherf = fopen(_PATH_ETHERTYPES, "r");
61  else
62  rewind(etherf);
63  ethertype_stayopen |= f;
64 }
65 
66 void endethertypeent(void)
67 {
68  if (etherf) {
69  fclose(etherf);
70  etherf = NULL;
71  }
72  ethertype_stayopen = 0;
73 }
74 
75 struct ethertypeent *getethertypeent(void)
76 {
77  char *e;
78  char *endptr;
79  register char *cp, **q;
80 
81  if (etherf == NULL
82  && (etherf = fopen(_PATH_ETHERTYPES, "r")) == NULL) {
83  return (NULL);
84  }
85 
86 again:
87  if ((e = fgets(line, BUFSIZ, etherf)) == NULL) {
88  return (NULL);
89  }
90  if (*e == '#')
91  goto again;
92  cp = strpbrk(e, "#\n");
93  if (cp == NULL)
94  goto again;
95  *cp = '\0';
96  et_ent.e_name = e;
97  cp = strpbrk(e, " \t");
98  if (cp == NULL)
99  goto again;
100  *cp++ = '\0';
101  while (*cp == ' ' || *cp == '\t')
102  cp++;
103  e = strpbrk(cp, " \t");
104  if (e != NULL)
105  *e++ = '\0';
106 // Check point
107  et_ent.e_ethertype = strtol(cp, &endptr, 16);
108  if (*endptr != '\0'
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;
113  if (e != NULL) {
114  cp = e;
115  while (cp && *cp) {
116  if (*cp == ' ' || *cp == '\t') {
117  cp++;
118  continue;
119  }
120  if (q < &ethertype_aliases[MAXALIASES - 1])
121  *q++ = cp;
122  cp = strpbrk(cp, " \t");
123  if (cp != NULL)
124  *cp++ = '\0';
125  }
126  }
127  *q = NULL;
128  return (&et_ent);
129 }
130 
131 
132 struct ethertypeent *getethertypebyname(const char *name)
133 {
134  register struct ethertypeent *e;
135  register char **cp;
136 
137  setethertypeent(ethertype_stayopen);
138  while ((e = getethertypeent()) != NULL) {
139  if (strcasecmp(e->e_name, name) == 0)
140  break;
141  for (cp = e->e_aliases; *cp != 0; cp++)
142  if (strcasecmp(*cp, name) == 0)
143  goto found;
144  }
145 found:
146  if (!ethertype_stayopen)
147  endethertypeent();
148  return (e);
149 }
150 
151 struct ethertypeent *getethertypebynumber(int type)
152 {
153  register struct ethertypeent *e;
154 
155  setethertypeent(ethertype_stayopen);
156  while ((e = getethertypeent()) != NULL)
157  if (e->e_ethertype == type)
158  break;
159  if (!ethertype_stayopen)
160  endethertypeent();
161  return (e);
162 }