Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2012, 03:15 AM   PM User | #1
Integral
New Coder

 
Join Date: Oct 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Integral is an unknown quantity at this point
"Ping" command in C?

I would like to know if it is possible to write a program in C that allows the user to use the "ping" command from command prompt on a windows machine and enter an IP address using the program.

Ex.

Enter an IP address you would like to ping:

User enters: 192.168.1.0

Programs will then execute the command prompt "ping" command on the IP address the user entered.

I just can't find anywhere how to call this ping command from the application and put that into the source code of the program. Any help?
Integral is offline   Reply With Quote
Old 02-05-2012, 05:46 AM   PM User | #2
crank01
New Coder

 
crank01's Avatar
 
Join Date: Jan 2011
Posts: 96
Thanks: 10
Thanked 2 Times in 2 Posts
crank01 is an unknown quantity at this point
why do you need a program to ping an IP when you have command prompt?

and please tell me you're not trying to build a ddos tool
crank01 is offline   Reply With Quote
Old 02-05-2012, 06:28 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You could use one of the exec or spawn variants to simply call ping directly.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-05-2012, 08:16 AM   PM User | #4
Integral
New Coder

 
Join Date: Oct 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Integral is an unknown quantity at this point
Quote:
Originally Posted by crank01 View Post
why do you need a program to ping an IP when you have command prompt?

and please tell me you're not trying to build a ddos tool
I was showing a program I made to a friends in class last week that allowed for an increment number of loops. My friend then told me that if it were to "ping" IP addresses you could potentially DDOS a site.

Trust me, I have no interest in bringing down websites; I was just intrigued by the idea my friend noted and thought it would be interesting if I was able to make such a tool. It's a mere programming exercise, that is all.
Integral is offline   Reply With Quote
Old 02-05-2012, 04:25 PM   PM User | #5
crank01
New Coder

 
crank01's Avatar
 
Join Date: Jan 2011
Posts: 96
Thanks: 10
Thanked 2 Times in 2 Posts
crank01 is an unknown quantity at this point
Quote:
Originally Posted by Integral View Post
I was showing a program I made to a friends in class last week that allowed for an increment number of loops. My friend then told me that if it were to "ping" IP addresses you could potentially DDOS a site.

Trust me, I have no interest in bringing down websites; I was just intrigued by the idea my friend noted and thought it would be interesting if I was able to make such a tool. It's a mere programming exercise, that is all.
well you'll need a number of computers to ping a website to even slow it down, let alone ddos it. Using google I was able to find you what you're looking for. I don't condone the use of these "kiddie" tools but the below code will show you that its more complicated then you think.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <string.h>
#include <unistd.h>


char dst_addr[20];
char src_addr[20];

unsigned short in_cksum(unsigned short *, int);
void parse_argvs(char**, char*, char* );
void usage();
char* getip();
char* toip(char*);

