[vox-tech] getting IP address in C

Ryan vox-tech@lists.lugod.org
Wed, 16 Apr 2003 09:18:41 -0700


actually I found that what best suits my needs, and is much easier, is to just
to run a wildcard IP using INADDR_ANY


----- Original Message -----
From: "Michael Wenk" <wenk@praxis.homedns.org>
To: <vox-tech@lists.lugod.org>
Sent: Wednesday, April 16, 2003 8:34 AM
Subject: Re: [vox-tech] getting IP address in C


> On Wednesday 16 April 2003 05:59 am, Ryan wrote:
> > My machine has two IPs I guess. 127.0.0.1 and 64.167.231.194.
> > I want to be able to detect the 64.167.231.194.
> >
> > Here is what I ended up doing to solve the problem
> >
> >         /*detecting hostname and ip*/
> >         printf("Getting hostname....\n");
> >         gethostname(hostname, 256);
> >         printf("Found %s... sound good to you?\n", hostname);
> >
> >         printf("Getting IP Address now...\n");
> >         h_entry = gethostbyname(hostname);
> >         if(h_entry == 0) crash("gethostbyname()");
> >
> >         printf("Aha! found %s\n", inet_ntoa(*((struct in_addr
> > *)h_entry->h_addr)));
> >         inet_aton(h_entry->h_addr, &adr_server.sin_addr);
> >
> >         len_inet = sizeof(adr_server);
> >         /*************************/
> >
>
>
>
> The biggest problem is when your system has multiple adresses(And I don't
> include the loopback address.)  You see this when you have systems with
> virtual hosts, or multi homed systems(My own system is an example of this.)
>
> For example my local system:
> eth0      Link encap:Ethernet  HWaddr 00:04:5A:87:08:07
>           inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0
>           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
>           RX packets:728145 errors:0 dropped:0 overruns:0 frame:0
>           TX packets:662645 errors:0 dropped:0 overruns:0 carrier:0
>           collisions:0 txqueuelen:100
>           RX bytes:118078376 (112.6 Mb)  TX bytes:454448771 (433.3 Mb)
>           Interrupt:11 Base address:0xe000
>
> eth1      Link encap:Ethernet  HWaddr 00:A0:CC:29:F0:D3
>           inet addr:12.209.95.93  Bcast:255.255.255.255  Mask:255.255.254.0
>           UP BROADCAST NOTRAILERS RUNNING MULTICAST  MTU:1500  Metric:1
>           RX packets:3237857 errors:1 dropped:0 overruns:0 frame:3
>           TX packets:636079 errors:3 dropped:0 overruns:3 carrier:0
>           collisions:0 txqueuelen:100
>           RX bytes:622292879 (593.4 Mb)  TX bytes:88681685 (84.5 Mb)
>           Interrupt:11 Base address:0xe400
>
> lo        Link encap:Local Loopback
>           inet addr:127.0.0.1  Mask:255.0.0.0
>           UP LOOPBACK RUNNING  MTU:16436  Metric:1
>           RX packets:37759 errors:0 dropped:0 overruns:0 frame:0
>           TX packets:37759 errors:0 dropped:0 overruns:0 carrier:0
>           collisions:0 txqueuelen:0
>           RX bytes:4609422 (4.3 Mb)  TX bytes:4609422 (4.3 Mb)
>
> There are three adresses, and the name of the system doesn't directly map to
> any of the systems addresses.  Since the last time I had to do anything even
> near this, I was programming a tool on HP-UX which implements an extension to
> the standard library, I must admit I am clueless.  So, I went and checked the
> source.  Apparently, ifconfig reads /proc/net/dev for a list of
> interfaces(plus some stats), and then goes thru and runs some ioctls(and
> possibly other stuff) to determine what the address is on them.  Im not sure
> if this is what you were looking for or not.  If you're ok with your program
> as is, then great.  Just remember that depending on the system you are on may
> change, and if it does and is more convoluted in configuration, your program
> may be invalid.  Its a PITA, but hey, its life.
>
>
>
> Mike
> --
> wenk@praxis.homedns.org
> Mike Wenk
>
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
>