Saturday, May 22, 2010

Convert Multicast IPv4 Address to MAC address

There is a mapping between Multicast IP address and MAC address. The first three bytes of Multicast MAC address are always 01:00:5e, the last three bytes are filled from Multicast IP-form.
We drop the highest nine bits of IP address and convert to MAC address. And this number will become Multicast MAC address.

For example, IP: 224.10.10.10 , its decimal is 375875314. After we drop the highest nine bits, it becomes 657930. So, the Multicast MAC address is: 01:00:5e:0a:0a:0a. Here is a example C source code (GPL) to show this:

/*
Copyright (C) 2010 YKLin

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void cvtIP2MAC(unsigned long, unsigned char*);

int main(int argc, char **argv){
char ipaddress[255]="224.10.0.1";
unsigned char buf[6] = {0x01, 0x0, 0x5e, 0x0, 0x0, 0x0};
struct in_addr inputAddress;
int ret;
memset(&inputAddress, 0x0, sizeof(inputAddress));
if(argc > 1){
strcpy(ipaddress, argv[1]);
}

ret = inet_aton(ipaddress, &inputAddress);
if(!ret){
printf("Dest address(%s) is incorrect:%d\r\n", ipaddress, ret);
return -1;
}
printf("Conver IP(%s, %u) to MAC...\r\n", ipaddress, ntohl(inputAddress.s_addr));
MIPv42MAC(ntohl(inputAddress.s_addr), buf);
printf("MAC: %02x %02x %02x %02x %02x %02x\r\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
return 0;
}
void cvtIP2MAC(unsigned long ip, unsigned char *buf){
if(!buf){
return;
}
buf[5] = ip & 0xff;
buf[4] = (ip & 0xff00)>>8;
buf[3] = (ip & 0xff0000)>>16;

}

Tuesday, May 18, 2010

oldmenuconfig

When we build linux kernel, we have to configure via menuconfig, gconfig or xconfig first. “make menuconfig” generate some auto header files(include/linux/autoconf.h, include/config/auto.conf, include/config/autoconf.cmd … etc). In my developing environment, we have pre-configure file and copy this pre-configure to Linux kernel source directory. Unfortunate, we also have old auto header files and they are not updated. Once I compile kernel modules before I update auto header files, the outcome is incorrect. In this case, we issue “make oldconfig” once before we compile kernel modules.

FYI:
linuxtopia:Chapter 7. Upgrading a kernel: http://www.linuxtopia.org/online_books/linux_kernel/kernel_configuration/ch07s03.html