00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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 }