Getting audio file from a UDP packet

0

Posting this here out of desperation. Any help is appreciated. Thank you.

Backstory:

I am helping my friend with a device that he got from China. The device supposedly sends a audio file to my server using UDP.

File size sent from device to server: 0000cf6c (I don't know how to decode this)

Instructions given from manufacturer:

Hex is allowed to upload files.
explain :
1)  Recording file package transfer data format: package offset (unsigned short) + package data
If the platform receives the frame with data length of 4 and packet offset > = firmware size, packet transmission ends.

MCU send :

field            lenth(byte)                            explain 
Frame head           2                                    0x55aa
version              1                                     0x00
Command word         1                                     0x1E
Data length          2                                   0x0004+m
data          offset_addr(4byte)+pack(mbyte)         offset_addr: Package offset address, pack:Package content
校验和               1                                The result of summing 
                                                       bytes from the frame 
                                                       header is the result 
                                                       of summing 256 bytes.

example :
To record a file with a file size of 530byte, (the last packet of data can be unresponsive)
(1) The first packet has a packet offset of 0x00000000 and a packet length of 
     256. 0x55aa 00 1e 0104 00000000 xx…xx XX
(2) Second packet data, packet offset is 0x00000100, packet length is 256
0x55aa 00 1e 0104 00000100 xx…xx XX
(3)The third packet data, with a packet offset of 0x00000200 and a packet length of 18
0x55aa 00 1e 0016 00000200 xx…xx XX
(4) Last packet, packet offset is 0x00000212, packet length is 0
0x55aa 00 1e 0004 00000212 xx...xx XX

I ran wireshark and the device is actually sending a packet out. I just don't know how to access this packet or what I am supposed to do with it. The packet size is 911 Bytes according to wireshark and the file size is 0000cf6c according to the device.

Packet data (Raw):

55aa001e038400000000a9b6ad98d292325329d3c93224210300d677ee7ddffbfff7e6f73eef58f176a8512bb6766fa35d021449195ab679e905f34e8052226a201c6a17c0a2158264ef00004000000001004400a4240200ad92eff7de7bfdfff9ebc673379582af59481a441e88093724703b505552e15c94481ce3262da807c1021889740216fb236d9364ac24c6b6214629210200b673eeb7defbffefefe3c6e8408a0e875099024c280159a6d001230aad0051f5a2abece727ad697e7c9ea2956a01d2f6996c19ab2294a6e6922446420200be35ef7bdf77fff76cf935daa8de22de01bb18b884ac200dfe80cbd0ff8cb90dab8d02401d74448050072438100998ef19cd64b2299208cb6a2624330200a616de33dffbfff779f5ef6b002b715aa3c018ff2ae99ba8038823debd87bb012e4f603ff72e280d8e22b70d311d92f789d94ae34252c8d292c649120100b5d1ffb7de73bff745f38ee09a1ba212dcda802c2c04d622006a7881767043c5438e143917b1664b503ac024b521c2fe49b249b5c494cc6444a6498c0200b532ceb5defbbdf7cdf516ee02284886db34b1400e42e8b80028baf3c7669d66e66b2fbaa01505220080054d805369de2b954969928992e4984952c80200a534eeb9defbfff7cbf116003d15721613a2008a20aa20c85b5cf345f673790e476aa21cb4b46590058e0c514d0325f7cd64c9cc192294cca6c492210200bd16eefbdef7fff7bff7c50794dc05d41d18a0adad116e2840a822f0a9c138b9f658c6e8223835dcd2024ec02e6664f733cb923189986566a244322902009d14bff5de7bffeefec7ac31d483a6ab5b481a450688c2149627220fd53716bd336c9e3ab3266249c17d553f6480a8f799c949a92253994b332648240100a5f3efb3be7bbff7fee589674c0162bc8bf62470df1b2113a724624b0920f77e2d47133319832c8c5815d837444ba4fd86cdcc6c8889226646499b120100bef3de79dff7fdf768e95ef0a2d909f1534644efaf8b6d0022282b486667b2d55a9da580e336648bec9b1022f11ba6dba6a4696b4449119b929449490200bd35dffbde7bfdf7ebe18473447d29a2601fd12c45e2937290ad23622250321e7f2a89fb96be28513aa8004004dbc9de43ab335646494ca69059929002009635ee37def7fff73cf7ad31ccd3f9579198b8756d5c343816ae6ad1880f51392c9d0d97960e650c0f23026c468f63eda68d25d2a2649cb124a624490200ae32cef79efbffffa4f3e6e3a79d7ec54c
python
networking
udp
hex
wireshark
asked on Stack Overflow Nov 6, 2019 by user3702643

1 Answer

1

assuming you want some Python code to do this automatically, here's how I'd validate and decode the packet:

import struct

def decode_packet(packet):
    framehead, version, command, datalen = struct.unpack_from('!HBBH', packet)

    valid = (
        framehead == 0x55aa and
        version == 0x00 and
        command == 0x1e and
        len(packet) <= datalen + 11
    )
    if not valid:
        # ignore other protocols using this address/port
        print(
            '  header invalid',
            f'{framehead:04x} {version:02x} {command:02x} {datalen:04x}'
        )
        return

    if len(packet) < datalen + 11:
        print('  warning: packet was truncated')

    offset, = struct.unpack_from('!I', packet, 6)
    if datalen == 4:
        print(f'  end of data: file size={offset}')
        return

    data = packet[10:10+datalen]
    print(f'  got data: offset={offset} len={len(data)} hex(data)={data.hex()}')

    if len(packet) == datalen + 11:
        print(f'  hex(checksum)={packet[datalen + 10:].hex()}')

it obviously prints out a lot of stuff, but this is good to seeing if the device is actually following the documented protocol. it doesn't seem to be, as the +4 on the data length doesn't seem to be being applied. you can test this with:

decode_packet(bytes.fromhex('55aa001e038400000000a9b6ad98d2923...'))

assuming you can get this to function correctly, you can put this into some code that listens for packets on the correct port:

import socket

def server(portnum):
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        sock.bind(('', portnum))

        while True:
            packet, addr = sock.recvfrom(10240)
            print(f'received {len(packet)} bytes from {addr[0]}')
            decode_packet(packet)

again, doesn't do much. you'd want to write the data to a file rather than printing it out, but you can pull the offset out and you get a signal for when the data has finished transferring

answered on Stack Overflow Nov 6, 2019 by Sam Mason

User contributions licensed under CC BY-SA 3.0