+
+pcap_t *pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) {
+ pcap_t *handle;
+ int fd;
+
+ /* Allocate a handle for this session. */
+
+ handle = malloc(sizeof(*handle));
+ if (handle == NULL) {
+ snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
+ pcap_strerror(errno));
+ return NULL;
+ }
+
+ /* Initialize some components of the pcap structure. */
+
+ memset(handle, 0, sizeof(*handle));
+ handle->snapshot = snaplen;
+ handle->md.timeout = to_ms;
+
+ handle->inject_op = pcap_inject_acn;
+ handle->setfilter_op = pcap_setfilter_acn;
+ handle->setdirection_op = pcap_setdirection_acn;
+ handle->set_datalink_op = NULL; /* can't change data link type */
+ handle->getnonblock_op = pcap_getnonblock_fd;
+ handle->setnonblock_op = pcap_setnonblock_fd;
+ handle->close_op = pcap_close_acn;
+ handle->read_op = pcap_read_acn;
+ handle->stats_op = pcap_stats_acn;
+
+ fd = acn_open_live(device, ebuf, &handle->linktype);
+ if (fd == -1) {
+ free(handle);
+ return NULL;
+ }
+ handle->md.clear_promisc = promisc;
+ handle->fd = fd;
+ handle->bufsize = handle->snapshot;
+
+ /* Allocate the buffer */
+
+ handle->buffer = malloc(handle->bufsize + handle->offset);
+ if (!handle->buffer) {
+ snprintf(ebuf, PCAP_ERRBUF_SIZE,
+ "malloc: %s", pcap_strerror(errno));
+ pcap_close_acn(handle);
+ free(handle);
+ return NULL;
+ }
+
+ /*
+ * "handle->fd" is a socket, so "select()" and "poll()"
+ * should work on it.
+ */
+ handle->selectable_fd = handle->fd;
+
+ return handle;
+}