I have an unsigned integer that is four bytes long that is sent to me via a network connection. When the server sends the integer, newlisp sees it as various characters ()&!@#*)}{":>?^`á–ó<Ф,Ù¸í.!ƒ/,á–ó< so on and so forth.
What I need to do is take those four bytes and 'convert' them into an integer value.
Where do I start when doing something like this? I've looked at (format) and played with it a little, but was unable to get it to determine an unsigned int value.
I am at a loss :-(
If you're interested in what I'm doing, you can try the challenge found here: http://www.pulltheplug.org/wargames/vortex/level00.html
Thanks for the help guys.
Determining the integeral value of a data.
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
Code: Select all
#!/usr/bin/newlisp
(context 'MAIN)
(setq host "69.55.233.82")
(setq port 5842)
(setq socket (net-connect host port))
(if socket
(begin
(println "Connected successfully!")
(print "Receiving data: ")
(setq final "")
(while (and (net-select socket "r" 1000000) (> (net-peek socket) 0))
(net-receive socket 'buffer 4)
(push ((unpack "<lu" buffer) 0) pool -1)
(print buffer)
)
(println)
(net-send socket (string (apply + pool)))
(while (and (net-select socket "r" 1000000) (> (net-peek socket) 0))
(net-receive socket 'buffer 1024)
(println buffer)
)
)
)
(net-close socket)
(exit)
The above code should be doing the following:
1- Connecting to remote host
2- Reading four bytes (which should be our first unsigned integer) into a buffer
3- Unpacking that four byte buffer
4- Throwing the results of the unpack into a list
5- Repeat steps 2-4 until four integers have been collected
6- Calculating a sum of all the elements of our newly created list
7- Returning our sum to the remote host
8- Capturing results (did we fail or not?)
Now, my code is flawed somewhere in it's unpacking/calculating sum stages. I cannot figure out what I am doing wrong, but it appears that both (map) and (unpack) are being used correctly.
If the server is telling me that I don't have the right sum, then where am I going wrong?
-statik
Have not analyzed your code much, but could crack the thing with following code:
as a result you get:
Lutz
ps: note that newLISP's signed integer addition overflows the 2billion 10^31, but it doesn't matter when converstin "<lu" everything will be fine.
Code: Select all
(set 'sock (net-connect "vortex.labs.pulltheplug.org" 5842))
(set 'sum 0)
(dotimes (i 4)
(net-receive sock 'buff 4)
(set 'val ((unpack "<lu" buff) 0))
(println "received:" val)
(push val pool)
)
(set 'sum (apply + pool))
(println "sending:" sum)
(set 'msg (pack "<lu" sum))
(net-send sock msg 4)
(net-receive sock 'info 1024)
(println info)
Code: Select all
received:574136882
received:1177762125
received:939103820
received:630841909
sending:-973122560
Username: level1 Password: Gq#qu3bF3
ps: note that newLISP's signed integer addition overflows the 2billion 10^31, but it doesn't matter when converstin "<lu" everything will be fine.
What an embarassment haha. Well I think I tracked down the flaw/bug...
I forgot to repack the data before sending it back. That would do it.
Thanks you guys for the help.
Code: Select all
(set 'msg (pack "<lu" sum))
Thanks you guys for the help.
-statik