int main(int argc, char* argv[])
{
    struct iphdr* ip;
    struct iphdr* ip_reply;
    struct icmphdr* icmp;
    struct sockaddr_in connection;
    char* packet;
    char* buffer;
    int sockfd;
    int optval;
    int addrlen;
    int siz;

    
    if (getuid() != 0)
    {
	fprintf(stderr, "%s: root privelidges needed\n", *(argv + 0));
	exit(EXIT_FAILURE);
    }

    parse_argvs(argv, dst_addr, src_addr);
    strncpy(dst_addr, toip(dst_addr), 20);
    strncpy(src_addr, toip(src_addr), 20);
    printf("Source address: %s\n", src_addr);
    printf("Destination address: %s\n", dst_addr);
    
    /*
     * allocate all necessary memory
    */
    packet = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
    buffer = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
    /****************************************************************/
    
    ip = (struct iphdr*) packet;
    icmp = (struct icmphdr*) (packet + sizeof(struct iphdr));
    
    /*  
     *	here the ip packet is set up
     */
ip->ihl			= 5;
    ip->version			= 4;
    ip->tos			= 0;
    ip->tot_len			= sizeof(struct iphdr) + sizeof(struct icmphdr);
    ip->id			= htons(0);
    ip->frag_off		= 0;
    ip->ttl			= 64;
    ip->protocol		= IPPROTO_ICMP;
    ip->saddr			= inet_addr(src_addr);
    ip->daddr			= inet_addr(dst_addr);
    ip->check			= in_cksum((unsigned short *)ip, sizeof(struct iphdr));
    
    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
    {
	perror("socket");
	exit(EXIT_FAILURE);
    }
    
    /* 
     *	IP_HDRINCL must be set on the socket so that
     *	the kernel does not attempt to automatically add
     *	a default ip header to the packet
     */
    
    setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
    
    /*
     *	here the icmp packet is created
     *	also the ip checksum is generated
     */
    icmp->type			= ICMP_ECHO;
    icmp->code			= 0;
    icmp->un.echo.id		= random();
    icmp->un.echo.sequence	= 0;
    icmp-> checksum		= in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
    
    
    connection.sin_family = AF_INET;
    connection.sin_addr.s_addr = inet_addr(dst_addr);
    
    /*
     *	now the packet is sent
     */
    
    sendto(sockfd, packet, ip->tot_len, 0, (struct sockaddr *)&connection, sizeof(struct sockaddr));
    printf("Sent %d byte packet to %s\n", ip->tot_len, dst_addr);
    
    /*
     *	now we listen for responses
     */
    addrlen = sizeof(connection);
    if (( siz = recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen)) == -1)
    {
	perror("recv");
    }
    else
    {
	printf("Received %d byte reply from %s:\n", siz , dst_addr);
        ip_reply = (struct iphdr*) buffer;
	printf("ID: %d\n", ntohs(ip_reply->id));
	printf("TTL: %d\n", ip_reply->ttl);
    }

    free(packet);
    free(buffer);
    close(sockfd);
    return 0;
}
void parse_argvs(char** argv, char* dst, char* src)
{
    int i;
    if(!(*(argv + 1))) 
    {
	/* there are no options on the command line */
	usage();
	exit(EXIT_FAILURE);	
    }
    if (*(argv + 1) && (!(*(argv + 2)))) 
    {
	/* 
	 *   only one argument provided
	 *   assume it is the destination server
	 *   source address is local host
	 */
	strncpy(dst, *(argv + 1), 15);
	strncpy(src, getip(), 15);
	return;
    }
    else if ((*(argv + 1) && (*(argv + 2))))
    {
	/* 
	 *    both the destination and source address are defined
	 *    for now only implemented is a source address and 
	 *    destination address
	 */
	strncpy(dst, *(argv + 1), 15);
	i = 2;
	while(*(argv + i + 1))
	{
	    if (strncmp(*(argv + i), "-s", 2) == 0)
	    {
		strncpy(src, *(argv + i + 1), 15);
		break;
	    }
	    i++;
	}
    }

}

void usage()
{
    fprintf(stderr, "\nUsage: pinger [destination] <-s [source]>\n");
    fprintf(stderr, "Destination must be provided\n");
    fprintf(stderr, "Source is optional\n\n");
}

char* getip()
{
    char buffer[256];
    struct hostent* h;
    
    gethostname(buffer, 256);
    h = gethostbyname(buffer);
    
    return inet_ntoa(*(struct in_addr *)h->h_addr);
    
}

/*
 * return the ip address if host provided by DNS name
 */
char* toip(char* address)
{
    struct hostent* h;
    h = gethostbyname(address);
    return inet_ntoa(*(struct in_addr *)h->h_addr);
}

/*
 * in_cksum --
 * Checksum routine for Internet Protocol
 * family headers (C Version)
 */
