ifconfig 源码, MAC地址分类

源码地址:http://www.tazenda.demon.co.uk/phil/net-tools/

 

$ strace -e socket,ioctl ifconfig eth0 

socket(PF_FILE, SOCK_DGRAM, 0)          = 3 
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4 
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5 
ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0 
ioctl(5, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=00:19:21:10:41:ef}) = 0 
ioctl(5, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0 
ioctl(5, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=1500}) = 0 
ioctl(5, SIOCGIFMAP, {ifr_name="eth0", ifr_map={mem_start=0, mem_end=0, base_addr=0, irq=20, dma=0, port=0}}) = 0 
ioctl(5, SIOCGIFMAP, {ifr_name="eth0", ifr_map={mem_start=0, mem_end=0, base_addr=0, irq=20, dma=0, port=0}}) = 0 
ioctl(5, SIOCGIFTXQLEN, {ifr_name="eth0", ifr_qlen=1000}) = 0 
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("10.67.7.119")}}) = 0 
ioctl(4, SIOCGIFDSTADDR, {ifr_name="eth0", ifr_dstaddr={AF_INET, inet_addr("10.67.7.119")}}) = 0 
ioctl(4, SIOCGIFBRDADDR, {ifr_name="eth0", ifr_broadaddr={AF_INET, inet_addr("10.67.7.255")}}) = 0 
ioctl(4, SIOCGIFNETMASK, {ifr_name="eth0", ifr_netmask={AF_INET, inet_addr("255.255.255.0")}}) = 0 

实际上ifconfig命令就是通过socket ioctl 拿到网卡的信息,诸如ip地址,MAC MTU 之类的 


MAC地址解析-广播地址、组播地址和单播地址的特征

紫梦芊 发表于: 三月 16th, 2011 | 阅读: | 评论数:  (0)

MAC地址分成三类,分别是广播地址、组播地址和单播地址。首先,FF:FF:FF:FF:FF:FF毫无疑问是广播地址。每个网卡出厂时被分配唯一一个单播地址,头24位是设备制造厂商的编号,由IEEE(电气与电子工程师协会)分配,后24位是设备厂商为网卡制定的唯一编号。例如08:00:20:0A:8C:6D是单播地址的例子,其中08:00:20是著名的CPU厂商AMD的编号。单播地址的特征是头8位的最低位为0,于是单播地址共有47位的地址空间。
另一类地址就是组播地址。MAC组播地址的特征是头8位的最低位是1,于是MAC组播地址空间相当巨大——除去全1的47位地址空间。例如01:80:C2:00:00:00是一个组播地址,表示802.1d网桥多播组。网桥就是使用这个地址,相互之间交换配置信息,运行分布式生成树算法,消除网络拓扑结构中的环路。

IP层也有单播、组播和广播的概念。MAC组播地址中的01:00:5E:00:00:00到01:00:5E:7F:FF:FF共计23位对应于IP组播地址。给定一个IP组播地址,将其低23位与01:00:5E:00:00:00的低位取“与”运算,即可的到一个 MAC组播地址。但IP组播地址有28位地址空间,对应的MAC组播地址却只有23位,这样每32个IP组播地址被映射到同一MAC地址上。
MAC组播地址空间远远大于IP组播地址空间。如果网络层仅有IP协议,那么完全能将IP组播地址一对一地映射到MAC组播地址!但MAC地址是一个数据链路层的概念,其上层的网络层远远不只有IP。

用ifconfig修改mac地址:

用ifconfig eth3 hw ether F1:AC:6F:B8:C2:BF 设置mac地址时,会检查它的类型,如果是广播或组播地址或全0的MAC,就会判断它无效,并提示:

SIOCSIFHWADDR: Cannot assign requested address信息。

Cannot assign requested address是由strerror 函数打印出来的信息。

errno 是错误代码,在 errno.h头文件中;
perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符);
strerror 是通过参数 errnum (就是errno),返回对应的错误信息。它的提示信息见:http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/gen/errlst.c?rev=1.8.12.1;content-type=text%2Fplain中介绍,如:

