public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: IP ADDRESS OF MACHINE
@ 2002-02-28  3:25 snodx
  2002-02-28  3:55 ` Edmund Green
  0 siblings, 1 reply; 23+ messages in thread
From: snodx @ 2002-02-28  3:25 UTC (permalink / raw)
  To: gcc-help

OK After reading Mr Rupert Wood's post I decided that enough is enough
and decided to test whether a NetWork Card is installed on my system or
not.

So I connected an Internet Router to my Linux computer. If there is a
NetWork Card installed on my computer then I should be able to access
the Internet.

And voila, there is a NetWork Card installed on my system.

I had already set the Computer's TCP/IP Address to 192.168.0.2. The
Internet router's address was set to 151.137.54.204

So by accessing the internet I was able to confirm that there is indeed a
NetWork Card on my computer.

But I decided to explore a bit further. I came across a command /bin/netstat

I ran this command /bin/netstat --route

This was the output that I got:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.0.2     *               255.255.255.255 UH        0 0          0 eth0
192.168.0.0     *               255.255.255.0   U         0 0          0 eth0
127.0.0.0       *               255.0.0.0       U         0 0          0 lo
default         151.137.54.204   0.0.0.0         UG        0 0          0 eth0

I am now slowly able to understand some of the Jargon that Mr Rupert wood was using.
He talked about interfaces and I understand now that there is a EtherNet Interface
eth0 installed on my system.

So now this is the final-most-precise-version of my question:

I have a EtherNet Interface eth0 installed on my system. The TCP/IP Address of
my computer has been set to 192.168.0.2.

I wish to develop a C/C++ program that can retrieve the local computer's TCP/IP Address.

I DONT WANT THE PROGRAM TO SEARCH IN THE /etc/hosts FILE WHICH IS I BELIEVE USED FOR
A DIFFERENT PURPOSE.

In short I am looking for a program like /bin/netstat --route only this program is to be
much much much simpler. It just needs to retrieve the TCP/IP Address 192.168.0.2 and display
it.

Thanx for everybody's inputs here.

Awaiting a final reply.
SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-28  3:25 IP ADDRESS OF MACHINE snodx
@ 2002-02-28  3:55 ` Edmund Green
  0 siblings, 0 replies; 23+ messages in thread
From: Edmund Green @ 2002-02-28  3:55 UTC (permalink / raw)
  To: snodx; +Cc: gcc-help


 > In short I am looking for a program like /bin/netstat --route only this program is to be
 > much much much simpler. It just needs to retrieve the TCP/IP Address 192.168.0.2 and display
 > it.

   Well unfortunately I don't think that you can do this so simply, because the machine itself doesn't have an IP 
address, it is the interfaces that have the IP addresses and you will be using different interfaces depending upon where 
you are connecting to (for example consider the common situation of a computer which is acting as a firewall / router 
between the internet and an home network, it will have one external IP address on the interface talking to the internet, 
a different IP address on the interface on the home network, and a third IP address (127.0.0.1) for talking to itself)

   If you open a socket connection to somewhere, _then_ you can ask what the IP address of the interface on your end of 
the socket was, but until you do that (or manually read through your routing tables and network card configurations) 
then you don't know which IP address you're interested in (for example in your case being surprised that you were given 
the localhost address)

   Alternativly going the get host by name route as a previous poster demonstrated will work, but you might have to try 
setting up your /etc/hosts file as:

127.0.0.1 localhost.localdomain localhost
192.168.0.2 mymachine.mydomain mymachine

   if you've also got your /etc/host.conf set up to use "hosts, bind", otherwise if you put the name of your machine on 
the 127.0.0.1 line then it will still (correctly) return the address 127.0.0.1, because that is the IP address you are 
interested in if you want to open a socket to yourself.

Edmund.

ps, is there a more appropriate newsgroup for this kind of question than gcc-help?


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: IP ADDRESS OF MACHINE
       [not found] <616BE6A276E3714788D2AC35C40CD18D53345D@whale.softwire.co.uk>
