|
| 1 | +#include <error.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sys/types.h> |
| 5 | +#include <sys/utsname.h> |
| 6 | +#include <sys/socket.h> |
| 7 | +#include <netinet/in.h> |
| 8 | +#include <netdb.h> |
| 9 | +#include <string.h> |
| 10 | +#include <errno.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <resolv.h> |
| 14 | +#include <netinet/ip_icmp.h> |
| 15 | + |
| 16 | +#define PACKETSIZE 64 |
| 17 | + |
| 18 | +int pid=-1; |
| 19 | +struct protoent *proto=NULL; |
| 20 | + |
| 21 | +struct packet |
| 22 | +{ |
| 23 | + struct icmphdr hdr; |
| 24 | + char msg[PACKETSIZE-sizeof(struct icmphdr)]; |
| 25 | +}; |
| 26 | + |
| 27 | + |
| 28 | +void usage() |
| 29 | +{ |
| 30 | + printf("USAGE:\t ccns <IP_address>\n"); |
| 31 | + exit(1); |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +unsigned short checksum(void *b, int len) |
| 36 | +{ |
| 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; |
| 49 | +} |
| 50 | + |
| 51 | +int ping(char *host) |
| 52 | +{ |
| 53 | + struct hostent *hname; |
| 54 | + struct sockaddr_in addr; |
| 55 | + |
| 56 | + proto = getprotobyname("ICMP"); |
| 57 | + hname = gethostbyname(host); |
| 58 | + |
| 59 | + bzero(&addr, sizeof(addr)); |
| 60 | + |
| 61 | + addr.sin_family = hname->h_addrtype; |
| 62 | + addr.sin_port = 0; |
| 63 | + addr.sin_addr.s_addr = *(long*)hname->h_addr; |
| 64 | + |
| 65 | + const int val=255; |
| 66 | + int i, sd, cnt=0; |
| 67 | + struct packet pckt; |
| 68 | + struct sockaddr_in r_addr; |
| 69 | + |
| 70 | + sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); |
| 71 | + if (sd < 0) { |
| 72 | + perror("socket"); |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + if (setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) |
| 77 | + perror("Set TTL option"); |
| 78 | + |
| 79 | + if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) |
| 80 | + perror("Request nonblocking I/O"); |
| 81 | + |
| 82 | + int count, icmp = 0; |
| 83 | + for(count = 0; count < 4; count++) { |
| 84 | + int len = sizeof(r_addr); |
| 85 | + |
| 86 | + bzero(&pckt, sizeof(pckt)); |
| 87 | + pckt.hdr.type = ICMP_ECHO; |
| 88 | + pckt.hdr.un.echo.id = pid; |
| 89 | + |
| 90 | + for(i = 0; i < sizeof(pckt.msg)-1; i++) |
| 91 | + pckt.msg[i] = i+'0'; |
| 92 | + |
| 93 | + pckt.msg[i] = 0; |
| 94 | + pckt.hdr.un.echo.sequence = cnt++; |
| 95 | + pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); |
| 96 | + |
| 97 | + printf("Msg #%d\n", cnt); |
| 98 | + if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr, sizeof(addr)) <= 0 ) |
| 99 | + perror("sendto"); |
| 100 | + |
| 101 | + sleep(1); |
| 102 | + |
| 103 | + if (recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &len) > 0 ) { |
| 104 | + icmp++; |
| 105 | + printf("***Got message!***\n"); |
| 106 | + if (icmp > 1) |
| 107 | + return icmp; |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + return icmp; |
| 112 | +} |
| 113 | + |
| 114 | +int main(int argc, char *argv[]) |
| 115 | +{ |
| 116 | + if(argv[1] == NULL) |
| 117 | + usage(); |
| 118 | + |
| 119 | + if (ping(argv[1]) > 0) |
| 120 | + printf("PING\t\tOK\n"); |
| 121 | + else |
| 122 | + printf("PING\t\tNO_ANSWER\n"); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
0 commit comments