1 /*
2  * This file is part of Gspoof-3 (a console/gtk+ tcp/ip packet forger)
3  *
4  * $Name: common.c $
5  * $Version: 3.2 $
6  * $Date: 2003/12/22 16:30:03 $
7  * $Author: Embyte <embyte@madlab.it> $
8  * $Copyright: Copyright (C) 2002-2003 by embyte $
9  * $License: This software is under GPL version 2 of license $
10  *
11  */
12 
13 #include <string.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <stdio.h>
17 #include "common.h"
18 
dn(char * s)19 char * dn (char * s)
20 {
21    if (s[strlen(s)-1]=='\n')
22      s[strlen(s)-1]='\0';
23    return s;
24 }
25 
ltostr(u_long a)26 u_char * ltostr(u_long a)
27 {
28    u_char *s;
29    u_short k=0;
30    u_long b=a;
31 
32    do
33      {
34 	b/=10;
35 	k++;
36      }
37    while (b);
38 
39    s=calloc(k+1, sizeof (u_char));
40    s+=k;
41 
42    for (;;)
43      {
44 	*s=(a%10)+'0';
45 	a/=10;
46 	if (!a) break;
47 	s--;
48      }
49 
50    return s;
51 }
52 
53 /* Yeah, I don't like libnet_hex_aton(), myconvert work better */
emb_hex_aton(u_char * in)54 u_char * emb_hex_aton(u_char *in)
55 {
56    u_short i, a=0;
57    u_char *out;
58    u_short retvalue;
59 
60    out = malloc(6*sizeof(u_char));
61 
62    for (i=0; i<6; i++)
63      {
64 	if (in[a+1]!=':')
65 	  {
66 	     if (in[a+2]!=':' && i!=5) /* check possibles errors */
67 	       return NULL;
68 	     else
69 	       {
70 		  if((retvalue=emb_ctoi(in[a]))==16)
71 		    return NULL;
72 		  retvalue*=16;
73 		  out[i]=retvalue;
74        		  a++;
75 		  retvalue=emb_ctoi(in[a]);
76 		  if (retvalue==16 && i!=5) /* we are not at the end */
77 		    return NULL;
78 		  else if (retvalue==16 && i==5) /*we are at the end */
79 		    out[i]=out[i]/16;
80 		  else
81 		    out[i]+=retvalue; /* there are not error */
82        	       }
83 	  }
84 	else
85 	  {
86 	     if((retvalue=emb_ctoi(in[a]))==16) return NULL;
87 	     out[i]=retvalue;
88 	  }
89 	a+=2;
90      }
91    return out;
92 }
93 
emb_ctoi(u_char ex)94 u_short emb_ctoi(u_char ex)
95 {
96    if (atoi(&ex)>0 && atoi(&ex)<10) return atoi(&ex);
97    else if (ex == '0') return 0;
98    else if (ex == 'a' || ex == 'A') return 10;
99    else if (ex == 'b' || ex == 'B') return 11;
100    else if (ex == 'c' || ex == 'C') return 12;
101    else if (ex == 'd' || ex == 'D') return 13;
102    else if (ex == 'e' || ex == 'E') return 14;
103    else if (ex == 'f' || ex == 'F') return 15;
104    else return 16; /* error code is 16 -> lol :=) */
105 }
106 
107 /* convert u_char to "##:##:##...##" format */
emb_hex_ntoa(u_char * s)108 char * emb_hex_ntoa (u_char *s)
109 {
110 
111       char *r = calloc (18, sizeof (char));
112 
113       sprintf (r, "%02X:%02X:%02X:%02X:%02X:%02X",
114 	                   s[0], s[1], s[2], s[3], s[4], s[5]);
115 
116       return r;
117 }
118