Results

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.


4 comments:

Ajay said...

I hope the code that you write for your company has better indenting. :-)

Deepesh Panicker said...

Dragonfly,
This looks good and useful. Just curious abt one thing: ifr_flags appears to be part of a union where u set the addr and netmask. So there could be chance that ifr_flags |= IFF_UP could keep other flags as set, and result in failure of the ioctl.
Also, user permissions could be a common issue while running the program. So prompting the errno string after the ioctls could be useful. Anyways, a setuid on the executable binary should be good enough, before running the script (leaving the security part).

Dragonfly said...

Ajay,
I guess you checked the blog while I was updating the blog, cos by the time I finished, there was your comment.

Deepesh,
Real thanks for the comments. I sure will check about the things you have suggested here. About the security, we were really not that much concerned about it. :)

Anonymous said...

Free ebay finally


Use http://www.BuySellDirect.net as a FREE home based ebay to make money at home. It is 100% free.


http://www.BuySellDirect.net is my FREE baby to make a second income at home.