| View previous topic :: View next topic |
| Author |
Message |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Thu Jun 01, 2006 6:06 pm Post subject: LongtoIp problem |
|
|
Im'm writing an irc client in lua. For the dcc send support i have to convert a long integer ip to an human readable ip. I wrote this function:
| Code: |
function longtoip(long)
ip = ""
for i=3,0,-1 do
ip = ip .. (long / math.pow(256,i))
long = long - math.floor((long / math.pow(256,i)))*math.pow(256,i)
if i>0 then ip = ip .. "." end
end
return ip
end
|
this function converts a number like this: 4294967295 to 255.255.255.255
i tested it on lua5.1 and works. But in luaplayer doesn't work... I don't know what to do |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Fri Jun 02, 2006 8:15 am Post subject: |
|
|
I suspect it doesn't work because psp lua uses float instead of double internally.
Can you use 'shift' and 'and' instead?
Pseudo code
ip=""
ip=ip .. addr shr 24
ip=ip .. "."
ip=ip .. (addr shr 16) and 255
ip=ip .. "."
ip=ip .. (addr shr 8) and 255
ip=ip .. "."
ip=ip .. addr and 255
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Fri Jun 02, 2006 8:05 pm Post subject: |
|
|
Your idea is good... but luaplayer 0.16 doesn't support shift operators.
Shine says that x>>y == math.floor(x / math.pow(2, y)).
http://forums.ps2dev.org/viewtopic.php?t=3138
So the result is the same as my function. |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Fri Jun 02, 2006 9:05 pm Post subject: |
|
|
What exactly is the output from the function in luaplayer? The problem lies somewhere in the double->float issue or in the math library. Either you are exceeding the precision of floats or you're using a function that doesn't work as expected.
Does math.pow() work in luaplayer?
Does math.floor() work?
If you assign a variable to the MAX_INT number (429...) then you examine the variable does it still contain the same value? |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Fri Jun 02, 2006 9:28 pm Post subject: |
|
|
| The function works fine with the first 3 numbers. But the last number is wrong. |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Fri Jun 02, 2006 9:41 pm Post subject: |
|
|
What exactly does it return for the last number?
Maybe math.pow(256,0) isn't returning 1. |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Fri Jun 02, 2006 9:52 pm Post subject: |
|
|
| math.pow(256,0) returns 1 |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Fri Jun 02, 2006 10:23 pm Post subject: |
|
|
I don't think a float has enough precision to hold all the digits in a long. The problem is that there are too many unique digits. I have a version of lua built on the PC that uses floats instead of doubles (you should really make that change to your test lua5.1). This is what I get:
| Code: | local f = 4294967295
print("Value is " .. f)
printf("LongToIp is " .. longtoip(f) .."\n")
-- Value is 4294967296
-- LongToIp is 256.0.0.0
|
You may see different results in luaplayer because the rounding/clamping is probably machine-dependent (FPU-dependent I suppose).
So, your function is probably correct, but the number gets mangled because it can't fit into a float. You're going to need to store the IP differently, maybe actually use four integers instead, or build a nice table/object with some methods. |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Fri Jun 02, 2006 10:49 pm Post subject: |
|
|
| can you give me an example? |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Sat Jun 03, 2006 1:07 am Post subject: |
|
|
Something like this maybe. I put this in a file ip.lua and then require("ip")
| Code: | --usage:
-- local ip = IP:new(255,255,255,0)
-- print(ip:toString())
-- local ip = IP:new({255,255,255,0})
-- print(ip:toString())
IP = {}
local mt = {}
mt.__index = IP
function IP:new(a,b,c,d)
local n = {}
-- if they just pass a table
if (a and type(a) == "table") then
for i=1,#s do
n[i] = s[i]
end
else if (a and b and c and d) then
-- if they pass four integers
n[1] = a
n[2] = b
n[3] = c
n[4] = d
end
return setmetatable(n, mt)
end
function IP:toString()
return self[1].."."..self[2].."."..self[3].."."..self[4]
end
|
You can add methods to IP like in the :toString() example. I'm sure it would be possible to parse a long that is stored in lua as a string and turn it into this IP object also. |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sat Jun 03, 2006 6:04 pm Post subject: |
|
|
Yes.. but the problem is that i'm writing an irc client and when a user sends a file via DCC he sends the following string:
nick!user@host PRIVMSG nick :\001DCC SEND Filename address port\001
so the user that receives the file has to connect to address:port, but address is in the long format. |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Sat Jun 03, 2006 7:12 pm Post subject: |
|
|
Ive faced a similar problem as well, and the simpilist thing to do is parse the data past where the IRC text goes. so instead of
nick!user@host privmsg nick :blah blah blah
you get "blah blah balh"
Might want to invest in creating a token reader.
In c++ I have one you can probably rip.
However I would use the parser that is already included in the LUA library. Useing Regular Expressions. |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sat Jun 03, 2006 7:19 pm Post subject: |
|
|
| you don't understand the problem. is converting a long int ip to an human readable ip. |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Sat Jun 03, 2006 7:33 pm Post subject: |
|
|
then explain what do you mean?
Example from the LUAApp's to disp your IP is
while true do
ipAddress = Wlan.getIPAddress()
if ipAddress then break end
System.sleep(100)
end
graphicsPrintln("the PSP IP address is: " .. ipAddress)
graphicsPrintln("try http:// " .. ipAddress .. " on your computer") |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sat Jun 03, 2006 7:50 pm Post subject: |
|
|
Ok... The long formatted ip is obtained from normal ip using this code:
| Code: |
tonumber(ip[4])+256*tonumber(ip[3])+256*256*tonumber(ip[2])+256*256*256*tonumber(ip[1])
|
where ip is a tab:
| Code: |
ip={"AAA","BBB","CCC","DDD"}
|
i have to convert the long formatted ip to a normal ip (AAA.BBB.CCC.DDD)
[/code] |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Jun 04, 2006 4:25 am Post subject: Re: LongtoIp problem |
|
|
| aserto wrote: | this function converts a number like this: 4294967295 to 255.255.255.255
i tested it on lua5.1 and works. But in luaplayer doesn't work... I don't know what to do |
In Lua Player 0.20 I've integrated Lua 5.1 with double as number type, to avoid such problems. Now it should work. |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sun Jun 04, 2006 7:25 pm Post subject: |
|
|
| Will Luaplayer 0.20 work on eloader 0.97? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Jun 04, 2006 8:13 pm Post subject: |
|
|
| aserto wrote: | | Will Luaplayer 0.20 work on eloader 0.97? |
I don't know, because currently I have only a 1.0 and 1.5 PSP, but you can try it, Lua Player 0.20 is released: http://forums.ps2dev.org/viewtopic.php?t=5885 |
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sun Jun 04, 2006 8:42 pm Post subject: |
|
|
Doesn't work.... :(
I think I'll write a php script to convert ip. Then I'll put the script on a web hosting. Then the lua program will connect to the web server and will get the converted ip.
I know... It's very lame... But I can't find another solution! |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Jun 04, 2006 9:57 pm Post subject: |
|
|
| aserto wrote: | Doesn't work.... :(
|
Maybe you can write a bit more about what doesn't work. Lua Player 0.20 works? Then this works, too:
| Code: |
function ip2string(ip)
local string = nil
for i = 1, 4 do
if string then string = string .. "." else string = "" end
string = string .. ip[i]
end
return string
end
function ip2number(ip)
local number = 0
local m = 1
for i = 1, 4 do
number = number + ip[i] * m
m = m * 256
end
return number
end
function number2ip(number)
local ip = {}
local m = 256
for i = 1, 4 do
ip[i] = number % m
number = math.floor(number / 256)
end
setmetatable(ip, { __tostring = ip2string })
return ip
end
tests = {
{ { 0, 0, 0, 0 }, 0 },
{ { 0, 0, 0, 1 }, 16777216 },
{ { 1, 0, 0, 0 }, 1 },
{ { 1, 2, 3, 4 }, 67305985 },
{ { 4, 3, 2, 1 }, 16909060 },
{ { 212, 227, 39, 202 }, 3391611860 },
{ { 255, 255, 255, 255 }, 4294967295 } }
y = 0
for _, test in pairs(tests) do
ip, expected = test[1], test[2]
number = ip2number(ip)
assert(number == expected)
ip2 = number2ip(number)
assert(ip[1] == ip2[1] and ip[2] == ip2[2] and ip[3] == ip2[3] and ip[4] == ip2[4])
screen:print(0, y, tostring(ip2), Color.new(255, 255, 255))
y = y + 10
end
screen.waitVblankStart()
screen.flip()
while true do
screen.waitVblankStart()
if Controls.read():start() then break end
end
|
On my PSP it prints this:
| Quote: |
0.0.0.0
0.0.0.1
1.0.0.0
1.2.3.4
4.3.2.1
212.227.39.202
255.255.255.255
|
|
|
| Back to top |
|
 |
aserto
Joined: 17 Apr 2006 Posts: 22
|
Posted: Sun Jun 04, 2006 10:25 pm Post subject: |
|
|
| I mean than luaplayer 0.20 doesn't work with eloader 0.97 |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Jun 04, 2006 10:34 pm Post subject: |
|
|
| aserto wrote: | | I mean than luaplayer 0.20 doesn't work with eloader 0.97 |
Then it would be a good idea to ask the author to enhance eloader 0.97. Maybe the module loading is not supported. A solution might be this thread: http://forums.ps2dev.org/viewtopic.php?t=5663 But I want to concentrate more on Lua Player core development, maybe someone with more knowledge of the PSP module concept can try it. |
|
| Back to top |
|
 |
|