nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <inttypes.h>
5  
6 #include "manufactor.h"
7 #include "mac_addr.h"
8  
9  
10 struct ether_addr parse_mac(char *input)
11 {
12 // Parsing input MAC adresses like 00:00:11:22:aa:BB or 00001122aAbB
13 struct ether_addr mac_p;
14 uint8_t *bytes = mac_p.ether_addr_octet;
15  
16 if (input[2] == ':') {
17 sscanf(input, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", bytes, bytes+1, bytes+2, bytes+3, bytes+4, bytes+5);
18 } else {
19 sscanf(input, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", bytes, bytes+1, bytes+2, bytes+3, bytes+4, bytes+5);
20 }
21  
22 return mac_p;
23 }
24  
25  
26 struct ether_addr parse_half_mac(char *input)
27 {
28 // Parsing input half MAC adresses like 00:00:11 or 000011
29 // Octets 3 to 5 will be 0x00
30 struct ether_addr mac_p;
31 uint8_t *bytes = mac_p.ether_addr_octet;
32  
33 if (input[2] == ':') {
34 sscanf(input, "%hhx:%hhx:%hhx", bytes, bytes+1, bytes+2);
35 } else {
36 sscanf(input, "%2hhx%2hhx%2hhx", bytes, bytes+1, bytes+2);
37 }
38  
39 bytes[3] = bytes[4] = bytes[5] = 0x00;
40  
41 return mac_p;
42 }
43  
44  
45 struct ether_addr generate_valid_mac(int type, int list_len)
46 {
47 int t, pos;
48 struct ether_addr mac_v;
49  
50 pos = random();
51 pos = pos % list_len;
52  
53 // SAMPLE LINE
54 // 000123000000/FFFFFF000000
55  
56 if (type == 0) {
57 for (t=0; t<ETHER_ADDR_LEN; t++) {
58 if (!memcmp(clients[pos]+(t*2+13), "FF", 2) || !memcmp(clients[pos]+(t*2+13), "ff", 2)) {
59 sscanf((char *) (clients[pos] + (2 * t)), "%2hhx", &mac_v.ether_addr_octet[t]);
60 } else mac_v.ether_addr_octet[t] = random();
61 }
62 } else {
63 for (t=0; t<ETHER_ADDR_LEN; t++) {
64 if (!memcmp(accesspoints[pos]+(t*2+13), "FF", 2) || !memcmp(accesspoints[pos]+(t*2+13), "ff", 2)) {
65 sscanf((char *) (accesspoints[pos] + (2 * t)), "%2hhx", &mac_v.ether_addr_octet[t]);
66 } else mac_v.ether_addr_octet[t] = random();
67 }
68 }
69  
70 return mac_v;
71 }
72  
73  
74 struct ether_addr generate_mac(enum mac_kind kind)
75 {
76 // Generate a random MAC adress
77 // kind : Which kind of MAC should be generated?
78 // 0 : random MAC
79 // 1 : valid client MAC
80 // 2 : valid accesspoint MAC
81  
82 struct ether_addr gmac;
83 int t;
84  
85 for (t=0; t<ETHER_ADDR_LEN; t++)
86 gmac.ether_addr_octet[t] = random();
87  
88 if (kind == MAC_KIND_CLIENT)
89 gmac = generate_valid_mac(0, clients_count);
90 if (kind == MAC_KIND_AP)
91 gmac = generate_valid_mac(1, accesspoints_count);
92  
93 return gmac;
94 }
95  
96  
97 void increase_mac_adress(struct ether_addr *macaddr)
98 {
99 macaddr->ether_addr_octet[2]++;
100 if (macaddr->ether_addr_octet[2] == 0) {
101 macaddr->ether_addr_octet[1]++;
102 if (macaddr->ether_addr_octet[1] == 0) {
103 macaddr->ether_addr_octet[0]++;
104 }
105 }
106 }
107  
108  
109 struct ether_addr get_next_mac(struct ether_addr mac_base, struct ether_addr *mac_lower)
110 {
111 static int pos = -2;
112 static struct ether_addr lowb;
113 static struct ether_addr upb;
114 struct ether_addr mac_v;
115  
116 if (pos == -2) {
117 MAC_SET_BCAST(lowb);
118 MAC_SET_BCAST(upb);
119 pos = -1;
120 }
121  
122 if (MAC_IS_NULL(mac_base)) { //Use internal database
123 //Increase lower bytes
124 increase_mac_adress(&lowb);
125 //Get new upper bytes?
126 if (! memcmp(lowb.ether_addr_octet, "\x00\x00\x00", 3)) {
127 //New pos in client list
128 pos++;
129 if (pos == clients_count) {
130 MAC_SET_BCAST((*mac_lower));
131 MAC_SET_NULL(mac_v);
132 return mac_v;
133 }
134 //Filling the first three bytes
135 sscanf((char *) clients[pos], "%2hhx%2hhx%2hhx", upb.ether_addr_octet, upb.ether_addr_octet+1, upb.ether_addr_octet+2);
136 }
137 memcpy(mac_v.ether_addr_octet, upb.ether_addr_octet, 3);
138 memcpy(mac_v.ether_addr_octet+3, lowb.ether_addr_octet, 3);
139 } else { //Use MAC given by user
140 increase_mac_adress(&lowb);
141  
142 if (! MAC_IS_NULL(*mac_lower)) { //Use start MAC given by user
143 memcpy(lowb.ether_addr_octet, mac_lower->ether_addr_octet, 3);
144 MAC_SET_NULL(*mac_lower);
145 }
146  
147 if (! memcmp(lowb.ether_addr_octet, "\xFF\xFF\xFF", 3)) {
148 MAC_SET_BCAST((*mac_lower));
149 MAC_SET_NULL(mac_v);
150 return mac_v;
151 }
152 memcpy(mac_v.ether_addr_octet, mac_base.ether_addr_octet, 3);
153 memcpy(mac_v.ether_addr_octet+3, lowb.ether_addr_octet, 3);
154 }
155  
156 return mac_v;
157 }
158  
159  
160 void print_mac(struct ether_addr pmac)
161 {
162 uint8_t *p = pmac.ether_addr_octet;
163  
164 printf("%02X:%02X:%02X:%02X:%02X:%02X", p[0], p[1], p[2], p[3], p[4], p[5]);
165 }