当前位置: 技术问答>linux和unix
为什么这个unix 版的ping 在 linux下编译无法通过?
来源: 互联网 发布时间:2015-03-30
本文导语: 为什么这个unix 版的ping 在 linux下编译无法通过? 出现了几百行各式各样的错误! 大家给在linux下编译一下吧,到底怎么回事啊? /* PING.C /* /* ping source code distribute by cpu || digger. /* for unix family only. compil...
为什么这个unix 版的ping 在 linux下编译无法通过?
出现了几百行各式各样的错误! 大家给在linux下编译一下吧,到底怎么回事啊?
/* PING.C
/*
/* ping source code distribute by cpu || digger.
/* for unix family only. compil and link success in sco unix.
/* i think linux no problem too. u can try it.
/* before read this code, you shoud know about the principle of
/* tcp/ip, especially icmp protocol, u also should also know some
/* about BSD socket API, and unix system signal programming.
/*
/* cc -o ping ping.c -lsocket, then u will get executable file,
/* but must act as root when cc it, and then set euid attribute
/* for this ping, then u can execute it as common user.
/* because only root can have authority to creat raw socket.
/*
/* i love socket, if so do u,
/* call me, cpu == digger
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
# define ICMP_ECHO 8 /* icmp echo requir */
# define ICMP_ECHOREPLY 0 /* icmp echo reply */
# define ICMP_HEADSIZE 8 /* icmp packet header size */
# define IP_HEADSIZE 20 /* ip packet header size */
typedef struct tagIpHead /* icmp packet header */
{
u_char ip_verlen; /* ip version and ip header lenth*/
u_char ip_tos; /* ip type of service */
u_short ip_len; /* ip packet lenghth */
u_short ip_id; /* ip packet identification */
u_short ip_fragoff; /* ip packet fragment and offset */
u_char ip_ttl; /* ip packet time to live */
u_char ip_proto; /* ip packet protocol type */
u_short ip_chksum; /* ip packet header checksum */
u_long ip_src_addr; /* ip source ip adress */
u_long ip_dst_addr; /* ip destination ip adress */
} IPHEAD;
typedef struct tagIcmpHead /* icmp header */
{
u_char icmp_type; /* icmp service type */
/* 8 echo require, 0 echo reply */
u_char icmp_code; /* icmp header code */
u_short icmp_chksum; /* icmp header chksum */
u_short icmp_id; /* icmp packet identification */
u_short icmp_seq; /* icmp packet sequent */
u_char icmp_data[1]; /* icmp data, use as pointer */
} ICMPHEAD;
u_short ChkSum( u_short * pIcmpData, int iDataLen )
/* for check sum of icmp header */
{
u_short iSum;
u_short iOddByte;
iSum = 0;
while ( iDataLen > 1 ) { /* xor the next unsigned int data */
iSum ^= *pIcmpData++;
iDataLen -= 2;
}
if ( iDataLen == 1 ) { /* the rest odd byte */
iOddByte = 0;
*((u_char *)&iOddByte) = *(u_char *)pIcmpData;
iSum ^= iOddByte;
}
iSum ^= 0xffff; /* xor 0xffff == not it */
return(iSum);
}
long time_now() /* return time passed by */
/* since 1970.1.1 00:00:00, */
/* in 1/1000000 second */
{
struct timeval now;
long lPassed;
gettimeofday(&now, 0);
lPassed = now.tv_sec * 1000000 + now.tv_usec;
/* now.tv_sec in second */
/* now.tv_usec in 1/1000000 second */
return lPassed;
}
char* host; /* destination host */
char* prog; /* program name */
extern errno; /* system global parameter */
long lSendTime; /* each time when send, change it */
u_short seq; /* the icmp packet seqence */
int iTimeOut; /* time out parameter */
int sock, sent, recvd, max, min, total;
/* sent : icmp packet already sent */
/* recvd: proper icmp packet received */
/* max, min: max min round trip time */
/* total: total round trip time */
/* store to calculate average */
u_long lHostIp; /* host ip adress */
struct sockaddr_in it; /* destination host information */
int ping();
void stat();
main(int argc, char** argv)
{
struct hostent* h;
char buf[200];
char dst_host[32];
int i, namelen;
IPHEAD* pIpHead;
ICMPHEAD* pIcmpHead;
if (argc
出现了几百行各式各样的错误! 大家给在linux下编译一下吧,到底怎么回事啊?
/* PING.C
/*
/* ping source code distribute by cpu || digger.
/* for unix family only. compil and link success in sco unix.
/* i think linux no problem too. u can try it.
/* before read this code, you shoud know about the principle of
/* tcp/ip, especially icmp protocol, u also should also know some
/* about BSD socket API, and unix system signal programming.
/*
/* cc -o ping ping.c -lsocket, then u will get executable file,
/* but must act as root when cc it, and then set euid attribute
/* for this ping, then u can execute it as common user.
/* because only root can have authority to creat raw socket.
/*
/* i love socket, if so do u,
/* call me, cpu == digger
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
# define ICMP_ECHO 8 /* icmp echo requir */
# define ICMP_ECHOREPLY 0 /* icmp echo reply */
# define ICMP_HEADSIZE 8 /* icmp packet header size */
# define IP_HEADSIZE 20 /* ip packet header size */
typedef struct tagIpHead /* icmp packet header */
{
u_char ip_verlen; /* ip version and ip header lenth*/
u_char ip_tos; /* ip type of service */
u_short ip_len; /* ip packet lenghth */
u_short ip_id; /* ip packet identification */
u_short ip_fragoff; /* ip packet fragment and offset */
u_char ip_ttl; /* ip packet time to live */
u_char ip_proto; /* ip packet protocol type */
u_short ip_chksum; /* ip packet header checksum */
u_long ip_src_addr; /* ip source ip adress */
u_long ip_dst_addr; /* ip destination ip adress */
} IPHEAD;
typedef struct tagIcmpHead /* icmp header */
{
u_char icmp_type; /* icmp service type */
/* 8 echo require, 0 echo reply */
u_char icmp_code; /* icmp header code */
u_short icmp_chksum; /* icmp header chksum */
u_short icmp_id; /* icmp packet identification */
u_short icmp_seq; /* icmp packet sequent */
u_char icmp_data[1]; /* icmp data, use as pointer */
} ICMPHEAD;
u_short ChkSum( u_short * pIcmpData, int iDataLen )
/* for check sum of icmp header */
{
u_short iSum;
u_short iOddByte;
iSum = 0;
while ( iDataLen > 1 ) { /* xor the next unsigned int data */
iSum ^= *pIcmpData++;
iDataLen -= 2;
}
if ( iDataLen == 1 ) { /* the rest odd byte */
iOddByte = 0;
*((u_char *)&iOddByte) = *(u_char *)pIcmpData;
iSum ^= iOddByte;
}
iSum ^= 0xffff; /* xor 0xffff == not it */
return(iSum);
}
long time_now() /* return time passed by */
/* since 1970.1.1 00:00:00, */
/* in 1/1000000 second */
{
struct timeval now;
long lPassed;
gettimeofday(&now, 0);
lPassed = now.tv_sec * 1000000 + now.tv_usec;
/* now.tv_sec in second */
/* now.tv_usec in 1/1000000 second */
return lPassed;
}
char* host; /* destination host */
char* prog; /* program name */
extern errno; /* system global parameter */
long lSendTime; /* each time when send, change it */
u_short seq; /* the icmp packet seqence */
int iTimeOut; /* time out parameter */
int sock, sent, recvd, max, min, total;
/* sent : icmp packet already sent */
/* recvd: proper icmp packet received */
/* max, min: max min round trip time */
/* total: total round trip time */
/* store to calculate average */
u_long lHostIp; /* host ip adress */
struct sockaddr_in it; /* destination host information */
int ping();
void stat();
main(int argc, char** argv)
{
struct hostent* h;
char buf[200];
char dst_host[32];
int i, namelen;
IPHEAD* pIpHead;
ICMPHEAD* pIcmpHead;
if (argc