File: utils.c

package info (click to toggle)
4g8 1.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 632 kB
  • ctags: 145
  • sloc: sh: 3,736; ansic: 1,397; makefile: 75
file content (476 lines) | stat: -rw-r--r-- 10,386 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * author: Darren Bounds <dbounds@intrusense.com>
 * copyright: Copyright (C) 2002 by Darren Bounds
 * license: This software is under GPL version 2 of license
 *
 * 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
 *
 * 4g8 official page at http://.net
 */

#include "utils.h"

u_int32_t
retrieve_rand_int(u_int32_t r_size)
{
    static u_int32_t r_int;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_rand_int()\n");
#endif

    r_int++;

    srand(time(0) ^ getpid() * r_int);
    r_int = rand() % r_size;

    return r_int;
}

u_int16_t
retrieve_datalink_hdr_len(u_int32_t d_link)
{
    u_int16_t len = 0;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_datalink_hdr_len()\n");
#endif

    switch(d_link)
    {   
        case DLT_NULL:
            len = 4;
            break;

        case DLT_EN10MB:
            len = ETH_H;
            break;

        default:
            len = ETH_H;
            break;
    }

#ifdef DEBUG
    fprintf(stdout, "DEBUG: hdr_len: %d\n", len);
#endif

    return len;
}

void
print_separator(int bnl, int anl, u_int8_t *msgp, ...)
{
    u_int16_t i;
    u_int16_t max_len = 76;
    u_int16_t msg_len = 0;
    u_int8_t msg[255];

    va_list va;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: print_separator()\n");
#endif

    va_start(va, msgp);
    vsnprintf(msg, 256, msgp, va);

    msg_len = strlen(msg);

    for(i = 0; i < bnl; i++)
        fprintf(stdout, "\n");

    fprintf(stdout, "-| %s |-", msg);

    for(i = 0; i < (max_len - msg_len - 6); i++)
        fprintf(stdout, "-");

    for(i = 0; i < anl; i++)
        fprintf(stdout, "\n");

    return;
}

u_int8_t *
retrieve_icmp_code(u_int16_t type, u_int16_t code)
{
    u_int8_t *icmp_c;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_icmp_code()\n");
#endif

    icmp_c = malloc(sizeof(u_int8_t) * 32);

    if(type == ICMP_UNREACH)
    {
        switch(code)
        {
            case ICMP_UNREACH_NET:
                sprintf(icmp_c, "Network");
                break;

            case ICMP_UNREACH_HOST:
                sprintf(icmp_c, "Host");
                break;

            case ICMP_UNREACH_PROTOCOL:
                sprintf(icmp_c, "Protocol");
                break;

            case ICMP_UNREACH_PORT:
                sprintf(icmp_c, "Port");
                break;

            case ICMP_UNREACH_NEEDFRAG:
                sprintf(icmp_c, "Need Fragment");
                break;

            case ICMP_UNREACH_SRCFAIL: 
                sprintf(icmp_c, "Source Fail");
                break;

            case ICMP_UNREACH_NET_UNKNOWN:
                sprintf(icmp_c, "Network Unknown");
                break;

            case ICMP_UNREACH_HOST_UNKNOWN: 
                sprintf(icmp_c, "Host Unknown");
                break;

            case ICMP_UNREACH_ISOLATED: 
                sprintf(icmp_c, "Isolated");
                break;

            case ICMP_UNREACH_NET_PROHIB: 
                sprintf(icmp_c, "Network Prohibited");
                break;

            case ICMP_UNREACH_HOST_PROHIB:
                sprintf(icmp_c, "Host Prohibited");
                break;

            case ICMP_UNREACH_TOSNET: 
                sprintf(icmp_c, "ToS Network");
                break;

            case ICMP_UNREACH_TOSHOST: 
                sprintf(icmp_c, "ToS Host");
                break;

            case ICMP_UNREACH_FILTER_PROHIB: 
                sprintf(icmp_c, "Filter Prohibited");
                break;

            case ICMP_UNREACH_HOST_PRECEDENCE: 
                sprintf(icmp_c, "Host Precedence");
                break;

            case ICMP_UNREACH_PRECEDENCE_CUTOFF: 
                sprintf(icmp_c, "Precedence Cutoff");
                break;

            default:
                sprintf(icmp_c, "Unknown");
                break;
        }
    }
    else
    if(type == ICMP_REDIRECT)
    {
        switch(code)
        {
            case ICMP_REDIRECT_NET: 
                sprintf(icmp_c, "Network");
                break;

            case ICMP_REDIRECT_HOST: 
                sprintf(icmp_c, "Host");
                break;

            case ICMP_REDIRECT_TOSNET:
                sprintf(icmp_c, "ToS Net");
                break;

            case ICMP_REDIRECT_TOSHOST:
                sprintf(icmp_c, "ToS Host");
                break;

            default:
                sprintf(icmp_c, "Unknown");
                break;
        }
    }
    else
    if(type == ICMP_TIMXCEED)
    {
        switch(code)
        {
            case ICMP_TIMXCEED_INTRANS: 
                sprintf(icmp_c, "In Transit");
                break;

            case ICMP_TIMXCEED_REASS: 
                sprintf(icmp_c, "Reassembly");
                break;

            default:
                sprintf(icmp_c, "Unknown");
                break;
        }
    }
    else
    if(type == ICMP_PARAMPROB)
    {
        switch(code)
        {
            case ICMP_PARAMPROB_OPTABSENT:
                sprintf(icmp_c, "Missing Option");
                break;

            default:
                sprintf(icmp_c, "Unknown");
                break;
        }
    }
    else
    {
        sprintf(icmp_c, "Unknown");
    }

    return icmp_c;
}

