Watt-32 tcp/ip  2.2 dev-rel.10
getprot.c
Go to the documentation of this file.
1 
6 /* Copyright (c) 1997-2002 Gisle Vanem <gvanem@yahoo.no>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  * must display the following acknowledgement:
18  * This product includes software developed by Gisle Vanem
19  * Bergen, Norway.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ME (Gisle Vanem) AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL I OR CONTRIBUTORS BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * 21.aug 1996 - Created
34  */
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 
41 #include "wattcp.h"
42 #include "strings.h"
43 #include "misc.h"
44 #include "run.h"
45 #include "language.h"
46 #include "pcconfig.h"
47 #include "get_xby.h"
48 
49 #if defined(USE_BSD_API)
50 
51 static struct _protoent *proto0 = NULL;
52 static char *protoFname = NULL;
53 static FILE *protoFile = NULL;
54 static BOOL protoClose = FALSE;
55 
56 static __inline struct protoent *fill_protoent (const struct _protoent *p)
57 {
58  static struct protoent ret;
59  static char name [30];
60  static char *aliases [MAX_PROTO_ALIASES+1];
61 
62  memcpy (&aliases, p->p_aliases, sizeof(aliases));
63  ret.p_name = _strlcpy (name, p->p_name, sizeof(name));
64  ret.p_proto = p->p_proto;
65  ret.p_aliases = aliases;
66  h_errno = NETDB_SUCCESS;
67  return (&ret);
68 }
69 
70 static void _endprotoent (void)
71 {
72  endprotoent();
73 }
74 
75 void W32_CALL ReadProtoFile (const char *fname)
76 {
77  static BOOL been_here = FALSE;
78 
79  if (!fname || !*fname)
80  return;
81 
82  if (been_here) /* loading multiple protocol files */
83  {
84  free (protoFname);
85  FCLOSE (protoFile);
86  protoFile = NULL;
87  }
88 
89  protoFname = strdup (fname);
90  if (!protoFname)
91  return;
92 
93  setprotoent (1);
94  if (!protoFile)
95  return;
96 
97  been_here = TRUE;
98 
99  while (1)
100  {
101  struct protoent *p = getprotoent();
102  struct _protoent *p2;
103  int i;
104 
105  if (!p)
106  break;
107 
108  p2 = (struct _protoent*) calloc (sizeof(*p2), 1);
109  if (!p2)
110  {
111  outs (protoFname);
112  outsnl (_LANG(" too big!"));
113  break;
114  }
115  for (i = 0; p->p_aliases[i]; i++)
116  p2->p_aliases[i] = strdup (p->p_aliases[i]);
117  p2->p_proto = p->p_proto;
118  p2->p_name = strdup (p->p_name);
119 
120  if (!p2->p_name)
121  break;
122 
123  p2->p_next = proto0;
124  proto0 = p2;
125  }
126  rewind (protoFile);
127  RUNDOWN_ADD (_endprotoent, 257);
128 
129 #if 0 /* test */
130  {
131  const struct _protoent *p;
132  int i;
133 
134  printf ("\n%s entries:\n", protoFname);
135  for (p = proto0; p; p = p->p_next)
136  {
137  printf ("proto %3d name %-10.10s Aliases:", p->p_proto, p->p_name);
138  for (i = 0; p->p_aliases[i]; i++)
139  printf (" %s,", p->p_aliases[i]);
140  puts ("");
141  }
142  fflush (stdout);
143  }
144 #endif
145 }
146 
147 /*------------------------------------------------------------------*/
148 
149 const char * W32_CALL GetProtoFile (void)
150 {
151  return (protoFname);
152 }
153 
154 void W32_CALL CloseProtoFile (void)
155 {
156  FCLOSE (protoFile);
157  protoFile = NULL;
158 }
159 
160 void W32_CALL ReopenProtoFile (void)
161 {
162  ReadProtoFile (protoFname);
163 }
164 
165 /*------------------------------------------------------------------*/
166 
167 struct protoent * W32_CALL getprotoent (void)
168 {
169  static struct _protoent p;
170  char *proto, *name;
171 
172  if (!netdb_init() || !protoFile)
173  {
174  h_errno = NO_DATA;
175  return (NULL);
176  }
177 
178  /* Protocol Name
179  * ----------------------------------------
180  * 0 reserved
181  * 1 icmp, internet control message
182  * 2 igmp, internet group management
183  * 3 ggp, # gateway-gateway protocol
184  */
185 
186  while (1)
187  {
188  char buf [2*MAX_NAMELEN], *tok, *tok_buf = NULL;
189 
190  if (!fgets(buf,sizeof(buf),protoFile))
191  {
192  h_errno = NO_DATA;
193  return (NULL);
194  }
195 
196  tok = strltrim (buf);
197  if (*tok == '#' || *tok == ';' || *tok == '\n')
198  continue;
199 
200  proto = strtok_r (tok, " \t", &tok_buf);
201  name = strtok_r (NULL, " ,\t\n", &tok_buf);
202  if (proto && name)
203  break;
204  }
205 
206  if (protoClose)
207  endprotoent();
208 
209  memset (&p, 0, sizeof(p));
210  p.p_proto = atoi (proto);
211  p.p_name = name;
212  return fill_protoent (&p);
213 }
214 
215 /*------------------------------------------------------------------*/
216 
217 struct protoent * W32_CALL getprotobyname (const char *proto)
218 {
219  struct _protoent fixed;
220  const struct _protoent *p;
221  int i;
222 
223  memset (&fixed, 0, sizeof(fixed));
224 
225  if (!netdb_init() || !proto)
226  {
227  h_errno = NO_DATA;
228  return (NULL);
229  }
230 
231  /* No chances UDP/TCP protocols are renumbered
232  */
233  if (!stricmp(proto,"udp"))
234  {
235  fixed.p_name = (char*) "udp";
236  fixed.p_proto = UDP_PROTO;
237  return fill_protoent (&fixed);
238  }
239 
240  if (!stricmp(proto,"tcp"))
241  {
242  fixed.p_name = (char*) "tcp";
243  fixed.p_proto = TCP_PROTO;
244  return fill_protoent (&fixed);
245  }
246 
247  for (p = proto0; p; p = p->p_next)
248  {
249  if (p->p_name && !stricmp(p->p_name,proto))
250  return fill_protoent (p);
251 
252  /* aliases not supported (no need)
253  */
254  for (i = 0; p->p_aliases[i] && i < DIM(p->p_aliases); i++)
255  if (!stricmp(proto,p->p_aliases[i]))
256  return fill_protoent (p);
257  }
258  h_errno = NO_DATA;
259  return (NULL);
260 }
261 
262 /*------------------------------------------------------------------*/
263 
264 struct protoent * W32_CALL getprotobynumber (int proto)
265 {
266  const struct _protoent *p;
267 
268  if (!netdb_init())
269  {
270  h_errno = NO_DATA;
271  return (NULL);
272  }
273 
274  for (p = proto0; p; p = p->p_next)
275  if (p->p_proto == proto)
276  return fill_protoent (p);
277  h_errno = NO_DATA;
278  return (NULL);
279 }
280 
281 /*------------------------------------------------------------------*/
282 
283 void W32_CALL setprotoent (int stayopen)
284 {
285  protoClose = (stayopen == 0);
286 
287  if (!netdb_init() || !protoFname)
288  return;
289 
290  if (!protoFile)
291  FOPEN_TXT (protoFile, protoFname);
292  else rewind (protoFile);
293 }
294 
295 /*------------------------------------------------------------------*/
296 
297 void W32_CALL endprotoent (void)
298 {
299  struct _protoent *p, *next;
300 
301  if (_watt_fatal_error)
302  return;
303 
304  if (protoFname)
305  free (protoFname);
306  if (protoFile)
307  FCLOSE (protoFile);
308  protoFname = NULL;
309  protoFile = NULL;
310 
311  for (p = proto0; p; p = next)
312  {
313  int i;
314  for (i = 0; p->p_aliases[i] && i < DIM(p->p_aliases); i++)
315  free (p->p_aliases[i]);
316  next = p->p_next;
317  free (p->p_name);
318  free (p);
319  }
320  proto0 = NULL;
321  protoClose = TRUE;
322 }
323 #endif /* USE_BSD_API */
char * strltrim(const char *s)
Return pointer to first non-blank (space/tab) in a string.
Definition: strings.c:243
Core definitions.
char * _strlcpy(char *dst, const char *src, size_t len)
Similar to strncpy(), but always returns 'dst' with 0-termination.
Definition: strings.c:226
BOOL _watt_fatal_error
Definition: misc.c:60
int netdb_init(void)
Called from most <netdb.h> functions in case watt_sock_init() wasn't called first to initialise things...
Definition: pcconfig.c:1215