@@ -42,33 +42,33 @@ int main() {
42
42
icmp_hdr_t pckt ;
43
43
44
44
//
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
46
46
//
47
47
pckt .type = 8 ; // The echo request is 8
48
48
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
50
50
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
52
52
pckt .data = 0 ; // We don't send anything.
53
53
54
- //
54
+ //
55
55
// 5. Calculate the checksum based on the whole header, and only then
56
56
// you add it to the header.
57
- //
57
+ //
58
58
pckt .chksum = checksum ((uint16_t * )& pckt , sizeof (pckt ));
59
59
60
60
//
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
+ //
63
63
struct sockaddr_in addr ;
64
64
addr .sin_family = AF_INET ;
65
65
addr .sin_port = 0 ;
66
66
addr .sin_addr .s_addr = inet_addr ("8.8.8.8" );
67
67
68
68
//
69
- // 7. Send our PING
69
+ // 7. Send our PING
70
70
//
71
- int actionSendResult = sendto (s , & pckt , sizeof (pckt ),
71
+ int actionSendResult = sendto (s , & pckt , sizeof (pckt ),
72
72
0 , (struct sockaddr * )& addr , sizeof (addr ));
73
73
74
74
//
@@ -81,15 +81,15 @@ int main() {
81
81
}
82
82
83
83
//
84
- // 8. Prepare all the necesary variable to handle the response
84
+ // 8. Prepare all the necessary variable to handle the response
85
85
//
86
86
unsigned int resAddressSize ;
87
87
unsigned char res [30 ] = "" ;
88
88
struct sockaddr resAddress ;
89
89
90
- //
90
+ //
91
91
// 9. Creating the struct to better handle the response
92
- //
92
+ //
93
93
typedef struct {
94
94
uint8_t type ;
95
95
uint8_t code ;
@@ -101,27 +101,27 @@ int main() {
101
101
//
102
102
// 10. Read the response from the remote host
103
103
//
104
- int ressponse = recvfrom (s , res , sizeof (res ), 0 , & resAddress ,
104
+ int ressponse = recvfrom (s , res , sizeof (res ), 0 , & resAddress ,
105
105
& resAddressSize );
106
106
107
107
//
108
- // -> Display the response by accessign the struct
108
+ // -> Display the response by accessing the struct
109
109
//
110
110
if (ressponse > 0 )
111
111
{
112
- //
113
- // 11. Create the response variable usign our custom struct
114
- //
112
+ //
113
+ // 11. Create the response variable using our custom struct
114
+ //
115
115
icmp_response_t * echo_response ;
116
116
117
117
//
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
119
119
//
120
120
echo_response = (icmp_response_t * )& res [20 ];
121
121
122
- //
123
- // -> Log the data that we'v got back
124
- //
122
+ //
123
+ // -> Log the data that we've got back
124
+ //
125
125
printf (
126
126
"type: %x, code: %x, checksum: %x, identifier: %x, sequence: %x\n" ,
127
127
echo_response -> type ,
@@ -132,8 +132,8 @@ int main() {
132
132
);
133
133
134
134
exit (0 );
135
- }
136
- else
135
+ }
136
+ else
137
137
{
138
138
perror ("Response Error" );
139
139
exit (0 );
@@ -151,12 +151,12 @@ int32_t checksum(uint16_t *buf, int32_t len)
151
151
// 1. Variable needed for the calculation
152
152
//
153
153
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
155
155
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
157
157
158
158
//
159
- // 2. Summ evry other byte from the header
159
+ // 2. Sum every other byte from the header
160
160
//
161
161
while (nleft > 1 )
162
162
{
@@ -165,12 +165,11 @@ int32_t checksum(uint16_t *buf, int32_t len)
165
165
}
166
166
167
167
//
168
- // 3. No idea
168
+ // 3. Handle odd headers
169
169
//
170
170
if (nleft == 1 )
171
171
{
172
- * (uint16_t * )(& answer ) = * (uint8_t * )w ;
173
- sum += answer ;
172
+ sum += * (uint8_t * )w ;
174
173
}
175
174
176
175
//
0 commit comments