alexr_rwx: (coffee)
[personal profile] alexr_rwx
There are days when you're poking around in /proc/net, and you see "2A00000A" and immediately recognize that as "10.0.0.42"... but you can't seem to figure out how to determine your own machine's ip address in any clever way... this is one of those days.

Because ifconfig (and busybox, actually... oh my goodness, busybox is huge, and I've now looked at more of it than I would have liked to...) know how to do it, but they do it in disgustingly convoluted ways that would be unpleasant to pull out...

So this is a question for you, O my hackerly readers -- how do you figure out what all of your IP addresses are? It seems like the information would be hidden in /proc/net somewheres, but is there a syscall or something? ... Because, elrond is not just called 127.0.0.1, and if I was cool for this proxying thing, I'd recognize when he's referred to as 10.0.0.1 as well... *bangs fist on table* *opts to say "hot dammit!" for emphasis*... hot dammit!

My apologies to the readers for whom that made no sense a'tall.

Date: 2004-07-06 06:08 am (UTC)
From: [identity profile] falun.livejournal.com
*whimper*

wish i knew

Date: 2004-07-06 06:50 am (UTC)
From: [identity profile] schizobovine.livejournal.com
This problem falls under "there is no good solution". Reading from /proc is a Linux-specific thing, making it teh suck if you need to port it to say, Solaris. Reading from ifconfig also sucks. Even parsing from proc is no good, since you need to know the interface ahead of time -- and it's not always eth0...

Two reasonable options come to mind: make a connection to another server you design and figure out the IP address you are connecting from there. Automated, but still might pick the wrong IP if the machine is multi-homed.

Otherwise, make it a command line parameter. If the user screws up, it's a configuration error. =)

Date: 2004-07-06 07:18 am (UTC)
From: [identity profile] falun.livejournal.com
Otherwise, make it a command line parameter. If the user screws up, it's a configuration error. =)

you're a dirty dirty man :p

Date: 2004-07-06 07:30 am (UTC)
From: [identity profile] neuroticmonk.livejournal.com
You can determine the IP addresses associated with a the interfaces fairly easily. Determining your internet routable IP is a different thing, but just getting your IP is easy in C. Look in the Pocket Sockets Guide, as I think they have an example. If not, go pull the SNORT project from sorcefordge and look in SNORT.c. It's nicely commented and shows you how to find all the interfaces you have and get all their IPs.

Date: 2004-07-06 09:48 am (UTC)
From: [identity profile] neuroticmonk.livejournal.com
I should also point out that if you already have a socket connected, then the socket has a way of giving you back a struct inet_addr, or something like that, which tells you the IP of the local socket.

Date: 2004-07-06 10:28 am (UTC)
ext_110843: (my fandom writes your software)
From: [identity profile] oniugnip.livejournal.com
Yesh :) That would be getsockname(2) and getpeername(2) (for the remote IP) ... it seemed like the former would be the appropriate thing to do...

... but I prob'ly, I'm just going to do "localhost" and whatever gethostname() returns, like Richard suggested... portability and all that...

Ooh, quiz time.

Date: 2004-07-08 07:13 am (UTC)
From: [identity profile] ryanov.livejournal.com
Seems you've already decided that /proc/net is not the way to go (though honestly, seems to me it's a small enough block of code to get the address that you'd want to make it platform specific anyway -- after all, the command line args to external programs are not all the same on different platforms anyway), but you can determine via the routing table pretty easily which interface is the one connected to the gateway (/proc/net/route) and then you will know which interface to be concerned with. < shrug > HTH, and if not, I didn't spend much time on it. ;)

Profile

alexr_rwx: (Default)
Alex R

May 2022

S M T W T F S
1234 567
891011121314
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 27th, 2025 04:22 am
Powered by Dreamwidth Studios