LQ Newbie
Registered: Jan 2012
Posts: 7
Original Poster
Rep:
|
Here you go...Plz help me.
#include <sys/ioctl.h>
#include <netinet/in.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>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
#define MAX_NUMBER_OF_NODES 5
struct cal_nodes{
int rackno;
int slotno;
int instanceno;
int card_type;
int node_type;
int node_state;
unsigned int nodeIP;
char* ipaddr;
};
struct ping_param {
int nbytes;
char interface[10];
int iRetries;
int inpackets;
int idelay;
int idelaybtwpings;
};
struct cal_nodes node_details[MAX_NUMBER_OF_NODES];
void* PingNodes(char* argv);
unsigned short in_cksum(unsigned short *, int);
void parse_argvs(char**, char*, char* );
void usage();
char* getip();
void* PingingThread(void * pa)
{
int iter = 0;
struct ping_param param;
param.nbytes = 100;
strcpy(param.interface,"eth-vf1.3073");
param.iRetries = 5;
param.inpackets = 1;
param.idelay = 2000;
param.idelaybtwpings= 300;
while(iter < 4)
{
PingNodes(node_details[iter].ipaddr);//here IP address is coming as properly. I have printed and verified.
iter++;
}
return 0;
}
void* PingNodes(char* argv)
{
struct sockaddr_in connection;
struct iphdr* ip;
struct iphdr* ip_reply;
struct icmphdr* icmp;
char* SendBuff;
char* RecvBuff;
char* RecvBuff1;
char* packet;
char* buffer;
char* buffer1;
int sockfd1;
int sockfd;
int optval;
int addrlen;
int err;
struct ifreq Interface;
int ibytes;
fd_set read_flags,write_flags,exce_flags;
struct timeval waitd;
char dst_addr[15];
char src_addr[15];
strcpy(dst_addr,argv);
/*
* allocate all necessary memory
*/
SendBuff = malloc(70);
RecvBuff = malloc(70);
RecvBuff1 = malloc(70);
if (SendBuff && RecvBuff && RecvBuff1)
{
memset(SendBuff,0,70);
memset(RecvBuff,0,70);
memset(RecvBuff1,0,70);
}
if ((sockfd1 = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
{
perror("socket sockfd1 Creation Failure");
exit(EXIT_FAILURE);
}
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
{
perror("socket sockfd Creation Failure");
exit(EXIT_FAILURE);
}
setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
memset(&Interface, 0, sizeof(Interface));
strncpy(Interface.ifr_ifrn.ifrn_name, "eth-vf1.3073", IFNAMSIZ);
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &Interface, sizeof(Interface)) < 0)
{
printf("Error in binding");
close(sockfd);
}
ioctl(sockfd, SIOCGIFADDR, &Interface);
printf("%s\n", inet_ntoa(((struct sockaddr_in *)&Interface.ifr_addr)->sin_addr));
strcpy(src_addr,inet_ntoa(((struct sockaddr_in *)&Interface.ifr_addr)->sin_addr));
printf("Source address: %s\n", src_addr);
ip = malloc(sizeof(struct iphdr));
ip_reply = malloc(sizeof(struct iphdr));
icmp = malloc(sizeof(struct icmphdr));
packet = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
buffer1 = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
buffer = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
memset(packet,0,sizeof(struct iphdr) + sizeof(struct icmphdr));
memset(buffer1,0,sizeof(struct iphdr) + sizeof(struct icmphdr));
memset(buffer,0,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);
icmp->type = 8;
icmp->code = 0;
icmp->un.echo.id = 0;
icmp->un.echo.sequence = 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
*/
if((ibytes = sendto(sockfd, packet, ip->tot_len, 0, (struct sockaddr *)&connection, sizeof(struct sockaddr))) <= 0 )
{
printf("Sendto Error\n");
} else {
printf("\nSent %d byte packet to %s and %ul\n", (int)ibytes, dst_addr,connection.sin_addr.s_addr );
ibytes = 0;
}
addrlen = sizeof(connection);
while(1)
{
waitd.tv_sec = 90; // Make select wait up to 90 second for data
waitd.tv_usec = 0; // and 0 milliseconds.
FD_ZERO(&read_flags); // Zero the flags ready for using
FD_ZERO(&write_flags);
FD_ZERO(&exce_flags);
FD_SET(sockfd1, &read_flags);
FD_SET(sockfd, &read_flags);
FD_SET(sockfd1, &write_flags);
FD_SET(sockfd, &write_flags);
FD_SET(sockfd1, &exce_flags);
FD_SET(sockfd, &exce_flags);
err=select(sockfd+1, &read_flags,&write_flags,&exce_flags,&waitd);
if(err < 0)
{
printf("\nTimeout in Select\n");
continue;
}
if(FD_ISSET(sockfd, &read_flags))
{
if ((ibytes = recvfrom(sockfd, buffer1, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen)) == -1)
{
printf("Hello Pavan you got error in recvfrom \n");
perror("recvfrom2 failed");
}
else
{
printf("Received same address ethvf1.3073 %d byte reply from %s ; %ul:\n", (int)ibytes, dst_addr,connection.sin_addr.s_addr);
ip_reply = (struct iphdr*)buffer1;
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
}
if ((ibytes=recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)0, (int*)0))>0)
{
printf("\nNo.of bytes received on sockfd eth-vf1.3073 deom any address: %d",ibytes);
ibytes = 0;
ip_reply = (struct iphdr*)buffer;
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
}else {
printf("\nrecvfrom Failed on sockfdnew: %d\n",ibytes);
perror("recvfrom0 failed");
}
FD_CLR(sockfd, &read_flags);
}
if(FD_ISSET(sockfd1, &read_flags))
{
if ((ibytes = recvfrom(sockfd1, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen))
== -1)
{
printf("Hello Pavan you got error in recvfrom \n");
perror("recvfrom2 failed");
}
else
{
printf("Received %d byte reply from1.general.same address. %s ; %ul:\n", (int)ibytes, dst_addr,connection.sin_addr.s_addr);
ip_reply = (struct iphdr*)buffer;
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
break;
}
FD_CLR(sockfd1, &read_flags);
}
if(FD_ISSET(sockfd, &exce_flags))
{
printf("\nException in sockfd\n");
}
}
close(sockfd1);
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);
printf("\n parse_argvs1.src : %s \n",src);
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);
printf("\n parse_argvs2.src : %s \n",src);
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);
printf("\nGethostname returns : %s\n",inet_ntoa(*(struct in_addr *)h->h_addr));
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);
}
int main(int argc, char* argv[])
{
int rvalue1 = 0;
int rvalue2 = 0;
pthread_t CMThread;
pthread_t PingThread;
rvalue2 = pthread_create(&PingThread, NULL,PingingThread, (void *)0 );
if (rvalue2 != 0) {
perror("pthread_create error");
printf("error in thread creation\n");
return 0;
}
pthread_join(CMThread, NULL);
pthread_join(PingThread, NULL);
return 0;
}
|