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