fix: Add CheckSum verification

Perform CheckSum validity verification when a FIX message is received:

* BodyLength and CheckSum tags' values are consistent i.e. BodyLength's
  value points to the CheckSum tag.

* CheckSum value is valid i.e. it is the sum (modulo 256) of every byte
  of the message up to but not including the checksum itself.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
[ penberg@kernel.org: cleanups ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
Use -Werror GCC option

Enable -Werror GCC option to keep libtrading compilation clean.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: fix_session_recv() shouldn't check socket if there are data in the RX buffer

Prevent fix_session_recv() from being blocked when waiting data from the
socket. Check whether there are data in the RX buffer. If so, try to
parse the buffer first.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
[ penberg@kernel.org: cleanups ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Code cleanups to fix_session_recv()

 - Fix local variable declaration ordering
 - Fix formatting glitches

Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Fix a bug - RX buffer must not be freed

There is a single RX buffer per FIX session which shouldn't
be freed. Any attempts to delete it during active FIX session
are erroneous and will lead to crash

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Fix partially received message parsing

Because we do not parse incoming FIX messages on the fly we are exposed
to the following problem: we have to distinguish between the cases when
RX buffer contains partially delivered message and when the buffer
contains a garbled FIX message. In the former case we have to wait until
the missing part is delivered and parse the whole message. In the latter
case we should consider the message to be garbled and skip it. Currently
we skip the data in both cases.

The proposed solution based on the assumption that there is a maximum
message size. So if the number of bytes to be parsed is greater than
that value we are not in the case of the message that has been partially
delivered.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
[ penberg@kernel.org: cleanups ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Keep the client alive until SIGINT is sent

Keep the FIX client alive until the user sends SIGINT which makes it
possible to test heartbeats, for example.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Stop relying on SIGINT for automated test

Commit f166cce ("fix: Keep the client alive until SIGINT is sent") causes make
check to fail as follows after 10 or so iterations:

  Server is listening to port 9000 using 'fix' protocol...
  Logon OK
  Logout FAILED
  make: *** [check] Error 1
  FAILURE

Relying on sleep timing is inherently broken for automated tests. Add a new
command line option '-i' to FIX test client that causes the client to send a
logout message immediately.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Use permanent buffers for message transmission

Each time a FIX message is transmitted a pair TX buffers are allocated.
This could be easily avoided by introducing a permanent pair of per
FIX session buffers which are to be reused if needed.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
[ penberg@kernel.org: cleanups ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Process resend requests

If a Resend Request message is received a single Sequence Reset
(Gap Fill mode) is sent back to cover the whole range of missed
messages.

It is quite reasonable to assume that one should use a reliable
transport protocol e.g. TCP/IP. In such a case there is a much
more powerful engine is responsible for data integrity than
simple FIX messages' sequence numbers. Moreover, we are getting
it just for free. Hence, Resend Request indicates that there is
a dramatic error on either side of communication link and FIX
connection should be closed immediately.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Track incoming message sequence numbers

If an incoming message's sequence number does not equal to the
expected incoming sequence number then FIX logout is initiated.
This approach is justified for the reliable network transport
protocols e.g. TCP/IP which ensure data integrity.

fix_session_resend_request() is presented for compatibility
purposes and is not used actually. But if the aforementioned
logic if changed, one will initiate Resend Request procedures.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
fix: Use permanent buffer for message receipt

At the moment, each time the incoming data are unparsed
a new struct fix_message is allocated which is rather
wasteful because of numerous alloc/free operations.

This patch suggests using a permanent per-session pointer
pointing to the most recently unparsed FIX message. If
parsing was successful then this pointer is to be used
for further analysis.

Signed-off-by: Marat Stanichenko <mstanichenko@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>