unsigned short in_cksum(unsigned short *addr, int len)
{
    register int sum = 0;
    u_short answer = 0;
    register u_short *w = addr;
    register int nleft = len;
    /*
     * Our algorithm is simple, using a 32 bit accumulator (sum), we add
     * sequential 16 bit words to it, and at the end, fold back all the
     * carry bits from the top 16 bits into the lower 16 bits.
     */
    while (nleft > 1)
    {
	  sum += *w++;
	  nleft -= 2;
    }
    /* mop up an odd byte, if necessary */
    if (nleft == 1)
    {
	  *(u_char *) (&answer) = *(u_char *) w;
	  sum += answer;
    }
    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);		/* add hi 16 to low 16 */
    sum += (sum >> 16);				/* add carry */
    answer = ~sum;				/* truncate to 16 bits */
    return (answer);
}
also http://research.microsoft.com/en-us/...ing/ping.c.htm
As you can see, its a lot more complicated then you think if you're trying to write it yourself and show your friends that you've made it.
crank01 is offline   Reply With Quote
Old 02-06-2012, 12:46 AM   PM User | #6
Integral
New Coder

 
Join Date: Oct 2011
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Integral is an unknown quantity at this point
Quote:
Originally Posted by crank01 View Post
well you'll need a number of computers to ping a website to even slow it down, let alone ddos it. Using google I was able to find you what you're looking for. I don't condone the use of these "kiddie" tools but the below code will show you that its more complicated then you think.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <string.h>
#include <unistd.h>


char dst_addr[20];
char src_addr[20];

unsigned short in_cksum(unsigned short *, int);
void parse_argvs(char**, char*, char* );
void usage();
char* getip();
char* toip(char*);

int main(int argc, char* argv[])
{
    struct iphdr* ip;
    struct iphdr* ip_reply;
    struct icmphdr* icmp;
    struct sockaddr_in connection;
    char* packet;
    char* buffer;
    int sockfd;
    int optval;
    int addrlen;
    int siz;

    
    if (getuid() != 0)
    {
	fprintf(stderr, "%s: root privelidges needed\n", *(argv + 0));
	exit(EXIT_FAILURE);
    }

    parse_argvs(argv, dst_addr, src_addr);
    strncpy(dst_addr, toip(dst_addr), 20);
    strncpy(src_addr, toip(src_addr), 20);
    printf("Source address: %s\n", src_addr);
    printf("Destination address: %s\n", dst_addr);
    
    /*
     * allocate all necessary memory
    */
    packet = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
    buffer = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
    /****************************************************************/
    
    ip = (struct iphdr*) packet;
    icmp = (struct icmphdr*) (packet + sizeof(struct iphdr));
    
    /*  
     *	here the ip packet is set up
     */
ip->ihl			= 5;
    ip->version			= 4;
    ip->tos			= 0;
    ip->tot_len			= sizeof(struct iphdr) + sizeof(struct icmphdr);
    ip->id			= htons(0);
    ip->frag_off		= 0;
    ip->ttl			= 64;
    ip->protocol		= IPPROTO_ICMP;
    ip->saddr			= inet_addr(src_addr);
    ip->daddr			= inet_addr(dst_addr);
    ip->check			= in_cksum((unsigned short *)ip, sizeof(struct iphdr));
    
    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
    {
	perror("socket");
	exit(EXIT_FAILURE);
    }
    
    /* 
     *	IP_HDRINCL must be set on the socket so that
     *	the kernel does not attempt to automatically add
     *	a default ip header to the packet
     */
    
    setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
    
    /*
     *	here the icmp packet is created
     *	also the ip checksum is generated
     */
    icmp->type			= ICMP_ECHO;
    icmp->code			= 0;
    icmp->un.echo.id		= random();
    icmp->un.echo.sequence	= 0;
    icmp-> checksum		= in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
    
    
    connection.sin_family = AF_INET;
    connection.sin_addr.s_addr = inet_addr(dst_addr);
    
    /*
     *	now the packet is sent
     */
    
    sendto(sockfd, packet, ip->tot_len, 0, (struct sockaddr *)&connection, sizeof(struct sockaddr));
    printf("Sent %d byte packet to %s\n", ip->tot_len, dst_addr);
    
    /*
     *	now we listen for responses
     */
    addrlen = sizeof(connection);
    if (( siz = recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen)) == -1)
    {
	perror("recv");
    }
    else
    {
	printf("Received %d byte reply from %s:\n", siz , dst_addr);
        ip_reply = (struct iphdr*) buffer;
	printf("ID: %d\n", ntohs(ip_reply->id));
	printf("TTL: %d\n", ip_reply->ttl);
    }

    free(packet);
    free(buffer);
    close(sockfd);
    return 0;
}
void parse_argvs(char** argv, char* dst, char* src)
{
    int i;
    if(!(*(argv + 1))) 
    {
	/* there are no options on the command line */
	usage();
	exit(EXIT_FAILURE);	
    }
    if (*(argv + 1) && (!(*(argv + 2)))) 
    {
	/* 
	 *   only one argument provided
	 *   assume it is the destination server
	 *   source address is local host
	 */
	strncpy(dst, *(argv + 1), 15);
	strncpy(src, getip(), 15);
	return;
    }
    else if ((*(argv + 1) && (*(argv + 2))))
    {
	/* 
	 *    both the destination and source address are defined
	 *    for now only implemented is a source address and 
	 *    destination address
	 */
	strncpy(dst, *(argv + 1), 15);
	i = 2;
	while(*(argv + i + 1))
	{
	    if (strncmp(*(argv + i), "-s", 2) == 0)
	    {
		strncpy(src, *(argv + i + 1), 15);
		break;
	    }
	    i++;
	}
    }

}

