Solved

Programmatically determine which eth0:x interfaces is requesting data

Posted on 2014-12-31
3
189 Views
Last Modified: 2014-12-31
Greetings,

I have edited the /etc/network/interfaces file to provide me with several eth0:x interfaces. Now I want to programmatically determine which interface or IP address a request is coming through.

For example; I have eth0:1 = 192.168.227.205 and eth0:2 = 192.168.227.206
From my laptop I can ping either address.
In linux I want to run a program that responds differently based on the IP address being hit. If .205 do this if .206 do that.

How can I programmatically determine which IP address is being hit?
0
Comment
Question by:RonWensley
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40526000
You can use 'getsockname()' (http://man7.org/linux/man-pages/man2/getsockname.2.html) for that purpose:

getsockname() returns the current address to which the socket sockfd is bound, in the buffer pointed to by addr.

For a code example, refer to http://www.ccplusplus.com/2011/09/getsockname-example-c.html
0
 

Author Closing Comment

by:RonWensley
ID: 40526024
Thank you for the quick response.  I looked up some example code for this and it looks like it will work perfectly.
0
 
LVL 4

Expert Comment

by:Tom Chadaravicius
ID: 40526169
There's getifaddrs function, defined in ifaddr.h. Calling it will retrieve an array of interfaces and IP addresses.
http://man7.org/linux/man-pages/man3/getifaddrs.3.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now