Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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").
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.
/*
* 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;
/*
* 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));
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");
}
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);
}
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???
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 ?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.