2020-03-29 12:48:11 +02:00
# P2P-JAVA-PROJECT version 1.2 (Binary protocol for step 2+)
2020-01-21 12:43:02 +01:00
2020-01-23 15:54:27 +01:00
All strings in the datagram are utf-8 encoded.
2020-03-25 14:05:48 +01:00
```text
2020-01-21 12:43:02 +01:00
2020-02-26 11:10:34 +01:00
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]
2020-01-21 12:43:02 +01:00
```
## Requests and responses codes
2020-03-29 12:48:11 +02:00
- `REQUESTS` (msb is 0):
- `LIST` (`0x00`)
- `LOAD` (`0x01`)
- `HASH` (`0x02`)
- `DISCOVER` (`0x03`)
- `REGISTER` (`0x04`)
- `UNREGISTER` (`0x05`)
- `RATIO` (`0x06`)
- `UPDATE RATIO` (`0x07`)
- `SIZE` (`0x08`)
- `RESPONSES` (msb is 1):
- `LIST` (`0x80`)
- `LOAD` (`0x81`)
- `HASH` (`0x82`)
- `DISCOVER` (`0x83`)
- `RATIO` (`0x86`)
- `DENIED` (`0x87`)
2020-03-27 18:41:27 +01:00
- `SIZE` (0x88)
2020-03-29 12:48:11 +02:00
- `VERSION ERROR` (`0xC0`)
- `PROTOCOL ERROR` (`0xC1`)
- `INTERNAL ERROR` (`0xC2`)
- `EMPTY DIRECTORY` (`0xC3`)
- `NOT FOUND` (`0xC4`)
- `EMPTY FILE` (`0xC5`)
- `NOT A TRACKER` (`0xC6`)
- `UNKNOWN HOST` (`0xC7`)
2020-01-21 12:43:02 +01:00
### List
Payload size for list request is always zero.
2020-01-22 13:02:03 +01:00
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.
2020-01-21 12:43:02 +01:00
2020-01-21 23:47:43 +01:00
### Load
2020-01-21 12:43:02 +01:00
#### Not found
Response when the file requested is not found on the server.
2020-01-21 23:47:43 +01:00
Payload size for Not found is zero.
2020-01-21 12:43:02 +01:00
#### Load response
Payload contains
2020-03-25 14:05:48 +01:00
```text
2020-02-26 11:10:34 +01:00
8 bytes: [(bytes 8-15): OFFSET OF FILE CONTENT IN BYTES]
4 bytes: [(bytes 24-27): FILENAME SIZE] (cannot be > to PAYLOAD_SIZE - 20 or be zero)
2020-01-23 15:54:27 +01:00
y bytes: [< FILENAME > ]
2020-01-26 14:52:40 +01:00
z bytes: [PARTIAL CONTENT]
2020-01-21 12:43:02 +01:00
```
2020-01-21 23:47:43 +01:00
#### Load request
2020-01-26 14:52:40 +01:00
Payload contains
2020-03-25 14:05:48 +01:00
```text
2020-02-26 11:10:34 +01:00
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)
2020-01-26 14:52:40 +01:00
y bytes: [< FILENAME > ]
2020-03-25 14:05:48 +01:00
2 bytes: port used to register on tracker
2020-03-27 16:22:42 +01:00
? bytes: hostname used to register on tracker
2020-01-26 14:52:40 +01:00
```
2020-03-29 12:48:11 +02:00
Possible responses: Load response, or Denied
2020-01-22 13:02:03 +01:00
2020-03-27 18:41:27 +01:00
### Size
#### Size request
Payload contains
```text
? bytes: filename
```
2020-03-29 12:48:11 +02:00
Possible responses: Size response, Empty file or Not found
2020-03-27 18:41:27 +01:00
#### Size response
Payload contains
```text
8 bytes: file size
? bytes: filename
```
2020-03-25 14:05:48 +01:00
### Hash
2020-03-04 17:40:21 +01:00
#### Hash request
Get hash of a file. Payload contains
2020-03-25 14:05:48 +01:00
```text
2020-03-04 17:40:21 +01:00
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [< FILENAME > ]
z bytes: [ALGO_NAMES requested separated by \n] (ex.: SHA-256, MD5)
```
2020-03-29 12:48:11 +02:00
If file does not exists, a Not found can be responded.
2020-03-04 17:40:21 +01:00
#### Hash response
Payload contains:
2020-03-25 14:05:48 +01:00
```text
2020-03-04 17:40:21 +01:00
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [< FILENAME > ]
[[ multiple algo hashes bloc]]
```
A algo hash bloc contains:
2020-03-25 14:05:48 +01:00
```text
2020-03-04 17:40:21 +01:00
4 bytes [ALGO_NAME size]
? [ALGO_NAME]
4 bytes: [HASH SIZE (bytes)] / or 0 if this hash algorithm is unsupported.
?? [HASH]
```
2020-03-19 14:04:33 +01:00
### Tracker specific messages
#### Register
Used by a server to register itself on a tracker.
Server may want to do a free `DISCOVER` to check if they have been registered.
2020-03-20 00:11:13 +01:00
Payload contains:
2020-03-25 14:05:48 +01:00
```text
2020-03-20 00:11:13 +01:00
2 bytes: [< PORT NUMBER > ]
```
2020-03-19 14:04:33 +01:00
#### Unregister
Used by a server to unregister itself from a tracker.
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.
2020-03-20 00:11:13 +01:00
Payload contains:
2020-03-25 14:05:48 +01:00
```text
2020-03-20 00:11:13 +01:00
2 bytes: [< PORT NUMBER > ]
2020-03-20 10:21:28 +01:00
? bytes: [< HOSTNAME > ]
2020-03-20 00:11:13 +01:00
```
2020-03-19 14:04:33 +01:00
#### 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.
2020-03-25 14:05:48 +01:00
```text
2020-03-19 14:04:33 +01:00
? bytes: [< FILENAME > ]
2020-03-20 10:21:28 +01:00
? bytes: [< HOSTNAME > ]
2020-03-19 14:04:33 +01:00
```
#### Discover response
Contains:
2020-03-25 14:05:48 +01:00
```text
2020-03-19 14:04:33 +01:00
4 bytes: [(bytes 8-11): FILENAME SIZE]
y bytes: [< FILENAME > ]
? bytes [multiple server blocks]
```
Server block is composed with:
2020-03-25 14:05:48 +01:00
```text
2020-03-19 14:04:33 +01:00
2 bytes: [port]
? bytes: hostname
\n
```
#### Not a Tracker
2020-03-29 12:48:11 +02:00
This error is raised when receiving a Discover, a Register, or an Unregister request,
2020-03-19 14:04:33 +01:00
but this application is not a tracker.
2020-03-04 17:40:21 +01:00
2020-03-25 14:05:48 +01:00
#### Ratio Request
Contains:
```text
2 bytes: port
? bytes: hostname
```
2020-03-29 12:48:11 +02:00
Possible responses: Ratio response, or Unknown host
2020-03-25 14:05:48 +01:00
#### Ratio Response
Contains:
```text
8 bytes: total sent bytes
8 bytes: total received bytes
2020-03-27 16:59:17 +01:00
2 bytes: port
? bytes: hostname
2020-03-25 14:05:48 +01:00
```
#### Update Ratio
Contains:
```text
2020-03-27 16:59:17 +01:00
8 bytes: bytes sent by the server
2020-03-25 14:05:48 +01:00
2 bytes: server port
2 bytes: client* port
? bytes: server hostname followed by \n
? bytes: client* hostname
* note: used by client to register on tracker
```
2020-03-29 12:48:11 +02:00
Possible responses: No response, or Unknown host (if client is not registered or server is not registered)
2020-03-25 14:05:48 +01:00
Note: client must have verified hash before sending Update Ratio to tracker for each server which participated.
#### Unknown Host
Payload size is zero.
#### Denied
Contains
```text
2020-03-27 18:00:06 +01:00
8 bytes: offset of content asked
? bytes: filename
2020-03-25 14:05:48 +01:00
```
2020-01-22 13:02:03 +01:00
### 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.