C Program For Generating Incremental MAC And IP Addresses

The other day I had this requirement to generate 4K incremental MAC and IP addresses and use them for some configuration. Typing 4k addresses by hand was never an option. So I spent some time to write a small C program that did the job for me for generating 4K mac and ip addresses starting from a given initial value. The idea was pretty simple

  • The program should be able to print out the addresses which I can copy paste to create eg a command file.
  • The program should take the starting mac and ip address from where the 4K incremental range would be generated.
  • The generated values should be same as what a traffic generator eg Ixia generates for incremental mac/IP addresses

water droplet on the screen
Creative Commons License photo credit: rick

From code perspective, I just took two arrays corresponding to size of MAC and IP address ( 6 and 4 bytes respectively ). Two indexes are maintained -- one is called increment index which is incremented each time to get the next mac or ip address. There is also a parent index which is incremented once the increment index reaches maximum value in a byte i.e 255/0xFF. The increment index represents the LSB and the parent index represents the MSB adjacent to LSB. The starting MAC and IP value was chosen as 00:00:00:00:00:01 and respectively.

#include <stdio.h>
#define MAC_ADDR_BYTES 6
#define IP_ADDR_BYTES  4
#define MAX_U8_VAL         0xFF

void incr_ip_mac_addr(unsigned char *pAddr, unsigned int *i, unsigned int *p, unsigned int max_val)
 unsigned int incr_index, parent_index ;
 incr_index = *i ;
 parent_index = *p ;
 if ( pAddr[incr_index] == max_val ){
 if ( pAddr[parent_index] == max_val) {
 pAddr[parent_index] = 0 ;
 pAddr[parent_index] +=1 ;
 pAddr[incr_index] = 0 ;
 pAddr[incr_index]++ ;

 *i = incr_index ;
 *p = parent_index ;
int main(void)
 unsigned int i;
 unsigned int mac_incr_idx = MAC_ADDR_BYTES - 1, mac_parent_idx = MAC_ADDR_BYTES - 2 ;
 unsigned int ip_incr_idx = IP_ADDR_BYTES - 1, ip_parent_idx = IP_ADDR_BYTES - 2 ;
 unsigned char mac_addr[MAC_ADDR_BYTES], ip_addr[IP_ADDR_BYTES] ;

 unsigned char init_mac_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } ;
 unsigned char init_ip_addr[] = { 100, 1, 1, 2 } ;

 memcpy( &mac_addr[0], &init_mac_addr, sizeof(unsigned char)*MAC_ADDR_BYTES) ;
 memcpy( &ip_addr[0], &init_ip_addr, sizeof(unsigned char)*IP_ADDR_BYTES) ;
 for(i=0; i<4096; i++)
 printf("\n %d.%d.%d.%d", ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
 printf(" %02x:%02x:%02x:%02x:%02x:%02x ", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
 incr_ip_mac_addr( ip_addr, &ip_incr_idx, &ip_parent_idx, MAX_U8_VAL) ;
 incr_ip_mac_addr( mac_addr, &mac_incr_idx, &mac_parent_idx, MAX_U8_VAL) ;

Following is partial snapshot of the output of this program. 00:00:00:00:00:f5 00:00:00:00:00:f6 00:00:00:00:00:f7 00:00:00:00:00:f8 00:00:00:00:00:f9 00:00:00:00:00:fa 00:00:00:00:00:fb 00:00:00:00:00:fc 00:00:00:00:00:fd 00:00:00:00:00:fe 00:00:00:00:00:ff 00:00:00:00:01:00 00:00:00:00:01:01 00:00:00:00:01:02 00:00:00:00:01:03 00:00:00:00:01:04 00:00:00:00:01:05 00:00:00:00:01:06 00:00:00:00:01:07 00:00:00:00:01:08 00:00:00:00:01:09 00:00:00:00:01:0a 00:00:00:00:01:0b 00:00:00:00:01:0c 00:00:00:00:01:0d 00:00:00:00:01:0e 00:00:00:00:01:0f 00:00:00:00:01:10 00:00:00:00:01:11

Don’t have a linux terminal handy? No problem, head over to codepad and copy-paste the above code. You can compile and run it online, copy paste the output and use it the way you want. Note that the above program may not be perfect. It is not checking all boundary conditions and all input combinations. But important thing is that you should be able to tweak it pretty quickly and align with your requirement.

I hope you find this piece of code useful. If you liked the post, do share it with your friends. You can always submit useful tools, web services and tips to us.

Inficone runs on the Genesis Framework

Genesis Framework
Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!
Follow On Twitter
Follow On Facebook
Follow Using RSS
Follow Using Email
Tweet This Post
Share Post On Facebook
Digg This Post
Stumble Upon

Speak Your Mind


This blog is kept spam free by WP-SpamFree.