Solved

Programmatically determine which eth0:x interfaces is requesting data

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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…

707 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