[fpc-pascal] netdb, DNS and TCP

Noel Duffy noelduffy at xtra.co.nz
Sun Oct 11 06:30:09 CEST 2020


Hi all,

To keep track of the work I've been doing on DNS in netdb, I created bug #37906 in Mantis.

https://bugs.freepascal.org/view.php?id=37906

I've attached to a note on that bug report a proof-of-concept version of netdb.pp and a small program that uses it to make text queries over TCP. This version of netdb has different record types for TCP and UDP (technically, it has three, one for UDP queries and replies, one for TCP queries, one for TCP replies). The wire protocol for DNS over TCP has a mandatory two-octet length field at the start for both queries and replies, and I didn't want to have to use a full-length 64k buffer for both when a smaller 512 byte buffer suffices.

Overall I'm not really enthused with this implementation. Having separate record types for the different payload types means having separate versions of a number of helper functions as well. Functions like BuildPayload, NextRR, and SkipAnsQueries.

To avoid that duplication, the code would need to use a dynamic buffer and fill it just before socket writing. The TCP variant would include its length field in the buffer. Additionally, after reading the response from the socket, the buffer contents would have to be copied into a record data structure. It's a tradeoff between execution efficiency and simplicity.

On a related point, does anyone have any tips for testing low-level network functions? I'm not sure if there's an easy way, perhaps with add-on test libraries, to simulate things like network errors, packet loss, and the like. I'm currently thinking the only way to do it would be to create mocked versions of the fpsend and fprecv calls. I know about tc netem on linux, but something that can be used inside unit tests would be ideal.



More information about the fpc-pascal mailing list