Accessing ifunit/ifaddr_byindex

Patrik Arlos Patrik.Arlos at bth.se
Thu Mar 17 05:15:45 PST 2005


Hi,

I'm writing a small program that tries to read out some parameters from a
ifnet structure. The code used ifunit() to obtain the ifnet structure for
the interface in question and later on it used ifaddr_byindex. The code
compiles nicely, but fails miserably to link, with undefined references to
the two mentioned functions. I've googled quite a lot (but obviously at the
wrong places), but can't find what library to include.  I've included the
code below, the compile command used is the following; 'gcc -o ifList.bsd
-lpcap ifList.c'. The system is a FreeBSD-5.3-Release (?). 

If there are any other ways of finding the hw address and MTU for an
Interface please let me know.

/Patrik

<Being code ifList.c>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <pcap.h>

#define AF_PACKET 17
#define LINE_LEN 20
#define ETH_ALEN 6

#define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))

void dispatch_handler(u_char*, const struct pcap_pkthdr *, const u_char*);
int main(int argc, char **argv){
  pcap_if_t *alldevs,*d;
  pcap_t *fp;
  int inum,i=1;
  struct ifreq ifr;
  int s;
  char my_mac[ETH_ALEN];

  char errbuf[PCAP_ERRBUF_SIZE+1];
  if( (pcap_findalldevs(&alldevs,errbuf))== -1) {
    fprintf(stderr,"Oh my god. %s \n",errbuf);
    exit(1);
  }
  for(d=alldevs;d;d=d->next){
    printf("%d : name = %s\n",i,d->name);
    printf("\t Description %s\n",d->description);
    printf("\t    Loopback %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
    i++;
  }
  pcap_freealldevs(alldevs);

  strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
  s = socket(AF_PACKET, SOCK_RAW, htons(0x0810));
  /* BSD MOD */
  // Get interface index, MTU and hardware address
  struct ifnet *ifp;
  ifp=(struct ifnet *)ifunit(ifr.ifr_name);
  int ifindex=ifp->if_index;
  // Get the MTU and HARDWARE address of interface.
  
  struct ifaddr *myAddress;
  struct sockaddr_dl *sdl;
  
  myAddress=(struct ifaddr *)ifaddr_byindex(ifp->if_index);
  sdl = (struct sockaddr_dl *)myAddress->ifa_addr;
  bcopy(&my_mac,LLADDR(sdl),ETH_ALEN);
  printf("ifAdd =
%02X:%02X:%02X:%02X:%02X:%02X\n",my_mac[0],my_mac[1],my_mac[2],my_mac[3],my_
mac[4],my_mac[5]);
  
  int myMTU=ifp->if_mtu;
  printf("MAC Interface MTU: %d \n",myMTU);
  
  fp=pcap_open_live(argv[1],56,1,20,errbuf);
  if(fp==-1){
    fprintf(stderr,"Error opening adapter %s",argv[1]);
    return(-1);
  }
  pcap_loop(fp,5,dispatch_handler,NULL);
   
  return(0);
}

void dispatch_handler(u_char *temp1, const struct pcap_pkthdr *header, const
u_char *pkt_data)
{
  u_int i=0;
  printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec,
header->len);
  
}
</end code>





Patrik Arlos
Tech. Lic Telecommunications systems
PhD Candidate Telecommunications
Blekinge Institute of Technology
School of Engineering, Telecommunications Group
371 79  KARLSKRONA
SWEDEN
+46 (0)455 385654 Office
+46 (0)733 800312 Mobile
 




More information about the freebsd-net mailing list