- for (packets = 0; max_packets == -1 || packets < max_packets;)
- {
- status = pcap_read_packet(handle, callback, user);
-
- if (status > 0) {
- packets += status;
- continue;
- } else if (status == -1)
- return -1;
-
- /*
- * If no packet is available we go to sleep. FIXME: This
- * might be better implemented using poll(?)
- */
- FD_ZERO(&read_fds);
- FD_SET(handle->fd, &read_fds);
- status = select(handle->fd + 1,
- &read_fds, NULL, NULL, &tv);
-
- if (status == -1) {
- if (errno == EINTR)
- return packets;
- snprintf(handle->errbuf, sizeof(handle->errbuf),
- "select: %s", pcap_strerror(errno));
- return -1;
- }
- else if (status == 0 ||
- (tv.tv_usec == 0 && tv.tv_sec == 0))
- return packets;
- }
-
- return packets;