我正在尝试用C创建一个ICMP ping测试程序,但是在成功发送数据包时遇到了困难. sendto函数返回字节数和所有内容但实际上没有发送数据包.我已经在目标计算机上使用WireShark验证了这一点.主机上的常规ping工作正常,但在WireShark中显示.
这是我的代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
unsigned short cksum(unsigned short *addr, int len);
int main(int argc, char *argv[])
{
int sock;
char send_buf[400], recv_buf[400], src_name[256], src_ip[15], dst_ip[15];
struct ip *ip = (struct ip *)send_buf;
struct icmp *icmp = (struct icmp *)(ip + 1);
struct hostent *src_hp, *dst_hp;
struct sockaddr_in src, dst;
struct timeval t;
int on;
int num = 10;
int failed_count = 0;
int bytes_sent, bytes_recv;
int dst_addr_len;
int result;
fd_set socks;
/* Initialize variables */
on = 1;
memset(send_buf, 0, sizeof(send_buf));
/* Check for valid args */
if(argc < 2)
{
printf("\nUsage: %s \n", argv[0]);
printf("- dst_server is the target\n");
exit(EXIT_FAILURE);
}
/* Check for root access */
if (getuid() != 0)
{
fprintf(stderr, "%s