@ 2002-03-05  1:18 ` Rupert Wood
  0 siblings, 0 replies; 23+ messages in thread
From: Rupert Wood @ 2002-03-05  1:18 UTC (permalink / raw)
  To: 'Bharathi S'; +Cc: gcc-help

Bharathi S wrote:

> In my machine i am having 2 nic.
> But the ip.c giving the address of one nic(local) only !?
> 
> How to get the address of the two nic ?
> 1 nic for local net and 2nd for external net.

This will probably depend on the OS you're using.

On Solaris, for example, have a look at inet/ip_if.h - but there aren't
any man pages for those functions.

For Linux, try reading the source to ifconfig.

Rup.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-28  6:03   ` Nelson Guedes Paulo Junior
  2002-02-28  6:49     ` John Love-Jensen
@ 2002-03-05  1:07     ` Bharathi S
  1 sibling, 0 replies; 23+ messages in thread
From: Bharathi S @ 2002-03-05  1:07 UTC (permalink / raw)
  To: GNC GCC

Hello all,
	In my machine i am having 2 nic.
        But the ip.c giving the address of one nic(local) only !?

	How to get the address of the two nic ?
        1 nic for local net and 2nd for external net.

Thanks
-- 
  Bharathi S
  Whenever possible mail to bharathi@lantana.tenet.res.in

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-28 10:15       ` Nelson Guedes Paulo Junior
@ 2002-03-01  6:34         ` John Love-Jensen
  0 siblings, 0 replies; 23+ messages in thread
From: John Love-Jensen @ 2002-03-01  6:34 UTC (permalink / raw)
  To: Nelson Guedes Paulo Junior
  Cc: Andrea 'Fyre Wyzard' Bocci, snodx, gcc-help

Hi Nelson,

>If you put a cast to (unsigned char) in fornt of some (*entry)[0] you get
the correct results too, but why in [his] machine this works whitout any
patch?

I assume a "%hhu" is supposed to be a 8-bit unsigned, a C extension in some
(but not all) libraries.

>Is this a portable solution?

*heh*  Depends what you consider "portable".

C and C++ don't support the notion of canonical data types.  Such as "int32"
or "nat8".  (In FORTRAN, you can do INTEGER*1 or INTEGER*4 or somesuch to
specify explicitly the size.)

Besides, you never know if the canonical data type "int32" is supposed to be
big-endian (Blefuscu style) or little-endian (Lilliput style).  Or in some
cases, something even more exotic.  Sometimes call UNIX style (big-endian,
also known as Motorola style), XINU style (little-endian, also known as Intel
style) ... plus NUXI style and IXUN style.  Oh dread!

Oh how I wish I could say:
typedef (7 ... 86) range_t
range_t i;  // A variable that can span 7 through 86, inclusive.

But alas.  I digress.

I also wish I could do...
explicit typedef int xcoord_t;
explicit typedef int ycoord_t;

And then do things like this...
void Foo(xcoord_t x);
void Foo(ycoord_t y);
...and xcoord_t and ycoord_t wouldn't be mere aliases to int, but distinct
types.

But alas.  I digress again.

To answer you question:  nope, commonly, C/C++ programs are not portable.

Many programs assume IPL32, but many new hardware platforms are PL64/I32.
And older hardware might be L32/IP16, or L32/I16/P20(32).  The I stands for
integer size, the P for pointer size, the L for long integer size.

To write a C/C++ program that is portable, you have to take meticulous care.
You cannot cavalier cast things from one thing into another,  You cannot make
assumptions about byte-ordering.  Or type sizes.  Or if integers are
represented in 2's complement or 1's complement ... or even BCD (I suppose).
You cannot rely upon OS API facilities (no GDI calls in Windows, no Quartz
calls in Mac OS X, no X11 calls) ... unless you are committed to that
technology, and thus limiting portability.  If a routine calls for a
"size_t", use a "size_t", don't use an "int".  Oh, the list goes on and on.

The more assumptions you make, the less portable your code.

