sending MAC packets --- again

Chuck Swiger cswiger at mac.com
Wed May 11 20:00:36 PDT 2005


Daniel Valencia wrote:
[ ...pcap delays... ]
> Has anyone in this list had any experience with
> libpcap over fbsd that can point me into the right
> direction?

Sure.  Are you trying to use non-blocking mode of PCAP by any chance?  I found 
that to be fairly busted on FreeBSD and would drop lots of packets, just as 
you've described.  PCAP timeouts in blocking mode also seem to not work very 
well, in the sense that the timeout starts after the first packet is received.

I've got a code snippet handy:

/* This routine obtains a list of all of the network interfaces on the machine
  * for each interface found, check to see whether the interface is UP and
  * whether the user wants this interface to be used.  If so, open the packet
  * capture interface and add this (struct interface) to IFL.
  */
void
init_interfaces() {
     struct ifaddrs *if_ptr;
     char *name;
     u_int flags;
     struct interface *new;
[ ... ]

     LIST_INIT(&IFL);

     if (getifaddrs(&ifap) == -1) {
         fatal(strerror(errno));
         /*NOTREACHED*/
     }

     /* iterate over the list of interfaces on the machine */
     for (if_ptr = ifap; if_ptr; if_ptr = if_ptr->ifa_next) {
         name = if_ptr->ifa_name;
         flags = if_ptr->ifa_flags;
         /* check that the interface is UP before we try to use it */
         if (!(flags & IFF_UP)) continue;

         switch (if_ptr->ifa_addr->sa_family) {
           case AF_INET:
             /* check whether the user specified this interface */
             if (check_interface(name)) {
                 new = calloc(1, sizeof(struct interface));
                 if (!new) fatal("can't calloc() interface structure");
                 new->name = name;
                 new->addr = (struct sockaddr_in *)if_ptr->ifa_addr;
[ ... ]
                 new->pcap_fd = pcap_open_live(name, CAPSIZE, 1, 50, errbuf);
                 if (new->pcap_fd == 0) {
                     logwarn("init_interfaces(): error calling 
pcap_open_live():\n");
                     fatal(errbuf);
                     /*NOTREACHED*/
                 }

#if 0  /* XXX: non-blocking mode seems to drop lots of packets, don't use */
                 if (pcap_setnonblock(new->pcap_fd, 1, errbuf) == -1) {
                     loginfo("init_interfaces(): pcap_setnonblock failed!\n");
                     fatal(errbuf);
                 }
#endif
[ ... ]

-- 
-Chuck



More information about the freebsd-net mailing list