void usage()
{
    fprintf(stderr, "\nUsage: pinger [destination] <-s [source]>\n");
    fprintf(stderr, "Destination must be provided\n");
    fprintf(stderr, "Source is optional\n\n");
}

char* getip()
{
    char buffer[256];
    struct hostent* h;
    
    gethostname(buffer, 256);
    h = gethostbyname(buffer);
    
    return inet_ntoa(*(struct in_addr *)h->h_addr);
    
}

/*
 * return the ip address if host provided by DNS name
 */
char* toip(char* address)
{
    struct hostent* h;
    h = gethostbyname(address);
    return inet_ntoa(*(struct in_addr *)h->h_addr);
}

/*
 * in_cksum --
 * Checksum routine for Internet Protocol
 * family headers (C Version)
 */
unsigned short in_cksum(unsigned short *addr, int len)
{
    register int sum = 0;
    u_short answer = 0;
    register u_short *w = addr;
    register int nleft = len;
    /*
     * Our algorithm is simple, using a 32 bit accumulator (sum), we add
     * sequential 16 bit words to it, and at the end, fold back all the
     * carry bits from the top 16 bits into the lower 16 bits.
     */
    while (nleft > 1)
    {
	  sum += *w++;
	  nleft -= 2;
    }
    /* mop up an odd byte, if necessary */
    if (nleft == 1)
    {
	  *(u_char *) (&answer) = *(u_char *) w;
	  sum += answer;
    }
    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);		/* add hi 16 to low 16 */
    sum += (sum >> 16);				/* add carry */
    answer = ~sum;				/* truncate to 16 bits */
    return (answer);
}
also http://research.microsoft.com/en-us/...ing/ping.c.htm
As you can see, its a lot more complicated then you think if you're trying to write it yourself and show your friends that you've made it.
Thanks for your post.

The idea of DDOS was just a concept, nothing more. I just wanted to see if something of the sorts could be done; thanks again for the code you posted and the fact that it's "harder than it looks" (as I expected it to be).
Integral is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:32 PM.


Advertisement
Log in to turn off these ads.