From owner-freebsd-net@FreeBSD.ORG Thu Mar 17 13:15:45 2005 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BAD6C16A4CE for ; Thu, 17 Mar 2005 13:15:45 +0000 (GMT) Received: from spinett.bth.se (spinett.bth.se [194.47.129.13]) by mx1.FreeBSD.org (Postfix) with ESMTP id C293F43D4C for ; Thu, 17 Mar 2005 13:15:44 +0000 (GMT) (envelope-from Patrik.Arlos@bth.se) Received: from Trantor (spinett.bth.se [194.47.129.13])j2HDFgTF019650 for ; Thu, 17 Mar 2005 14:15:42 +0100 From: "Patrik Arlos" To: Date: Thu, 17 Mar 2005 14:15:31 +0100 Organization: Blekinge Institute of Technology Message-ID: <00c701c52af3$6528cf40$73942fc2@Trantor> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.4510 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Scanned-By: MIMEDefang 2.51 on 194.47.129.13 Subject: Accessing ifunit/ifaddr_byindex X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Mar 2005 13:15:45 -0000 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 #include #include #include #include #include #include #include #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 } 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