aiocoap icon indicating copy to clipboard operation
aiocoap copied to clipboard

Verbosity of aiocoap and dtls

Open hvanderlaan opened this issue 6 years ago • 15 comments

Hey,

A year or two ago I created a python2 application to connect with the Ikea Tradfri devices. This was done with libcoap. Now I'm rewriting this code to python3.7 and using aiocoap 0.4a1 installed via pip as stated on the document section.

When I'm running my code I've noticed that dtls in aiocoap is putting "extra" output in the terminal. For my commandline tools this is very annoying because dtls is outputting the extra information to standard error and standard output. For all my commandline tool is have to redirect stderr to /dev/null and grep -v the decrypt messages.

Is there a way to make this less verbose, or create a option like somefunc(......., debug=False/True).?

--- snip ---
decrypt_verify(): found 300 bytes cleartext
decrypt_verify(): found 24 bytes cleartext
decrypt_verify(): found 306 bytes cleartext
+--------+-------------------------------------+-------+
| dev id | name                                | state |
+--------+-------------------------------------+-------+
| 65550  | lamp 1                              | off   |
| 65541  | lamp 2                              | off   |
| 65546  | lamp 3                              | off   |
| 65539  | lamp 4                              | on    |
| 65540  | lamp 5                              | on    |
| 65545  | lamp 6                              | off   |
| 65538  | lamp 7                              | on    |
| 65537  | lamp 8                              | on    |
| 65542  | lamp 9                              | on    |
+--------+-------------------------------------+-------+
Error in sys.excepthook:

Original exception was:
Error in sys.excepthook:

hvanderlaan avatar Dec 06 '18 09:12 hvanderlaan

There is a way, but it requires some adaptions.

If you download DTLSSocket (https://pypi.org/project/DTLSSocket/#files) and make the following changes in the setup.py file:

define_macros=[('DTLSv12', '1'), ('WITH_SHA256', '1'), ('DTLS_CHECK_CONTENTTYPE', '1'), ('_GNU_SOURCE', '1'), ('NDEBUG', '1') ], # undef_macros = [ "NDEBUG" ], ),]

and then install DTLSSocket with $ python3 setup.py install, the extension will be compiled without the debug messages...

moroen avatar Dec 06 '18 20:12 moroen

There's nothing aiocoap can do about this -- unfortunately, DTLSSocket outputs some extra data (which, as a library, it shouldn't). Please file an issue there (but feel free to report back here if there is no response in a reasonable timeframe).

chrysn avatar Dec 06 '18 20:12 chrysn

I'll file an issue on the DTLSSocket repository. If there's no response, I'm willing to fork the module, make the required changes and try to maintain the the module for this project.

moroen avatar Dec 06 '18 22:12 moroen

Turns out filing an issue isn't trivial, it requires singning in, and I haven't found a way to register... Might be blind, though...

moroen avatar Dec 06 '18 22:12 moroen

On Thu, Dec 06, 2018 at 02:22:58PM -0800, moroen wrote:

it requires singning in, and I haven't found a way to register...

Oh, that's their university server -- I've dropped the author a mail pointing to this issue, that should be the easiest way forward.

chrysn avatar Dec 06 '18 22:12 chrysn

The easiest way would probably be to set the loglevel [1], if that does'n help i can disable the logging in the config. AFAIK aiocoap doesn't use the DTLSSocket abstraction wich by default sets the loglevel to LOG_EMERG [2]

I'm not sure if you can file bugs in our gitlab without a University account, but you should see my mail on commits.

[1] https://git.fslab.de/jkonra2m/tinydtls-cython/blob/master/DTLSSocket/dtls.pyx#L265 [2] https://git.fslab.de/jkonra2m/tinydtls-cython/blob/master/DTLSSocket/DTLSSocket.py#L15

kabel42 avatar Dec 07 '18 10:12 kabel42

Thanks already guys hopefully we can file an issue

hvanderlaan avatar Dec 07 '18 12:12 hvanderlaan

The decrypt_verify() messages are from tinydtls, and from the code, it looks like the only way to disable it, is by compiling with NDEBUG defined?

moroen avatar Dec 07 '18 12:12 moroen

setting the loglevel should work for all messages, otherwise you might want to file a bug against tinydtls here: https://git.eclipse.org/r/#/q/status%3Aopen+project%3Atinydtls/org.eclipse.tinydtls

kabel42 avatar Dec 07 '18 12:12 kabel42

It's also possible to disable the decrypt_verify message by a couple of small changes to the setup.py file for DTLSSocket:

define_macros=[('DTLSv12', '1'), ('WITH_SHA256', '1'), ('DTLS_CHECK_CONTENTTYPE', '1'), ('_GNU_SOURCE', '1'), ('NDEBUG', '1') ], and # undef_macros = [ "NDEBUG" ], ),]

moroen avatar Dec 07 '18 12:12 moroen

ok, i've had some time to look at the code and the problem seems to be this line: https://git.eclipse.org/c/tinydtls/org.eclipse.tinydtls.git/tree/dtls.c#n3035

the "decrypt_verify(): found %i bytes cleartext\n" is output using printf not the logsystem.

i'll change the setup.py but we should notify upstream

[edit] fixed version should now be on pypi [/edit]

kabel42 avatar Dec 07 '18 18:12 kabel42

Just installed latest from pypi, and confirmed that the decrypt_verify message is gone!

Thanks!

moroen avatar Dec 07 '18 23:12 moroen

Thanks @kabel42 for fixing this!

Concerning the setLogLevel function (yes, aiocoap uses dtls.DTLS directly) -- and thus the logging: Is this aiocoap could in any form capture to ensure that it pipes any DTLS warnings into its own log, or is setting setLogLevel(DTLS_LOG_EMERG) actually best practice when using the library?

[edit: clarified between DTLSSocket and the library as a whole]

chrysn avatar Dec 07 '18 23:12 chrysn

the underlying tinydtls is using printf for logging (when not running on contiki), i don't think there is an easy was to redirect that to a python buffer without modifying tinydtls. If you don't want the library to pollute your output yout want to set the loglevel to at least DTLS_LOG_CRIT.

PS. If anyone wants to make a PR, the logging happens in this funktion: https://git.eclipse.org/c/tinydtls/org.eclipse.tinydtls.git/tree/dtls_debug.c#n209

kabel42 avatar Dec 08 '18 09:12 kabel42

How to get rid of unwanted tinydtls messages:

When you download curl, you then download tinydtls separate and into the libcurl/ext directory.

When you're done downloading tinydtls into: libcurl/ext/tinydtls, do the following:

:libcurl/ext/tinydtsl$> configure --without-debug

Then return to the libcurl directory and make libcurl as you normally would. It'll make the tinydtls system, too, but will remove debug messages.

bwilcutt avatar May 03 '20 18:05 bwilcutt