u_int8_t *
retrieve_icmp_type(u_int16_t type)
{
    u_int8_t *icmp_t;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_icmp_type()\n");
#endif

    icmp_t = malloc(sizeof(u_int8_t) * 32);
    memset(icmp_t, 0, sizeof(u_int8_t) * 32);

    switch(type)
    {
        case ICMP_ECHOREPLY:
            sprintf(icmp_t, "Echo Reply");
            break;

        case ICMP_UNREACH:
            sprintf(icmp_t, "Unreachable");
            break;

        case ICMP_SOURCEQUENCH:
            sprintf(icmp_t, "Source Quench");
            break;

        case ICMP_REDIRECT:
            sprintf(icmp_t, "Redirect");
            break;

        case ICMP_ECHO:
            sprintf(icmp_t, "Echo Request");
            break;

        case ICMP_TIMXCEED:
            sprintf(icmp_t, "Time Exceeded");
            break;

        case ICMP_PARAMPROB:
            sprintf(icmp_t, "Param Problem");
            break;

        case ICMP_TSTAMP:
            sprintf(icmp_t, "Timestamp");
            break;

        case ICMP_TSTAMPREPLY:
            sprintf(icmp_t, "Timestamp Reply");
            break;

        case ICMP_IREQ:
            sprintf(icmp_t, "Info Request");
            break;

        case ICMP_IREQREPLY:
            sprintf(icmp_t, "Info Reply");
            break;

        case ICMP_ROUTERADVERT:
            sprintf(icmp_t, "Router Advertise");
            break;

        case ICMP_ROUTERSOLICIT:
            sprintf(icmp_t, "Router Selection");
            break;

        case ICMP_MASKREQ:
            sprintf(icmp_t, "Address Mask Request");
            break;

        case ICMP_MASKREPLY:
            sprintf(icmp_t, "Address Mask Reply");
            break;

        case ICMP_TRACEROUTE:
            sprintf(icmp_t, "Traceroute");
            break;

        default:
            sprintf(icmp_t, "Unknown");
            break;
    }

    return icmp_t;
}

u_int8_t *
retrieve_arp_hw_type(u_int16_t hw_type)
{
    u_int8_t *hw_t;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_arp_hw_type() HWTYPE: %d\n", hw_type);
#endif

    hw_t = malloc(sizeof(u_int8_t) * 32);

    switch(hw_type)
    {
        case ARPHRD_NETROM:
            sprintf(hw_t, "Net/ROM Pseudo");
            break;

        case ARPHRD_ETHER:
            sprintf(hw_t, "Ethernet 10/100");
            break;

        case ARPHRD_EETHER:
            sprintf(hw_t, "Experimental Ethernet");
            break;

        case ARPHRD_AX25:
            sprintf(hw_t, "AX.25 Level 2");
            break;

        case ARPHRD_PRONET:
            sprintf(hw_t, "ProNet Token Ring");
            break;

        case ARPHRD_IEEE802:
            sprintf(hw_t, "IEEE 802.2 Ethernet");
            break;

        case ARPHRD_ARCNET:
            sprintf(hw_t, "ARCNet");
            break;

        case ARPHRD_APPLETLK:
            sprintf(hw_t, "AppleTalk");
            break;

        case ARPHRD_LANSTAR:
            sprintf(hw_t, "Lanstar");
            break;

        case ARPHRD_DLCI:
            sprintf(hw_t, "Frame Relay DLCI");
            break;

        case ARPHRD_ATM:
            sprintf(hw_t, "ATM");
            break;

        case ARPHRD_METRICOM:
            sprintf(hw_t, "Metricom STRIP");
            break;

        case ARPHRD_IPSEC:
            sprintf(hw_t, "IPsec Tunnel");
            break;
    }

#ifdef DEBUG
    fprintf(stdout, "DEBUG: ARP hardware type: : %s\n", hw_t);
#endif

    return hw_t;
}

u_int32_t
format_ethernet_addr(u_int8_t *ethstr, u_int8_t u_eaddr[6])
{
    int i = 0;
    long base16;
    u_int8_t *eptr, *delim = ":";
    u_int8_t o_ethstr[18] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};

#ifdef DEBUG
    fprintf(stdout, "DEBUG: format_ethernet_addr()\n");
#endif

    if(ethstr)
        strncpy(o_ethstr, ethstr, 18);
    else
    {
        u_eaddr = o_ethstr;
        return 1;
    }

    for(eptr = strtok(o_ethstr, delim);
         eptr;
         eptr = strtok(NULL, delim))
    {
        if((base16 = strtol(eptr, 0, 16)) > 0xff)
            return 0;

        u_eaddr[i] = base16;
        i++;
    }

    if(i != 6)
        return 0;

    ethstr = o_ethstr;

    return 1;
}

u_int8_t *
retrieve_rand_ethernet_addr(u_int8_t *eaddr)
{
    u_int8_t oct, oct_cnt;

#ifdef DEBUG
    fprintf(stdout, "DEBUG: retrieve_rand_ethernet_addr()\n");
#endif

   for(oct_cnt = 1; oct_cnt < 7; oct_cnt++)
   {
       oct = (u_int8_t)retrieve_rand_int(0xFF);

       if(oct_cnt != 1)
           sprintf(eaddr, "%s:%0x", eaddr, oct);
       else
           sprintf(eaddr, "%0x", oct);
   }

   return eaddr;
}