-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
117 lines (102 loc) · 2.3 KB
/
common.c
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
/*
* This file is part of Gspoof-3 (a console/gtk+ tcp/ip packet forger)
*
* $Name: common.c $
* $Version: 3.2 $
* $Date: 2003/12/22 16:30:03 $
* $Author: Embyte <embyte@madlab.it> $
* $Copyright: Copyright (C) 2002-2003 by embyte $
* $License: This software is under GPL version 2 of license $
*
*/
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include "common.h"
char * dn (char * s)
{
if (s[strlen(s)-1]=='\n')
s[strlen(s)-1]='\0';
return s;
}
u_char * ltostr(u_long a)
{
u_char *s;
u_short k=0;
u_long b=a;
do
{
b/=10;
k++;
}
while (b);
s=calloc(k+1, sizeof (u_char));
s+=k;
for (;;)
{
*s=(a%10)+'0';
a/=10;
if (!a) break;
s--;
}
return s;
}
/* Yeah, I don't like libnet_hex_aton(), myconvert work better */
u_char * emb_hex_aton(u_char *in)
{
u_short i, a=0;
u_char *out;
u_short retvalue;
out = malloc(6*sizeof(u_char));
for (i=0; i<6; i++)
{
if (in[a+1]!=':')
{
if (in[a+2]!=':' && i!=5) /* check possibles errors */
return NULL;
else
{
if((retvalue=emb_ctoi(in[a]))==16)
return NULL;
retvalue*=16;
out[i]=retvalue;
a++;
retvalue=emb_ctoi(in[a]);
if (retvalue==16 && i!=5) /* we are not at the end */
return NULL;
else if (retvalue==16 && i==5) /*we are at the end */
out[i]=out[i]/16;
else
out[i]+=retvalue; /* there are not error */
}
}
else
{
if((retvalue=emb_ctoi(in[a]))==16) return NULL;
out[i]=retvalue;
}
a+=2;
}
return out;
}
u_short emb_ctoi(u_char ex)
{
if (atoi(&ex)>0 && atoi(&ex)<10) return atoi(&ex);
else if (ex == '0') return 0;
else if (ex == 'a' || ex == 'A') return 10;
else if (ex == 'b' || ex == 'B') return 11;
else if (ex == 'c' || ex == 'C') return 12;
else if (ex == 'd' || ex == 'D') return 13;
else if (ex == 'e' || ex == 'E') return 14;
else if (ex == 'f' || ex == 'F') return 15;
else return 16; /* error code is 16 -> lol :=) */
}
/* convert u_char to "##:##:##...##" format */
char * emb_hex_ntoa (u_char *s)
{
char *r = calloc (18, sizeof (char));
sprintf (r, "%02X:%02X:%02X:%02X:%02X:%02X",
s[0], s[1], s[2], s[3], s[4], s[5]);
return r;
}