You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Projet_JAVA_P2P_STRI2A/doc/protocol.md

158 lines
3.9 KiB
Markdown

# P2P-JAVA-PROJECT version 1.2 (Binary protocol for step 2)
All strings in the datagram are utf-8 encoded.
```Datagram format
1 byte: [(byte 0 ): VERSION(0x11, first quartet is major version, second is minor)]
1 byte: [(byte 1 ): REQUEST/RESPONSE CODE]
2 bytes: [(bytes 2-3): CHECKSUM (UDP only)]
4 bytes: [(bytes 4-7): PAYLOAD SIZE IN BYTES]
x bytes: [(bytes 8-?): PAYLOAD]
```
## Requests and responses codes
- REQUESTS (msb is 0):
- `LIST` (0x00)
- `LOAD` (0x01)
4 years ago
- `HASH` (0x02)
- `DISCOVER` (0x03)
- `REGISTER` (0x04)
- `UNREGISTER` (0x05)
- RESPONSES (msb is 1):
- `LIST` (0x80)
- `LOAD` (0x81)
4 years ago
- `HASH` (0x82)
- `DISCOVER` (0x83)
- `VERSION ERROR` (0xC0)
- `PROTOCOL ERROR` (0xC1)
- `INTERNAL ERROR` (0xC2)
- `EMPTY DIRECTORY` (0xC3)
- `NOT FOUND` (0xC4)
- `EMPTY FILE` (0xC5)
- `NOT A TRACKER` (0xC6)
### List
Payload size for list request is always zero.
Payload for list response is filenames separated by `\n`. Payload size for list response is never zero.
#### Empty directory
When directory is empty.
Payload size for empty directory is always zero.
### Load
#### Not found
Response when the file requested is not found on the server.
Payload size for Not found is zero.
#### Load response
Payload contains
```
8 bytes: [(bytes 8-15): OFFSET OF FILE CONTENT IN BYTES]
8 bytes: [(bytes 16-23): TOTAL FILESIZE]
4 bytes: [(bytes 24-27): FILENAME SIZE] (cannot be > to PAYLOAD_SIZE - 20 or be zero)
y bytes: [<FILENAME>]
z bytes: [PARTIAL CONTENT]
```
#### Load request
Payload contains
```
8 bytes: [(bytes 8-15): OFFSET OF FILE CONTENT IN BYTES]
8 bytes: [(bytes 16-23): MAX SIZE OF PARTIAL CONTENT (partial content in response should not excess this size, but this can be less (by example if endoffile is reached or server doesn't have the full block requested)
4 bytes: [(bytes 24-27): FILENAME SIZE] (cannot be > to PAYLOAD_SIZE - 20 or be zero)
y bytes: [<FILENAME>]
```
4 years ago
### Hash
#### Hash request
Get hash of a file. Payload contains
```
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [<FILENAME>]
z bytes: [ALGO_NAMES requested separated by \n] (ex.: SHA-256, MD5)
```
If file does not exists, a NotFound can be responded.
#### Hash response
Payload contains:
```
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [<FILENAME>]
[[ multiple algo hashes bloc]]
```
A algo hash bloc contains:
```
4 bytes [ALGO_NAME size]
? [ALGO_NAME]
4 bytes: [HASH SIZE (bytes)] / or 0 if this hash algorithm is unsupported.
?? [HASH]
```
### Tracker specific messages
#### Register
Used by a server to register itself on a tracker.
Payload contains `0x01` if working on udp, `0x02` if working on tcp, `0x03` if working on both.
Server may want to do a free `DISCOVER` to check if they have been registered.
#### Unregister
Used by a server to unregister itself from a tracker.
Payload contains `0x01` if unregister from udp, `0x02` if unregister from tcp, `0x03` if unregister on both.
No error is raised if the server was not registered.
Server may want to do a free `DISCOVER` to check if they have been unregistered.
#### Discover request
If payload size is null, lists all servers registered.
If payload contains a filename, list all servers having this file in their list.
```
? bytes: [<FILENAME>]
```
#### Discover response
Contains:
```
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [<FILENAME>]
? bytes [multiple server blocks]
```
Server block is composed with:
```
2 bytes: [port]
1 byte: [protocols (udp: 0x01, tcp: 0x02, tcp+udp: 0x03)]
? bytes: hostname
\n
```
#### Not a Tracker
This error is raised when receiving a DISCOVER, a REGISTER, or an UNREGISTER request,
but this application is not a tracker.
4 years ago
### Other response code (errors)
#### Version error
Response when datagram received use wrong version code.
#### Protocol error
Response when the request cannot be interpreted (but version is correct).
Payload size for Protocol error is zero
#### Internal error
Response in internal failure case.
Payload size for Internal error is zero.