数据包收发c语言程序,在C程序中发送ICMP数据包

本文介绍了一个使用C语言编写的ICMP ping测试程序,通过创建RAW套接字发送ICMP数据包。代码中包含了IP和ICMP结构体的初始化、校验和计算以及sendto和recvfrom函数的使用。尽管遇到发送数据包的问题,经过调整,程序最终能够成功发送并接收ICMP响应。
摘要由CSDN通过智能技术生成

我正在尝试用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值