我正在努力让 AIO 在 Linux(3.19 版)上工作以在 RAW 套接字上接收数据包,但无济于事。我已经成功地将 AIO 用于 UDP 和 TCP 套接字,但不能使其适用于 RAW 套接字。我已经尝试过 IPv4 和 IPv6。
有谁知道 AIO 是否支持 RAW 套接字?
这是我的应用程序中的一些代码片段:
void readCallback(sigval_t sigval) {
debug(LOG_DEBUG, "RAW packet received\n");
}
int main(int argc, char *argv[]) {
int sock = socket(domain, SOCK_RAW, IPPROTO_RAW);
if (-1 == sock) {
debug(LOG_CRIT, "FAILED to create raw socket\n");
return 1;
}
char *iface = "eth0";
ifreq ifr;
memset (&ifr, 0, sizeof(ifr));
snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), "%s", iface);
if (ioctl (sock, SIOCGIFINDEX, &ifr) < 0) {
debug(LOG_CRIT, "FAILED to query interface '%s' index\n", iface);
return 1;
}
// Set flag so socket expects us to provide IP header.
const int on = 1;
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
debug(LOG_CRIT, "FAILED to configure raw socket on '%s'\n", iface);
return 1;
}
// Bind socket to interface index.
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) {
debug(LOG_CRIT, "FAILED to bind socket to %s/%u\n", iface, ifr.ifr_ifindex);
return 1;
}
// listen for packets
struct aiocb aio;
char buf[MAX_PACKET];
bzero((char*)&aio, sizeof(struct aiocb));
aio.aio_fildes = sock;
aio.aio_buf = &buf;
aio.aio_nbytes = MAX_PACKET;
aio.aio_offset = 0;
aio.aio_sigevent.sigev_notify = SIGEV_THREAD;
aio.aio_sigevent.sigev_notify_function = readCallback;
aio.aio_sigevent.sigev_notify_attributes = NULL;
aio.aio_sigevent.sigev_value.sival_ptr = buf;
if (!RequestAioRead(&aio)) {
debug(LOG_DEBUG, "FAILED to listen on raw socket...\n");
return 1;
}
debug(LOG_DEBUG, "Listening on raw socket...\n");
// main loop
while (true) {
usleep(100000);
}
close(sock);
return 0;
}