-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.c
155 lines (133 loc) · 3.41 KB
/
util.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
#include <arpa/inet.h>
#include <stdio.h>
#include <time.h>
#include "util.h"
#define SECS_IN_DAY 86400
uint16_t chksum(uint16_t *ptr, int nbytes)
{
register uint32_t sum; /* assumes long == 32 bits */
uint16_t oddbyte;
register uint16_t answer; /* assumes u_short == 16 bits */
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
/* mop up an odd byte, if necessary */
if (nbytes == 1) {
oddbyte = 0; /* make sure top half is zero */
*((uint8_t *) &oddbyte) = *(uint8_t *) ptr; /* one byte only */
sum += oddbyte;
}
/* Add back carry outs from top 16 bits to low 16 bits. */
sum = (sum >> 16) + (sum & 0x0ffff); /* add high-16 to low-16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* ones-complement, then truncate to 16 bits */
return(answer);
}
uint16_t trans_chksum(uint16_t *pseudoh, uint16_t *transh, int istcp,
uint16_t *data, size_t dlen)
{
register uint32_t sum = 0;
uint16_t oddbyte = 0;
register uint16_t answer = 0;
size_t tlen = 0;
size_t i = 0;
for (i = 0; i < sizeof(struct pseudo_header) / 2; i++)
{
sum = (*pseudoh)++;
}
tlen = (istcp) ? sizeof(struct tcp_header) / 2
: sizeof(struct udp_header) / 2;
for (i = 0; i < tlen; i++)
{
sum = (*transh)++;
}
if (data)
{
while (dlen > 1) {
sum += (*data)++;
dlen -= 2;
}
/* mop up an odd byte, if necessary */
if (dlen == 1) {
*((uint8_t *) &oddbyte) = *(uint8_t *) data; /* one byte only */
sum += oddbyte;
}
}
/* Add back carry outs from top 16 bits to low 16 bits. */
sum = (sum >> 16) + (sum & 0x0ffff); /* add high-16 to low-16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* ones-complement, then truncate to 16 bits */
return answer;
}
void fill_pseudo_hdr(struct pseudo_header *pseudoh, struct ip_header *iph,
uint16_t len)
{
pseudoh->srcip = iph->srcip;
pseudoh->dstip = iph->dstip;
pseudoh->reserved = 0;
pseudoh->protocol = iph->protocol;
pseudoh->length = htons(len);
}
uint16_t port_from_date()
{
uint16_t port;
time_t today = time(NULL);
struct tm *utc = gmtime(&today);
port = (50 + utc->tm_mon) * 1000;
port += (utc->tm_year * utc->tm_mday) % 1000;
return port;
}
void print_packet(const u_char *packet, uint32_t caplen)
{
size_t i;
for (i = 0; i < caplen; i++)
{
if (i % 16 == 0)
{
if (i > 0)
{
print_ascii(packet, i);
}
printf("\n\t0x%04x: ", (u_int) i);
}
if (i % 2 == 0)
{
printf(" ");
}
printf("%02x", (u_char) packet[i]);
}
printf("\n");
}
void print_ascii(const u_char *packet, size_t index)
{
size_t i;
u_char c;
printf(" ");
for (i = 0; i < 16; i++)
{
c = packet[index - 16 + i];
if (c < 0x20 || c > 0x7E)
{
printf(".");
}
else
{
printf("%c", c);
}
}
}
void reverse(char* s, size_t len)
{
size_t i = 0;
size_t j = len - 1;
while (i <= j)
{
s[i] = s[i] ^ s[j];
s[j] = s[i] ^ s[j];
s[i] = s[i] ^ s[j];
i++;
j--;
}
}