Openvario / sensord

Daemon to poll air data from Openvario sensorboard
6 stars 11 forks source link

incomplete sting being sent over socket #4

Closed bomilkar closed 4 years ago

bomilkar commented 4 years ago

In main() there are 3 places like this: send(sock, s, strlen(s), 0) with s holding the NMEA sentence to be sent. I think it would be better to send the complete string including the terminating '\0', which is part of the string (in C notation) but not counted by strlen(). I suggest to do this instead: send(sock, s, strlen(s)+1, 0) This would include the '\0' which was previously appended by sprintf().

The reason is not so esoteric as it sounds. There is no control over the number of sentences being sent in one package. This becomes obvious when observing the receiving end. The function recv() may very well receive multiple sentences in one chunk, even if each sentence was sent by an individual send(). It would be safer if the received data can be considered a concatenation of strings. It would also eliminate the need to mark the end of the sentence in any other way (e.g. '\n', '\r' ...).

linuxianer99 commented 4 years ago

Have you tried the fix with strlen(s)+1 ? Does it solve the problem ?

bomilkar commented 4 years ago

I have seen spurious chars on the receiving end with the string being incompletely sent. Does this surprise you?? Instead of running XCSoar you can run netcat to see what happens on port 4353.

linuxianer99 commented 4 years ago

Think this is indeed an error. As the docu of strlen() states that the ending character is not included. Please provide a pull request to fix this.

bomilkar commented 4 years ago

After a bit more digging into the code it's now clear that it must say send(sock, s, strlen(s)+1, 0) instead of send(sock, s, strlen(s), 0) . Run it with XCSoar, turn on "Setup->Logger->NMEA_logger" and look at the log files with the terminating '\0' being sent or without it. Understood??

linuxianer99 commented 4 years ago

You are meaning send(sock, s, strlen(s)+1, 0) instead of send(sock, s, strlen(s), 0) ??

bomilkar commented 4 years ago

Correct. Copy/Paste error. ;-) strlen(s) returns the length of the string w/o the terminating '\0'. But the terminating '\0' should be included in the outgoing sentence since the receiving end may think of it as a string, not being lead by the number of bytes received. The best what the receiving end may do is to think it's an incomplete sentence looking for the rest of it coming from the next recv().

linuxianer99 commented 4 years ago

OK, agree. PR #6 merged.