Skip to content

Commit 502b25e

Browse files
a bit cosmetic
1 parent 7084c25 commit 502b25e

File tree

1 file changed

+63
-60
lines changed

1 file changed

+63
-60
lines changed

sockets/RAW/icmp.c

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/* icmp.c: simple ICMP request implementaion
2+
*
3+
* Alexander Naumov <posix.ru@gmail.com>
4+
*
5+
*/
6+
17
#include <error.h>
28
#include <fcntl.h>
39
#include <unistd.h>
@@ -20,93 +26,90 @@ struct protoent *proto=NULL;
2026

2127
struct packet
2228
{
23-
struct icmphdr hdr;
24-
char msg[PACKETSIZE-sizeof(struct icmphdr)];
29+
struct icmphdr hdr;
30+
char msg[PACKETSIZE-sizeof(struct icmphdr)];
2531
};
2632

27-
2833
void usage()
2934
{
30-
printf("USAGE:\t ccns <IP_address>\n");
31-
exit(1);
35+
printf("USAGE:\ticmp <IP_address>\n");
36+
exit(1);
3237
}
3338

34-
3539
unsigned short checksum(void *b, int len)
3640
{
37-
unsigned short *buf = b;
38-
unsigned int sum=0;
39-
unsigned short result;
40-
41-
for(sum = 0; len > 1; len -= 2)
42-
sum += *buf++;
43-
if(len == 1)
44-
sum += *(unsigned char*)buf;
45-
sum = (sum >> 16) + (sum & 0xFFFF);
46-
sum += (sum >> 16);
47-
result = ~sum;
48-
return result;
41+
unsigned short *buf = b;
42+
unsigned int sum=0;
43+
unsigned short result;
44+
45+
for(sum = 0; len > 1; len -= 2)
46+
sum += *buf++;
47+
if(len == 1)
48+
sum += *(unsigned char*)buf;
49+
sum = (sum >> 16) + (sum & 0xFFFF);
50+
sum += (sum >> 16);
51+
result = ~sum;
52+
return result;
4953
}
5054

5155
int ping(char *host)
5256
{
53-
struct hostent *hname;
54-
struct sockaddr_in addr;
57+
struct hostent *hname;
58+
struct sockaddr_in addr;
5559

56-
proto = getprotobyname("ICMP");
57-
hname = gethostbyname(host);
60+
proto = getprotobyname("ICMP");
61+
hname = gethostbyname(host);
5862

59-
bzero(&addr, sizeof(addr));
63+
bzero(&addr, sizeof(addr));
6064

61-
addr.sin_family = hname->h_addrtype;
62-
addr.sin_port = 0;
63-
addr.sin_addr.s_addr = *(long*)hname->h_addr;
65+
addr.sin_family = hname->h_addrtype;
66+
addr.sin_port = 0;
67+
addr.sin_addr.s_addr = *(long*)hname->h_addr;
6468

65-
const int val=255;
66-
int i, sd, cnt=0;
67-
struct packet pckt;
68-
struct sockaddr_in r_addr;
69+
const int val=255;
70+
int i, sd, cnt=0;
71+
struct packet pckt;
72+
struct sockaddr_in r_addr;
6973

70-
sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
71-
if (sd < 0) {
72-
perror("socket");
73-
exit(1);
74-
}
74+
sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
75+
if (sd < 0) {
76+
perror("socket");
77+
exit(1);
78+
}
7579

76-
if (setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
77-
perror("Set TTL option");
80+
if (setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
81+
perror("Set TTL option");
7882

79-
if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
80-
perror("Request nonblocking I/O");
83+
if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
84+
perror("Request nonblocking I/O");
8185

82-
int count, icmp = 0;
83-
for(count = 0; count < 4; count++) {
84-
int len = sizeof(r_addr);
86+
int count, icmp = 0;
87+
for(count = 0; count < 4; count++) {
88+
int len = sizeof(r_addr);
8589

86-
bzero(&pckt, sizeof(pckt));
87-
pckt.hdr.type = ICMP_ECHO;
88-
pckt.hdr.un.echo.id = pid;
90+
bzero(&pckt, sizeof(pckt));
91+
pckt.hdr.type = ICMP_ECHO;
92+
pckt.hdr.un.echo.id = pid;
8993

90-
for(i = 0; i < sizeof(pckt.msg)-1; i++)
91-
pckt.msg[i] = i+'0';
94+
for(i = 0; i < sizeof(pckt.msg)-1; i++)
95+
pckt.msg[i] = i+'0';
9296

93-
pckt.msg[i] = 0;
94-
pckt.hdr.un.echo.sequence = cnt++;
95-
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
97+
pckt.msg[i] = 0;
98+
pckt.hdr.un.echo.sequence = cnt++;
99+
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
96100

97101
printf("Msg #%d\n", cnt);
98-
if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr, sizeof(addr)) <= 0 )
99-
perror("sendto");
102+
if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr, sizeof(addr)) <= 0 )
103+
perror("sendto");
100104

101-
sleep(1);
105+
sleep(1);
102106

103-
if (recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &len) > 0 ) {
104-
icmp++;
107+
if (recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &len) > 0 ) {
108+
icmp++;
105109
printf("***Got message!***\n");
106-
if (icmp > 1)
107-
return icmp;
108-
}
109-
110+
if (icmp > 1)
111+
return icmp;
112+
}
110113
}
111114
return icmp;
112115
}
@@ -120,7 +123,7 @@ int main(int argc, char *argv[])
120123
printf("PING\t\tOK\n");
121124
else
122125
printf("PING\t\tNO_ANSWER\n");
123-
126+
124127
return 0;
125128
}
126129

0 commit comments

Comments
 (0)