~ubuntu-branches/ubuntu/hardy/nast/hardy

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
/*
    nast - Network analyzer sniffer tool

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "include/nast.h"

#define BYTES	16
#define SHORTS	(BYTES / 2)
#define DIGITS	5
#define LINE	(DIGITS * SHORTS)

void print_ascii_hex(char *data, u_int l, FILE *log)
{
   u_int offset;
   u_int i;
   int s1, s2;
   int nshorts;
   char hex[BYTES*DIGITS+1], *hsp;
   char ascii[BYTES+1], *asp;

   row_s=0;
   line_s+=3;

   nshorts = l / sizeof(u_short);
   offset = i = 0;
   hsp = hex; asp = ascii;
   while (--nshorts >= 0)
     {
	s1 = *data++;
	s2 = *data++;
	(void)snprintf(hsp, sizeof(hex) - (hsp - hex),
		       " %02x%02x", s1, s2);
	hsp += DIGITS;
	*(asp++) = (isgraph(s1) ? s1 : '.');
	*(asp++) = (isgraph(s2) ? s2 : '.');
	if (++i >= SHORTS)
	  {
	     *hsp = *asp = '\0';
	     /*fprintf(log, "\n0x%04x   %-*s    %s",
		     offset, LINE,
		     hex, ascii);*/
	     if(!graph) printf("\n");
	     n_print("princ",line_s,row_s,lg,"0x%04x   %-*s    %s",
		     offset, LINE,
		     hex, ascii);
	     i = 0; hsp = hex; asp = ascii;
	     offset += BYTES;
	     ++line_s;
	  }
     }
   if (l & 1)
     {
	s1 = *data++;
	(void)snprintf(hsp, sizeof(hex) - (hsp - hex),
		       " %02x", s1);
	hsp += 3;
	*(asp++) = (isgraph(s1) ? s1 : '.');
	++i;
     }
   if (i > 0)
     {
	*hsp = *asp = '\0';
	if(!graph) printf("\n");
	n_print("princ",line_s,row_s,lg,"0x%04x   %-*s    %s",
		offset, LINE,
		hex, ascii);
	++line_s;
     }
}

void data_sniffo (char *data_info, u_int len, FILE *log)
{
   int i,ld;

   row_s = ld = 0;

   if(log==stdout)
     ld = 0;
   else ld = 1;

   line_s+=3;

   if(graph && !ld)
     {

	if (data_info == NULL)
	  {
	     n_print("princ",line_s,row_s,lg,"NULL DATA");
	     ++line_s;
	  }

	for (i = 0; i < len; i++)
	  {
	     if (ispunct(data_info[i]) || isalnum(data_info[i]))
	       {
		  n_print("princ",line_s,row_s,lg,"%c",data_info[i]);
		  row_s++;
	       }
	     else if (data_info[i]=='\n')
	       {
		  n_print("princ",++line_s,row_s,lg,"");
		  row_s=0;
	       }
	     else if (data_info[i]=='\r')
	       row_s = row_s + 5;
	     else if (data_info[i]=='\t')
	       row_s = row_s + 3;
	     else
	       row_s++;

	  }

     }

   if(!graph || (graph && ld))
     {
        fputc('\n', log);
	line_s-=3;

	if (data_info == NULL)
	  fprintf(log, "NULL DATA\n");

	for (i = 0; i < len; i++)
	  {
	     if (ispunct(data_info[i]) || isalnum(data_info[i]))
	       fputc (data_info[i], log);
	     else if (data_info[i]=='\n')
	       fputc('\n', log);
	     else if (data_info[i]=='\r')
	       fputc('\r', log);
	     else if (data_info[i]=='\t')
	       fputc ('\t', log);
	     else
	       fputc (' ', log);
	  }
     }

}