LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2012, 11:40 PM   #1
pavaneee
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Rep: Reputation: Disabled
Unhappy Ping Response Is Not Coming


Hi All,

This is my first post in this forum.Please help me.

I need to use ping utility which is there in linux. Could you please let me know what are the exposed APIs for sending and receiving ICMP echo request/response.

I have written small ping application.But i am not able to get response for ICMP echo request.When i used standard ping X.X.X.X response is coming properly.

Plz note that i do not want to use this command in my code. system("ping xxx.xxx.xxx.xxx").

Code snippets would be of great help.

Thanks a ton in advance.
 
Old 01-12-2012, 12:50 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello and welcome to LinuxQuestions,

Apparently your program has an error/flaw somewhere since the regular ping (included in the system) works and yours doesn't. If asking questions about code it's always a good idea to post the relevant code here so that LQ users can have a look at it and indicate what's missing or erroneous. Also, when you post a specific question, like in this case about programming, post it in the adequate forum or if you don't know where to post, in Linux Newbie. The moderation team would then move it if needed to the correct forum so that your question gets the attention it deserves.

Looking forward to your participation in the forums.

Kind regards,

Eric
 
Old 01-12-2012, 05:48 AM   #3
pavaneee
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Here you go :

/*
* pinger.c
* This is a ping imitation program
* It will send an ICMP ECHO packet to the server of
* your choice and listen for an ICMP REPLY packet
* Have fun!
*/
#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[15];
char src_addr[15];

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

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;

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

parse_argvs(argv, dst_addr, src_addr);
printf("Source address: %s\n", src_addr);
printf("Destination address: %s\n", dst_addr);

/*
* allocate all necessary memory
*/
ip = malloc(sizeof(struct iphdr));
ip_reply = malloc(sizeof(struct iphdr));
icmp = malloc(sizeof(struct icmphdr));
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 except checksum
*/
ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct icmphdr);
ip->id = htons(random());
ip->ttl = 255;
ip->protocol = IPPROTO_ICMP;
ip->saddr = inet_addr(src_addr);
ip->daddr = inet_addr(dst_addr);


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 = 0;
icmp->un.echo.sequence = 0;
icmp->checksum = 0;
icmp-> checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));

ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));

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", sizeof(packet), dst_addr);

/*
* now we listen for responses
*/
addrlen = sizeof(connection);
if (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", sizeof(buffer), dst_addr);
ip_reply = (struct iphdr*) buffer;
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
}
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);

}
/*
* 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);
}
 
Old 01-13-2012, 09:10 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by pavaneee View Post
Hi All,
This is my first post in this forum.Please help me.

I need to use ping utility which is there in linux. Could you please let me know what are the exposed APIs for sending and receiving ICMP echo request/response. I have written small ping application.But i am not able to get response for ICMP echo request.When i used standard ping X.X.X.X response is coming properly.

Plz note that i do not want to use this command in my code. system("ping xxx.xxx.xxx.xxx"). Code snippets would be of great help.
Thanks a ton in advance.
Not sure what you're asking here. You say your system is working fine, and the built-in ping utility is working fine, but the program you wrote isn't. Are you wanting us to debug your program for you???

And why can't you use the system-included ping?? And have you tried Google, since there are many existing code samples for doing this?
http://www.linuxforums.org/forum/net...ng-ping-c.html
http://www.cplusplus.com/forum/unices/4078/
 
Old 01-17-2012, 07:19 AM   #5
pavaneee
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
I guess...I found the solution for this problem.
I have many virtual interfaces as follows :
$ ifconfig
eth-vf1 Link encap:Ethernet HWaddr 00:11:22:00:01:00
inet addr:192.0.1.2 Bcast:192.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::211:22ff:fe00:100/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:687 errors:0 dropped:0 overruns:0 frame:0
TX packets:428 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:589959 (576.1 KiB) TX bytes:32756 (31.9 KiB)

eth-vf1.513 Link encap:Ethernet HWaddr 92:01:01:00:00:11
inet6 addr: fe80::9001:1ff:fe00:11/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:60 (60.0 b) TX bytes:492 (492.0 b)

eth-vf1.3073 Link encap:Ethernet HWaddr 92:01:01:00:00:11
inet addr:192.0.4.1 Bcast:192.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::9001:1ff:fe00:11/64 Scope:Link
UP BROADCAST RUNNING ALLMULTI MULTICAST MTU:1500 Metric:1
RX packets:89 errors:0 dropped:0 overruns:0 frame:0
TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7069 (6.9 KiB) TX bytes:4959 (4.8 KiB)

eth0 Link encap:Ethernet HWaddr 92:01:00:00:00:11
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::9001:ff:fe00:11/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:134 errors:0 dropped:0 overruns:0 frame:0
TX packets:126 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:20209 (19.7 KiB) TX bytes:18241 (17.8 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3747 errors:0 dropped:0 overruns:0 frame:0
TX packets:3747 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:224545 (219.2 KiB) TX bytes:224545 (219.2 KiB)

I am sending ping request thorugh eth-vf1.3073 interface.But always response is coming to eth-vf1 interface. Even standard linux ping utility is also behaving in this way.I have verified this behaviour with tcpdump.Plz help me in finding the reason for this ?

Thanks,
Pavan
 
Old 01-17-2012, 09:32 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by pavaneee View Post
I guess...I found the solution for this problem.
I have many virtual interfaces as follows :

I am sending ping request thorugh eth-vf1.3073 interface.But always response is coming to eth-vf1 interface. Even standard linux ping utility is also behaving in this way.I have verified this behaviour with tcpdump.
Plz help me in finding the reason for this ?
Spell out your words. And it would have been nice if you had mentioned the virtual interfaces in the beginning. Did you read the man page on the Linux ping utility??
Code:
-B     Do not allow ping to change source address of probes.  The address is bound to one 
       selected when ping starts.

...and....

-I interface address
      Set  source  address  to  specified  interface  address.  Argument may be numeric IP 
      address or name of device. When pinging IPv6 link-local address this option is required.
..two ways to make sure the packets return to the same address/interface.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
no ping response from local dsl jmaron Linux - Networking 4 11-01-2011 11:31 AM
Ping rounding response times wookaka Linux - Server 1 10-15-2010 07:46 AM
how to stop ping response/count manuleka Linux - Newbie 1 07-02-2009 05:08 AM
No Ping Response neoform Linux - Networking 5 05-20-2009 04:46 AM
no ping response ewan Linux - Networking 5 07-24-2003 02:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration