2 * cheap and dirty network database lookup functions
5 * uses local files only
8 * currently searches the protocols only
11 #include <net-snmp/net-snmp-config.h>
13 #if (defined(WIN32) || defined(cygwin) || defined(aix4))
16 #define _NO_PROTO /* Hack, you say ? */
20 #include <sys/types.h>
31 #include <netinet/in.h>
37 static int h_stay_open, s_stay_open, p_stay_open, n_stay_open;
38 static FILE *h_fp, *s_fp, *p_fp, *n_fp;
39 static char *h_fn, *s_fn, *p_fn, *n_fn;
42 #define ROOT_BASE "/etc/"
43 #define PROT_FN "protocols"
45 #define ROOT_BASE "\\SYSTEM32\\DRIVERS\\ETC\\"
46 #define PROT_FN "protocol"
48 #define HOST_FN "hosts"
49 #define SERV_FN "services"
50 #define NETW_FN "networks"
52 static int pre_env_done = 0;
64 cp = getenv("SYSTEMROOT");
68 * printf ("Root is '%s'\n", cp);
76 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(PROT_FN));
78 sprintf(p_fn, "%s%s%s", cp, cproot, PROT_FN);
81 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(HOST_FN));
83 sprintf(h_fn, "%s%s%s", cp, cproot, HOST_FN);
85 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(SERV_FN));
87 sprintf(s_fn, "%s%s%s", cp, cproot, SERV_FN);
89 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(NETW_FN));
91 sprintf(n_fn, "%s%s%s", cp, cproot, NETW_FN);
96 * sets can open. ends must close.
131 sethostent(int stay_open)
135 h_stay_open = stay_open;
139 setservent(int stay_open)
143 s_stay_open = stay_open;
147 setprotoent(int stay_open)
151 p_stay_open = stay_open;
155 setnetent(int stay_open)
159 n_stay_open = stay_open;
162 #define STRTOK_DELIMS " \t\n"
165 * get next entry from data base file, or from NIS if possible.
168 * returns 0 if there are no more entries to read.
184 char *cp, **alp, lbuf[256];
185 static struct protoent spx;
186 static char *ali[10];
187 struct protoent *px = &spx;
190 for (alp = ali; *alp; free(*alp), *alp = 0, alp++);
198 p_fp = fopen(p_fn, "r");
202 while (fgets(lbuf, sizeof(lbuf), p_fp)) {
208 cp = strtok(lbuf, STRTOK_DELIMS);
212 px->p_name = strdup(cp);
214 cp = strtok(NULL, STRTOK_DELIMS);
219 px->p_proto = (short) atoi(cp);
221 for (alp = px->p_aliases; cp; alp++) {
222 cp = strtok(NULL, STRTOK_DELIMS);
242 getnetbyaddr(long net, int type)
248 * Return the network number from an internet address
251 inet_netof(struct in_addr in)
253 register u_long i = ntohl(in.s_addr);
254 u_long ii = (i >> 24) & 0xff;
255 if (0 == (ii & 0x80))
256 return ((0xff000000 & i) >> 24);
257 if (0x80 == (ii & 0xc0))
258 return ((0xffff0000 & i) >> 16);
260 * if (0xc0 == (ii & 0xe0))
262 return ((0xffffff00 & i) >> 8);
266 * Return the host number from an internet address
269 inet_lnaof(struct in_addr in)
271 register u_long i = ntohl(in.s_addr);
272 u_long ii = (i >> 24) & 0xff;
273 if (0 == (ii & 0x80))
274 return (0x00ffffff & i);
275 if (0x80 == (ii & 0xc0))
276 return (0x0000ffff & i);
278 * if (0xc0 == (ii & 0xe0))
280 return (0x000000ff & i);
283 #endif /* WIN32 or cygwin */