我正在尝试获取传出数据包的准确时间戳(使用原始套接字发送)。根据Linux/Documentation/networking/timestamping.txt
,“对于发送时间戳,传出的数据包将循环回套接字的错误队列,并附上发送时间戳。它可以通过 recvmsg(flags=MSG_ERRQUEUE) 接收。”。
不幸的是,在原始套接字上调用时recvmsg
总是返回(使用并设置为with创建)。我究竟做错了什么?有没有更好的方法来获取传出数据包的准确时间戳?-1
socket(PF_INET, SOCK_RAW, IPPROTO_RAW)
SO_TIMESTAMP
1
setsockopt
附录(信息):
我还尝试从通过 UDP 套接字(下面的源代码)发送的数据包中获取时间戳并recvmsg
返回-1
:错误是“资源暂时不可用”(EAGAIN
)。
附录(源代码):
#include <arpa/inet.h>
#include <linux/net_tstamp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
void die(char* s)
{
perror(s);
exit(1);
}
int main(int argc, char* argv[])
{
char* destination_ip = "10.0.0.1";
int destination_port = 1234;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
die("socket()");
}
int timestamp_flags = SOF_TIMESTAMPING_TX_SOFTWARE;
if (setsockopt(sock, SOL_SOCKET, SO_TIMESTAMPING, ×tamp_flags, sizeof(timestamp_flags)) < 0) {
die("setsockopt()");
}
struct sockaddr_in si_server;
memset(&si_server, 0, sizeof(si_server));
si_server.sin_family = AF_INET;
si_server.sin_port = htons(destination_port);
if (inet_aton(destination_ip, &si_server.sin_addr) == 0) {
die("inet_aton()");
}
const int buffer_len = 256;
char buffer[buffer_len];
const int n_packets = 10;
for (int i = 0; i < n_packets; ++i) {
sprintf(buffer, "Packet %d", i);
if (sendto(sock, buffer, buffer_len, 0, (const sockaddr*) &si_server, sizeof(si_server)) < 0) {
die("sendto()");
}
// Obtain the sent packet timestamp.
char data[256];
struct msghdr msg;
struct iovec entry;
struct sockaddr_in from_addr;
struct {
struct cmsghdr cm;
char control[512];
} control;
int res;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &entry;
msg.msg_iovlen = 1;
entry.iov_base = data;
entry.iov_len = sizeof(data);
msg.msg_name = (caddr_t)&from_addr;
msg.msg_namelen = sizeof(from_addr);
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
if (recvmsg(sock, &msg, MSG_ERRQUEUE) < 0) {
die("recvmsg()");
}
}
return 0;
}