Protocols/OSCAR/FLAP

From NINA Wiki
Revision as of 10:34, 14 February 2021 by AD (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
OSCAR Protocol
IntroductionTermsClients
Basic
DatatypesFLAPSNACTLV
UUIDsErrorsTool IDs
Host Interaction
Rate LimitsMigrationMessages
Other Services
ADMINADVERTALERT
BARTBOSBUCPCHAT
CHAT_NAV
Tutorials
Sign OnBARTRendezvous
ICBMLocateBuddies
Foodgroups
OSERVICE (0x0001)
LOCATE (0x0002)
BUDDY (0x0003)
ICBM (0x0004)
ADVERT (0x0005)
INVITE (0x0006)
ADMIN (0x0007)
POPUP (0x0008)
PD (0x0009)
USER_LOOKUP (0x000A)
STATS (0x000B)
TRANSLATE (0x000C)
CHAT_NAV (0x000D)
CHAT (0x000E)
ODIR (0x000F)
BART (0x0010)
FEEDBAG (0x0013)
ICQ (0x0015)
BUCP (0x0017)
ALERT (0x0018)
PLUGIN (0x0022)
UNNAMED_FG_24 (0x0024)
MDIR (0x0025)
ARS (0x044A)


FLAP (Frame Layer Protocol) provides the packet framing on top of TCP or TLS layer for the OSCAR protocol. Each message sent to and from the AIM backend is encapsulated in a FLAP frame that is easily identified using a 6 byte header followed by a variable length data segment. The payload of a FLAP frame is in most cases a SNAC.

FLAP Header

The header contains the frame type, a sequence number, and the length of the following data segment. The sequence number is independently sequential in each direction. Packets from the server to client have one sequence number, while the packets from the client to server have a different independently increasing number.

If the server receives a sequence number out of order it will terminate the connection. A common mistake is to use a uint8 (byte) to represent the sequence number, which will roll over at 255 and cause the server to disconnect the client.

Name Type Notes
FLAP__MARKER uint8 (byte) ASTERISK (literal ASCII "*")
FLAP__FRAME uint8 (byte) Class: FLAP__FRAME_TYPE
FLAP__SEQUENCE uint16 (word) Initialized to a random value, increments for each send
FLAP__LENGTH uint16 (word) Length of data, does not include the 6 byte header length

Class: FLAP__FRAME

There are several different frame types, with the most common being the DATA frame.

Name Value Notes
FLAP__FRAME_SIGNON 0x01 Initialize the FLAP connection
FLAP__FRAME_DATA 0x02 Messages using the FLAP connection, usually a SNAC message
FLAP__FRAME_ERROR 0x03 A FLAP error - rare
FLAP__FRAME_SIGNOFF 0x04 Close down the FLAP connection gracefully
FLAP__FRAME_KEEP_ALIVE 0x05 Send a heartbeat to server to help keep connection open

FLAP__FRAME_SIGNON

On connection, the server and client each send a FLAP SIGNON frame to initialize the connection. The sequence number field should be initialized with a random value. The data portion of the frame contains a 4 byte version number, currently always 1, followed by any TLVs that are required.

Name Type Notes
header FLAP Header FLAP header, type will be FLAP__FRAME_SIGNON
flapVersion uint32 (dword) Always 1
tlvs Array of TLV length payloadLength-4 [Class: FLAP__SIGNON_TAGS] TLVs

Example in hex: 2a011234000400000001

FLAP__FRAME_DATA

A data frame will always just contain a single SNAC.

Name Type Notes
header FLAP Header FLAP header, type will be FLAP__FRAME_KEEP_DATA
snacHeader SNAC Header SNAC header
snac blob snac described by the SNAC header

FLAP__FRAME_ERROR

Name Type Notes
header FLAP Header FLAP header, type will be FLAP__FRAME_ERROR

FLAP__FRAME_SIGNOFF

This FLAP frame type is sent both to the client and server.

  • Sent to the client to tell it that it is getting bumped off gracefully. The client should NOT try to auto reconnect if it receives one of these messages.
  • Sent to the server by the client to tell it that it is going away gracefully.
Name Type Notes
header FLAP Header FLAP header, type will be FLAP__FRAME_SIGNOFF

FLAP__FRAME_KEEP_ALIVE

Many modern operating systems and networks come with firewalls and packet inspectors to protect the user. Unfortunately, these services and devices have proven to be buggy with long lived connections. We recommend that a client sends a FLAP KEEP_ALIVE packet to the server every minute after the connection has gone idle to help keep the connection alive. Please do not send it more often then once a minute or when the connection is not idle. There is no response to this message and a client will never receive a KEEP_ALIVE packet from the server.

Name Type Notes
header FLAP Header FLAP header, type will be FLAP__FRAME_KEEP_ALIVE

From Aleksandr Shutko: Basic OSCAR information: FLAP

 FLAP transport (version 1.0) 
 

     FLAP is a low-level communications protocol that facilitates the development of higher-level, datagram-oriented, communications layers. It is used on the TCP connection between all clients and servers. Here is format of FLAP datagram:

 2A   byte   FLAP id byte
 xx   byte   FLAP channel
 xx xx   word   FLAP datagram seq number
 xx xx   word   FLAP data size
 ...... FLAP data


     FLAP id byte is always 0x2A. It is frame-start sign.

     The flap sequence numbers used for errors detection. So server can detect problem when client set flap data size field = 10 and write 20 bytes 0x2A as data. The flap sequence number origins are picked quite randomly. There is no connection between the sequence number set from the server and the set from the client. Sequence numbers are always incremented upward (towards 0x8000) for each command sent. If the sequence number does reach 0x8000, it will wrap to 0x0000, for obvious reasons. If you start a new connection, it is recommended that a new sequence number origin is picked for that connection, for purposes of internal coherency. Sequence numbers are independent of channels: there's a single series of sequence numbers per TCP connection (per socket).

     Channels are the method used to multiplex separate paths of communication across the same TCP socket. These are analogous to TCP/UDP port numbers. Five channels are currently used by OSCAR:

  • 0x01 - New Connection Negotiation
  • 0x02 - SNAC data
  • 0x03 - FLAP-level Error
  • 0x04 - Close Connection Negotiation
  • 0x05 - Keep alive

     After a new connection (socket) is set up using channel 0x01, data should only be carried on channel 0x02, until a low-level FLAP error occurs (channel 0x03) or there is planned termination, which gets "negotiated" (on channel 0x04). Most live events processed during the lifespan of the client are done over channel 0x02. SNACs are never transmitted on any channel other than 0x02

     The best way to read an incoming FLAP command is to first read only the starting 6 bytes (the FLAP headers). From these 6bytes, you can determine how many more bytes you need to read to complete the command, and how much memory you need to allocate to store it. Never read more or less than the number of bytes specified in the FLAP headers, or your read will result in a truncated or uninterpretable command. (If you read too much, you will probably end up reading the start of the next command, which is bad. Lost data is unacceptable in the AIM standard.)

     Because every command must follow FLAP guidelines, I'd recommend using a low-level routine to add the FLAP headers (normally, this will be the "flush transmit queue" routine, so that addition of sequence numbers and the rest of the FLAP headers is done as close timewise as possible to the command being put on the wire). This is the best way to prevent out-of-order seqnums from getting used (which, as stated earlier, is quite fatal).