File: ana.c

package info (click to toggle)
rpld 1.8beta1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 832 kB
  • ctags: 621
  • sloc: ansic: 2,230; asm: 434; yacc: 225; makefile: 169; sh: 40
file content (266 lines) | stat: -rw-r--r-- 4,965 bytes parent folder | download | duplicates (3)
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
264
265
266
#include <stdio.h>
#include <strings.h>

static char *rcsid = "$Id: ana.c,v 1.5 2000/09/26 03:48:23 root Exp $";

/* 
 * $Log: ana.c,v $
 * Revision 1.5  2000/09/26 03:48:23  root
 * #
 *
 * Revision 1.4  2000/09/26 03:44:29  root
 * #
 *
 * Revision 1.3  2000/07/16 13:18:10  root
 * #
 *
 * Revision 1.1  2000/07/16 13:16:33  root
 * #
 *
 * Revision 1.2  1999/09/13 12:37:06  root
 * #
 *
 * Revision 1.1  1999/09/13 12:36:18  root
 * #
 *
 * Revision 1.4  1999/09/02 09:59:02  root
 * #
 *
 * Revision 1.3  1999/09/02 09:58:02  root
 * #
 *
 */

#include <sys/time.h>
#include "nit.h"

#define ETH_ALEN 6
unsigned char pbuf[4096];
int plen;


int
pull_short (unsigned char *c)
{
  int ret;
  ret = *c;
  c++;
  ret <<= 8;
  ret += *c;
  return (ret);
}
unsigned int
pull_long (unsigned char *c)
{
  unsigned int ret;

  ret = *c;
  c++;
  ret <<= 8;
  ret += *c;
  c++;
  ret <<= 8;
  ret += *c;
  c++;
  ret <<= 8;
  ret += *c;

  return (ret);
}

dump_hex (unsigned char *pp, int pl)
{
  while (pl--)
    {
      printf (" %02x", *(pp++));
    }
}

dump_hexa (unsigned char *pp, int pl)
{
  while (pl--)
    {
      int j = *pp;
      if ((j < ' ') || (j > 126))
        j = '.';
      printf (" %02x%c", *pp, j);
      pp++;
    }
}

ana_token (int s, unsigned char *pp, int pl)
{
  int i;
  printf ("    Token 0x%04x:", s);
  switch (s)
    {
    case 0x4003:
      printf (" The mighty zero: %d\n", pull_long (pp));
      return;
    case 0x4006:
      printf (" My MAC addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
              pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]);
      return;
    case 0x4007:
      printf (" My SAP addr: %02x\n", pp[0]);
      return;
    case 0x4009:
      printf (" Frame len: %d\n", pull_short (pp));
      return;
    case 0x400b:
      printf (" The small zero: %d\n", *pp);
      return;
    case 0x400c:
      printf (" Your MAC addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
              pp[0], pp[1], pp[2], pp[3], pp[4], pp[5]);
      return;
    case 0x4011:
      printf (" Block number: %d\n", pull_long (pp));
      return;
    case 0x4018:
      printf (" data block: (ommitted %d bytes of guff)\n", pl);
      return;
    case 0xc005:
      printf (" IDENT: ");
      dump_hexa (pp, pl);
      printf ("\n");
      return;
    case 0xc014:
      printf (" addr block: Load @ 0x%08x, Run @ 0x%08x, Flags: 0x%02x",
              pull_long (pp), pull_long (pp + 4), pp[8]);
      switch (pp[8])
        {
        case 0x20:
          printf (" (More to come)");
          break;
        case 0xc0:
          printf (" (All done, execute)");
          break;
        default:
          printf (" ?");
        }

      printf ("\n");
      return;
    default:
      printf (" ?: ");
      dump_hex (pp, pl);
      printf ("\n");

    }

}

int
ana_frag (unsigned char *pp, int pl)
{
  int s;

  while (pl > 0)
    {

      s = pull_short (pp);

      if (s & 0xc000)
        {
          ana_token (s, pp + 2, pl - 2);
          return;
        }
      else
        {
          ana_frag (pp + 2, s - 2);
          pp += s;
          pl -= s;
        }

    }

}




int
main (int argc, char *argv[])
{
  int i, s;
  char *pptr;
  struct nit *n;
  struct timeval tv;


  if (argc == 2)
    {
      n = nit_open (argv[1]);
    }
  else
    {
      n = nit_open (NULL);
    }

  if (!n)
    {
      printf ("Failed to open network device\n");
    }
  while (!feof (stdin))
    {
      unsigned char from[ETH_ALEN];

      plen = nit_recv (n, pbuf, sizeof (pbuf), from, NULL);
      gettimeofday (&tv, NULL);

      if (
          (pbuf[0] == 0xfc) || (pbuf[1] == 0xfc) ||
          (pbuf[0] == 0xf8) || (pbuf[1] == 0xf8) ||
          (pbuf[0] == 0xf4) || (pbuf[1] == 0xf4))
        {

          printf ("%d.%06d  From %02x:%02x:%02x:%02x:%02x:%02x \n",
                  (int) tv.tv_sec,
                  (int) tv.tv_usec,
                  from[0], from[1], from[2], from[3], from[4], from[5]);
          printf (" Ssap %02x Dsap %02x Cmd %02x Length %d(%d)\n",
                  pbuf[0],
                  pbuf[1], pbuf[2], plen, ntohs (*(short *) &pbuf[3]));


          pptr = pbuf;
          pptr += 5;            /*Length */
          plen -= 5;

          printf ("  ");
          dump_hex (pptr, plen);
          printf ("\n");

          printf ("  Pack Type:");
          switch (pull_short (pptr))
            {
            case 0x1:
              printf (" FIND:");
              break;
            case 0x2:
              printf (" FOUND:");
              break;
            case 0x10:
              printf (" SEND.FILE.REQUEST:");
              break;
            case 0x20:
              printf (" FILE.DATA.RESPONSE:");
              break;
            default:
              printf (" 0x%04x ?:", pull_short (pptr));
            }
          printf (" Length=%d\n", plen);
          pptr += 2;
          plen -= 2;

          s = ana_frag (pptr, plen);


          printf ("\n\n");
        }
    }

  exit (0);

}