#!/bin/false
# PODNAME: Net::DHCP::Packet::OrderOptions
# ABSTRACT: Option ordering quirks for Net::DHCP
use strict;
use warnings FATAL => 'uninitialized';
use 5.8.0;

package Net::DHCP::Packet::OrderOptions;
$Net::DHCP::Packet::OrderOptions::VERSION = '0.9';
use Exporter 'import';
our @EXPORT_OK = qw( reorder_options );
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

use Net::DHCP::Constants qw(
    DHO_VENDOR_CLASS_IDENTIFIER
    DHO_VENDOR_ENCAPSULATED_OPTIONS
    DHO_DHCP_AGENT_OPTIONS
);

#=======================================================================
sub reorder_options {
    my @codes = @_;

    # Quirk: Intel PXE wants option 60 before option 43
    my ( $i60, $i43 );
    for ( my $i = 0; $i < @codes; $i++ ) {
        $i60 = $i if $codes[$i] == DHO_VENDOR_CLASS_IDENTIFIER();
        $i43 = $i if $codes[$i] == DHO_VENDOR_ENCAPSULATED_OPTIONS();
    }
    if ( defined $i60 && defined $i43 && $i60 > $i43 ) {
        @codes[ $i60, $i43 ] = @codes[ $i43, $i60 ];
    }

    # Quirk: Cablelabs want option 82 at the very end
    my @no82 = grep { $_ != DHO_DHCP_AGENT_OPTIONS() } @codes;
    @codes = ( @no82, DHO_DHCP_AGENT_OPTIONS() ) if @no82 != @codes;

    return @codes
}

#=======================================================================

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Net::DHCP::Packet::OrderOptions - Option ordering quirks for Net::DHCP

=head1 VERSION

version 0.9

=head1 SYNOPSIS

    use Net::DHCP::Packet::OrderOptions qw( reorder_options );

=head1 DESCRIPTION

Applies known option-ordering quirks to a list of option codes.
DHCP clients can be picky about option order; this module collects
those work-arounds in one place.

=head1 QUIRK WORK-AROUNDS

=over 4

=item Intel PXE

Wants option 60 (Vendor class identifier) before option 43 (Vendor-specific
options).

=item Cablelabs

Wants option 82 (Relay agent information) to always be last.

=back

=head1 METHODS

=over 4

=item reorder_options( LIST )

Accepts a list of option-code numbers, returns them reordered to
satisfy known client quirks.  Codes not mentioned in any quirk keep
their relative order.

=back

=head1 SEE ALSO

L<Net::DHCP>, L<Net::DHCP::Packet>, L<Net::DHCP::Constants>.

=head1 AUTHOR

Dean Hamstead <dean@fragfest.com.au>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Dean Hamstead.

This is free software, licensed under:

  The MIT (X11) License

=cut
