-
Notifications
You must be signed in to change notification settings - Fork 0
The Protocol
SmallChat is a peer to peer UDP-based protocol (level 5, 6 and 7 ISO/OSI) initially designed to create a local network chat without needing to be connected to the Internet. Now, it can actually also be used for communicating with hosts outside the network. Despite the fact that it was designed for chat, the protocol could also be suitable for other scenarios.
It is assigned the 4412 UDP port and the smallchat service name.
SmallChat is intended to allow communication among multiple hosts in a network.
Different session of communication can happen within the same network, all on port 4412, without any conflict happening.
That is thanks to the ChatID, a non-secret string (it is not encrypted) used to identify a communication session univocally.
It can and should be a human-readable string and it shall be independent from the key.
Each host must store a list of data about all other hosts taking part in the same conversation (the ChatID is the same).
The SmallChat PDU (Protocol Data Unit) contains two parts: the first one is clear, while the second one is encrypted with SCEDA (SmallChat Encryption-Decryption Algorithm).
The clear part contains:
- The version of the protocol, stored as big endian (2 bytes): It is currently always 1.
- The ChatID (it doesn't have a fixed length and it has a null string terminator).
- The initialization vector randomly generated to be used to encrypt the encrypted part (8 bytes).
The encrypted part contains:
- The uppercase type of PDU (3 bytes).
- The character encoding used in the payload of the PDU (it doesn't have a fixed length and it has a null string terminator): it is also required in empty PDUs.
- The payload of the PDU (it doesn't have a terminator, nor a fixed length).
All text fields except the payload must always be encoded as us-ASCII.
Hello PDUs (HLO
) are used to discover other hosts to communicate with.
They are requests for a Welcome PDU from all other hosts and their payload is the nickname to be associated with the sender's IP.
Welcome PDUs (ACK
) are sent as a response to Hello PDUs.
Their payload is the nickname to be associated with the sender's IP.
Leave PDUs (LEV
) are sent by hosts which are leaving the communication.
They have no payload.
Message PDUs (MSG
) PDUs are used to send ordinary chat messages to other hosts.
Malformed PDU notifications (BAD
) are sent as a response to broken PDUs and PDUs which have been encrypted with the wrong key.
Nickname conflict notifications (CNF
) are sent when the IPs of two different hosts are associated with the same nickname to the involved hosts.
Their payload is the dot representation of the IP address of the host the receiver is in conflict with (or 0.0.0.0
if the receiver is in conflict with the sender).
The first PDU sent by a host (host A) is a Hello PDU and it is usually sent as a broadcast (but it can also be sent to a specific address and this is useful, for instance, when one wants to communicate with a host outside the local network).
When other hosts receive the Hello PDU, they add the nickname and the IP address of host A to the list of other hosts and they (all of them) answer with a Welcome PDU.
When host A receives the Welcome PDU, it adds data obout other hosts to its own lists and it can start communicating (Welcome PDUs don't need a reply).
When wanting to communicate, host A can send a message PDU. When host A receives a PDU from another hosts, it doesn't need to send any confirm.
If host A receives a PDU or a message it is not able to decrypt or understand, it must send a BAD notification to the sender of such PDU, until it reaches a maximum of for BAD notifications per ten minutes (this is intended to avoid congestion when two hosts are using different encryption keys: without this idea they would keep sending bigger and bigger BAD notifications).
If host A receives a broken PDU, but it can't send a BAD notification, it simply discards it.
When host A receives a Hello PDU it adds (or update) data about the senders in its list and it replies with a Welcome PDU. If it detects a nickname conflict (another host is using the same nicknake) it replies with a CNF notification and it also sends a CNF notification to the hother host with the same nicnkame.
When host A receives a Leave PDU, it removes the sender from its list of other hosts and it sends no reply.
When host A wants to leave the conversation, it sends a Leave PDU.