java 监听ipv6的udp_IPv6原始套接字的UDP错误长度

我的程序应该发送UDP数据报,使用Raw套接字用于IPv6协议 .

我必须在套接字选项中使用 IPV6 CHECKSUM . 代码编译得很好,但是TCPDump的长度很差 .

IP6 fe80 :: 20c:29ff:fe95:c051.24576> waw02s13-in-x03.1e100.net.0:UDP,坏长度12280> 40

我错过了什么或我做错了什么?

int main(int argc, char** argv) {

int sockfd;

int offset;

int retval;

struct addrinfo hints;

struct addrinfo *rp, *result;

unsigned char datagram[sizeof(struct ip6_hdr) + sizeof(struct udphdr)

] = {0};

struct ip6_hdr *ip_header = (struct ip6_hdr *)datagram;

struct udphdr *udp_header = (struct udphdr *)

(datagram + sizeof(struct ip6_hdr));

if (argc != 3) {

fprintf(

stderr,

"Invocation: %s \n",

argv[0]

);

exit(EXIT_FAILURE);

}

memset(&hints, 0, sizeof(struct addrinfo));

hints.ai_family = AF_INET6;

hints.ai_socktype = SOCK_RAW;

hints.ai_protocol = IPPROTO_UDP;

retval = getaddrinfo(argv[1], NULL, &hints, &result);

if (retval != 0) {

fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(retval));

exit(EXIT_FAILURE);

}

offset = 6;

for (rp = result; rp != NULL; rp = rp->ai_next) {

sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

if (sockfd == -1) {

perror("socket()");

continue;

}

retval = setsockopt(

sockfd,

IPPROTO_IPV6, IPV6_CHECKSUM,

&offset, sizeof(offset)

);

if (retval == -1) {

perror("setsockopt()");

exit(EXIT_FAILURE);

} else {

break;

}

}

if (rp == NULL) {

fprintf(stderr, "Client failure: could not create socket.\n");

exit(EXIT_FAILURE);

}

ip_header->ip6_flow = htonl((6 << 28) | (0 << 20) | 0);

ip_header->ip6_hops = 255;

ip_header->ip6_nxt = IPPROTO_UDP;

ip_header->ip6_plen = sizeof(struct ip6_hdr)

+ sizeof(struct udphdr);

inet_pton(AF_INET6, SOURCE_ADDRESS, &(ip_header->ip6_src));

inet_pton(AF_INET6, argv[1], &(ip_header->ip6_dst));

udp_header->uh_sport = htons(SOURCE_PORT);

udp_header->uh_dport = htons(atoi(argv[2]));

udp_header->uh_ulen = htons(ip_header->ip6_plen);

fprintf(stdout, "Sending UDP...\n");

for (;;) {

retval = sendto(

sockfd,

datagram, ip_header->ip6_plen,

0,

rp->ai_addr, rp->ai_addrlen

);

if (retval == -1) {

perror("sendto()");

}

sleep(1);

}

exit(EXIT_SUCCESS);

}

有任何想法吗?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值