Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 17 Mar 2005 14:15:31 +0100
From:      "Patrik Arlos" <Patrik.Arlos@bth.se>
To:        <freebsd-net@freebsd.org>
Subject:   Accessing ifunit/ifaddr_byindex
Message-ID:  <00c701c52af3$6528cf40$73942fc2@Trantor>

next in thread | raw e-mail | index | archive | help
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 (?).=20

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=3D1;
  struct ifreq ifr;
  int s;
  char my_mac[ETH_ALEN];

  char errbuf[PCAP_ERRBUF_SIZE+1];
  if( (pcap_findalldevs(&alldevs,errbuf))=3D=3D -1) {
    fprintf(stderr,"Oh my god. %s \n",errbuf);
    exit(1);
  }
  for(d=3Dalldevs;d;d=3Dd->next){
    printf("%d : name =3D %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 =3D socket(AF_PACKET, SOCK_RAW, htons(0x0810));
  /* BSD MOD */
  // Get interface index, MTU and hardware address
  struct ifnet *ifp;
  ifp=3D(struct ifnet *)ifunit(ifr.ifr_name);
  int ifindex=3Difp->if_index;
  // Get the MTU and HARDWARE address of interface.
 =20
  struct ifaddr *myAddress;
  struct sockaddr_dl *sdl;
 =20
  myAddress=3D(struct ifaddr *)ifaddr_byindex(ifp->if_index);
  sdl =3D (struct sockaddr_dl *)myAddress->ifa_addr;
  bcopy(&my_mac,LLADDR(sdl),ETH_ALEN);
  printf("ifAdd =3D
%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]);
 =20
  int myMTU=3Difp->if_mtu;
  printf("MAC Interface MTU: %d \n",myMTU);
 =20
  fp=3Dpcap_open_live(argv[1],56,1,20,errbuf);
  if(fp=3D=3D-1){
    fprintf(stderr,"Error opening adapter %s",argv[1]);
    return(-1);
  }
  pcap_loop(fp,5,dispatch_handler,NULL);
  =20
  return(0);
}

void dispatch_handler(u_char *temp1, const struct pcap_pkthdr *header, =
const
u_char *pkt_data)
{
  u_int i=3D0;
  printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec,
header->len);
 =20
}
</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
=20




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?00c701c52af3$6528cf40$73942fc2>