pynetwork / pypcap

pypcap - python libpcap module, forked from code.google.com/p/pypcap
Other
299 stars 74 forks source link

get a fd by pcap_get_selectable_fd #21

Open xirs opened 8 years ago

xirs commented 8 years ago

fd returned by fileno() can't be used by select() or poll(), please refer http://www.tcpdump.org/manpages/pcap_get_selectable_fd.3pcap.html

will pypcap add a new method to return fd by pcap_get_selectable_fd

guyharris commented 8 years ago

Actually, if you've opened a regular network interface, on most platforms, the descriptor returned by pcap_fileno() is the same as the descriptor returned by pcap_get_selectable_fd(). But it's still best to use pcap_get_selectable_fd() if it's available, as it's not guaranteed to return the same descriptor as pcap_fileno(), and it's more likely to return -1 if, in fact, there is no way to do a select()/poll()/etc. on the device (you can't do them on a DAG card, for example).

brifordwylie commented 8 years ago

@guyharris you seem to know quite a bit about his.. is there a feature request here? or just documentation/example request?

guyharris commented 8 years ago

you seem to know quite a bit about his

Being a core libpcap developer for many years helps. :-)

is there a feature request here?

I would say "yes".

I would, on UN*Xes, provide an API that uses pcap_get_selectable_fd() if available and pcap_fileno() otherwise. Whether it should fail or return fileno(pcap_file(pcap)) if pcap_file(pcap) is non-null is another matter - select()/poll() on a plain file should always return TRUE, but that means there's no point in doing a select() or poll() on it unless the program logic makes it harder not to do so.

I would, on Windows, not provide that API, because there isn't a file descriptor that can be provided that would be usable with WinSock's select() call, and would, instead, provide an API that uses pcap_getevent(), which returns a HANDLE that can be used with WaitForSingleObject(), WaitForSingleObjectEx(), WaitForMultipleObjects(), WaitForMultipleObjectsEx(), MsgWaitForMultipleObjects(), and MsgWaitForMultipleObjectsEx().

brifordwylie commented 8 years ago

@guyharris: you know way more than I do.. you should have write access to the repo now. Please make an Issue/PR at your convenience.