const char *const sys_errlist[] = {
	"Undefined error: 0",			/*  0 - ENOERROR */
	"Operation not permitted",		/*  1 - EPERM */
	"No such file or directory",		/*  2 - ENOENT */
	"No such process",			/*  3 - ESRCH */
	"Interrupted system call",		/*  4 - EINTR */
	"Input/output error",			/*  5 - EIO */
	"Device not configured",		/*  6 - ENXIO */
	"Argument list too long",		/*  7 - E2BIG */
	"Exec format error",			/*  8 - ENOEXEC */
	"Bad file descriptor",			/*  9 - EBADF */
	"No child processes",			/* 10 - ECHILD */
	"Resource deadlock avoided",		/* 11 - EDEADLK */
	"Cannot allocate memory",		/* 12 - ENOMEM */
	"Permission denied",			/* 13 - EACCES */
	"Bad address",				/* 14 - EFAULT */
	"Block device required",		/* 15 - ENOTBLK */
	"Device busy",				/* 16 - EBUSY */
	"File exists",				/* 17 - EEXIST */
	"Cross-device link",			/* 18 - EXDEV */
	"Operation not supported by device",	/* 19 - ENODEV */
	"Not a directory",			/* 20 - ENOTDIR */
	"Is a directory",			/* 21 - EISDIR */
	"Invalid argument",			/* 22 - EINVAL */
	"Too many open files in system",	/* 23 - ENFILE */
	"Too many open files",			/* 24 - EMFILE */
	"Inappropriate ioctl for device",	/* 25 - ENOTTY */
	"Text file busy",			/* 26 - ETXTBSY */
	"File too large",			/* 27 - EFBIG */
	"No space left on device",		/* 28 - ENOSPC */
	"Illegal seek",				/* 29 - ESPIPE */
	"Read-only file system",		/* 30 - EROFS */
	"Too many links",			/* 31 - EMLINK */
	"Broken pipe",				/* 32 - EPIPE */

/* math software */
	"Numerical argument out of domain",	/* 33 - EDOM */
	"Result too large",			/* 34 - ERANGE */

/* non-blocking and interrupt i/o */
	"Resource temporarily unavailable",	/* 35 - EAGAIN */
						/* 35 - EWOULDBLOCK */
	"Operation now in progress",		/* 36 - EINPROGRESS */
	"Operation already in progress",	/* 37 - EALREADY */

/* ipc/network software -- argument errors */
	"Socket operation on non-socket",	/* 38 - ENOTSOCK */
	"Destination address required",		/* 39 - EDESTADDRREQ */
	"Message too long",			/* 40 - EMSGSIZE */
	"Protocol wrong type for socket",	/* 41 - EPROTOTYPE */
	"Protocol not available",		/* 42 - ENOPROTOOPT */
	"Protocol not supported",		/* 43 - EPROTONOSUPPORT */
	"Socket type not supported",		/* 44 - ESOCKTNOSUPPORT */
	"Operation not supported",		/* 45 - EOPNOTSUPP */
	"Protocol family not supported",	/* 46 - EPFNOSUPPORT */
						/* 47 - EAFNOSUPPORT */
	"Address family not supported by protocol family",
	"Address already in use",		/* 48 - EADDRINUSE */
	"Can't assign requested address",	/* 49 - EADDRNOTAVAIL */ ->就是上面例子中的提示

/* ipc/network software -- operational errors */
	"Network is down",			/* 50 - ENETDOWN */
	"Network is unreachable",		/* 51 - ENETUNREACH */
	"Network dropped connection on reset",	/* 52 - ENETRESET */
	"Software caused connection abort",	/* 53 - ECONNABORTED */
	"Connection reset by peer",		/* 54 - ECONNRESET */
	"No buffer space available",		/* 55 - ENOBUFS */
	"Socket is already connected",		/* 56 - EISCONN */
	"Socket is not connected",		/* 57 - ENOTCONN */
	"Can't send after socket shutdown",	/* 58 - ESHUTDOWN */
	"Too many references: can't splice",	/* 59 - ETOOMANYREFS */
	"Operation timed out",			/* 60 - ETIMEDOUT */
	"Connection refused",			/* 61 - ECONNREFUSED */

	"Too many levels of symbolic links",	/* 62 - ELOOP */
	"File name too long",			/* 63 - ENAMETOOLONG */

/* should be rearranged */
	"Host is down",				/* 64 - EHOSTDOWN */
	"No route to host",			/* 65 - EHOSTUNREACH */
	"Directory not empty",			/* 66 - ENOTEMPTY */

/* quotas & mush */
	"Too many processes",			/* 67 - EPROCLIM */
	"Too many users",			/* 68 - EUSERS */
	"Disc quota exceeded",			/* 69 - EDQUOT */

/* Network File System */
	"Stale NFS file handle",		/* 70 - ESTALE */
	"Too many levels of remote in path",	/* 71 - EREMOTE */
	"RPC struct is bad",			/* 72 - EBADRPC */
	"RPC version wrong",			/* 73 - ERPCMISMATCH */
	"RPC prog. not avail",			/* 74 - EPROGUNAVAIL */
	"Program version wrong",		/* 75 - EPROGMISMATCH */
	"Bad procedure for program",		/* 76 - EPROCUNAVAIL */

	"No locks available",			/* 77 - ENOLCK */
	"Function not implemented",		/* 78 - ENOSYS */
	"Inappropriate file type or format",	/* 79 - EFTYPE */
	"Authentication error",			/* 80 - EAUTH */
	"Need authenticator",			/* 81 - ENEEDAUTH */
	"Identifier removed",			/* 82 - EIDRM */
	"No message of desired type",		/* 83 - ENOMSG */
	"Value too large to be stored in data type", /* 84 - EOVERFLOW */
	"Operation canceled",			/* 85 - ECANCELED */
	"Illegal byte sequence",		/* 86 - EILSEQ */
	"Attribute not found",			/* 87 - ENOATTR */

/* General */
	"Programming error",			/* 88 - EDOOFUS */

	"Bad message",				/* 89 - EBADMSG */
	"Multihop attempted",			/* 90 - EMULTIHOP */
	"Link has been severed",		/* 91 - ENOLINK */
	"Protocol error",			/* 92 - EPROTO */
};
const int sys_nerr = sizeof(sys_errlist) / sizeof(sys_errlist[0]);

在linux系统中,它的定义在 include/asm-generic/errno.h中



你可能感兴趣的:(其他)