2 * pcap-dag.c: Packet capture interface for Endace DAG card.
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
7 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
8 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
9 * is not defined then nothing is altered - the dag_ functions will be
10 * called as required from their pcap-linux/bpf equivalents.
12 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
19 static const char rcsid[] _U_ =
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.21.2.7 2007/06/22 06:43:58 guy Exp $ (LBL)";
27 #include <sys/param.h> /* optionally get BSD define */
36 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
42 struct mbuf; /* Squelch compiler warnings on some platforms for */
43 struct rtentry; /* declarations in <net/if.h> */
49 #define ATM_CELL_SIZE 52
50 #define ATM_HDR_SIZE 4
53 * A header containing additional MTP information.
55 #define MTP2_SENT_OFFSET 0 /* 1 byte */
56 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
57 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
58 #define MTP2_HDR_LEN 4 /* length of the header */
60 #define MTP2_ANNEX_A_NOT_USED 0
61 #define MTP2_ANNEX_A_USED 1
62 #define MTP2_ANNEX_A_USED_UNKNOWN 2
64 /* SunATM pseudo header */
66 unsigned char flags; /* destination and traffic type */
67 unsigned char vpi; /* VPI */
68 unsigned short vci; /* VCI */
71 typedef struct pcap_dag_node {
72 struct pcap_dag_node *next;
77 static pcap_dag_node_t *pcap_dags = NULL;
78 static int atexit_handler_installed = 0;
79 static const unsigned short endian_test_word = 0x0100;
81 #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
85 /* This code is required when compiling for a DAG device only. */
88 /* Replace dag function names with pcap equivalent. */
89 #define dag_open_live pcap_open_live
90 #define dag_platform_finddevs pcap_platform_finddevs
93 #define MAX_DAG_PACKET 65536
95 static unsigned char TempPkt[MAX_DAG_PACKET];
97 static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
98 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
99 static int dag_set_datalink(pcap_t *p, int dlt);
100 static int dag_get_datalink(pcap_t *p);
101 static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
104 delete_pcap_dag(pcap_t *p)
106 pcap_dag_node_t *curr = NULL, *prev = NULL;
108 for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
112 if (curr != NULL && curr->p == p) {
114 prev->next = curr->next;
116 pcap_dags = curr->next;
122 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
123 * in the pcap_t structure, and closes the file descriptor for the DAG card.
127 dag_platform_close(pcap_t *p)
131 #ifdef HAVE_DAG_STREAMS_API
132 if(dag_stop_stream(p->fd, p->md.dag_stream) < 0)
133 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
135 if(dag_detach_stream(p->fd, p->md.dag_stream) < 0)
136 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
138 if(dag_stop(p->fd) < 0)
139 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
140 #endif /* HAVE_DAG_STREAMS_API */
141 if(dag_close(p->fd) < 0)
142 fprintf(stderr,"dag_close: %s\n", strerror(errno));
145 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
151 while (pcap_dags != NULL) {
152 if (pcap_dags->pid == getpid()) {
153 dag_platform_close(pcap_dags->p);
155 delete_pcap_dag(pcap_dags->p);
161 new_pcap_dag(pcap_t *p)
163 pcap_dag_node_t *node = NULL;
165 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
169 if (!atexit_handler_installed) {
170 atexit(atexit_handler);
171 atexit_handler_installed = 1;
174 node->next = pcap_dags;
176 node->pid = getpid();
184 * Read at most max_packets from the capture stream and call the callback
185 * for each of them. Returns the number of packets handled, -1 if an
186 * error occured, or -2 if we were told to break out of the loop.
189 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
191 unsigned int processed = 0;
192 int flags = p->md.dag_offset_flags;
193 unsigned int nonblocking = flags & DAGF_NONBLOCK;
195 /* Get the next bufferful of packets (if necessary). */
196 while (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) {
199 * Has "pcap_breakloop()" been called?
203 * Yes - clear the flag that indicates that
204 * it has, and return -2 to indicate that
205 * we were told to break out of the loop.
211 #ifdef HAVE_DAG_STREAMS_API
212 /* dag_advance_stream() will block (unless nonblock is called)
213 * until 64kB of data has accumulated.
214 * If to_ms is set, it will timeout before 64kB has accumulated.
215 * We wait for 64kB because processing a few packets at a time
216 * can cause problems at high packet rates (>200kpps) due
218 * This does mean if to_ms is not specified the capture may 'hang'
219 * for long periods if the data rate is extremely slow (<64kB/sec)
220 * If non-block is specified it will return immediately. The user
221 * is then responsible for efficiency.
223 p->md.dag_mem_top = dag_advance_stream(p->fd, p->md.dag_stream, &(p->md.dag_mem_bottom));
225 /* dag_offset does not support timeouts */
226 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
227 #endif /* HAVE_DAG_STREAMS_API */
229 if (nonblocking && (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
231 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
237 (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
239 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
245 /* Process the packets. */
246 while (p->md.dag_mem_top - p->md.dag_mem_bottom >= dag_record_size) {
248 unsigned short packet_len = 0;
250 struct pcap_pkthdr pcap_header;
252 #ifdef HAVE_DAG_STREAMS_API
253 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_bottom);
255 dag_record_t *header = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
256 #endif /* HAVE_DAG_STREAMS_API */
258 u_char *dp = ((u_char *)header) + dag_record_size;
262 * Has "pcap_breakloop()" been called?
266 * Yes - clear the flag that indicates that
267 * it has, and return -2 to indicate that
268 * we were told to break out of the loop.
274 rlen = ntohs(header->rlen);
275 if (rlen < dag_record_size)
277 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
280 p->md.dag_mem_bottom += rlen;
282 switch(header->type) {
286 if (header->type == TYPE_AAL5) {
287 packet_len = ntohs(header->wlen);
288 caplen = rlen - dag_record_size;
293 if (header->type == TYPE_MC_ATM) {
294 caplen = packet_len = ATM_CELL_SIZE;
300 if (header->type == TYPE_MC_AAL5) {
301 packet_len = ntohs(header->wlen);
302 caplen = rlen - dag_record_size - 4;
306 if (header->type == TYPE_ATM) {
307 caplen = packet_len = ATM_CELL_SIZE;
309 if (p->linktype == DLT_SUNATM) {
310 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
311 unsigned long rawatm;
313 rawatm = ntohl(*((unsigned long *)dp));
314 sunatm->vci = htons((rawatm >> 4) & 0xffff);
315 sunatm->vpi = (rawatm >> 20) & 0x00ff;
316 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
317 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
318 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
319 ((dp[ATM_HDR_SIZE] == 0xaa &&
320 dp[ATM_HDR_SIZE+1] == 0xaa &&
321 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
324 packet_len -= ATM_HDR_SIZE;
325 caplen -= ATM_HDR_SIZE;
330 #ifdef TYPE_DSM_COLOR_ETH
331 case TYPE_DSM_COLOR_ETH:
333 #ifdef TYPE_COLOR_ETH
337 packet_len = ntohs(header->wlen);
338 packet_len -= (p->md.dag_fcs_bits >> 3);
339 caplen = rlen - dag_record_size - 2;
340 if (caplen > packet_len) {
345 #ifdef TYPE_DSM_COLOR_HDLC_POS
346 case TYPE_DSM_COLOR_HDLC_POS:
348 #ifdef TYPE_COLOR_HDLC_POS
349 case TYPE_COLOR_HDLC_POS:
352 packet_len = ntohs(header->wlen);
353 packet_len -= (p->md.dag_fcs_bits >> 3);
354 caplen = rlen - dag_record_size;
355 if (caplen > packet_len) {
359 #ifdef TYPE_COLOR_MC_HDLC_POS
360 case TYPE_COLOR_MC_HDLC_POS:
364 packet_len = ntohs(header->wlen);
365 packet_len -= (p->md.dag_fcs_bits >> 3);
366 caplen = rlen - dag_record_size - 4;
367 if (caplen > packet_len) {
370 /* jump the MC_HDLC_HEADER */
372 if (p->linktype == DLT_MTP2_WITH_PHDR) {
373 /* Add the MTP2 Pseudo Header */
374 caplen += MTP2_HDR_LEN;
375 packet_len += MTP2_HDR_LEN;
377 TempPkt[MTP2_SENT_OFFSET] = 0;
378 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
379 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
380 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
381 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
387 /* Unhandled ERF type.
388 * Ignore rather than generating error
393 if (caplen > p->snapshot)
394 caplen = p->snapshot;
396 /* Count lost packets. */
397 switch(header->type) {
398 #ifdef TYPE_COLOR_HDLC_POS
399 /* in this type the color value overwrites the lctr */
400 case TYPE_COLOR_HDLC_POS:
403 #ifdef TYPE_COLOR_ETH
404 /* in this type the color value overwrites the lctr */
408 #ifdef TYPE_DSM_COLOR_HDLC_POS
409 /* in this type the color value overwrites the lctr */
410 case TYPE_DSM_COLOR_HDLC_POS:
413 #ifdef TYPE_DSM_COLOR_ETH
414 /* in this type the color value overwrites the lctr */
415 case TYPE_DSM_COLOR_ETH:
418 #ifdef TYPE_COLOR_MC_HDLC_POS
419 case TYPE_COLOR_MC_HDLC_POS:
425 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
426 p->md.stat.ps_drop = UINT_MAX;
428 p->md.stat.ps_drop += ntohs(header->lctr);
433 /* Run the packet filter if there is one. */
434 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
436 /* convert between timestamp formats */
437 register unsigned long long ts;
439 if (IS_BIGENDIAN()) {
440 ts = SWAPLL(header->ts);
445 pcap_header.ts.tv_sec = ts >> 32;
446 ts = (ts & 0xffffffffULL) * 1000000;
447 ts += 0x80000000; /* rounding */
448 pcap_header.ts.tv_usec = ts >> 32;
449 if (pcap_header.ts.tv_usec >= 1000000) {
450 pcap_header.ts.tv_usec -= 1000000;
451 pcap_header.ts.tv_sec++;
454 /* Fill in our own header data */
455 pcap_header.caplen = caplen;
456 pcap_header.len = packet_len;
458 /* Count the packet. */
459 p->md.stat.ps_recv++;
461 /* Call the user supplied callback function */
462 callback(user, &pcap_header, dp);
464 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
466 if (processed == cnt)
468 /* Reached the user-specified limit. */
478 dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
480 strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards",
486 * Get a handle for a live capture from the given DAG device. Passing a NULL
487 * device will result in a failure. The promisc flag is ignored because DAG
488 * cards are always promiscuous. The to_ms parameter is also ignored as it is
489 * not supported in hardware.
491 * snaplen is now also ignored, until we get per-stream slen support. Set
492 * slen with approprite DAG tool BEFORE pcap_open_live().
497 dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
499 char conf[30]; /* dag configure string */
504 char * newDev = NULL;
505 #ifdef HAVE_DAG_STREAMS_API
507 struct timeval maxwait;
511 if (device == NULL) {
512 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));
515 /* Allocate a handle for this session. */
517 handle = malloc(sizeof(*handle));
518 if (handle == NULL) {
519 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
523 /* Initialize some components of the pcap structure. */
525 memset(handle, 0, sizeof(*handle));
527 #ifdef HAVE_DAG_STREAMS_API
528 newDev = (char *)malloc(strlen(device) + 16);
529 if (newDev == NULL) {
530 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
534 /* Parse input name to get dag device and stream number if provided */
535 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
536 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
541 if (handle->md.dag_stream%2) {
542 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
546 if (strncmp(device, "/dev/", 5) != 0) {
547 newDev = (char *)malloc(strlen(device) + 5);
548 if (newDev == NULL) {
549 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
552 strcpy(newDev, "/dev/");
553 strcat(newDev, device);
556 #endif /* HAVE_DAG_STREAMS_API */
558 /* setup device parameters */
559 if((handle->fd = dag_open((char *)device)) < 0) {
560 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
564 #ifdef HAVE_DAG_STREAMS_API
565 /* Open requested stream. Can fail if already locked or on error */
566 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
567 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
571 /* Set up default poll parameters for stream
572 * Can be overridden by pcap_set_nonblock()
574 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
575 &mindata, &maxwait, &poll) < 0) {
576 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
580 /* Amount of data to collect in Bytes before calling callbacks.
581 * Important for efficiency, but can introduce latency
582 * at low packet rates if to_ms not set!
586 /* Obey to_ms if supplied. This is a good idea!
587 * Recommend 10-100ms. Calls will time out even if no data arrived.
589 maxwait.tv_sec = to_ms/1000;
590 maxwait.tv_usec = (to_ms%1000) * 1000;
592 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
593 mindata, &maxwait, &poll) < 0) {
594 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
599 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
600 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
604 #endif /* HAVE_DAG_STREAMS_API */
606 /* XXX Not calling dag_configure() to set slen; this is unsafe in
607 * multi-stream environments as the gpp config is global.
608 * Once the firmware provides 'per-stream slen' this can be supported
609 * again via the Config API without side-effects */
611 /* set the card snap length to the specified snaplen parameter */
612 /* This is a really bad idea, as different cards have different
613 * valid slen ranges. Should fix in Config API. */
614 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
615 snaplen = MAX_DAG_SNAPLEN;
616 } else if (snaplen < MIN_DAG_SNAPLEN) {
617 snaplen = MIN_DAG_SNAPLEN;
619 /* snap len has to be a multiple of 4 */
620 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
622 if(dag_configure(handle->fd, conf) < 0) {
623 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
628 #ifdef HAVE_DAG_STREAMS_API
629 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
630 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
634 if(dag_start(handle->fd) < 0) {
635 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
638 #endif /* HAVE_DAG_STREAMS_API */
641 * Important! You have to ensure bottom is properly
642 * initialized to zero on startup, it won't give you
643 * a compiler warning if you make this mistake!
645 handle->md.dag_mem_bottom = 0;
646 handle->md.dag_mem_top = 0;
647 handle->md.dag_fcs_bits = 32;
649 /* Query the card first for special cases. */
650 daginf = dag_info(handle->fd);
651 if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code))
653 /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */
654 handle->md.dag_fcs_bits = 0;
657 /* Then allow an environment variable to override. */
658 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
659 if ((n = atoi(s)) == 0 || n == 16|| n == 32) {
660 handle->md.dag_fcs_bits = n;
662 snprintf(ebuf, PCAP_ERRBUF_SIZE,
663 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
668 handle->snapshot = snaplen;
669 handle->md.dag_timeout = to_ms;
671 handle->linktype = -1;
672 if (dag_get_datalink(handle) < 0) {
673 strcpy(ebuf, handle->errbuf);
679 if (new_pcap_dag(handle) < 0) {
680 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
685 * "select()" and "poll()" don't work on DAG device descriptors.
687 handle->selectable_fd = -1;
689 if (newDev != NULL) {
690 free((char *)newDev);
693 handle->read_op = dag_read;
694 handle->inject_op = dag_inject;
695 handle->setfilter_op = dag_setfilter;
696 handle->setdirection_op = NULL; /* Not implemented.*/
697 handle->set_datalink_op = dag_set_datalink;
698 handle->getnonblock_op = pcap_getnonblock_fd;
699 handle->setnonblock_op = dag_setnonblock;
700 handle->stats_op = dag_stats;
701 handle->close_op = dag_platform_close;
702 handle->md.stat.ps_drop = 0;
703 handle->md.stat.ps_recv = 0;
706 #ifdef HAVE_DAG_STREAMS_API
708 if (handle != NULL) {
709 if (dag_stop_stream(handle->fd, handle->md.dag_stream) < 0)
710 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
714 if (handle != NULL) {
715 if (dag_detach_stream(handle->fd, handle->md.dag_stream) < 0)
716 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
720 if (handle != NULL) {
721 if (dag_stop(p->fd) < 0)
722 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
724 #endif /* HAVE_DAG_STREAMS_API */
727 if (handle != NULL) {
728 if (dag_close(handle->fd) < 0)
729 fprintf(stderr,"dag_close: %s\n", strerror(errno));
732 delete_pcap_dag(handle);
735 if (newDev != NULL) {
736 free((char *)newDev);
738 if (handle != NULL) {
740 * Get rid of any link-layer type list we allocated.
742 if (handle->dlt_list != NULL) {
743 free(handle->dlt_list);
752 dag_stats(pcap_t *p, struct pcap_stat *ps) {
753 /* This needs to be filled out correctly. Hopefully a dagapi call will
754 provide all necessary information.
756 /*p->md.stat.ps_recv = 0;*/
757 /*p->md.stat.ps_drop = 0;*/
765 * Simply submit all possible dag names as candidates.
766 * pcap_add_if() internally tests each candidate with pcap_open_live(),
767 * so any non-existent devices are dropped.
768 * For 2.5 try all rx stream names as well.
771 dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
773 char name[12]; /* XXX - pick a size */
777 /* Try all the DAGs 0-9 */
778 for (c = 0; c < 9; c++) {
779 snprintf(name, 12, "dag%d", c);
780 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
786 #ifdef HAVE_DAG_STREAMS_API
789 for(stream=0;stream<16;stream+=2) {
790 snprintf(name, 10, "dag%d:%d", c, stream);
791 if (pcap_add_if(devlistp, name, 0, NULL, errbuf) == -1) {
799 #endif /* HAVE_DAG_STREAMS_API */
805 * Installs the given bpf filter program in the given pcap structure. There is
806 * no attempt to store the filter in kernel memory as that is not supported
810 dag_setfilter(pcap_t *p, struct bpf_program *fp)
815 strncpy(p->errbuf, "setfilter: No filter specified",
820 /* Make our private copy of the filter */
822 if (install_bpf_program(p, fp) < 0)
831 dag_set_datalink(pcap_t *p, int dlt)
839 dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
842 * Set non-blocking mode on the FD.
843 * XXX - is that necessary? If not, don't bother calling it,
844 * and have a "dag_getnonblock()" function that looks at
845 * "p->md.dag_offset_flags".
847 if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
849 #ifdef HAVE_DAG_STREAMS_API
852 struct timeval maxwait;
855 if (dag_get_stream_poll(p->fd, p->md.dag_stream,
856 &mindata, &maxwait, &poll) < 0) {
857 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
861 /* Amount of data to collect in Bytes before calling callbacks.
862 * Important for efficiency, but can introduce latency
863 * at low packet rates if to_ms not set!
870 if (dag_set_stream_poll(p->fd, p->md.dag_stream,
871 mindata, &maxwait, &poll) < 0) {
872 snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
876 #endif /* HAVE_DAG_STREAMS_API */
878 p->md.dag_offset_flags |= DAGF_NONBLOCK;
880 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
886 dag_get_datalink(pcap_t *p)
891 memset(types, 0, 255);
893 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
894 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
900 #ifdef HAVE_DAG_GET_ERF_TYPES
901 /* Get list of possible ERF types for this card */
902 if (dag_get_erf_types(p->fd, types, 255) < 0) {
903 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
907 while (types[index]) {
909 /* Check the type through a dagapi call. */
910 types[index] = dag_linktype(p->fd);
914 switch(types[index]) {
917 #ifdef TYPE_COLOR_HDLC_POS
918 case TYPE_COLOR_HDLC_POS:
920 #ifdef TYPE_DSM_COLOR_HDLC_POS
921 case TYPE_DSM_COLOR_HDLC_POS:
923 if (p->dlt_list != NULL) {
924 p->dlt_list[index++] = DLT_CHDLC;
925 p->dlt_list[index++] = DLT_PPP_SERIAL;
926 p->dlt_list[index++] = DLT_FRELAY;
929 p->linktype = DLT_CHDLC;
933 #ifdef TYPE_COLOR_ETH
936 #ifdef TYPE_DSM_COLOR_ETH
937 case TYPE_DSM_COLOR_ETH:
940 * This is (presumably) a real Ethernet capture; give it a
941 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
942 * that an application can let you choose it, in case you're
943 * capturing DOCSIS traffic that a Cisco Cable Modem
944 * Termination System is putting out onto an Ethernet (it
945 * doesn't put an Ethernet header onto the wire, it puts raw
946 * DOCSIS frames out on the wire inside the low-level
949 if (p->dlt_list != NULL) {
950 p->dlt_list[index++] = DLT_EN10MB;
951 p->dlt_list[index++] = DLT_DOCSIS;
954 p->linktype = DLT_EN10MB;
967 if (p->dlt_list != NULL) {
968 p->dlt_list[index++] = DLT_ATM_RFC1483;
969 p->dlt_list[index++] = DLT_SUNATM;
972 p->linktype = DLT_ATM_RFC1483;
975 #ifdef TYPE_COLOR_MC_HDLC_POS
976 case TYPE_COLOR_MC_HDLC_POS:
980 if (p->dlt_list != NULL) {
981 p->dlt_list[index++] = DLT_CHDLC;
982 p->dlt_list[index++] = DLT_PPP_SERIAL;
983 p->dlt_list[index++] = DLT_FRELAY;
984 p->dlt_list[index++] = DLT_MTP2;
985 p->dlt_list[index++] = DLT_MTP2_WITH_PHDR;
988 p->linktype = DLT_CHDLC;
994 p->linktype = DLT_NULL;
998 snprintf(p->errbuf, sizeof(p->errbuf), "unknown DAG linktype %d", types[index]);
1004 p->dlt_count = index;