#!/usr/bin/python


from pydhcplib import dhcp_constants
from pydhcplib import dhcp_packet
from pydhcplib import dhcp_network

from pydhcplib import type_hw_addr
from pydhcplib import type_ipv4
from pydhcplib import type_strlist

import sys

from optparse import OptionParser


parser = OptionParser()

""" Action options """
parser.add_option("-L", "--listen", action="store_true",dest="listen", help="",default=False)
parser.add_option("-E", "--emit",  action="store_true",dest="emit", help="",  default=False)
parser.add_option("-R", "--readable-conversion",  action="store_true",dest="readable", help="", default=False)
parser.add_option("-B", "--binary-conversion",  action="store_true",dest="binary", help="", default=False)
parser.add_option("-s", "--source-file", action="store",dest="source", help="", default=False, type="string")
parser.add_option("-d", "--destination-file", action="store",dest="destination", help="", default=False, type="string")
parser.add_option("-p", "--port", action="store",dest="port", help="", default="67", type="int")
parser.add_option("-a", "--address", action="store",dest="address", help="", default="0.0.0.0", type="string")
parser.add_option("-r", "--raw", action="store",dest="raw", help="", default=False,type="string")
parser.add_option("-n", "--number", action="store",dest="number", help="", default="0", type="int")



(options, args) = parser.parse_args()

print options



def main() :
    ActionSum = 0
    for Action in (options.listen,options.emit,options.readable,options.binary) :
        if Action == True : ActionSum += 1
    if ActionSum > 1 :
        print "Command line error : [-L -E -R -B] Only one of these actions can be taken."
        sys.exit(0)

    if options.readable == True : r_conversion()

    if options.binary == True : b_conversion()

    if options.listen == True :
        if options.raw == False:
            listen_address(options.address,options.port,int(options.number))
        else  :
            print "Listen RAW : ",options.raw
            listen_packet(options.raw,options.number)

    if options.emit == True : emit(options.address,options.port)


def listen_address(address,port,number) :
    listener = dhcp_network.DhcpClient(address,port,port)

    if  (number == 0 ) :
        while True :
            packet = listener.GetNextDhcpPacket()
            packet.PrintHeaders()
            packet.PrintOptions()

    else :
        while number > 0 :
            packet = listener.GetNextDhcpPacket()
            packet.PrintHeaders()
            packet.Print()

            number -= 1

def listen_packet(interface,number) :
    listener = dhcp_network.DhcpRawClient(mysocket)

    if  (number == 0 ) :
        while True :
            packet = dhcp_packet.DhcpPacket()
            
            packet.DecodePacket(listener.Receive(1024))
            packet.PrintHeaders()
            packet.PrintOptions()

    else :
        while number > 0 :
            packet = dhcp_packet.DhcpPacket()
            
            packet.DecodePacket(listener.Receive(1024))
            packet.PrintHeaders()
            packet.PrintOptions()

            number -= 1
    

def emit(address,port) :
    pass

def r_conversion() :
    rawdata = sys.stdin.read()
    while ( len(rawdata)>0 ) :
        readdata = dhcp_packet.DhcpPacket()
        readdata.DecodePacket(rawdata)
        readdata.PrintHeaders()
        readdata.PrintOptions()
        rawdata = sys.stdin.read()

def b_conversion() :
    """
    pythondata =  sys.stdin.read()
    while ( len(pythondata)>0 ) :
        data = dhcp_packet.DhcpPacket()
        data.DecodePacket(rawdata)
        
        pythondata = sys.stdin.read()
    """

main()
