import socket
#
# #orice nu e comentat a se intelege ca sunt niste setari care asa se cer facute
# dhcp_discover = {
#     'op': b'\x01', #message type = 1 => DHCPDISCOVER
#     'htype': b'\x01',
#     'hlen': b'\x06',
#     'hops': b'\x00',
#     'xid': b'\x37\xa5\x48\xa1', #transaction identifier generated by client : punem si noi un nr acolo
#     'secs': b'\x00\x00',
#     'flags': b'\x80\x00',
#     'ciaddr': b'\x00\x00\x00\x00',
#     'yiaddr': b'\x00\x00\x00\x00',
#     'siaddr': b'\x00\x00\x00\x00',
#     'giaddr': b'\x00\x00\x00\x00',
#     'chaddr': b'\x81\x25\xf5\x9f\xef\xac\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
#     'sname': b'\x00' * 64,
#     'file': b'\x00' * 128,
#     'magic cookie': b'\x63\x82\x53\x63',
#     'options': b'\x35\x01\x01' +  #option 53 : message type  = discover ; code = 35 (53 in decimal), length = 01 = 1 octet (adica 2 litere in hexa) , 01 e valoarea (dhcp discover)
#                b'\x3d\x07\x81\x25\xf5\x9f\xef\xac\x54' +  #optiunea 61 : Client Identifier : mostly the chaddr + alte numere;
#                b'\x32\x04\xc0\xa8\x00\x04' +  # option 50 : se cere o adresa ip specifica
#                b'\x37\x0c\x01\x0f\x03\x06\x02\x1c\x1f\x21\x79\xf9\x2b' +  #optiunea 55 parameter request = lista de coduri cu optiunile cerute de client, aici spre exemplu e 1,15,3,6,2,28....
#                b'\xff'  #endmark
# }
#


dhcp_discover = {
    'op': b'01', #message type = 1 => DHCPDISCOVER
    'htype': b'01',
    'hlen': b'06',
    'hops': b'00',
    'xid': b'37a548a1', #transaction identifier generated by client : punem si noi un nr acolo
    'secs': b'0000',
    'flags': b'8000',
    'ciaddr': b'00000000',
    'yiaddr': b'00000000',
    'siaddr': b'00000000',
    'giaddr': b'00000000',
    'chaddr': b'8125f59fefac00000000000000000000',
    'sname': b'00' * 64,
    'file': b'00' * 128,
    'magic cookie': b'63825363',
    'options': b'350101' +  #option 53 : message type  = discover ; code = 35 (53 in decimal), length = 01 = 1 octet (adica 2 litere in hexa) , 01 e valoarea (dhcp discover)
               b'3d078125f59fefac54' +  #optiunea 61 : Client Identifier : mostly the chaddr + alte numere;
               b'3204c0a80004' +  # option 50 : se cere o adresa ip specifica
               b'370c010f0306021c1f2179f92b' +  #optiunea 55 parameter request = lista de coduri cu optiunile cerute de client, aici spre exemplu e 1,15,3,6,2,28....
               b'ff'  #endmark
}


def dict_to_bytes(message):
    rez = b''
    for key, val in message.items():
        rez = rez + val
    return rez

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client_socket.bind(('0.0.0.0', 68))
print("\nC:About to send a discover message")

detrimis = dict_to_bytes(dhcp_discover)


client_socket.sendto(detrimis, ('255.255.255.255', 67))
print("\nDHCPDISCOVER sent")

data = client_socket.recvfrom(1024)

print("C: Received this thing : " + data[0].decode('utf-8'))

print("\nC:About to send a request message")

dhcp_request = {
    'op': b'01', #message type = 1 => DHCPDISCOVER
    'htype': b'01',
    'hlen': b'06',
    'hops': b'00',
    'xid': b'37a548a1', #transaction identifier generated by client : punem si noi un nr acolo
    'secs': b'0000',
    'flags': b'8000',
    'ciaddr': b'00000000',
    'yiaddr': b'00000000',
    'siaddr': b'00000000',
    'giaddr': b'00000000',
    'chaddr': b'8125f59fefac00000000000000000000',
    'sname': b'00' * 64,
    'file': b'00' * 128,
    'magic cookie': b'63825363',
    'options': b'350103' +  #option 53 : message type  = discover ; code = 35 (53 in decimal), length = 01 = 1 octet (adica 2 litere in hexa) , 01 e valoarea (dhcp discover)
               b'3d078125f59fefac54' +  #optiunea 61 : Client Identifier : mostly the chaddr + alte numere;
               b'3204c0a80004' +  # option 50 : se cere o adresa ip specifica
               b'370c010f0306021c1f2179f92b' +  #optiunea 55 parameter request = lista de coduri cu optiunile cerute de client, aici spre exemplu e 1,15,3,6,2,28....
               b'ff'  #endmark
}


client_socket.sendto(dict_to_bytes(dhcp_request), ('255.255.255.255', 67))
print("\nDHCPREQUEST sent")


