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:

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).

SNAC format description

A SNAC is the basic communication unit that is exchanged between clients and servers.
The SNAC communication layers sits on top of the FLAP layer. SNAC is the normal contents
of the FLAP Data Field for channel 0x02. SNACs are only sent over channel 0x02. Data sent
across other channels are not considered complete SNACs. There can be only one SNAC per
FLAP frame. Here is the format of SNAC:

There is no formal declaration of the length of the SNAC data portion (that information
must be assumed from the FLAP headers). Families, identified by the "family ID", constitute
a group of services. Subtypes are a subdivision of the families. Each subtype ID is different
depending on the specific service or information provided in the data section.

Request IDs are 32bit values used to identify non-atomic information. The client can generate
completely random reqid's as long as it remembers what the request was for. Often, though,
the results of the SNAC are irrelevant, and the reqid's can be forgotten. But, in
information-requestion SNACs, it is imperative you remember the reqid you sent because that's
the only way to link it to the response! If this is not done, it will be impossible to have
more than one pending request of the same SNAC subtype (which is unlikely at best). For
server-initiated SNACs, the reqid most significant bit=1, and this num count up to zero from
than from zero.

Flags is a general SNAC properties. There is not enough information about snac flags, but known
that if bit1 of flags=1 there are more SNACs for this request-id was sent. Bit16=1 mean that
SNAC contain some unknown information at the beginning (first come a length of additional data
(word) and then data itself).

TLV (Type-Length-Value) tuple description

TLVs are a very convenient and efficient method of putting data into an organized
format, especially variable length strings, etc. TLV literally stands for "Type,
Length, Value". And that's exactly what it is: a 16bit Type code, a 16bit value
for the length of the Value field, and then the actual data in the Value field
(variable length). Here is TLV format:

xx xx

word

TLV type number

xx xx

word

TLV length value

......

TLV data

TLVs can be be in SNACs, but that's not required. TLVs often are used directly
in the FLAP Data Field, but normally are inside of SNACs. More than one
TLV of each Type code may exist in a single FLAP command (SNAC or not). TLVs
must follow the strict tuple-rule, or they're really not TLVs, they're raw
data. One tlv may contain nested tlv chain inside.

TLVs are a big win. They make sending a variable length string like, e.g.,
"afritz@iname" as simple as defining a TLV with values {0x0011, 0x000c,
"afritz@iname.com"}. (The type 0x0011 is used throughout the authorization
process as the "email address type".) A side note about strings: strings in
this protocol are never NULL-terminated. If they look like they are, that's
probably a word-length value behind it.