FSM – Arivi Network Layer

Finite State Machine – Arivi Network Layer

In order for a node to securely communicate service data with other nodes in a P2P network, an underlying secure network connection (tunnel) is necessary (over TCP/UDP). This involves establishing a secure connection by performing key exchange handshake to derive a shared secret key (ssk) and subsequently employing the ssk for encrypting (symmetric cryptography) the payload. Also mechanisms for keep-alive and graceful-disconnection would be needed. A connection is stateful, and state machine represents the precise nature of the state transitions for the various events that it may encounter. If we are in state S and the event E occurs, we should perform the actions A and make a transition to the state S'. At any instant, a connection can be in one of the states Sn, and it encounters events originating either from the application (higher layer) or from the network (TCP/UDP servers). The action performed on this even could translate into a new network event or service event along with the associated state transition.


Arivi network layer has 5 states which are listed below:

IDLE state

This state indicates the existence of a connection instance and not the connection itself has it not yet created ( i.e no peer is connected using the current instance which is in IDLE state ) node is waiting for higher or lower layer to either start a event for a handshake initiation or to accept a handshake request by other peers.


This state is only valid in the perspective of initiator ( i.e a node which starts a handshake process). It is set with a timer called Handshake init timer of about 30 seconds to receive a handshake response parcel from the recipient. In the event that response is not received before timer expires then state will change to terminated. Note: Connection is not yet formed between peers when they are either in IDLE or INVITED state.


A connection is said to be completely formed when it gets associated with ESTABLISHED state either when node’s handshake init request was accepted by a peer node to form a secure connection or when it itself accepts handshake request of a peer to do the same. Established state is mainly concerned with exchange of data packets hence whenever a data exchange takes place data message timer is set which expires in 60 seconds if there is no response from the connected peer in this time frame then state will be changed to PINGED state.

PINGED state

If a node’s connection is in pinged state then it will send ping parcels to its peer with timer set to 30 seconds, if the peer replies with the data parcel or pong parcel then connection will go back to ESTABLISHED state but in the event peer fails to reply with either data or pong parcel in under 30 seconds connection will be marked stale and move to TERMINATED state.


If this state is encountered then we assume connection was destroyed and we clear up the socket.


Event drives the state change in the connection instance when paired with states each pair results in different outcome. Event is written to a queue called eventQueue. Each connection will have a individual eventQueue and there are about 10 possible events which will result in state change, which are:
  1. Handshake initiation event HS_INIT_EVENT
  2. Key exchange initiation event Key_Ex_INIT_EVENT
  3. Key exchange response event Key_Ex_RESP_EVENT
  4. Send data event Send_DATA_EVENT
  5. Receive data event Recv_DATA_EVENT
  6. Ping data event Ping_EVENT
  7. Pong data event Pong_EVENT
  8. Clean up event Clean_Up_EVENT
  9. Terminate connection event Terminate_EVENT
  10. Timeout event TimeOut_EVENT : {HandS, DataEx, PING}
Keywords : (i) represents initiator & (r) represents recipient

When a connection instance is in a IDLE state it will be waiting for a event to be written into a eventQueue. There are only two possible events which can drive a valid state change if the current state is IDLE. first is Handshake Initiation Event which occurs in initiator node and second is Key Exchange Initiation Event which is with respect to a recipient node.


This state and event pair creates handshake initiation parcel and sends it to the required peer node while driving a state change to INVITED from IDLE this pair of state and event classifies the node as a Initiator.


This state and event pair occurs when a node in IDLE receives a handshake initiation request from a peer node (i.e a initiator) in turn classifying the parent node as a recipient. If the request is not discarded by the node then it will send a parcel accepting the handshake response to the initiator while also driving a state change to ESTABLISHED from IDLE.

The INVITED state can only be entered by a initiator after sending handshake initiation request. When initiator sends the request it sets a timer for 30 seconds known as Handshake Timer.


In the event that the request is received before the handshake timer runs out then node will drive a state change to ESTABLISHED from INVITED.


If the initiator does not get a response for handshake request back before timer runs out then state change to Terminated from INVITED will take place.

ESTABLISHED state is where node exchange data using a secure connection. Node after sending or receiving the data will start a timer set to 60 seconds which is known as a Data exchange timer.


This state and event pair notifies the node that upper layer wants to send data to the connected peer node. Data for it will be read from a another Queue called dataQueue assigned for each connection. After sending the data node the data exchange timer resets.


If node does not receive any data from the peer node before the timer expires then node will drive a state change to PINGED from ESTABLISHED.


This state and event pair is executed when there is data to be read and processed after this event is completed data exchange timer is reset.


When a node receives a ‘bye’ parcel i.e peer node requesting to break the connection. Then this state and event pair is executed later making the node to drive a state change to TERMINATED from ESTABLISHED.

PINGED state is entered by any connection instance from ESTABLISHED state when the data exchange timer expires.


As soon as node enters a PINGED state it sends a ping parcel to the peer to check connectivity between them and sets a timer of 30 seconds known as Ping timer.


When a node in PINGED state receives a pong parcel from peer node before the ping timer expires the state changes to ESTABLISHED from PINGED and data exchange timer is reset.


This event is similar to PONG_EVENT but here the data is returned back which needs additional processing with the change in state.


In the case of peer fails to reply before ping timer expires the state is changed to TERMINATED from PINGED.


Whenever a instance enters the TERMINATED state it destroys the newly created socket for connection and kills the FSM thread irrespective of event or state it came from.