Skip to content

Instantly share code, notes, and snippets.

Created Aug 14, 2016
Embed
What would you like to do?
import socket
from bitstruct import pack, byteswap, calcsize
from binascii import hexlify
frame_header_format = 'u16u2u1u1u12u32'
frame_header_byteswap = '224'
frame_address_format = 'u64u48u6u1u1u8'
frame_address_byteswap = '8611'
protocol_header_format = 'u64u16u16'
protocol_header_byteswap = '822'
def send_packet(packet):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(packet, ("192.168.1.255", 56700))
def sizeof(format):
return int(calcsize(format) / 8)
def make_frame_header(size, tagged, source):
unswapped_header = pack(frame_header_format, size, 0, tagged, 1, 1024, source)
return byteswap(frame_header_byteswap, unswapped_header)
def make_frame_address(target, ack_required, res_required, sequence):
unswapped_header = pack(frame_address_format, target, 0, 0, ack_required, res_required, sequence)
return byteswap(frame_address_byteswap, unswapped_header)
def make_protocol_header(message_type):
unswapped_header = pack(protocol_header_format, 0, message_type, 0)
return byteswap(protocol_header_byteswap, unswapped_header)
def send_discovery():
packet_size = sizeof(frame_header_format + frame_address_format + protocol_header_format)
# CREATE THE HEADERS
frame_header = make_frame_header(packet_size, 1, 25061990)
frame_address = make_frame_address(0, 0, 1, 0)
protocol_header = make_protocol_header(2)
header = frame_header + frame_address + protocol_header
packet = header
send_packet(packet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment