aMule Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

We're back! (IN POG FORM)

Author Topic: BUILDING APP USING EC - Help!!!  (Read 3207 times)

pirosb3

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
BUILDING APP USING EC - Help!!!
« on: October 15, 2009, 02:01:13 AM »

Hi all
I'm trying to build a client app using the EC libraries that amuled answers to.
I looked at the wiki and the source code and.. i must admit im quite puzzled :D
for now i would like to do some sort of basic authentication...
i thought of using Telnet:
     telnet localhost 4711
     0x02 (from what i understand this is basic authentication)
     0x04 (for password hash... how do i send it? it just terminates my session...)

is there any way i could use text and not hex values?

Thanks ;D


Logged

wuischke

  • Developer
  • Hero Member
  • *****
  • Karma: 183
  • Offline Offline
  • Posts: 4292
Re: BUILDING APP USING EC - Help!!!
« Reply #1 on: October 15, 2009, 10:44:44 AM »

You have to send these values as binary values. Telnet is probably not possible, but python for instance will do the job:
Code: [Select]
import socket

login_packet = '\x00\x00\x00\x22\x00\x00\x00\x36\x02\x04\xc8\x80\x06\x0d\x61\x6d\x75\x6c\x65\x2d\x72\x65\x6d\x6f\x74\x65\x00\xc8\x82\x06\x07\x30\x78\x30\x30\x30\x31\x00\x04\x03\x02\x02\x00\x02\x09\x10\x47\xbc\xe5\xc7\x4f\x58\x9f\x48\x67\xdb\xd5\x7e\x9c\xa9\xf8\x08'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 4712))
s.send(login_packet)
s.close()
This is the binary code of a login packet for aMule 2.2.5 (or 2.2.6) with client name "amule-remote", client version "0x0001" and the password "aaa". In this version of aMule, the password happens to be a simple MD5 sum, so you could change by executing
Code: [Select]
import hashlib
hashlib.md5('aaa').hexdigest()
to get a new password hash and replacing the '47bce5c74f589f4867dbd57e9ca9f808' password hash in above binary code.
Logged

pirosb3

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
Re: BUILDING APP USING EC - Help!!!
« Reply #2 on: October 15, 2009, 08:04:40 PM »

ok thats great!!! aMuled authenticates me, and spits me back:

'\x00\x00\x00"\x00\x00\x00\x0b\x04\x01\xe0\xa8\x96\x06\x04SVN\x00'
(im using the Adunanza version for italian ISP Fastweb)

everything is correct! the only thing is... how do i decifer this?
how can i start a search? im having trouble with the docs!!

Thanks for your reply
Logged

Stu Redman

  • Administrator
  • Hero Member
  • *****
  • Karma: 214
  • Offline Offline
  • Posts: 3739
  • Engines screaming
Re: BUILDING APP USING EC - Help!!!
« Reply #3 on: October 15, 2009, 09:02:48 PM »

Adunanza is NO-GO here.
And this won't work. I always marvel at wuischke's patience, but this will take like 200 posts at least.
There are no detailed docs. If you can't figure things out by looking at the source, it is hopeless.
Logged
The image of mother goddess, lying dormant in the eyes of the dead, the sheaf of the corn is broken, end the harvest, throw the dead on the pyre -- Iron Maiden, Isle of Avalon

pirosb3

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
Re: BUILDING APP USING EC - Help!!!
« Reply #4 on: October 15, 2009, 10:33:14 PM »

ok... im sorry if i upset you Stu Redman in some way, there is no reason of you answering that way....

wanna get rid of me?? just tell me how i can do a search query and catch the results.

Thanks
Logged

wuischke

  • Developer
  • Hero Member
  • *****
  • Karma: 183
  • Offline Offline
  • Posts: 4292
Re: BUILDING APP USING EC - Help!!!
« Reply #5 on: October 15, 2009, 11:48:17 PM »

We won't support Adunanza in any way - if something goes wrong, you're on your own.

Regarding deciphering the packet: You'll need the OPcodes for different operations and some understanding of the packet structure. If you prefer python, you can find an incomplete implementation at Google code, but there's always the wiki and the C++ source code, too.

Let's just walk through the response:

Code: [Select]
0x00 0x00 0x00 0x22 # Packet header, is always 0x20 + flags; the flags in this case are 0x02, which is UTF8 numbers
0x00 0x00 0x00 0x0b # Content length - 11 decimal
0x04 # OPCode indicating that authentication was OK
0x01 # Tag count. A tag is basically a collection of name, type and value
0xe0 0xa8 0x96 # This is the encoded number 0x050b, which is the tag name "server version" I'll explain decoding further down
0x06 # tag type string
0x04 # string length
0x53 0x56 0x4e 0x00 # String 'SVN\0'
Translated to XML, to make the transmitted data more obvious:
Code: [Select]
<ecpacket>
 <tag>
  <name>server_version</name>
  <type>string</type>
  <value>SVN</value>
 </tag>
</ecpacket>

UTF-8 decoding:
Code: [Select]
if ord(data[0]) in range(0x7F):
    name_len = 1
elif ord(data[0]) in range(0xc3,0xdf):
    name_len = 2
elif ord(data[0]) in range(0xe0,0xef):
    name_len = 3
tag_value = ord(data[:name_len].decode("utf-8"))
tag_name = tag_value/2

I won't explain all details of UTF8, but basically you can use different character lengths (from 1 Byte for ASCII up to 4 Bytes) and by looking at the first Byte you know how many Bytes there are. Because multiple Bytes are possible, you have more than the 255 values possible with ASCII. For instance the character "™" (trademark) has the character number 8482 and the utf-8 representation '\xe2\x84\xa2'.
To get a tag name, you get the character number for the utf-8 representation and divide it by two. (See what the EC wiki entry says on subtags to learn about the need to divide by two.)

I can't tell you how search works, because I don't know - I recommend you to understand the packet structure and then use wireshark to capture the packets of a search request by amulecmd.
« Last Edit: October 15, 2009, 11:49:52 PM by wuischke »
Logged

pirosb3

  • Approved Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
Re: BUILDING APP USING EC - Help!!!
« Reply #6 on: October 17, 2009, 01:15:19 PM »

Thanks very much! This has been major help for me.. your explanation was easy and now im getting the hang of it... (i managed to turn my server off using EC=) big success for me!)
i have a few questions:

Considering...
Code: [Select]
char peer0_2[] = {

0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x1b,

0x26, 0x01, 0xe0, 0xb8, 0x83, 0x02, 0x17, 0x02,

0xe0, 0xb8, 0x84, 0x06, 0x07, 0x66, 0x65, 0x64,

0x6f, 0x72, 0x61, 0x00, 0xe0, 0xb8, 0x8a, 0x06,

0x01, 0x00, 0x02 };

now...
Code: [Select]
0x00, 0x00, 0x00, 0x22,
[Packet header]

0x00, 0x00, 0x00, 0x1b,
[Packet Length]

0x26,
[OPCode, means SEARCH]
0x01,
[1 tag]

0xe0, 0xb8, 0x83, 0x02, 0x17, 0x02,
[Have no idea]

0xe0, 0xb8, 0x84
[I really dony know what this is :D']

0x06,
[STRING, tag-code]

0x07,
[String Length... in this case 7]

66 65 64 6F 72 61
[This is a UTF8 string saying Fedora]

0x00, 0xe0, 0xb8, 0x8a, 0x06,
[Have no idea]

0x01, 0x00, 0x02
[Have no idea]

In any case.. the program im writing is based on Objective C... The idea is to release a software for iPhone more or less like aMuleGUI.
Is there any pre-built class i could use? was looking at /libs/ec/cpp/* but still.. have no clue of how amulecmd interacts with them :(

Thanks
Logged

wuischke

  • Developer
  • Hero Member
  • *****
  • Karma: 183
  • Offline Offline
  • Posts: 4292
Re: BUILDING APP USING EC - Help!!!
« Reply #7 on: October 17, 2009, 04:06:48 PM »

Please have a look at the cocoa-mule directory. There should be a fairly complete EC implementation in obj-c by lfroen.

Regarding your packet: We've subtags and now it get's really unintuitive and confusing.
Quote
0x00, 0x00, 0x00, 0x22,
[Packet header]

0x00, 0x00, 0x00, 0x1b,
[Packet Length]

0x26,
[OPCode, means SEARCH]
0x01,
[1 tag]

0xe0, 0xb8, 0x83
[Tag type: Search type; Bit 0 is set-> has subtags]

0x02
[tag type: uint8 (tag type for search type)]

0x17
[tag length]
0x02
[num subtags]

0xe0, 0xb8, 0x84
[Tag type: Search name]

0x06,
[STRING, tag-code]

0x07,
[String Length... in this case 7]

66 65 64 6F 72 61 0x00 -> NULL terminated
[This is a UTF8 string saying Fedora]

0xe0, 0xb8, 0x8a
[Tag type: Search file type]

0x06
[String]

0x01
[length 1]

0x00
[empty string, only \0]

0x02
[Value for search type]
XML:
Code: [Select]
<ecpacket>
 <search>
  <type>2</type>
  <name>Fedora</name>
  <file_type></file_type>
 </search>
</ecpacket>

Hints:
- If something starts with 0xe0, it's most probably one of these UTF8 encoded numbers with the length of 3. The order is tag name, tag type, tag value.
- If such a number is odd, it will have subtags. I don't particularly like the design of subtags: First there's the tag type, then the length of the tag (possibly wrong), the number of subtags, the subtags and finally the tag value.

Regarding interaction: The whole stuff is hidden behind many layers of abstraction. You need to understand quite some code to get the hang of it all.
Logged

lfroen

  • Guest
Re: BUILDING APP USING EC - Help!!!
« Reply #8 on: October 17, 2009, 10:39:37 PM »

Quote
Please have a look at the cocoa-mule directory. There should be a fairly complete EC implementation in obj-c by lfroen.

Second that. If you're looking for objc code - cocoa-mule is almost complete implementation.
Logged