Debian Codegraph @

apps/snmpnetstat/winstub.c

raw
1 /*
2 * cheap and dirty network database lookup functions
3 */
4 /*
5 * uses local files only
6 */
7 /*
8 * currently searches the protocols only
9 */
10
11 #include <net-snmp/net-snmp-config.h>
12 #include <net-snmp/net-snmp-includes.h>
13
14 #if (defined(WIN32) || defined(cygwin) || defined(aix4))
15
16 #ifdef aix4
17 #define _NO_PROTO /* Hack, you say ? */
18 #endif
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #ifdef HAVE_STDLIB_H
23 #include <stdlib.h>
24 #endif
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
30 #endif
31 #ifdef HAVE_NETDB_H
32 #ifdef cygwin
33 #define getnetent cygwin_getnetent
34 #define getnetbyaddr cygwin_getnetbyaddr
35 #endif
36 #include <netdb.h>
37 #ifdef cygwin
38 #undef getnetent
39 #undef getnetbyaddr
40 #endif
41 #endif
42
43 static int h_stay_open, s_stay_open, p_stay_open, n_stay_open;
44 static FILE *h_fp, *s_fp, *p_fp, *n_fp;
45 static char *p_fn;
46 #ifdef notused
47 static char *h_fn, *s_fn, *n_fn;
48 #endif
49
50 #ifdef aix4
51 #define ROOT_BASE "/etc/"
52 #define PROT_FN "protocols"
53 #else
54 #define ROOT_BASE "\\SYSTEM32\\DRIVERS\\ETC\\"
55 #define PROT_FN "protocol"
56 #endif
57 #define HOST_FN "hosts"
58 #define SERV_FN "services"
59 #define NETW_FN "networks"
60
61 static int pre_env_done = 0;
62 static void
63 pre_env(void)
64 {
65 const char *cproot;
66 const char *cp = "";
67
68 if (pre_env_done)
69 return;
70 pre_env_done = 1;
71
72 #ifndef aix4
73 cp = getenv("SYSTEMROOT");
74 if (cp) {
75 ;
76 /*
77 * printf ("Root is '%s'\n", cp);
78 */
79 } else
80 cp = "C:\\WINNT";
81 #endif
82
83 cproot = ROOT_BASE;
84 p_fn =
85 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(PROT_FN));
86 if (p_fn)
87 sprintf(p_fn, "%s%s%s", cp, cproot, PROT_FN);
88 #ifdef notused
89 h_fn =
90 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(HOST_FN));
91 if (h_fn)
92 sprintf(h_fn, "%s%s%s", cp, cproot, HOST_FN);
93 s_fn =
94 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(SERV_FN));
95 if (s_fn)
96 sprintf(s_fn, "%s%s%s", cp, cproot, SERV_FN);
97 n_fn =
98 (char *) malloc(3 + strlen(cp) + strlen(cproot) + strlen(NETW_FN));
99 if (n_fn)
100 sprintf(n_fn, "%s%s%s", cp, cproot, NETW_FN);
101 #endif
102 }
103
104 /*
105 * sets can open. ends must close.
106 */
107 void
108 endhostent(void)
109 {
110 if (h_fp)
111 fclose(h_fp);
112 h_fp = 0;
113 }
114
115 void
116 endservent(void)
117 {
118 if (s_fp)
119 fclose(s_fp);
120 s_fp = 0;
121 }
122
123 void
124 endprotoent(void)
125 {
126 if (p_fp)
127 fclose(p_fp);
128 p_fp = 0;
129 }
130
131 void
132 endnetent(void)
133 {
134 if (n_fp)
135 fclose(n_fp);
136 n_fp = 0;
137 }
138
139 void
140 sethostent(int stay_open)
141 {
142 pre_env();
143 endhostent();
144 h_stay_open = stay_open;
145 }
146
147 void
148 setservent(int stay_open)
149 {
150 pre_env();
151 endservent();
152 s_stay_open = stay_open;
153 }
154
155 void
156 setprotoent(int stay_open)
157 {
158 pre_env();
159 endprotoent();
160 p_stay_open = stay_open;
161 }
162
163 void
164 setnetent(int stay_open)
165 {
166 pre_env();
167 endnetent();
168 n_stay_open = stay_open;
169 }
170
171 #define STRTOK_DELIMS " \t\n"
172
173 /*
174 * get next entry from data base file, or from NIS if possible.
175 */
176 /*
177 * returns 0 if there are no more entries to read.
178 */
179 struct hostent *
180 gethostent(void)
181 {
182 return 0;
183 }
184
185 struct servent *
186 getservent(void)
187 {
188 return 0;
189 }
190
191 struct protoent *
192 getprotoent(void)
193 {
194 char *cp, **alp, lbuf[256];
195 static struct protoent spx;
196 static char *ali[10];
197 struct protoent *px = &spx;
198 int linecnt = 0;
199 char *st;
200
201 for (alp = ali; *alp; free(*alp), *alp = 0, alp++);
202 if (px->p_name)
203 free(px->p_name);
204 px->p_aliases = ali;
205
206 if (!p_fn)
207 return 0;
208 if (!p_fp)
209 p_fp = fopen(p_fn, "r");
210
211 if (!p_fp)
212 return 0;
213 while (fgets(lbuf, sizeof(lbuf), p_fp)) {
214 linecnt++;
215 cp = lbuf;
216 if (*cp == '#')
217 continue;
218
219 cp = strtok_r(lbuf, STRTOK_DELIMS, &st);
220 if (!cp)
221 continue;
222 if (cp)
223 px->p_name = strdup(cp);
224
225 cp = strtok_r(NULL, STRTOK_DELIMS, &st);
226 if (!cp) {
227 free(px->p_name);
228 continue;
229 }
230 px->p_proto = (short) atoi(cp);
231
232 for (alp = px->p_aliases; cp; alp++) {
233 cp = strtok_r(NULL, STRTOK_DELIMS, &st);
234 if (!cp)
235 break;
236 if (*cp == '#')
237 break;
238 *alp = strdup(cp);
239 }
240
241 return (px);
242 }
243
244 return 0;
245 }
246
247 struct netent *
248 getnetent(void)
249 {
250 return 0;
251 }
252
253 struct netent *
254 getnetbyaddr(long net, int type)
255 {
256 return 0;
257 }
258
259 /*
260 * Return the network number from an internet address
261 */
262 u_long
263 inet_netof(struct in_addr in)
264 {
265 register u_long i = ntohl(in.s_addr);
266 u_long ii = (i >> 24) & 0xff;
267 if (0 == (ii & 0x80))
268 return ((0xff000000 & i) >> 24);
269 if (0x80 == (ii & 0xc0))
270 return ((0xffff0000 & i) >> 16);
271 /*
272 * if (0xc0 == (ii & 0xe0))
273 */
274 return ((0xffffff00 & i) >> 8);
275 }
276
277 /*
278 * Return the host number from an internet address
279 */
280 u_long
281 inet_lnaof(struct in_addr in)
282 {
283 register u_long i = ntohl(in.s_addr);
284 u_long ii = (i >> 24) & 0xff;
285 if (0 == (ii & 0x80))
286 return (0x00ffffff & i);
287 if (0x80 == (ii & 0xc0))
288 return (0x0000ffff & i);
289 /*
290 * if (0xc0 == (ii & 0xe0))
291 */
292 return (0x000000ff & i);
293 }
294
295 #endif /* WIN32 or cygwin */