March 29, 2024, 12:20:35 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Pointer problem

Started by Egil, February 24, 2011, 11:18:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

I have been playing around with UDP functions lately, copying and adjusting IWB code into CB code. With a lot of help from sapero, the receive routines now works fine. But I feel that the collection  needs two more functions to be really useful. When trying to understand the two foutines already working in IWB code, I ran into problems. I don't understand how the multiple indirection pointers work, and how/if they can be converted to CB code.
If there is someone out there able to explain this to me, I would be very grateful. The IWB subs are inserted below, ant the lines in question are the two with calls to inet_ntoa.

Regards,
Egil



Sub udpGetMyIP(),string
'return the local host's IP address
def ip:string
def he:HostEnt
he = gethostbyname(udpGetMyName())
ip = inet_ntoa(he.*<POINTER>*<POINTER>addrlist)
Return ip
EndSub




Sub udpResolveIP(name:string),string
'get an ip address from a host name
def he:POINTER
he = gethostbyname(name)
if he<>NULL
return inet_ntoa(*<HostEnt>he.*<POINTER>*<POINTER>addrlist)
Else
return "Unknown"
EndIf
EndSub

Support Amateur Radio  -  Have a ham  for dinner!

sapero

February 24, 2011, 12:23:20 PM #1 Last Edit: February 24, 2011, 02:39:30 PM by sapero
The addrlist (official name h_addr_list) member of hostent structure points to an array of pointers to IP numbers. This array is NULL terminated (the last pointer is NULL).

The IPs:
char ip0[4] ' for IPv4
char ip1[4]
...
char ipN[4]
---------------------
the array of pointers :
array[] = pointer_to_ip0, pointer_to_ip1, ..., pointer_to_ipN, NULL
hostent.h_addr_list = &array

he.*<POINTER>*<POINTER>addrlistThis loads the fist IP in two steps:
1. X = *<POINTER>addrlist - first element of the array
2. Y = *<POINTER>X - the IP number as an pointer. It could be Y = *<UINT>X because inet_ntoa expects an UINT (officially in_addr structure by value).

Egil

Thanks sapero!

That made thing a little more clear to me... but it also means I have to study more to see how I can make it work in CB lingo...
Tomorrow I'll be attending a meeting in Trondheim, and before I go home I'll check into one of the student bookshops there to see if they have some book describing sockets, written i such a way that an old dog can understand.... hehe

Regards,
Egil
Support Amateur Radio  -  Have a ham  for dinner!

Egil

I was not able to figure out how to use pointer "magic" to retrieve the IP addresses on my LAN, so started to look for other methods.
User Earn had posted his solution for a similar problem in the IB-std section over at CodingMonkeys:  http://www.codingmonkeys.com/index.php?topic=623.0
His method appears to be more flexible than the routines I tried to convert to CB from IWB, so I decided to use his ideas and try to make routines that cover my own needs. Here is what i have done so far:


TYPE MIB_TCPROW
DEF dwState:INT
DEF dwLocalAddr:INT
DEF dwLocalPort:INT
DEF dwRemoteAddr:INT
DEF dwRemotePort:INT
ENDTYPE

DECLARE "IPHLPAPI.dll",GetTcpTable(pTcpTable:MEMORY, pdwSize:POINTER, border:INT),INT
DECLARE "ws2_32.dll",inet_ntoa(lIn:INT),STRING

DECLARE IP_Filter(TcpInf:MIB_TCPROW)
DECLARE UDP_GetMyIP()

OPENCONSOLE
def myip:string
myip=""
UDP_GetMyIP()
print myip
do:until inkey$=chr$(27)
CLOSECONSOLE
end

SUB UDP_GetMyIP()
DEF TcpTableSize,i,count:INT
DEF TcpRow:MIB_TCPROW
DEF null:MEMORY
DEF mTcpTable:MEMORY
GetTcpTable(null, TcpTableSize,0)
if TcpTableSize > 0
ALLOCMEM mTcpTable,1,TcpTableSize
if GetTcpTable(mTcpTable, TcpTableSize, 1) = 0
READMEM mTcpTable,1,count
for i = 1 to count
READMEM mTcpTable,i,TcpRow,len(count)
IP_Filter(TcpRow)
if myip <> "" then i = count
next i
endif
FREEMEM mTcpTable
endif
RETURN

SUB IP_Filter(TcpInf:MIB_TCPROW)
def my:string
my = inet_ntoa(TcpInf.dwLocalAddr)
if my="0.0.0.0" then my=""
if left$(my,3)="127" then my=""
if my<>"" then myip=my
RETURN


I had to define the variable myip globally to make the routines work, instead of returning the information from the subroutines, and have still to figure out why.
By modifying the IP_Filter subroutine slightly, it is also possible to retreive IP's for the other pc's on the LAN and what ports they use.

A big thank you to Earn for a great idea!

Regards,
Egil
Support Amateur Radio  -  Have a ham  for dinner!