My code compiles (and sometimes runs...) on several platforms, so I get to
experience the joys of agnostic C++ coding every day.

If platform independence in important for you, I recommend Java, Ada, or
LISP.  Or if you're weird, Squeak.

Otherwise, if C or C++ is your poison of choice, strive to write code that is
platform agnostic.  For any OS API calls, create a Operating System
Abstraction Layer that insulates your program from platform dependencies.  It
is possible, albeit strife with landmines.  GNU suite of tools is a case in
point.  Angband is another.

Sincerely,
--Eljay


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-26  4:18 snodx
  2002-02-26  5:46 ` Stephano Mariani
@ 2002-02-28 14:02 ` Florian Weimer
  1 sibling, 0 replies; 23+ messages in thread
From: Florian Weimer @ 2002-02-28 14:02 UTC (permalink / raw)
  To: snodx; +Cc: gcc-help

snodx@hotmail.com writes:

> So I wish to obtain my standalone computer's IP Address through a
> C/C++ program. What function should I use for this?

The assumption that a machine has got a single IP address is wrong,
generally speaking.  Quite a few hosts with IP stacks do not even know
the IP address with which they are visible on the Internet.

You should examine your application and determine what "IP address"
really means in this context.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-28  6:49     ` John Love-Jensen
@ 2002-02-28 10:15       ` Nelson Guedes Paulo Junior
  2002-03-01  6:34         ` John Love-Jensen
  0 siblings, 1 reply; 23+ messages in thread
From: Nelson Guedes Paulo Junior @ 2002-02-28 10:15 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: Andrea 'Fyre Wyzard' Bocci, snodx, gcc-help


Hi Eljay,

Thanks for helping, but did you, or others now how the program could work
without this patch on fwyzard's machine like her described?

If you put a cast to (unsigned char) in fornt of some (*entry)[0] you get
the correct results too, but why in her's machine this works whitout any
patch? Is this a portable solution?

Thanks.



-----
Nelson Guedes Paulo Junior   
E-mail:  <npaulo@linux.ime.usp.br>   UIN: 2489382 (Tender [:alpha:]*)
"LAPTOPirose é a doença causada pela urina do mouse"

On Thu, 28 Feb 2002, John Love-Jensen wrote:

> Hi Nelson,
> 
> Change the one line to...
>     typedef unsigned char byte;
>      printf ("address:    %hhu.%hhu.%hhu.%hhu\n", \
>        (*entry)[0] & 0xFF,
>        (*entry)[1] & 0xFF,
>        (*entry)[2] & 0xFF,
>        (*entry)[3] & 0xFF);
> 
> What you are seeing is an octet being treated as signed, and the signed
> extension value being displayed as an unsigned int.
> 
> The mask will slice the sign extended high-bits off properly.
> 
> --Eljay
> 
> 
> 



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-28  6:03   ` Nelson Guedes Paulo Junior
@ 2002-02-28  6:49     ` John Love-Jensen
  2002-02-28 10:15       ` Nelson Guedes Paulo Junior
  2002-03-05  1:07     ` Bharathi S
  1 sibling, 1 reply; 23+ messages in thread
From: John Love-Jensen @ 2002-02-28  6:49 UTC (permalink / raw)
  To: Nelson Guedes Paulo Junior
  Cc: Andrea 'Fyre Wyzard' Bocci, snodx, gcc-help

Hi Nelson,

Change the one line to...
    typedef unsigned char byte;
     printf ("address:    %hhu.%hhu.%hhu.%hhu\n", \
       (*entry)[0] & 0xFF,
       (*entry)[1] & 0xFF,
       (*entry)[2] & 0xFF,
       (*entry)[3] & 0xFF);

What you are seeing is an octet being treated as signed, and the signed
extension value being displayed as an unsigned int.

The mask will slice the sign extended high-bits off properly.

--Eljay


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-27 19:32 ` Andrea 'Fyre Wyzard' Bocci
@ 2002-02-28  6:03   ` Nelson Guedes Paulo Junior
  2002-02-28  6:49     ` John Love-Jensen
  2002-03-05  1:07     ` Bharathi S
  0 siblings, 2 replies; 23+ messages in thread
From: Nelson Guedes Paulo Junior @ 2002-02-28  6:03 UTC (permalink / raw)
  To: Andrea 'Fyre Wyzard' Bocci; +Cc: snodx, gcc-help



Hi,

I,ve tryed the following program on my machine (Debian) and
got the following address line:

address:    4294967232.4294967208.4294967280.4294967243


GCC Version:

Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.2/specs
gcc version 2.95.2 20000220 (Debian GNU/Linux)


What's wrong?

Should I use some special options on GCC?

I've compiled with -Wall and no warnings have apeared.

Thanks.




-----
Nelson Guedes Paulo Junior   
E-mail:  <npaulo@linux.ime.usp.br>   UIN: 2489382 (Tender [:alpha:]*)
"LAPTOPirose é a doença causada pela urina do mouse"

On Thu, 28 Feb 2002, Andrea 'Fyre Wyzard' Bocci wrote:

> I have this one working on my Linux machine:
> 
> /* ip.c */
> #include <unistd.h>
> #include <netdb.h>
> 
> #define SIZE 256
> 
> char hostname[SIZE];
> char **alias;
> char **entry;
> struct hostent *host;
> 
> int main (int argc, char **argv) {
>    gethostname (hostname, SIZE);
>    host = gethostbyname (hostname);
> 
>    printf ("host name:  %s\n", host->h_name);
>    for (alias = host->h_aliases; *alias; alias++)
>      printf ("alias:      %s\n", *alias);
>    for (entry = host->h_addr_list; *entry; entry++)
>      printf ("address:    %hhu.%hhu.%hhu.%hhu\n", \
>        (*entry)[0], (*entry)[1] , (*entry)[2] , (*entry)[3]);
> 
>    return 0;
> }
> /* end of ip.c */
> 
> The output is
> 
> host name:  stufis06.fisica.unifi.it
> alias:      stufis06
> address:    150.217.141.25
> 
> HTH, fwyzard
> 
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: IP ADDRESS OF MACHINE
       [not found] <616BE6A276E3714788D2AC35C40CD18D47C245@whale.softwire.co.uk>
@ 2002-02-28  5:39 ` Rupert Wood
  0 siblings, 0 replies; 23+ messages in thread
From: Rupert Wood @ 2002-02-28  5:39 UTC (permalink / raw)
  To: snodx; +Cc: gcc-help

Snodx wrote:

> BUT the interface through which an internet browser like Netscape
> Navigator or Lynx interacts with the Internet HAS to be after all
> one.

No. It will ask the OS for a connection, and the OS will determine which
interface to use from its route table. If you access both an intranet
and an intranet link then NS/Lynx may well use two different local IPs
during one session.

> I am talking about THIS IP Address that the system is using to talk
> to the internet.

The interface which talks to the internet will have:

    * a gateway address configured (which is not also a local address;
      sometimes you'll see interfaces configured with gateway their
      own address or loopback)
    * an unrestrictive routing entry, usually destination address
      0.0.0.0, netmask 0.0.0.0.

I'm not sure if both are necessary (or even sufficient); certainly if
there's only one non-local gateway configured then you use that
interface but if there are multiple then you'll have to check the route
table. Again, I don't know which system calls you'd use to do this on
any platform or if they'd be portable.

There's also no guarantee that the IP on that interface will be exposed
to the internet at all, e.g. the gateway might be a local NAT/firewall.
A browser can even just be targeted at a local web proxy without  any
gateways configured at all.

Rup.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: IP ADDRESS OF MACHINE
  2002-02-28  4:04 snodx
@ 2002-02-28  4:30 ` Michal Lipták
  0 siblings, 0 replies; 23+ messages in thread
From: Michal Lipták @ 2002-02-28  4:30 UTC (permalink / raw)
  To: gcc-help

I would connect to some internet IP address and use getsockname() on the
connected socket..

int socket;
sockaddr local_addr;
socklen_t namelen=sizeof(local_addr);

getsockname(socket,&local_addr,&namelen);
printf("My IP is %s",inet_ntoa(local_addr.sin_addr));

m.

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> Behalf Of snodx@hotmail.com
> Sent: Thursday, February 28, 2002 1:02 PM
> To: gcc-help@gcc.gnu.org
> Subject: Re: IP ADDRESS OF MACHINE
>
>
> Alright Mr Green, that was a nice point. Your post has made me
> refine my question a bit more.
>
> Maybe there are several interfaces installed on a system. BUT the
> interface
> through which an internet browser like Netscape Navigator or Lynx
> interacts with
> the Internet HAS to be after all one. I am talking about THIS IP
> Address that the
> system is using to talk to the internet. How does the internet
> browser access the
> Internet? I want a C/C++ program to detect THIS interface and
> display IT'S IP Address.
>
> That leads to the second part of your question is there a better
> place where this
> question can be posted. I think the best place would be where
> network issues are
> discussed. I shall indeed do so. I posted this question here because
>
> So now how do I obtain the TCP/IP Address of that interface
> through which an internet
> browser interacts?
>
> Thanks for the input anyway.
>
> SNODX
>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-28  4:04 snodx
  2002-02-28  4:30 ` Michal Lipták
  0 siblings, 1 reply; 23+ messages in thread
From: snodx @ 2002-02-28  4:04 UTC (permalink / raw)
  To: gcc-help

Alright Mr Green, that was a nice point. Your post has made me
refine my question a bit more.

Maybe there are several interfaces installed on a system. BUT the interface
through which an internet browser like Netscape Navigator or Lynx interacts with
the Internet HAS to be after all one. I am talking about THIS IP Address that the
system is using to talk to the internet. How does the internet browser access the
Internet? I want a C/C++ program to detect THIS interface and display IT'S IP Address.

That leads to the second part of your question is there a better place where this
question can be posted. I think the best place would be where network issues are
discussed. I shall indeed do so. I posted this question here because 

So now how do I obtain the TCP/IP Address of that interface through which an internet
browser interacts?

Thanks for the input anyway.

SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-28  3:56 snodx
  0 siblings, 0 replies; 23+ messages in thread
From: snodx @ 2002-02-28  3:56 UTC (permalink / raw)
  To: gcc-help

Alright Mr Green, that was a nice point. Your post has made me
refine my question a bit more.

Maybe there are several interfaces installed on a system. BUT the interface
through which an internet browser like Netscape Navigator or Lynx interacts with
the Internet HAS to be after all one. I am talking about THIS IP Address that the
system is using to talk to the internet. How does the internet browser access the
Internet? I want a C/C++ program to detect THIS interface and display IT'S IP Address.

That leads to the second part of your question is there a better place where this
question can be posted. I think the best place would be where network issues are
discussed. I shall indeed do so. I posted this question here because 

So now how do I obtain the TCP/IP Address of that interface through which an internet
browser interacts?

Thanks for the input anyway.

SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: IP ADDRESS OF MACHINE
       [not found] <616BE6A276E3714788D2AC35C40CD18D47C203@whale.softwire.co.uk>
@ 2002-02-28  2:43 ` Rupert Wood
  0 siblings, 0 replies; 23+ messages in thread
From: Rupert Wood @ 2002-02-28  2:43 UTC (permalink / raw)
  To: snodx; +Cc: gcc-help

Snodx wrote:

> But 127.0.0.1 is set BY DEFAULT. Using the /bin/netconf 
> application I set my standalone computer's proper TCP/IP
> address which was 192.168.0.2.

Does such a thing exist? IP addresses are usually tied to interfaces; if
you have no network card then the only interface you're likely to have
is the loopback one. What could your OS usefully do with this IP? Where
would it store it and why?

Which begs the question what netconf is actually doing. I don't have
access to a linux system here to test. Do you know what it's actually
doing with this IP, which system calls it is using to configure it in
the kernel, etc?

(On a similar note, I'm not sure the gethostbyname(gethostname) solution
is that clean; yes, it should work, but it's relying on cooperation of
/etc/hosts and/or a DNS server. Better would be to obtain a list of
interfaces from the OS and then iterate through asking each for its
address(es). However, I don't know how to do this or if it could be done
portably.)

Rup.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-28  1:38 snodx
  0 siblings, 0 replies; 23+ messages in thread
From: snodx @ 2002-02-28  1:38 UTC (permalink / raw)
  To: gcc-help

Thanks Andrea 'Fyre Wyzard' Bocci for your response.

I ran the code that you gave. It DID give me the IP Address of the machine which was: 127.0.0.1

This means that my system has been set to localhost address.

This localhost address will be used by those application servers which will be installed and configured on my system
such as the Apache server.

But 127.0.0.1 is set BY DEFAULT. Using the /bin/netconf application I set my standalone computer's proper TCP/IP
address which was 192.168.0.2.

Using a C/C++ program I wanted to obtain THIS address (192.168.0.2) not 127.0.0.1

I understand that the confusion has arisen because I posted the wrong question.

I am rephrasing the question here:

I have a standalone computer and I dont know whether there is a NetWork Card installed and configured on this computer.

Using /bin/netconf I set the local TCP/IP address of this computer which is 192.168.0.2

Now using a C/C++ program I wish to obtain the TCP/IP address of the machine. How do I go about this.

SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-27  9:17 snodx
@ 2002-02-27 19:32 ` Andrea 'Fyre Wyzard' Bocci
  2002-02-28  6:03   ` Nelson Guedes Paulo Junior
  0 siblings, 1 reply; 23+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2002-02-27 19:32 UTC (permalink / raw)
  To: snodx, gcc-help

I have this one working on my Linux machine:

/* ip.c */
#include <unistd.h>
#include <netdb.h>

#define SIZE 256

char hostname[SIZE];
char **alias;
char **entry;
struct hostent *host;

int main (int argc, char **argv) {
   gethostname (hostname, SIZE);
   host = gethostbyname (hostname);

   printf ("host name:  %s\n", host->h_name);
   for (alias = host->h_aliases; *alias; alias++)
     printf ("alias:      %s\n", *alias);
   for (entry = host->h_addr_list; *entry; entry++)
     printf ("address:    %hhu.%hhu.%hhu.%hhu\n", \
       (*entry)[0], (*entry)[1] , (*entry)[2] , (*entry)[3]);

   return 0;
}
/* end of ip.c */

The output is

host name:  stufis06.fisica.unifi.it
alias:      stufis06
address:    150.217.141.25

HTH, fwyzard

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-27  9:17 snodx
  2002-02-27 19:32 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 1 reply; 23+ messages in thread
From: snodx @ 2002-02-27  9:17 UTC (permalink / raw)
  To: gcc-help

Greetings,
           Thanks to Stephano Mariani and Robert Lopez for their inputs.

            Robert Lopez wrote:
             >The GNU C Library Reference Manual might be useful to you. See the
sections on Host Addresses and  Host Names in the Sockets section.

            Where can I get the GNU C Library Reference Manual?

            Stephano Mariani wrote:

            >See `man gethostbyname`

            I gather that you suggested that I use this function which exists in the netdb.h header file

            I wrote the following C program:

            #include<unistd.h>
            #include<netdb.h>

            main()
            {
              int snodsize=12;
              char snodarr[100],arrsnod[100],*sndptr,*snodptr,**snodptrptr;
              struct hostent *hstptr;

              sndptr=snodarr;
              snodptr=arrsnod;
              snodptrptr=&snodptr;

                gethostname(sndptr,(size_t)snodsize);

              hstptr=gethostbyname(sndptr);
              snodptrptr=hstptr->h_addr_list;

                printf("%s",*snodptrptr);
            }
___________________________________________________________________________________________________

        The program is simple to understand:

        It declares a pointer to a character ARRAY called sndptr

        It declares another pointer to another character array snodptr

        It declares a pointer to a character pointer called snodptrptr

        It retrieves the hostname of the current system using gethostname of unistd.h

        It retrieves a pointer to a hostent structure using gethostbyname of netdb.h

        Next I equate the pointer-to-character-pointer variable snodptrptr
        to the h_addr_list field of the hostent structure. This is because
        h_addr_list is the most likely field where the IP Address might be
        stored and it is a pointer-to-pointer which cannot be accessed directly.
        Hence snodptrptr

        I display snodptrptr.

        But I am not getting the IP Address as I wanted it.
__________________________________________________________________________________________________

         What am I doing wrong? Are there any modifications in the code?
         SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-27  5:13 snodx
  0 siblings, 0 replies; 23+ messages in thread
From: snodx @ 2002-02-27  5:13 UTC (permalink / raw)
  To: gcc-help

Greetings,
           Thanks to Stephano Mariani and Robert Lopez for their inputs.

            Robert Lopez wrote:
             >The GNU C Library Reference Manual might be useful to you. See the
sections on Host Addresses and  Host Names in the Sockets section.

            Where can I get the GNU C Library Reference Manual?

            Stephano Mariani wrote:

            >See `man gethostbyname`

            I gather that you suggested that I use this function which exists in the netdb.h header file

            I wrote the following C program:

            #include<unistd.h>
            #include<netdb.h>

            main()
            {
              int snodsize=12;
              char snodarr[100],arrsnod[100],*sndptr,*snodptr,**snodptrptr;
              struct hostent *hstptr;

              sndptr=snodarr;
              snodptr=arrsnod;
              snodptrptr=&snodptr;

                gethostname(sndptr,(size_t)snodsize);

              hstptr=gethostbyname(sndptr);
              snodptrptr=hstptr->h_addr_list;

                printf("%s",*snodptrptr);
            }
___________________________________________________________________________________________________

        The program is simple to understand:

        It declares a pointer to a character ARRAY called sndptr

        It declares another pointer to another character array snodptr

        It declares a pointer to a character pointer called snodptrptr

        It retrieves the hostname of the current system using gethostname of unistd.h

        It retrieves a pointer to a hostent structure using gethostbyname of netdb.h

        Next I equate the pointer-to-character-pointer variable snodptrptr
        to the h_addr_list field of the hostent structure. This is because
        h_addr_list is the most likely field where the IP Address might be
        stored and it is a pointer-to-pointer which cannot be accessed directly.
        Hence snodptrptr

        I display snodptrptr.

        But I am not getting the IP Address as I wanted it.
__________________________________________________________________________________________________

         What am I doing wrong? Are there any modifications in the code?
         SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-26 18:29 Robert Lopez
  0 siblings, 0 replies; 23+ messages in thread
From: Robert Lopez @ 2002-02-26 18:29 UTC (permalink / raw)
  To: snodx, gcc-help


> So I wish to obtain my standalone computer's IP Address through a
> C/C++ program. What function should I use for this?

The GNU C Library Reference Manual might be useful to you. See the
sections on Host Addresses and  Host Names in the Sockets section.

Robert.Lopez@Philips.com

^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: IP ADDRESS OF MACHINE
  2002-02-26  4:18 snodx
@ 2002-02-26  5:46 ` Stephano Mariani
  2002-02-28 14:02 ` Florian Weimer
  1 sibling, 0 replies; 23+ messages in thread
From: Stephano Mariani @ 2002-02-26  5:46 UTC (permalink / raw)
  To: snodx, gcc-help

See `man gethostbyname`

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]
On Behalf Of snodx@hotmail.com
> Sent: Tuesday, 26 February 2002 11 16
> To: gcc-help@gcc.gnu.org
> Subject: Re: IP ADDRESS OF MACHINE
> 
> I am working on a standalone computer. This is why I said that I dont
> know whether there is a NetWork card installed and configured. I just
set
> the IP Address using the /bin/netconf utility
> 
> So I wish to obtain my standalone computer's IP Address through a
> C/C++ program. What function should I use for this?
> 
> SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
@ 2002-02-26  4:18 snodx
  2002-02-26  5:46 ` Stephano Mariani
  2002-02-28 14:02 ` Florian Weimer
  0 siblings, 2 replies; 23+ messages in thread
From: snodx @ 2002-02-26  4:18 UTC (permalink / raw)
  To: gcc-help

I am working on a standalone computer. This is why I said that I dont
know whether there is a NetWork card installed and configured. I just set
the IP Address using the /bin/netconf utility

So I wish to obtain my standalone computer's IP Address through a
C/C++ program. What function should I use for this?

SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: IP ADDRESS OF MACHINE
  2002-02-26  0:35 snodx
@ 2002-02-26  1:29 ` Frank Schafer
  0 siblings, 0 replies; 23+ messages in thread
From: Frank Schafer @ 2002-02-26  1:29 UTC (permalink / raw)
  To: snodx; +Cc: gcc-help

On Tue, 2002-02-26 at 09:37, snodx@hotmail.com wrote:
> Gretings,
>          I was exploring the header files that come alongwith the GNU Gcc package when I came across the function gethostname in unistd.h
> 
>          When I called the function I got the hostname of the machine on which I was running the program.
> 
>          Is there a similar function which can return the IP Address of the machine? I tried gethostid but it is returning a number in long int format.

... of WHICH machine?

>          ... I dont know if I have a network card installed ....

... !!!!!!!!!!??????????!!??????????!!!!???????

>          SNODX
> 

Regards
Frank

^ permalink raw reply	[flat|nested] 23+ messages in thread

* IP ADDRESS OF MACHINE
@ 2002-02-26  0:35 snodx
  2002-02-26  1:29 ` Frank Schafer
  0 siblings, 1 reply; 23+ messages in thread
From: snodx @ 2002-02-26  0:35 UTC (permalink / raw)
  To: gcc-help

Gretings,
         I was exploring the header files that come alongwith the GNU Gcc package when I came across the function gethostname in unistd.h

         When I called the function I got the hostname of the machine on which I was running the program.

         Is there a similar function which can return the IP Address of the machine? I tried gethostid but it is returning a number in long int format.

         I may mention that I have RedHat Linux 6.0 with gcc version 2.91 installed. I dont know if I have a network card installed but I set the IP Address using the /bin/netconf utility.

         SNODX

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2002-03-05  9:18 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-28  3:25 IP ADDRESS OF MACHINE snodx
2002-02-28  3:55 ` Edmund Green
     [not found] <616BE6A276E3714788D2AC35C40CD18D53345D@whale.softwire.co.uk>
2002-03-05  1:18 ` Rupert Wood
     [not found] <616BE6A276E3714788D2AC35C40CD18D47C245@whale.softwire.co.uk>
2002-02-28  5:39 ` Rupert Wood
  -- strict thread matches above, loose matches on Subject: below --
2002-02-28  4:04 snodx
2002-02-28  4:30 ` Michal Lipták
2002-02-28  3:56 snodx
     [not found] <616BE6A276E3714788D2AC35C40CD18D47C203@whale.softwire.co.uk>
2002-02-28  2:43 ` Rupert Wood
2002-02-28  1:38 snodx
2002-02-27  9:17 snodx
2002-02-27 19:32 ` Andrea 'Fyre Wyzard' Bocci
2002-02-28  6:03   ` Nelson Guedes Paulo Junior
2002-02-28  6:49     ` John Love-Jensen
2002-02-28 10:15       ` Nelson Guedes Paulo Junior
2002-03-01  6:34         ` John Love-Jensen
2002-03-05  1:07     ` Bharathi S
2002-02-27  5:13 snodx
2002-02-26 18:29 Robert Lopez
2002-02-26  4:18 snodx
2002-02-26  5:46 ` Stephano Mariani
2002-02-28 14:02 ` Florian Weimer
2002-02-26  0:35 snodx
2002-02-26  1:29 ` Frank Schafer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).