Solved

Programmatically determine which eth0:x interfaces is requesting data

Posted on 2014-12-31
3
197 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automating a script for user accounts LINUX 14 67
Shell script to extract rar files in severa directories 7 74
Linux hostname change 2 52
mcrypt_create_iv() is deprecated 4 67
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

919 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

20 Experts available now in Live!

Get 1:1 Help Now