ether.cc

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1993-1994, 1998 The Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. All advertising materials mentioning features or use of this software
00014  *    must display the following acknowledgement:
00015  *      This product includes software developed by the University of
00016  *      California, Berkeley and the Network Research Group at
00017  *      Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #if 0
00036 #ifndef lint
00037 static const char rcsid[] =
00038     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/emulate/ether.cc,v 1.4 2000/02/08 23:35:12 salehi Exp $ (LBL)";
00039 #endif
00040 #endif
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <sys/types.h>
00044 #include <sys/socket.h>
00045 #include <sys/ioctl.h>
00046 #include <net/ethernet.h>
00047 
00048 #include "config.h"
00049 #include "ether.h"
00050 
00051 char Ethernet::hex[] = "0123456789abcdef";
00052 
00053 void
00054 Ethernet::ether_print(const u_char *bp)
00055 {
00056         const struct ether_header *ep;
00057  
00058         ep = (const struct ether_header *)bp;
00059     printf("src: %s\n",
00060          etheraddr_string(ep->ether_shost));
00061     printf("dst: %s\n",
00062          etheraddr_string(ep->ether_dhost));
00063     printf("prot: %hx\n",
00064          ntohs(ep->ether_type));
00065 }
00066 
00067 char *
00068 Ethernet::etheraddr_string(const u_char *ep)
00069 {   
00070         unsigned i, j;
00071         register char *cp;
00072         static char buf[sizeof("00:00:00:00:00:00")];
00073     
00074         cp = buf;
00075         if ((j = *ep >> 4) != 0)
00076                 *cp++ = hex[j]; 
00077         *cp++ = hex[*ep++ & 0xf]; 
00078         for (i = 5; (int)--i >= 0;) {
00079                 *cp++ = ':';
00080                 if ((j = *ep >> 4) != 0)
00081                         *cp++ = hex[j]; 
00082                 *cp++ = hex[*ep++ & 0xf]; 
00083         }       
00084         *cp = '\0'; 
00085     return(buf);
00086 }   
00087 
00088 #include <net/if.h>
00089 
00090 u_char *
00091 Ethernet::nametoaddr(const char *devname)
00092 {
00093     static struct ifreq ifr;
00094     int s;
00095 
00096     memset(&ifr, 0, sizeof(ifr));
00097     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00098         fprintf(stderr, "Ethernet::nametoaddr-- failed socket\n");
00099         return NULL;
00100     }
00101     strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
00102     if (ioctl(s, SIOCGIFADDR, (char *)&ifr) < 0) {
00103         fprintf(stderr, "Ethernet::nametoaddr-- failed SIOCGIFADDR\n");
00104         return NULL;
00105     }
00106     return ((u_char*) &ifr.ifr_data);
00107 }

Generated on Tue Mar 6 16:47:45 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6