Aliasing an Ethernet Interface in Linux

For the last couple of weeks, I was busy with my work. I’ve had a really good time and I thought I will share something I learned during that time here.
My work was related to some thing of the client-server model. For testing, we were simulating multiple clients and servers in the same PC. First we were using the same IP address and different ports. Later, before moving to the real world scenario, we wanted to test with different IP address. Since the number of clients was huge, we couldn’t run the tests in single PCs. Thats then we decided to bring up multiple interfaces in a single PC and test it. This helped in testing one more level deeper before moving to the actual hardware. [Even then, we had to use different TCP/UDP ports since there is only one TCP/UDP stack in the system]

For this purpose, I was doing some research on how to alias and bring up multiple Ethernet interfaces in a Linux system. I couldn’t easily find any help on how to bring up aliased interfaces using C [I found many references about aliasing using Ifconfig command]. So, I wrote a generic application which can bring up multiple aliased interfaces. Here is the code. I believe I have made it as simple as I can and you can modify and try experiments on your own. To start with, you can try changing different members in the ifreq structure.

When I showed this to a colleague of mine - who happens to be a scripting guru - he wrote a small script which can be used to bring up multiple interfaces. This may also be useful I guess. Anyway, you are free to take this and do experiments on your own.

For more info about the structures used in the program, here are the links.
struct sockaddr_in
struct ifreq
Please share your comments and thoughts so that I can improve the ways I do coding. Special thanks to Bala who wrote the script and Adil, who provided me with the code to start off with.

ps: I am really sorry that I have put the code as an image, I don't know why, but I was not able to put the code as it is, it was giving me some problems.