Advertisement
benjojo

Super Ping

Jul 20th, 2011
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <netinet/in.h>
  2. #include <netinet/ip.h>
  3. #include <netinet/ip_icmp.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <sys/signal.h>
  10. #include <string.h>
  11.  
  12. #define DEFDATALEN      56
  13. #define MAXIPLEN        60
  14. #define MAXICMPLEN      76
  15.  
  16. static char *hostname = NULL;
  17.  
  18. static int in_cksum(unsigned short *buf, int sz)
  19. {
  20.   int nleft = sz;
  21.   int sum = 0;
  22.   unsigned short *w = buf;
  23.   unsigned short ans = 0;
  24.  
  25.   while (nleft > 1) {
  26.     sum += *w++;
  27.     nleft -= 2;
  28.   }
  29.  
  30.   if (nleft == 1) {
  31.     *(unsigned char *) (&ans) = *(unsigned char *) w;
  32.     sum += ans;
  33.   }
  34.  
  35.   sum = (sum >> 16) + (sum & 0xFFFF);
  36.   sum += (sum >> 16);
  37. //   ans = .sum;
  38.   ans = ~sum;
  39.   return (ans);
  40. }
  41.  
  42. static void noresp(int ign)
  43. {
  44.   printf("No response from %s\n", hostname);
  45.   exit(0);
  46. }
  47.  
  48. static void ping(const char *host)
  49. {
  50.  
  51.   struct hostent *h;
  52.   struct sockaddr_in pingaddr;
  53.   struct icmp *pkt;
  54.   int pingsock, c;
  55.   char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
  56.  
  57.   if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) {       /* 1 == ICMP */
  58.     perror("ping: creating a raw socket");
  59.     exit(1);
  60.   }
  61.  
  62.   /* drop root privs if running setuid */
  63.   setuid(getuid());
  64.  
  65.   memset(&pingaddr, 0, sizeof(struct sockaddr_in));
  66.  
  67.   pingaddr.sin_family = AF_INET;
  68.   if (!(h = gethostbyname(host))) {
  69.     fprintf(stderr, "ping: unknown host %s\n", host);
  70.     exit(1);
  71.   }
  72.   memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
  73.   hostname = h->h_name;
  74.  
  75.   pkt = (struct icmp *) packet;
  76.   memset(pkt, 0, sizeof(packet));
  77.   pkt->icmp_type = ICMP_ECHO;
  78.   pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
  79.  
  80.   c = sendto(pingsock, packet, sizeof(packet), 0,
  81.              (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
  82.   if (c < 0 || c != sizeof(packet)) {
  83.     if (c < 0)
  84.       perror("ping: sendto");
  85.     fprintf(stderr, "ping: write incomplete\n");
  86.     exit(1);
  87.   }
  88.  
  89.   close(pingsock);
  90.  
  91.   return;
  92. }
  93.  
  94. int main ()
  95. {
  96. //printf( "Well fuck:" );
  97. int ip1; // X.X.X.1
  98. int ip2; // X.X.1.X
  99. int ip3; // X.1.X.X
  100. int ip4; // 1.X.X.X
  101. ip1 = 1;
  102. ip2 = 1;
  103. ip3 = 1;
  104. ip4 = 50;
  105. char s1[6];
  106. char s2[6];
  107. char s3[6];
  108. char s4[6];
  109. char full[50];
  110.  
  111.  
  112.  
  113. while(1){
  114. ip1++;
  115. if(ip1 == 254){
  116.     ip2++;
  117.     ip1 = 0;
  118. }
  119. if(ip2 == 254){
  120.     ip3++;
  121.     ip2 = 0;
  122.     printf( "\nIP: %s", full );
  123. }
  124. if(ip3 == 254){
  125.     ip4++;
  126.     ip3 = 0;
  127. }
  128.  
  129. sprintf(s1, "%d", ip1);
  130. sprintf(s2, "%d", ip2);
  131. sprintf(s3, "%d", ip3);
  132. sprintf(s4, "%d", ip4);
  133.  
  134. /*
  135.   strcpy( full, s1 );
  136.   strcat( full, "." );
  137.   strcpy( full, s2 );
  138.   strcat( full, "." );
  139.   strcpy( full, s3 );
  140.   strcat( full, "." );
  141.   strcpy( full, s4 ); */
  142.   sprintf( full, "%s.%s.%s.%s", s4, s3, s2, s1 );
  143.  
  144.   //printf( "\nIP: %s", full );
  145.  
  146.  
  147. ping (full);
  148.  
  149. }
  150. return;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement