Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-03

getting my local ip

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ron wilson

im having difficulty getting my ip from within a winsock program. this is mainly due to my inability to understand this:

    type
       hostent = record
          { official name of host  }
          h_name: pchar;
          { alias list  }
          h_aliases: ^pchar;
          { host address type  }
          h_addrtype: SmallInt;
          { length of address  }
          h_length: SmallInt;
          { list of addresses  }
          case byte of
             0: (h_addr_list: ^pchar);
             1: (h_addr: ^pchar)
       end;

how do i understand the case statement? what does 'case byte of' refer to? in all the c examples i have read, they indicate that h_addr_list should contain an array of addresses for the host, i.e. me. however, the length of this list is always 4 bytes, which is only one address, and it's always the same as h_addr, both pointing to the loopback address. the c examples all use h_addr_list as an array. why isn't this an array here? im seriously confused.

can anybody help me out? i need to know my modem's ip address so that i can instruct other people to connect to that address. its kind of a pain to always have to run ipconfig. my users won't like that, and i shouldn't have to teach them to do that if i can just divine the address within my program.

regards,
ron wilson

Michael Van Canneyt
to ron wilson
ron wilson wrote 26 lines
im having difficulty getting my ip from within a winsock program. this is mainly due to my inability to understand this: type hostent = record { official name of host } h_name: pchar; { alias list } h_aliases: ^pchar; { host address type } h_addrtype: SmallInt; { length of address } h_length: SmallInt; { list of addresses } case byte of 0: (h_addr_list: ^pchar); 1: (h_addr: ^pchar) end; how do i understand the case statement? what does 'case byte of' refer to? in all the c examples i have read, they indicate that h_addr_list should contain an array of addresses for the host, i.e. me. however, the length of this list is always 4 bytes, which is only one address, and it's always the same as h_addr, both pointing to the loopback address. the c examples all use h_addr_list as an array. why isn't this an array here? im seriously confused.

It is not an array here because it is not a real array; It's just a pointer to a null-terminated list of names.

The h_addr_list and h_addr pointers can be used interchangably. Probably a name change occurred, and for compatibility reasons both names have been retained.

Michael.

ron wilson
to Michael Van Canneyt

ok - thats what i thought - so they both point to the same memory area. my question is this then: how do i access the list. i guess my pascal syntax is too far gone. i understand pointers pretty well, but in this case, i'm lost. what we have here is a pointer to pchar - ^pchar. so to access the first pchar in the list i would code this:

program test;
var
  pc: pchar;
  h: hostent;
begin
  /* fill h here */
  pc:=stralloc(sizeof(h.h_addr_list^));
  strpcopy(pc,h.h_addr_list^);
  writeln(pc);
end.

how do i access the second pchar? the third? from high school (been a while) i seem to recall adding 1 to the pointer? h.h_addr_list^+1? or maybe inc(h.h_addr_list^)? in my program, the first pchar in the list for the host is almost always 127.0.0.1 - which is a no-brainer. what i need is the second or third address in the list, i.e. my modem ip.

the reverend
(: the goodguys :)
icq 83250263
http://thereverend.coastgames.com/tradewars/

Earlier message 53 lines
-----Original Message----- From: Behalf Of Michael Van Canneyt Sent: Friday, March 15, 2002 2:36 AM To: Subject: Re: [fpc-pascal]getting my local ip On Thu, 14 Mar 2002, ron wilson wrote: > im having difficulty getting my ip from within a winsock program. this is > mainly due to my inability to understand this: > > type > hostent = record > { official name of host } > h_name: pchar; > { alias list } > h_aliases: ^pchar; > { host address type } > h_addrtype: SmallInt; > { length of address } > h_length: SmallInt; > { list of addresses } > case byte of > 0: (h_addr_list: ^pchar); > 1: (h_addr: ^pchar) > end; > > how do i understand the case statement? what does 'case byte of' refer to? > in all the c examples i have read, they indicate that h_addr_list should > contain an array of addresses for the host, i.e. me. however, the length of > this list is always 4 bytes, which is only one address, and it's always the > same as h_addr, both pointing to the loopback address. the c examples all > use h_addr_list as an array. why isn't this an array here? im seriously > confused. It is not an array here because it is not a real array; It's just a pointer to a null-terminated list of names. The h_addr_list and h_addr pointers can be used interchangably. Probably a name change occurred, and for compatibility reasons both names have been retained. Michael.
Michael Van Canneyt
to ron wilson
ron wilson wrote 16 lines
ok - thats what i thought - so they both point to the same memory area. my question is this then: how do i access the list. i guess my pascal syntax is too far gone. i understand pointers pretty well, but in this case, i'm lost. what we have here is a pointer to pchar - ^pchar. so to access the first pchar in the list i would code this: program test; var pc: pchar; h: hostent; begin /* fill h here */ pc:=stralloc(sizeof(h.h_addr_list^)); strpcopy(pc,h.h_addr_list^); writeln(pc); end.

No, no. You can use the fact that a pointer can be considered as a pointer to an array.

I:=0;
While h.h_addr_list[i]Nil do
  begin
  PC:=h_addr_list[i];  // Get pchar pointer
  S:=StrPas(pc);       // Convert to pascal string.
  // Do something with S.
  inc(i)
  end;

Have a look at the inet unit, the GetAlias method. It does exactly the same for Linux.

Michael.

ron wilson
to Michael Van Canneyt

thanks - i tried this once before and i got this compiler error:

_revproxy.pp(37,29) Error: Array type required
_revproxy.pp(344) Fatal: There were 1 errors compiling module, stopping

here is my code. (its under construction, but this at least should compile.

21 function getlocalip: ansistring;
22 var
23   pc: pchar;
24   ac: array[0..255] of char;
25   phe: phostent;
26   i: integer;
27 //  addr: in_addr;
28   s: ansistring;
29 begin
30   if (gethostname(ac,sizeof(ac))=SOCKET_ERROR) then halt;
31   writeln('host name is: ',ac);
32   phe:=gethostbyname(ac);
33   if phe=nil then halt;
34 //  writeln(sizeof(phe^.h_addr_list^));
35
36   i:=0;
37   while not(phe^.h_addr_list[i]=nil) do
38   begin
39     pc:=phe^.h_addr_list[i];
40     s:=strpas(pc);
41     writeln('address ',i,': ',s);
42     inc(i);
43   end;
44   getlocalip:=s;
45 end;

i just assumed i was doing something wrong with my syntax when i got that error. any suggestions?

ron wilson

Earlier message 50 lines
-----Original Message----- From: Behalf Of Michael Van Canneyt Sent: Friday, March 15, 2002 9:12 AM To: Subject: RE: [fpc-pascal]getting my local ip On Fri, 15 Mar 2002, ron wilson wrote: > ok - thats what i thought - so they both point to the same memory area. my > question is this then: how do i access the list. i guess my pascal syntax > is too far gone. i understand pointers pretty well, but in this case, i'm > lost. what we have here is a pointer to pchar - ^pchar. so to access the > first pchar in the list i would code this: > > program test; > var > pc: pchar; > h: hostent; > begin > /* fill h here */ > pc:=stralloc(sizeof(h.h_addr_list^)); > strpcopy(pc,h.h_addr_list^); > writeln(pc); > end. No, no. You can use the fact that a pointer can be considered as a pointer to an array. I:=0; While h.h_addr_list[i]Nil do begin PC:=h_addr_list[i]; // Get pchar pointer S:=StrPas(pc); // Convert to pascal string. // Do something with S. inc(i) end; Have a look at the inet unit, the GetAlias method. It does exactly the same for Linux. Michael.
ron wilson
to ron wilson

i think i found the problem... i had delphi compatibility on for the compiler - turned it off and now it compiles... now i'll see if it gives useful information.

the reverend
(: the goodguys :)
icq 83250263
http://thereverend.coastgames.com/tradewars/

Earlier message 96 lines
-----Original Message----- From: Behalf Of ron wilson Sent: Friday, March 15, 2002 10:08 AM To: Subject: RE: [fpc-pascal]getting my local ip thanks - i tried this once before and i got this compiler error: _revproxy.pp(37,29) Error: Array type required _revproxy.pp(344) Fatal: There were 1 errors compiling module, stopping here is my code. (its under construction, but this at least should compile. 21 function getlocalip: ansistring; 22 var 23 pc: pchar; 24 ac: array[0..255] of char; 25 phe: phostent; 26 i: integer; 27 // addr: in_addr; 28 s: ansistring; 29 begin 30 if (gethostname(ac,sizeof(ac))=SOCKET_ERROR) then halt; 31 writeln('host name is: ',ac); 32 phe:=gethostbyname(ac); 33 if phe=nil then halt; 34 // writeln(sizeof(phe^.h_addr_list^)); 35 36 i:=0; 37 while not(phe^.h_addr_list[i]=nil) do 38 begin 39 pc:=phe^.h_addr_list[i]; 40 s:=strpas(pc); 41 writeln('address ',i,': ',s); 42 inc(i); 43 end; 44 getlocalip:=s; 45 end; i just assumed i was doing something wrong with my syntax when i got that error. any suggestions? ron wilson -----Original Message----- From: Behalf Of Michael Van Canneyt Sent: Friday, March 15, 2002 9:12 AM To: Subject: RE: [fpc-pascal]getting my local ip On Fri, 15 Mar 2002, ron wilson wrote: > ok - thats what i thought - so they both point to the same memory area. my > question is this then: how do i access the list. i guess my pascal syntax > is too far gone. i understand pointers pretty well, but in this case, i'm > lost. what we have here is a pointer to pchar - ^pchar. so to access the > first pchar in the list i would code this: > > program test; > var > pc: pchar; > h: hostent; > begin > /* fill h here */ > pc:=stralloc(sizeof(h.h_addr_list^)); > strpcopy(pc,h.h_addr_list^); > writeln(pc); > end. No, no. You can use the fact that a pointer can be considered as a pointer to an array. I:=0; While h.h_addr_list[i]Nil do begin PC:=h_addr_list[i]; // Get pchar pointer S:=StrPas(pc); // Convert to pascal string. // Do something with S. inc(i) end; Have a look at the inet unit, the GetAlias method. It does exactly the same for Linux. Michael.