Skip to content
This repository was archived by the owner on Aug 3, 2021. It is now read-only.

Commit f55402d

Browse files
author
David Gatti
committed
Proofing
1 parent c2a6068 commit f55402d

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

C/pingWithChecksum.c

+28-29
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,33 @@ int main() {
4242
icmp_hdr_t pckt;
4343

4444
//
45-
// 4. Set the apropriate values to our struct, which is our ICMP header
45+
// 4. Set the appropriate values to our struct, which is our ICMP header
4646
//
4747
pckt.type = 8; // The echo request is 8
4848
pckt.code = 0; // No need
49-
pckt.chksum = 0; // The chacksum first needs to be calcualted
49+
pckt.chksum = 0; // The checksum first needs to be calculated
5050
pckt.identifier = getpid(); // Set a random Nr. in this case the app ID
51-
pckt.sequence_number = 1; // Normaly you would increment this nummber
51+
pckt.sequence_number = 1; // Normally you would increment this number
5252
pckt.data = 0; // We don't send anything.
5353

54-
//
54+
//
5555
// 5. Calculate the checksum based on the whole header, and only then
5656
// you add it to the header.
57-
//
57+
//
5858
pckt.chksum = checksum((uint16_t *)&pckt, sizeof(pckt));
5959

6060
//
61-
// 6. Creatign a IP Header from a struct that exists in another library
62-
//
61+
// 6. Creation a IP Header from a struct that exists in another library
62+
//
6363
struct sockaddr_in addr;
6464
addr.sin_family = AF_INET;
6565
addr.sin_port = 0;
6666
addr.sin_addr.s_addr = inet_addr("8.8.8.8");
6767

6868
//
69-
// 7. Send our PING
69+
// 7. Send our PING
7070
//
71-
int actionSendResult = sendto(s, &pckt, sizeof(pckt),
71+
int actionSendResult = sendto(s, &pckt, sizeof(pckt),
7272
0, (struct sockaddr*)&addr, sizeof(addr));
7373

7474
//
@@ -81,15 +81,15 @@ int main() {
8181
}
8282

8383
//
84-
// 8. Prepare all the necesary variable to handle the response
84+
// 8. Prepare all the necessary variable to handle the response
8585
//
8686
unsigned int resAddressSize;
8787
unsigned char res[30] = "";
8888
struct sockaddr resAddress;
8989

90-
//
90+
//
9191
// 9. Creating the struct to better handle the response
92-
//
92+
//
9393
typedef struct {
9494
uint8_t type;
9595
uint8_t code;
@@ -101,27 +101,27 @@ int main() {
101101
//
102102
// 10. Read the response from the remote host
103103
//
104-
int ressponse = recvfrom(s, res, sizeof(res), 0, &resAddress,
104+
int ressponse = recvfrom(s, res, sizeof(res), 0, &resAddress,
105105
&resAddressSize);
106106

107107
//
108-
// -> Display the response by accessign the struct
108+
// -> Display the response by accessing the struct
109109
//
110110
if(ressponse > 0)
111111
{
112-
//
113-
// 11. Create the response variable usign our custom struct
114-
//
112+
//
113+
// 11. Create the response variable using our custom struct
114+
//
115115
icmp_response_t* echo_response;
116116

117117
//
118-
// 12. Map our resposne to our response struct starting from byte 20
118+
// 12. Map our response to our response struct starting from byte 20
119119
//
120120
echo_response = (icmp_response_t *)&res[20];
121121

122-
//
123-
// -> Log the data that we'v got back
124-
//
122+
//
123+
// -> Log the data that we've got back
124+
//
125125
printf(
126126
"type: %x, code: %x, checksum: %x, identifier: %x, sequence: %x\n",
127127
echo_response->type,
@@ -132,8 +132,8 @@ int main() {
132132
);
133133

134134
exit(0);
135-
}
136-
else
135+
}
136+
else
137137
{
138138
perror("Response Error");
139139
exit(0);
@@ -151,12 +151,12 @@ int32_t checksum(uint16_t *buf, int32_t len)
151151
// 1. Variable needed for the calculation
152152
//
153153
int32_t nleft = len; // Save how big is the header
154-
int32_t sum = 0; // Container for the calcualted value
154+
int32_t sum = 0; // Container for the calculated value
155155
uint16_t *w = buf; // Save the first 2 bytes of the header
156-
uint16_t answer = 0; // The state of our final anwser
156+
uint16_t answer = 0; // The state of our final answer
157157

158158
//
159-
// 2. Summ evry other byte from the header
159+
// 2. Sum every other byte from the header
160160
//
161161
while(nleft > 1)
162162
{
@@ -165,12 +165,11 @@ int32_t checksum(uint16_t *buf, int32_t len)
165165
}
166166

167167
//
168-
// 3. No idea
168+
// 3. Handle odd headers
169169
//
170170
if(nleft == 1)
171171
{
172-
*(uint16_t *)(&answer) = *(uint8_t *)w;
173-
sum += answer;
172+
sum += *(uint8_t *)w;
174173
}
175174

176175
//

0 commit comments

Comments
 (0)