Solved

Programmatically determine which eth0:x interfaces is requesting data

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux 13 105
FTP'ing Huge files ... How to ? FTP in segments -or- pick up ftp where you left off 5 77
Samba 4, Users Permission, 5 64
Finding Divisors 5 38
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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