📄 icmp6.c
字号:
/***************************************************************************** * * Copyright (C) 2001 Uppsala University and Ericsson AB. * Copyright (C) 2003 Simon Fraser University and NewMIC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Authors: Erik Nordstr鰉, <erik.nordstrom@it.uu.se> * : Peter Lee <peter.lee@shaw.ca> * *****************************************************************************/#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/ip_icmp.h> //PL: for the type and code defination#include <netinet/icmp6.h>#include <netinet/ip6.h>#include "defs.h"#include "debug.h"#include "l_icmp6.h"#include "ipv6_utils.h"#define ICMP_BUFSIZE sizeof(struct icmp6_hdr) + 60 + 40char icmp_send_buf[ICMP_BUFSIZE];int icmp_socket;static unsigned short cksum(unsigned short *w, int len){ int sum = 0; unsigned short answer = 0; while (len > 1) { sum += *w++; len -= 2; } if (len == 1) { *(unsigned char *) (&answer) = *(unsigned char *) w; sum += answer; } sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); answer = ~sum; return (answer);}/* Data = IP header + 64 bits of data */int icmp6_send_host_unreachable(char *data, int len){ struct icmp6_hdr *icmp; struct sockaddr_in6 dst_addr; int ret, icmp_socket; char tos = IPTOS_PREC_INTERNETCONTROL; int i; icmp_socket = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); if (icmp_socket < 0) return -1; setsockopt(icmp_socket, SOL_IPV6, IP_TOS, &tos, sizeof(char)); memset(icmp_send_buf, 0, ICMP_BUFSIZE); icmp = (struct icmp6_hdr *) icmp_send_buf; //icmp->icmp6_type = ICMP_DEST_UNREACH; //icmp->icmp6_code = ICMP_HOST_UNREACH; icmp->icmp6_type = ICMP6_DST_UNREACH; icmp->icmp6_code = ICMP6_DST_UNREACH_ADDR; memcpy(icmp_send_buf + sizeof(struct icmp6_hdr), data, len); icmp->icmp6_cksum = cksum((u_short *) icmp, len + sizeof(struct icmp6_hdr)); dst_addr.sin6_family = AF_INET6; dst_addr.sin6_port = htons(INADDR_ANY); //PL: Need to verify //dst_addr.sin6_port = htons(AODV_PORT); /* Send ICMP message on all AODV enabled interfaces */ for (i = 0; i < MAX_NR_INTERFACES; i++) { if (!DEV_NR(i).enabled) continue; DEBUG(LOG_DEBUG, 0, "icmp_send: Sending HOST_UNREACHABLE to %s, len=%d", ip6_to_str(DEV_NR(i).ipaddr), len); //dst_addr.sin_addr.s_addr = htonl(DEV_NR(i).ipaddr); memcpy(&dst_addr.sin6_addr, &(DEV_NR(i).ipaddr), sizeof(DEV_NR(i).ipaddr)); ret = sendto(icmp_socket, icmp_send_buf, len + sizeof(struct icmp6_hdr), 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)); if (ret < 0) log(LOG_WARNING, errno, "icmp: Could not sent ICMPV6 msg to %s", ip6_to_str(DEV_NR(i).ipaddr)); } close(icmp_socket); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -