protocol

package
v0.0.0-...-1aa968d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

This file contains global constants of the protocol

This file describes various headers of the protocol and how to use them

Examples of packets, ready for transportation in pseudo-code: []byte(|34|FILEDATA~fe2[gkr3j930f]fwpglkrt[o]) []byte(|57|FILENAME~theBestFileNameEver_Existed_in”'theUniverse.txt) general structure: PACKETSIZEDELIMETER packetsize PACKETSIZEDELIMETER packet.Header HEADERDELIMETER packet.Body (without spaces between)

Index

Constants

View Source
const HEADERDELIMETER string = "~"

HEADERDELIMETER. Character that delimits header of the packet from the body of the packet. ie: FILEINFO~img.png

View Source
const MAXPACKETSIZE uint = 512000 // 50 kilobytes

MAXPACKETSIZE. How many bytes can contain one packet (header + body) at maximum (packets with size bigger than MAXPACKETSIZE are invalid and will not be sent)

View Source
const PACKETSIZEDELIMETER string = "|"

PACKETSIZEDELIMETER. Character that delimits one and the other sides of the next incoming packet. ie: |packet_size_here|packet_here, where "|" is PACKETSIZEDELIMETER

Variables

This section is empty.

Functions

func MeasurePacketSize

func MeasurePacketSize(packet Packet) uint64

Returns a size of the given packet as if it would be sent and presented in bytes. ie: FILESIZE~[49 49 56 55 56 53 50 49 54] DOES COUNT THE PACKETSIZEDELIMETER

func PacketToBytes

func PacketToBytes(packet Packet) ([]byte, error)

Converts given packet struct into ready-to-transfer bytes, constructed by following the protocol

func ReadFromConn

func ReadFromConn(connection net.Conn) ([]byte, error)

Reads a packet from given connection, returns its bytes. ASSUMING THAT THE PACKETS ARE SENT BY `SendPacket` function !!!!

func SendEncryptedPacket

func SendEncryptedPacket(connection net.Conn, packetToSend Packet, key []byte) error

Sends given packet to connection, as the normal `SendPacket` method, but encodes given packet`s BODY with AES encryption

func SendPacket

func SendPacket(connection net.Conn, packetToSend Packet) error

Sends given packet to connection, following all the protocol`s rules. ALL packets MUST be sent by this method

Types

type Header string
const HeaderAccept Header = "FILEACCEPT"

FILEACCEPT. The opposite of the previous FILEREJECT. Send by receiver when the user has agreed to download the file. ie: |11|FILEACCEPT~

const HeaderAcceptance Header = "DOYOUACCEPT?"

DOYOACCEPT. Sent by sender after all the information about the transfered file has been sent. Receiving a packet with this header means that there will be no more additional information about the file and the sender is waiting for response (acceptance or rejection of the file). ie: |13|DOYOUACCEPT?~

const HeaderChecksum Header = "CHECKSUM"

CHECKSUM. Just like before, this header must be sent in a packet only by sender, BODY must contain a checksum of the transported file. ie: |74|CHECKSUM~1673f585148148d0c105af0d55646d6cbbf37e33a7366d3b72d8c5caca13434a

const HeaderDisconnecting Header = "BYE!"

BYE!. Packet with this header can be sent both by receiver and sender. It`s used when the sender or the receiver are going to disconnect and will not be able to communicate. (Usually it`s when the error has happened, OR, in a good situation, after the DONE header has been sent by sender, warning receiver that there is no data to send) The BODY is better to be empty. ie: |5|BYE!~

const HeaderDone Header = "DONE"

DONE. Sent by sender. Warns the receiver that the file transfer has been done and there is no more information to give. ie: |5|DONE~ Usually after the packet with this header has been sent, the receiver will send another packet back with header BYE!, telling that it`s going to disconnect

const HeaderEncryptionKey Header = "ENCRKEY"

ENCRKEY. The FIRST header to be sent. Sent immediately after the connection has been established by sender. Body contains randomly generated by sender aes encryption key. ie: |40|ENCRKEY~SUPER_SECURE_ENCRYPTION_KEY_YESS

const HeaderFileBytes Header = "FILEBYTES"

FILEBYTES. Sent only by sender. The packet`s body must contain a portion of transported file`s bytes. ie: |70|FILEBYTES~fj2pgfjek;hjg02yg082qyuhg83hvuahjvlhsaoughuihgp9earhguhergh\n

const HeaderFileSize Header = "FILESIZE"

FILESIZE. This header is sent only by sender. The packet with this header must contain a size of the transported file in its BODY. ie: |15|FILESIZE~512442

const HeaderFilename Header = "FILENAME"

FILENAME. This header is sent only by sender. The packet with this header must contain a name of the transported file in BODY. ie: |18|FILENAME~image.png

const HeaderReady Header = "READY"

READY. Sent by receiver when it hass read and processed the last FILEBYTES packet. The sender does not allowed to "spam" FILEBYTES packets without the permission of receiver. ie: |7|READY!~

const HeaderReject Header = "FILEREJECT"

FILEREJECT. Sent only by receiver if the user has decided to not download the file. The BODY may or may not be empty (preferably empty, of course), in any way, it will not be used in any way. ie: |11|FILEREJECT~

type Packet

type Packet struct {
	Header Header
	Body   []byte
}

Internal representation of packet before|after the transportation

func BytesToPacket

func BytesToPacket(packetbytes []byte) Packet

Converts packet bytes into Packet struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL