Ich habe Schwierigkeiten, AIO unter Linux (Version 3.19) für den Empfang von Paketen auf einem RAW-Socket zu bekommen, aber ohne Erfolg. Ich habe AIO erfolgreich für UDP- und TCP-Sockets verwendet, kann es aber nicht für einen RAW-Socket verwenden. Ich habe sowohl IPv4 als auch IPv6 ausprobiert.Unterstützt Linux AIO RAW-Sockets?
Weiß jemand, ob AIO RAW-Sockets unterstützt?
Hier einige Code-Schnipsel aus meiner Anwendung:
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;
}
ja, tut es; Entschuldigung, habe keine Zeit den Code atm anzusehen. –
Ok, großartig, danke für deine Antwort. Zumindest weiß ich es * sollte * dann funktionieren. Ich werde weiter graben. – HannesM
Ihre Antwort gab mir das Vertrauen, die Fehlersuche fortzusetzen; stellt sich heraus, dass das Socket-Protokoll falsch war. Das korrekte Protokoll scheint htons (0x0800) zu sein: Socket (AF_PACKET, SOCK_RAW, htons (0x0800)); Damit scheint Aio gut zu funktionieren. Vielen Dank für Ihre schnelle Antwort! – HannesM