bjeanes / go-lifx

Go implementation of the LIFX bulb protocol, including a command line client, a client library, and a debug-oriented traffic snooper
18 stars 8 forks source link

Something weird is definitely happening with datagram sizes #2

Closed bjeanes closed 10 years ago

bjeanes commented 10 years ago

lifx-snoop output:


DATA: 58 00 00 14 00 00 00 00 d0 73 d5 00 00 00 00 00 
      d0 73 d5 00 00 00 00 00 00 00 00 00 00 00 00 00 
      6b 00 00 00 43 44 f2 a8 ff 
MSG:  LIFXMessage(lightState){Color:{Hue:17475 Saturation:43250
                                     Brightness:65535 Kelvin:3500} 
                              Dim:0 Power:65535 Label:emu Tags:0}

DATA: 58 00 00 14 00 00 00 00 d0 73 d5 00 00 00 00 00 
      d0 73 d5 00 00 00 00 00 00 00 00 00 00 00 00 00 
      6b 00 00 00 43 44 f2 a8 ff ff ac 0d 00 00 ff ff 
      65 6d 75 00 00 00 00 00 00 00 00 00 00 00 00 00 
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
      00 00 00 00 00 00 00 00 
MSG:  LIFXMessage(lightState){Color:{Hue:17475 Saturation:43250 
                                     Brightness:65535 Kelvin:3500}
                              Dim:0 Power:65535 Label:emu Tags:0}

Same message type (lightState), same declared message size (0x58 0x00: little endian uint16 -> 0x58 -> 88). The second datagram has the correct 88 bytes, but the first only has 41.

I think we're reading incomplete UDP packets but still parsing them. I assumed that this code would avoid this, but clearly not:

if reader.Len() != binary.Size(payload) {
    return message{}, errors.New(fmt.Sprintf("Unexpected payload size for %T", payload))
}
bjeanes commented 10 years ago

This time I got what I expected (from this error handler):

DATA: length=41 (0029)
      58 00 00 54 00 00 00 00 d0 73 d5 00 14 cd 00 00 
      4c 49 46 58 56 32 00 00 00 00 00 00 00 00 00 00 
      6b 00 00 00 00 00 00 00 ee 
ERR:  Incorrect message size (data: 41, header: 88)

I wonder if lifx-snoop is mismatching the bytes with the message. It's possible since the error sender can drop errors if the channel is blocked

bjeanes commented 10 years ago

Yup, they're getting mismatched:

DATA: length=88
      000  58 00 00 14 00 00 00 00  d0 73 d5 00 00 00 00 00  |X........s......|
      010  d0 73 d5 00 00 00 00 00  00 00 00 00 00 00 00 00  |.s..............|
      020  6b 00 00 00 43 44 f2 a8  ff ff ac 0d 00 00 ff ff  |k...CD..........|
      030  65 6d 75 00 00 00 00 00  00 00 00 00 00 00 00 00  |emu.............|
      040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
      050  00 00 00 00 00 00 00 00                           |........|
MSG:  LIFXMessage(lightState){Color:{Hue:17475 Saturation:43250 Brightness:65535 Kelvin:3500} Dim:0 Power:65535 Label:emu Tags:0}

DATA: length=88
      000  58 00 00 14 00 00 00 00  d0 73 d5 00 00 00 00 00  |X........s......|
      010  d0 73 d5 00 00 00 00 00  00 00 00 00 00 00 00 00  |.s..............|
      020  6b 00 00 00 43 44 f2 a8  ff ff ac 0d 00 00 ff ff  |k...CD..........|
      030  65 6d 75 00 00 00 00 00  00 00 00 00 00 00 00 00  |emu.............|
      040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
      050  00 00 00 00 00 00 00 00                           |........|
MSG:  LIFXMessage(lightState){Color:{Hue:0 Saturation:0 Brightness:28142 Kelvin:3700} Dim:0 Power:65535 Label:📈 Tags:1}

Note the emu (the label for an emulator bulb) in both hex dumps, but the second decoded message has the label for one of my other bulbs (the emoji 📈).

Damn.