-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
175 lines (145 loc) · 3.08 KB
/
utils.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
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
/* file: utils.c
* description: utility functions for the network stack
* date: 12/2020
* author: Sergio Johann Filho <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/ustack.h"
uint16_t checksum(uint8_t *buf, int32_t len)
{
uint32_t i, sum = 0;
uint16_t val, *ptr = (uint16_t *)buf;
for (i = 0; i < len - 1; i += 2) {
val = *ptr++;
sum += ntohs(val);
}
if (len & 1) {
val = *ptr;
sum += ntohs(val) & 0xff00;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int32_t is_broadcast_mac(uint8_t *frame)
{
uint8_t bcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
struct eth_s *ethernet = (struct eth_s *)frame;
if (!memcmp(ðernet->dst_addr, bcast_addr, 6))
return 1;
else
return 0;
}
int32_t is_any_mac(uint8_t *frame)
{
uint8_t bcast_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
struct eth_s *ethernet = (struct eth_s *)frame;
if (!memcmp(ðernet->dst_addr, bcast_addr, 6))
return 1;
else
return 0;
}
int32_t is_local_mac(uint8_t *frame)
{
struct eth_s *ethernet = (struct eth_s *)frame;
if (!memcmp(ðernet->dst_addr, mymac, 6))
return 1;
else
return 0;
}
int32_t ip_addr_maskcmp(uint8_t addr1[4], uint8_t addr2[4], uint8_t mask[4])
{
int32_t i;
uint8_t ad1[4], ad2[4];
for (i = 0; i < 4; i++) {
ad1[i] = addr1[i] & mask[i];
ad2[i] = addr2[i] & mask[i];
}
if (!memcmp(ad1, ad2, 4))
return 1;
else
return 0;
}
int32_t ip_addr_cmp(uint8_t addr1[4], uint8_t addr2[4])
{
if (!memcmp(addr1, addr2, 4))
return 1;
else
return 0;
}
int32_t ip_addr_isany(uint8_t addr[4])
{
uint8_t addr_any[4] = {0x00, 0x00, 0x00, 0x00};
if (!memcmp(addr, addr_any, 4))
return 1;
else
return 0;
}
int32_t ip_addr_isbroadcast(uint8_t addr[4], uint8_t mask[4])
{
int32_t i;
uint8_t ad[4], msk[4];
uint8_t addr_any[4] = {0x00, 0x00, 0x00, 0x00};
uint8_t addr_all[4] = {0xff, 0xff, 0xff, 0xff};
for (i = 0; i < 4; i++) {
ad[i] = addr[i] & ~mask[i];
msk[i] = ~mask[i] & 0xff;
}
if (!memcmp(ad, msk, 4) || !memcmp(ad, addr_any, 4) || !memcmp(ad, addr_all, 4))
return 1;
else
return 0;
}
int32_t ip_addr_ismulticast(uint8_t addr[4])
{
int32_t i;
uint8_t ad[4];
uint8_t ad1[4] = {0xf0, 0x00, 0x00, 0x00};
uint8_t ad2[4] = {0xe0, 0x00, 0x00, 0x00};
for (i = 0; i < 4; i++)
ad[i] = addr[i] & ad1[i];
if (!memcmp(ad, ad2, 4))
return 1;
else
return 0;
}
int32_t hexdump(uint8_t *buf, uint32_t size)
{
uint32_t k, l;
char ch;
for (k = 0; k < size; k += 16) {
for (l = 0; l < 16; l++) {
printf("%02x ", buf[k + l]);
if (l == 7) putchar(' ');
}
printf(" |");
for (l = 0; l < 16; l++) {
ch = buf[k + l];
if ((ch >= 32) && (ch <= 126))
putchar(ch);
else
putchar('.');
}
printf("|\n");
}
return 0;
}
int32_t config(uint8_t addr[4], char *str)
{
int32_t i;
char *p;
char buf[128];
const char delim[] = ".";
strncpy(buf, str, sizeof(buf)-1);
p = strtok(buf, delim);
for (i = 0; i < 4 && p != NULL; i++) {
addr[i] = atoi(p);
p = strtok(NULL, delim);
}
if (i < 4)
return -1;
else
return 0;
}