Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Programmatically determine which eth0:x interfaces is requesting data

Posted on 2014-12-31
3
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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