af_packet: AF_Packet: Use negative socket_fd for error indication

Technically, socket() can return 0, so shouldn't use it as an
indication of a non existent / closed socket.

I'm not 100% sure about the Close() contract here: If something
goes haywire with a packet source Zeek calls FatalError without
calling Close() nor properly destructing the PktSrc. Oh yikes.
This commit is contained in:
Arne Welzel 2023-03-29 15:38:28 +02:00 committed by Tim Wojtulewicz
parent 17d60e4ab9
commit df4beb6054

View file

@ -34,6 +34,9 @@ AF_PacketSource::AF_PacketSource(const std::string& path, bool is_live)
props.path = path; props.path = path;
props.is_live = is_live; props.is_live = is_live;
socket_fd = -1;
rx_ring = nullptr;
checksum_mode = zeek::BifConst::AF_Packet::checksum_validation_mode->AsEnum(); checksum_mode = zeek::BifConst::AF_Packet::checksum_validation_mode->AsEnum();
} }
@ -225,13 +228,15 @@ uint32_t AF_PacketSource::GetFanoutMode(bool defrag)
} }
void AF_PacketSource::Close() void AF_PacketSource::Close()
{ {
if ( ! socket_fd ) if ( socket_fd < 0 )
return; return;
delete rx_ring; delete rx_ring;
close(socket_fd); rx_ring = nullptr;
socket_fd = 0;
close(socket_fd);
socket_fd = -1;
Closed(); Closed();
} }