Watt-32 tcp/ip  2.2 dev-rel.10
getnet.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  * 18.aug 1996 - Created
34  */
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <arpa/inet.h>
40 
41 #include "wattcp.h"
42 #include "misc.h"
43 #include "run.h"
44 #include "pcconfig.h"
45 #include "language.h"
46 #include "strings.h"
47 #include "get_xby.h"
48 
49 #if defined(USE_BSD_API)
50 
51 static struct _netent *network0 = NULL;
52 static char *networkFname = NULL;
53 static FILE *networkFile = NULL;
54 static BOOL networkClose = FALSE;
55 
56 static __inline struct netent *fill_netent (const struct _netent *n)
57 {
58  static struct netent ret;
59  static char name [MAX_NAMELEN];
60  static char *aliases [MAX_NETENT_ALIASES+1];
61 
62  memcpy (&aliases, n->n_aliases, sizeof(aliases));
63  ret.n_name = _strlcpy (name, n->n_name, sizeof(name));
64  ret.n_aliases = aliases;
65  ret.n_net = n->n_net;
66  ret.n_addrtype = AF_INET;
67  return (&ret);
68 }
69 
70 static void _endnetent (void)
71 {
72  endnetent();
73 }
74 
75 void W32_CALL ReadNetworksFile (const char *fname)
76 {
77  static BOOL been_here = FALSE;
78 
79  if (!fname || !*fname)
80  return;
81 
82  if (been_here) /* loading multiple network files */
83  {
84  free (networkFname);
85  FCLOSE (networkFile);
86  networkFile = NULL;
87  }
88 
89  networkFname = strdup (fname);
90  if (!networkFname)
91  return;
92 
93  setnetent (1);
94  if (!networkFile)
95  return;
96 
97  been_here = TRUE;
98 
99  while (1)
100  {
101  struct netent *n = getnetent();
102  struct _netent *n2;
103  int i;
104 
105  if (!n)
106  break;
107 
108  n2 = (struct _netent*) calloc (sizeof(*n2), 1);
109  if (!n2)
110  {
111  outs (networkFname);
112  outsnl (_LANG(" too big!"));
113  break;
114  }
115 
116  for (i = 0; n->n_aliases[i]; i++)
117  n2->n_aliases[i] = strdup (n->n_aliases[i]);
118  n2->n_net = n->n_net;
119  n2->n_addrtype = n->n_addrtype;
120  n2->n_name = strdup (n->n_name);
121  if (!n2->n_name)
122  break;
123  n2->n_next = network0;
124  network0 = n2;
125  }
126  rewind (networkFile);
127  RUNDOWN_ADD (_endnetent, 251);
128 
129 #if 0 /* test */
130  {
131  struct _netent *n;
132 
133  printf ("\n%s entries:\n", networkFname);
134 
135  for (n = network0; n; n = n->n_next)
136  {
137  int i;
138  printf ("net %-15.15s name %-10.10s Aliases:",
139  inet_ntoa(inet_makeaddr(n->n_net,0)), n->n_name);
140  for (i = 0; n->n_aliases[i]; i++)
141  printf (" %s,", n->n_aliases[i]);
142  puts ("");
143  }
144  fflush (stdout);
145  }
146 #endif
147 }
148 
149 /*
150  * Return name of networks file
151  */
152 const char * W32_CALL GetNetFile (void)
153 {
154  return (networkFname);
155 }
156 
157 /*
158  * To prevent running out of file-handles, one should close the
159  * 'networks' file before spawning a new shell.
160  */
161 void W32_CALL CloseNetworksFile (void)
162 {
163  FCLOSE (networkFile);
164  networkFile = NULL;
165 }
166 
167 void W32_CALL ReopenNetworksFile (void)
168 {
169  ReadNetworksFile (networkFname);
170 }
171 
172 /*
173  * Return the next (non-commented) line from the network-file
174  * Format is:
175  * name [=] net [alias..] {\n | # ..}
176  *
177  * e.g.
178  * loopback 127
179  * arpanet 10 arpa
180  */
181 struct netent * W32_CALL getnetent (void)
182 {
183  struct _netent n;
184  char *name, *net, *alias, *tok_buf = NULL;
185  char buf [2*MAX_NAMELEN], *tok;
186  int i;
187 
188  if (!netdb_init() || !networkFile)
189  return (NULL);
190 
191  while (1)
192  {
193  if (!fgets(buf,sizeof(buf),networkFile))
194  return (NULL);
195 
196  tok = strltrim (buf);
197  if (*tok == '#' || *tok == ';' || *tok == '\n')
198  continue;
199 
200  name = strtok_r (tok, " \t", &tok_buf);
201  net = strtok_r (NULL, "= \t\n", &tok_buf);
202  if (name && net)
203  break;
204  }
205 
206  if (networkClose)
207  endnetent();
208 
209  memset (&n, 0, sizeof(n));
210  n.n_net = inet_network (net);
211  n.n_name = name;
212  alias = strtok_r (NULL, " \t\n", &tok_buf);
213 
214  for (i = 0; alias && i < MAX_NETENT_ALIASES; i++)
215  {
216  static char aliases [MAX_NETENT_ALIASES][MAX_NAMELEN];
217 
218  if (*alias == '#' || *alias == ';')
219  break;
220 
221  n.n_aliases[i] = _strlcpy (aliases[i], alias, sizeof(aliases[i]));
222  alias = strtok_r (NULL, " \t\n", &tok_buf);
223  }
224  return fill_netent (&n);
225 }
226 
227 /*
228  * Return a 'netent' structure for network 'name' or
229  * NULL if not found.
230  */
231 struct netent * W32_CALL getnetbyname (const char *name)
232 {
233  const struct _netent *n;
234 
235  if (!netdb_init() || !name)
236  return (NULL);
237 
238  for (n = network0; n; n = n->n_next)
239  {
240  int i;
241 
242  if (n->n_name && !stricmp(n->n_name,name))
243  return fill_netent (n);
244 
245  for (i = 0; n->n_aliases[i]; i++)
246  if (!stricmp(name,n->n_aliases[i]))
247  return fill_netent (n);
248  }
249  return (NULL);
250 }
251 
252 /*
253  * Return a 'netent' structure for network number 'net' or
254  * NULL if not found.
255  */
256 struct netent * W32_CALL getnetbyaddr (long net, int type)
257 {
258  const struct _netent *n;
259 
260  if (!netdb_init())
261  return (NULL);
262 
263  for (n = network0; n; n = n->n_next)
264  if ((u_long)net == n->n_net && type == n->n_addrtype && n->n_name)
265  return fill_netent (n);
266  return (NULL);
267 }
268 
269 /*
270  * Open the network file.
271  */
272 void W32_CALL setnetent (int stayopen)
273 {
274  networkClose = (stayopen == 0);
275 
276  if (!netdb_init() || !networkFname)
277  return;
278 
279  if (!networkFile)
280  FOPEN_TXT (networkFile, networkFname);
281  else rewind (networkFile);
282 }
283 
284 /*
285  * Close the network file and free associated memory
286  */
287 void W32_CALL endnetent (void)
288 {
289  struct _netent *n, *next;
290 
291  if (_watt_fatal_error)
292  return;
293 
294  if (networkFname)
295  free (networkFname);
296  if (networkFile)
297  FCLOSE (networkFile);
298  networkFname = NULL;
299  networkFile = NULL;
300 
301  for (n = network0; n; n = next)
302  {
303  int i;
304  for (i = 0; n->n_aliases[i]; i++)
305  free (n->n_aliases[i]);
306  next = n->n_next;
307  free (n->n_name);
308  free (n);
309  }
310  network0 = NULL;
311  networkClose = TRUE;
312 }
313 #endif /* USE_BSD_API */
Definition: netdb.h:115
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
struct in_addr W32_CALL inet_makeaddr(u_long net, u_long host)
Formulate an Internet address from network + host (with subnet address).
Definition: netaddr.c:377
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