Ejemplo n.º 1
0
func (dp DhcpPacket) GetTypeMessage() (option.MessageType, error) {
	opt53 := new(option.Option53MessageType)
	found, err := dp.GetOption(opt53)
	if err != nil {
		return option.UNKNOWN, err
	}
	if !found {
		return option.UNKNOWN, dherrors.Opt53NotFound
	}
	return opt53.GetMessageType(), nil
}
Ejemplo n.º 2
0
func (dp *DhcpPacket) ConstructWithPreAllocatedBuffer(buffer []byte, msgType option.MessageType) (err error) {
	if cap(buffer) < 1500 {
		buffer = make([]byte, 241, 1500)
	} else if len(buffer) != 241 {
		buffer = buffer[:241]
	}
	dp.Raw = buffer
	dp.SetHardwareType(byte(1))
	dp.SetHardwareLength(byte(6))
	dp.SetMagicCookie()
	err = dp.ResetAllOptions()
	if err != nil {
		return err
	}
	opt53 := new(option.Option53MessageType)
	opt53.Construct(msgType)
	dp.AddOption(opt53)

	switch msgType {
	case option.DHCPDISCOVER:
		fallthrough
	case option.DHCPREQUEST:
		fallthrough
	case option.DHCPDECLINE:
		fallthrough
	case option.DHCPRELEASE:
		dp.SetOpCode(OP_BOOTREQUEST)
		// break
	case option.DHCPOFFER:
		fallthrough
	case option.DHCPACK:
		fallthrough
	case option.DHCPNAK:
		dp.SetOpCode(OP_BOOTREPLY)
		// break
	}
	return nil
}
Ejemplo n.º 3
0
func fuzzParse(data []byte) (int, *DhcpPacket, option.SpecificOption) {
	var o option.SpecificOption
	p, err := Parse(data)
	if err != nil {
		if p != nil {
			panic("packet != nil on error")
		}
		return 0, nil, o
	}

	if p == nil {
		panic("packet == nil when no error")
	}

	if !p.ContainsMagicCookie() {
		panic("No magic Cookie...")
	}

	if p.GetMacAddr() == nil {
		panic("MacAddr is nil")
	}

	if len(p.GetMacAddr()) > 16 {
		panic("MacAddr is greater than 16")
	}

	{ // option 53
		opt := new(option.Option53MessageType)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if opt.GetLength() != 1 {
					panic("len opt53 != 1")
				}
				if len(opt.GetValue()) != int(opt.GetLength()) {
					panic("len opt53 inconsistency")
				}

				o = opt
			}
		}
	}

	{ // option 51 and all TypeUint32 option type (e.g. option 54, option 58, option 59 ...)
		opt := new(option.Option51IpAddressLeaseTime)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if opt.GetLength() != 4 {
					panic("len opt51 != 4")
				}
				if len(opt.GetValue()) != int(opt.GetLength()) {
					panic("len opt51 inconsistency")
				}

				o = opt
			}
		}
	}

	{ // option 82
		opt := new(option.Option82DhcpAgentOption)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if len(opt.GetValue()) != int(opt.GetLength()) {
					panic("len opt82 inconsistency")
				}

				{ // option 82.1
					ssOpt := new(option.Option82_1CircuitId)
					opt.GetSubOptions(ssOpt)
				}

				{ // option 82.2
					ssOpt := new(option.Option82_2RemoteId)
					opt.GetSubOptions(ssOpt)
				}

				{ // option 82.9
					ssOpt := new(option.Option82_9VendorSpecificInformation)
					_, _ = opt.GetSubOptions(ssOpt)
				}

				o = opt
			}
		}
	}

	{ // option 60 and all TypeString option type (option 15 ...)
		opt := new(option.Option60VendorClassIdentifier)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if len(opt.GetString()) != int(opt.GetLength()) {
					panic("len opt60 inconsistency")
				}
			}
		}
	}

	{ // option 6 and all TypeListUint32 option type (option 10, option 11 ...)
		opt := new(option.Option6DomainNameServerOption)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if len(opt.GetValue()) != int(opt.GetLength()) {
					panic("len opt6 inconsistency")
				}
			}
		}
	}

	{ // option 119
		opt := new(option.Option119DomainSearch)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				_ = opt.GetSearchString()
			}
		}
	}

	{ // option 120
		opt := new(option.Option120SipServers)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				switch opt.GetEnc() {
				case option.ENC_FQDN:
					fqdns, e := opt.GetFqdnList()
					if e != nil && fqdns != nil {
						panic("opt120 ENC_FQDN fqdns != nil on error")
					}
				case option.ENC_IP:
					addrs, e := opt.GetIpAddresses()
					if e != nil && addrs != nil {
						panic("opt120 ENC_IP addrs != nil on error")
					}
				default:
					panic("opt120 with ENC_UNKNOWN is not possible to have error == nil on p.GetOption(opt)")
				}
			}
		}
	}

	{ // option 119
		opt := new(option.Option119DomainSearch)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				_ = opt.GetSearchString()
			}
		}
	}

	{ // option 61
		opt := new(option.Option61ClientIdentifier)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				_ = opt.GetClientIdentifier()
			}
		}
	}

	{ // option 77
		opt := new(option.Option77UserClass)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				if len(opt.GetValue()) != int(opt.GetLength()) {
					panic("len opt77 inconsistency")
				}
				for _, uc := range opt.UserClassDatas {
					if len(uc.GetUcData()) != int(uc.GetUcLen()) {
						panic("opt77 UserClass len inconsistency")
					}
				}
			}
		}
	}

	{ // option 90
		opt := new(option.Option90Authentificiation)
		if f, e := p.GetOption(opt); e == nil {
			if f {
				opt.GetProtocol()
				opt.GetAlgorithm()
				opt.GetRdm()
				opt.GetReplayDetection()
				opt.GetAuthenticationInformation()
			}
		}
	}

	{ // option 121
		opt := new(option.Option121ClasslessStaticRoute)
		p.GetOption(opt)
	}

	return 1, p, o
}