forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

LongtoIp problem

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Thu Jun 01, 2006 6:06 pm    Post subject: LongtoIp problem Reply with quote

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
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Fri Jun 02, 2006 8:15 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Fri Jun 02, 2006 8:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
SSpeare



Joined: 23 May 2006
Posts: 63

PostPosted: Fri Jun 02, 2006 9:05 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Fri Jun 02, 2006 9:28 pm    Post subject: Reply with quote

The function works fine with the first 3 numbers. But the last number is wrong.
Back to top
View user's profile Send private message
SSpeare



Joined: 23 May 2006
Posts: 63

PostPosted: Fri Jun 02, 2006 9:41 pm    Post subject: Reply with quote

What exactly does it return for the last number?

Maybe math.pow(256,0) isn't returning 1.
Back to top
View user's profile Send private message Visit poster's website
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Fri Jun 02, 2006 9:52 pm    Post subject: Reply with quote

math.pow(256,0) returns 1
Back to top
View user's profile Send private message
SSpeare



Joined: 23 May 2006
Posts: 63

PostPosted: Fri Jun 02, 2006 10:23 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Fri Jun 02, 2006 10:49 pm    Post subject: Reply with quote

can you give me an example?
Back to top
View user's profile Send private message
SSpeare



Joined: 23 May 2006
Posts: 63

PostPosted: Sat Jun 03, 2006 1:07 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sat Jun 03, 2006 6:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Sat Jun 03, 2006 7:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sat Jun 03, 2006 7:19 pm    Post subject: Reply with quote

you don't understand the problem. is converting a long int ip to an human readable ip.
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Sat Jun 03, 2006 7:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sat Jun 03, 2006 7:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Jun 04, 2006 4:25 am    Post subject: Re: LongtoIp problem Reply with quote

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
View user's profile Send private message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sun Jun 04, 2006 7:25 pm    Post subject: Reply with quote

Will Luaplayer 0.20 work on eloader 0.97?
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Jun 04, 2006 8:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sun Jun 04, 2006 8:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Jun 04, 2006 9:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
aserto



Joined: 17 Apr 2006
Posts: 22

PostPosted: Sun Jun 04, 2006 10:25 pm    Post subject: Reply with quote

I mean than luaplayer 0.20 doesn't work with eloader 0.97
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Jun 04, 2006 10:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group