File: processinetrc.c

package info (click to toggle)
netdiag 0.7-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,460 kB
  • ctags: 1,286
  • sloc: ansic: 12,984; makefile: 288; awk: 130; sh: 64
file content (263 lines) | stat: -rw-r--r-- 5,137 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
	INITIALIZE Program according to System Configuration  
	Revised for Version 0.7b
	
	Copyright G. MacKay under the GNU GPL 
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

FILE *fp;
extern char configfile[];
extern char ethdevname[];
/* static char fname[] = "rc.inet1"; */
static char buf[256];
static char temp[200];

void
convint2char (unsigned int hold[], unsigned char cbuf[])
{
  int i;

  for (i = 0; i < 4; i++)
    cbuf[i] = (unsigned char) hold[i];

}

int 
mkint (char asc)
{
  int tmp;

  if (isxdigit (asc))
    {
      tmp = asc - '0';
      if (tmp > 9)
	tmp = asc - 'A' + 10;
      if (tmp > 15)
	tmp = asc - 'a' + 10;
      return (tmp);
    }
  else
    return (0);
}

void 
cvhextohost (char *hex, unsigned char *host)
{
  int cvhi, cvlo;

  host += 3;
  cvhi = mkint (*hex);
  hex++;
  cvlo = mkint (*hex);
  hex++;
  *host = (cvhi) << 4 | cvlo;
  host--;
  cvhi = mkint (*hex);
  hex++;
  cvlo = mkint (*hex);
  hex++;
  *host = (cvhi) << 4 | cvlo;
  host--;
  cvhi = mkint (*hex);
  hex++;
  cvlo = mkint (*hex);
  hex++;
  *host = (cvhi) << 4 | cvlo;
  host--;
  cvhi = mkint (*hex);
  hex++;
  cvlo = mkint (*hex);
  *host = 0;
  *host = (cvhi) << 4 | cvlo;
}

void 
processproc (FILE * fp, unsigned char *netmask, unsigned char *local, int *ok)
{
  char buf[255];
  int cnt = 0;
  int num;
  char devname[10], dest[20], gateway[20], flags[10], ref[10];
  char met[10], use[10], mask[20], rest[80];


  while (fgets (buf, 255, fp) != NULL)
    {
      if (cnt)
	{
	  num = sscanf (buf, "%s %s %s %s %s %s %s %s %s",
			devname, dest, gateway, flags, ref, met, use, mask,
			rest);
	  if (!strcmp (ethdevname, devname))
	    {
	      if ((atoi (flags) & 1) == 1)  /* Thanks Dave...! */
		{
		  cvhextohost (dest, local);
		  cvhextohost (mask, netmask);
		  *ok = 1;
		  return;
		}
	    }
	}
      cnt++;
    }
}


/*
   Change to check "/proc/net/route" first to get the
   parameters from the ACTUAL system
   If no /proc filesystem... use the rc.inet1 file
 */
void
processinetrc (unsigned char *netmask, unsigned char *local, int *ok)
{

  unsigned int hold[4];
  int ethere = 0;
  int netmaskglobal = 0;
  int ipglobal = 0;
  int i;
  char *tok;
  int ipnext, netmasknext;


  *ok = 0;
  if ((fp = fopen ("/proc/net/route", "r")))
    {
      processproc (fp, netmask, local, ok);
      fclose (fp);
      return;
    }
  fp = fopen (configfile, "r");
  if (fp == NULL)
    {
      return;
    }
  while (fgets (buf, 256, fp))
    {
      ethere = 0;
      if (buf[0] == '#')
	continue;
      if (!strncmp (buf, "NETMASK=\"", 9))
	{
	  sscanf (buf, "%[^=]=\"%u.%u.%u.%u\"", temp, &hold[0],
		  &hold[1], &hold[2], &hold[3]);
	  convint2char (hold, netmask);
	  netmaskglobal = 1;
	}
      else if (!strncmp (buf, "NETMASK=", 8))
	{
	  sscanf (buf, "%[^=]=%u.%u.%u.%u", temp, &hold[0],
		  &hold[1], &hold[2], &hold[3]);
	  convint2char (hold, netmask);
	  netmaskglobal = 1;
	}
      if (!strncmp (buf, "IPADDR=\"", 8))
	{
	  sscanf (buf, "%[^=]=\"%u.%u.%u.%u\"", temp, &hold[0],
		  &hold[1], &hold[2], &hold[3]);
	  convint2char (hold, local);
	  ipglobal = 1;
	}
      else if (!strncmp (buf, "IPADDR=", 7))
	{
	  sscanf (buf, "%[^=]=%u.%u.%u.%u", temp, &hold[0],
		  &hold[1], &hold[2], &hold[3]);
	  convint2char (hold, local);
	  ipglobal = 1;
	}
      if (strstr (buf, ethdevname))
	{
	  *ok = 1;
	  ethere = 1;
	}
      if (strstr (buf, "ifconfig"))
	{
	  if (ethere)
	    {
	      /* PARSE the IFCONFIG line */
	      ipnext = 0;
	      netmasknext = 0;
	      tok = strtok (buf, " ");
	      i = 1;
	      do
		{
		  if (ipnext)
		    {
		      if (*tok != '$')	/* IPGLOBAL NOT USED */
			{
			  if (*tok == '\"')
			    {
			      sscanf (tok + 1, "%u.%u.%u.%u", &hold[0],
				      &hold[1], &hold[2], &hold[3]);
			      convint2char (hold, local);
			    }
			  else
			    {
			      sscanf (tok, "%u.%u.%u.%u", &hold[0],
				      &hold[1], &hold[2], &hold[3]);
			      convint2char (hold, local);
			    }
			}
		      ipnext = 0;
		    }
		  else if (netmasknext)
		    {
		      if (*tok != '$')	/* NETMASK GLOBAL NOT USED */
			{
			  if (*tok == '\"')
			    {
			      sscanf (tok + 1, "%u.%u.%u.%u", &hold[0],
				      &hold[1], &hold[2], &hold[3]);
			      convint2char (hold, netmask);
			    }
			  else
			    {
			      sscanf (tok, "%u.%u.%u.%u", &hold[0],
				      &hold[1], &hold[2], &hold[3]);
			      convint2char (hold, netmask);
			    }
			}
		      netmasknext = 0;
		    }
		  else if (!strcmp (tok, ethdevname))
		    ipnext = 1;
		  else if (!strcmp (tok, "netmask"))
		    netmasknext = 1;

/*                printf("%d=%s$\n",i,tok);  */
		  i++;
		}
	      while ((tok = strtok (NULL, " ")) != NULL);

	    }

	}

    }
  fclose (fp);
  return;
}

/*      TEST PROGRAM FOR ROUTINE  

   char ethdevname[]="eth0";
   char configfile[]="/etc/rc.d/rc.inet1";

   int main()
   {
   int ok;
   unsigned char mask[4];
   unsigned char loc[4];

   processinetrc(mask,loc,&ok);
   printf("NETMASK=%d.%d.%d.%d\n",mask[0],mask[1],mask[2],mask[3]);
   printf("IPADDR=%d.%d.%d.%d\n",loc[0],loc[1],loc[2],loc[3]);
   return;
   } 
 */