LINUX怎么查询icmp模式,Linux 发送 ICMP 数据

ICMP使用场景

Ping命令使用的就是ICMP协议

Traceroute 程序主要用来侦测源主机到目的主机之间所经过的路由的情况。

Traceroute 使用 ICMP 报文和 IP 首部中的 TTL 字段,它充分利用了 ICMP 超时消息。其原理很简单,开始时发送一个 TTL 字段为 1 的 UDP 数据报,而后每次收到 ICMP 超时效后,按顺序再发送一个 TTL 字段加 1 的 UDP 数据报,以确定路径中的每个路由器,而每个路由器在丢弃 UDP 数据报时都会返回一个 ICMP 超时报文,而最终到达目的主机后,由于 ICMP选择了一个不可能的值作为 UDP 端口(大于30000)。这样目的主机就会发送一个端口不可达的 ICMP 差错报文。

发送ICMP数据包:

以下程序必须在root权限下编译和运行

#include 

#include           /* See NOTES */

#include 

#include 

#include 

#include 

#include 

#include 

#include 

void send_echo_req(int sockfd, struct sockaddr_in *dstaddr);

uint16_t in_cksum(uint16_t *addr, int len);

void recv_echo_reply(int sockfd);

void err_sys(const char *errmsg)

{

perror(errmsg);

exit(1);

}

int main(int argc, char **argv)

{

int sockfd;

struct sockaddr_in dstaddr;

if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)

err_sys("socket");

bzero(&dstaddr, sizeof(dstaddr));

dstaddr.sin_family = AF_INET;

dstaddr.sin_port = htons(0);

if (inet_pton(AF_INET, argv[1], &dstaddr.sin_addr) <= 0)

err_sys("inet_pton");

send_echo_req(sockfd, &dstaddr);

recv_echo_reply(sockfd);

exit(0);

}

//发送icmp请求,指定请求地址。请求[struct icmp]数据包

void send_echo_req(int sockfd, struct sockaddr_in *dstaddr)

{

char buf[100];

size_t len = sizeof(struct icmp);

struct icmp *icmp; //icmp数据包

socklen_t dstlen = sizeof(struct sockaddr_in);

bzero(buf, sizeof(buf));

icmp = (struct icmp *)buf;

icmp->icmp_type = ICMP_ECHO;//8

icmp->icmp_code = 0;

icmp->icmp_id = getpid();

icmp->icmp_seq = 1;

icmp->icmp_cksum = in_cksum((uint16_t *) icmp, sizeof(struct icmp));

if (sendto(sockfd, buf, len, 0, (struct sockaddr *)dstaddr, dstlen) == -1)

err_sys("sendto");

}

//接受icmp回应,返回IP数据包,[struct ip],通过返回源地址,可获取路由信息

void recv_echo_reply(int sockfd)

{

char buf[100];

ssize_t n;

struct ip *ip;

struct icmp *icmp;

while (1) {

alarm(5); /* set timeout */

if ((n = read(sockfd, buf, sizeof(buf))) == -1)

err_sys("read");

ip = (struct ip *)buf;          //转化成ip数据报

if (ip->ip_p != IPPROTO_ICMP) { //如果是icmp协议

fprintf(stderr, "protocol error.");

exit(1);

}

//定位到icmp报文头

icmp = (struct icmp *)(buf + sizeof(struct ip));

if (icmp->icmp_type == ICMP_ECHOREPLY) {

if (icmp->icmp_id != getpid()) {

fprintf(stderr, "not this process.");

exit(1);

} else {

printf("destination host is alive.\n");

break;

}

}

}

}

//

uint16_t in_cksum(uint16_t *addr, int len)

{

int nleft = len;

uint32_t sum = 0;

uint16_t *w = addr;

uint16_t answer = 0;

while (nleft > 1) {

sum += *w++;

nleft -= 2;

}

if (nleft == 1) {

*(unsigned char *)(&answer) = *(unsigned char *)w ;

sum += answer;

}

sum = (sum >> 16) + (sum & 0xffff);

sum += (sum >> 16);

answer = ~sum;

return(answer);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值