- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Linux (Ubuntu 18.04) 上的 C 中创建一个测试程序,该程序使用 SOCK_RAW
通过 AF_PACKET/PF_PACKET 套接字发送一个空的 UDP 数据包.在这个程序中,我知道源和目标 MAC 地址和 IP。但是,它不起作用,我不确定我做错了什么。遗憾的是,我无法在网上找到很多关于这个问题的资源,因为我发现的大多数线程都更适用于在 AF_PACKET 套接字上接收数据包。该程序还声明它将正确数量的字节发送到目的地。不过,我在使用 tcpdump
时看不到数据包在源虚拟机和目标虚拟机上。
这是程序的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/udp.h>
#include <net/ethernet.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <inttypes.h>
#define REDIRECT_HEADER
#include "csum.h"
#define MAX_PCKT_LENGTH 65535
int main()
{
int sockfd;
struct sockaddr_ll dst;
char pckt[MAX_PCKT_LENGTH];
sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
if (sockfd <= 0)
{
perror("socket");
exit(1);
}
dst.sll_family = AF_PACKET;
dst.sll_protocol = ETH_P_IP;
if ((dst.sll_ifindex = if_nametoindex("ens18")) == 0)
{
fprintf(stdout, "Interface 'ens18' not found.\n");
exit(1);
}
// Do destination ethernet MAC (ae:21:14:4b:3a:6d).
dst.sll_addr[0] = 0xAE;
dst.sll_addr[1] = 0x21;
dst.sll_addr[2] = 0x14;
dst.sll_addr[3] = 0x4B;
dst.sll_addr[4] = 0x3A;
dst.sll_addr[5] = 0x6D;
dst.sll_halen = 6;
// I tried doing this with and without bind. Still not working.
if (bind(sockfd, (struct sockaddr *)&dst, sizeof(dst)) < 0)
{
perror("bind");
exit(1);
}
struct ethhdr *ethhdr = (struct ethhdr *) (pckt);
struct iphdr *iphdr = (struct iphdr *) (pckt + sizeof(struct ethhdr));
struct udphdr *udphdr = (struct udphdr *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr));
unsigned char *data = (unsigned char *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr));
// Do source ethernet MAC (1a:c4:df:70:d8:a6).
ethhdr->h_source[0] = 0x1A;
ethhdr->h_source[1] = 0xC4;
ethhdr->h_source[2] = 0xDF;
ethhdr->h_source[3] = 0x70;
ethhdr->h_source[4] = 0xD8;
ethhdr->h_source[5] = 0xA6;
// Copy destination MAC to sockaddr_ll.
memcpy(ethhdr->h_dest, dst.sll_addr, 6);
// Protocol.
ethhdr->h_proto = ETH_P_IP;
// Fill out ip header.
iphdr->ihl = 5;
iphdr->version = 4;
iphdr->frag_off = 0;
iphdr->id = rand();
iphdr->protocol = IPPROTO_UDP;
iphdr->tos = 0x0;
iphdr->ttl = 64;
iphdr->saddr = inet_addr("10.50.0.3");
iphdr->daddr = inet_addr("10.50.0.4");
iphdr->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr);
iphdr->check = 0;
iphdr->check = ip_fast_csum(iphdr, iphdr->ihl);
// Fill out UDP header.
udphdr->source = htons(27000);
udphdr->dest = htons(27015);
udphdr->len = htons(sizeof(struct udphdr));
udphdr->check = 0;
udphdr->check = csum_tcpudp_magic(iphdr->saddr, iphdr->daddr, sizeof(struct udphdr), IPPROTO_UDP, csum_partial(udphdr, sizeof(struct udphdr), 0));
// Send packet
uint16_t sent;
if ((sent = sendto(sockfd, pckt, iphdr->tot_len + sizeof(struct ethhdr), 0, (struct sockaddr *)&dst, sizeof(dst))) < 0)
{
perror("sendto");
}
fprintf(stdout, "Sent %d of data.\n", sent);
close(sockfd);
exit(0);
}
ens18: flags=323<UP,BROADCAST,RUNNING,PROMISC> mtu 1500
inet 10.50.0.3 netmask 255.255.255.0 broadcast 10.50.0.255
inet6 fe80::18c4:dfff:fe70:d8a6 prefixlen 64 scopeid 0x20<link>
ether 1a:c4:df:70:d8:a6 txqueuelen 1000 (Ethernet)
RX packets 1959766813 bytes 1896024793559 (1.8 TB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1936101432 bytes 1333123918522 (1.3 TB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.50.0.4 netmask 255.255.255.0 broadcast 10.50.0.255
inet6 fe80::ac21:14ff:fe4b:3a6d prefixlen 64 scopeid 0x20<link>
ether ae:21:14:4b:3a:6d txqueuelen 1000 (Ethernet)
RX packets 1032069029 bytes 1251754298166 (1.2 TB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 74446483 bytes 9498785163 (9.4 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
SOCK_RAW
在这种情况下,因为我想获得更多经验,并且我不希望内核对数据包做任何事情(我读到使用
AF_PACKET
+
SOCK_RAW
将导致内核不会弄乱数据包)。
AF_PACKET
提交每个数据包之前,我是否只需向目标服务器发送一个 ARP 请求并获取 MAC 地址? +
SOCK_RAW
?
最佳答案
我能够弄清楚这个问题。我没有通过 htons()
将以太网协议(protocol) ( ETH_P_IP ) 转换为网络字节顺序因为它是一个大端。话虽如此,我不得不转换 iphdr->total_len
网络字节顺序也是如此。否则,根据 tcpdump
,IP header 的总长度将不正确。 .我没有在我制作的其他程序中这样做,并且效果很好。因此,我假设内核自动将 IP header 的总长度转换为网络字节顺序。
由于我使用的是 AF_PACKET 套接字发送,我必须做内核通常会做的事情。
这是任何想知道的人的程序的最终代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/udp.h>
#include <net/ethernet.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <inttypes.h>
#define REDIRECT_HEADER
#include "csum.h"
#define MAX_PCKT_LENGTH 65535
int main()
{
int sockfd;
struct sockaddr_ll dst;
char pckt[MAX_PCKT_LENGTH];
sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
if (sockfd <= 0)
{
perror("socket");
exit(1);
}
dst.sll_family = PF_PACKET;
dst.sll_protocol = htons(ETH_P_IP);
if ((dst.sll_ifindex = if_nametoindex("ens18")) == 0)
{
fprintf(stdout, "Interface 'ens18' not found.\n");
exit(1);
}
// Do destination ethernet MAC (ae:21:14:4b:3a:6d).
dst.sll_addr[0] = 0xAE;
dst.sll_addr[1] = 0x21;
dst.sll_addr[2] = 0x14;
dst.sll_addr[3] = 0x4B;
dst.sll_addr[4] = 0x3A;
dst.sll_addr[5] = 0x6D;
dst.sll_halen = ETH_ALEN;
// I tried doing this with and without bind. Still not working.
if (bind(sockfd, (struct sockaddr *)&dst, sizeof(dst)) < 0)
{
perror("bind");
exit(1);
}
struct ethhdr *ethhdr = (struct ethhdr *) (pckt);
struct iphdr *iphdr = (struct iphdr *) (pckt + sizeof(struct ethhdr));
struct udphdr *udphdr = (struct udphdr *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr));
unsigned char *data = (unsigned char *) (pckt + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr));
// Do source ethernet MAC (1a:c4:df:70:d8:a6).
ethhdr->h_source[0] = 0x1A;
ethhdr->h_source[1] = 0xC4;
ethhdr->h_source[2] = 0xDF;
ethhdr->h_source[3] = 0x70;
ethhdr->h_source[4] = 0xD8;
ethhdr->h_source[5] = 0xA6;
for (int i = 0; i < 30; i++)
{
memcpy(data + i, "b", 1);
}
// Copy destination MAC to sockaddr_ll.
memcpy(ethhdr->h_dest, dst.sll_addr, ETH_ALEN);
// Protocol.
ethhdr->h_proto = htons(ETH_P_IP);
// Fill out ip header.
iphdr->ihl = 5;
iphdr->version = 4;
iphdr->frag_off = 0;
iphdr->id = htons(0);
iphdr->protocol = IPPROTO_UDP;
iphdr->tos = 0x0;
iphdr->ttl = 64;
iphdr->saddr = inet_addr("10.50.0.3");
iphdr->daddr = inet_addr("10.50.0.4");
iphdr->tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr) + 30);
iphdr->check = 0;
iphdr->check = ip_fast_csum(iphdr, iphdr->ihl);
// Fill out UDP header.
udphdr->source = htons(27000);
udphdr->dest = htons(27015);
udphdr->len = htons(sizeof(struct udphdr) + 30);
udphdr->check = 0;
udphdr->check = csum_tcpudp_magic(iphdr->saddr, iphdr->daddr, sizeof(struct udphdr) + 30, IPPROTO_UDP, csum_partial(udphdr, sizeof(struct udphdr) + 30, 0));
// Send packet
uint16_t sent;
int len = ntohs(iphdr->tot_len) + sizeof(struct ethhdr) + 30;
if ((sent = sendto(sockfd, pckt, len, 0, (struct sockaddr *)&dst, sizeof(dst))) < 0)
//if ((sent = write(sockfd, pckt, len)) < 0)
{
perror("sendto");
}
fprintf(stdout, "Sent %d of data. %d is IPHdr len. %d is len.\n", sent, iphdr->tot_len, len);
close(sockfd);
exit(0);
}
关于c - AF_PACKET 套接字在 C 中不发送带有 SOCK_RAW 的空 UDP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60685437/
我刚刚了解了 udp 校验和计算。 但我很困惑算法是否检测到所有错误。 最佳答案 当然不是。没有校验和可以检测到所有错误。 关于udp - UDP 校验和是否检测到所有错误?,我们在Stack Ove
下面这个函数有什么问题?? 它应该抛出错误,因为我没有在本地主机上运行 UDP 服务器。 int openUdpSocket(int port) { int sock,sin_size;
我正在尝试修改这两个程序。我想让 udpclient.c 能够接收消息并让 udpserver.c 将消息回显给客户端。我还想在 udpclient 发送消息之前先获取它们的时间标记消息。收到的消息应
我有相当简单的 UDP 服务器写在 c 上。 有时我需要知道在套接字中排队的所有 udp 数据包(字节)的当前长度。 据我了解,getsockopt 没有得到这样的信息。 欢迎使用 Linux 和 F
除了直播音乐/视频外,谁能告诉在哪里使用 UDP 协议(protocol)? UDP 的默认用例是什么? 最佳答案 其他任何您需要性能但如果数据包在途中丢失时可以生存的东西。例如,多人游戏浮现在脑海中
与 TCP 的监视方法相反,UDP 是否会在所有数据包可用时立即发送它们? 谢谢。 最佳答案 TCP 有拥塞控制,UDP 没有,因为它是无连接的。 但是您的问题涉及多个问题:发送消息是否会导致立即发送
我知道 UDP 本质上是不可靠的,但是当连接到 localhost 时,我希望内核以不同的方式处理连接,因为一切都可以在内部处理。那么在这种特殊情况下,UDP 是否被认为是一种可靠的协议(protoc
我正在尝试使用 flash 和 rtmfp 协议(protocol)开发一个实时视频聊天应用程序,但我有疑问rtmfp 如何保证连接对等点,尤其是当对等点位于不同网络时。 最佳答案 RTMFP 依靠中
我发现所有使用 Netty 4.0 的 TCP 服务器实现都使用了 ServerBootstrap 实例。 The biggest and only difference between a serv
对于个人 MMO 游戏项目,我正在 java 中实现一个自制的可靠的基于 UDP 的协议(protocol)。鉴于我当前的设置,我相信窥探者劫持 session 相对简单,因此为了防止这种情况,我借此
我正在尝试手动计算各种 UDP 数据包的校验和,但与 Wireshark 中显示的结果相比,我总是得到错误的结果。下面是我如何做到这一点的示例: Source Address: 192.168.0.1
我正在尝试手动计算各种 UDP 数据包的校验和,但与 Wireshark 中显示的结果相比,我总是得到错误的结果。下面是我如何做到这一点的示例: Source Address: 192.168.0.1
我有一个奇怪的问题。我有一个成功运行的 C++ (boost asio) P2P 应用程序,它可以在大多数 NAT 上运行。问题是,当我将初始启动端口号指定为 1000 时,它会检查 1000 是否空
带有数据源的短 radio 链路,需要通过 IPv6 的 1280 Kbps 吞吐量,使用 UDP 停止和等待协议(protocol),该区域内没有其他客户端或明显的噪声源。我到底如何才能计算出最佳数
似乎可以在没有有效负载的情况下发送 UDP 数据包。 我能想到的唯一不需要有效载荷的就是用于 NAT 打洞。 这还能用来做什么? 这与我之前的问题有关Under Linux, can recv eve
我有一个客户端,我无法更改其代码 - 但我想(重新)使用 编写代码ZeroMQ socket 。 客户端同时使用原始 TCP 和原始 UDP socket 。 我知道我可以使用 ZMQ_ROUTER_
网络 4.0.24 我通过 UDP 传递 XML。收到 UPD 数据包时,数据包的长度始终为 2048,截断消息。尽管如此,我尝试将接收缓冲区大小设置为更大的值(4096、8192、65536),但它
我正在尝试编写一个有关 UDP 连接的简单程序来了解它们。我已经实现了一些基本的事情,但是当我尝试发送并取回我发送的内容时,我遇到了一些问题,例如, 当我这样做时;发送一个字符串 “asd”到服务器我
当我检查时,我在 UDP 客户端每 100 毫秒从服务器发送 UDP 数据包 接收频率不等于 100 毫秒,有时它要少得多,例如 3 毫秒…10 毫秒。 我知道UDP client server是异步
海友我是一个学习winsock2的新手。以下是我的udp服务器和客户端程序。 我这个程序客户端不知道服务器的IP地址,只知道端口。但是服务器会在整个网络中广播一条消息。 当客户端收到消息时,它会回溯服
我是一名优秀的程序员,十分优秀!