in reply to How to create DNS packet

Can't tell for sure... but your problem might just be that @label and @labels are two different variables.  (Had you been using strictures, you'd probably have caught that typo yourself...)

#!/usr/bin/perl use strict; use warnings; my $hostname = "foo.bar"; my @labels; my $nlabels = 0; for (split /\./, $hostname) { push @labels, length, $_; $nlabels++; } print "\n@labels\n"; my $question = pack("(C a*)$nlabels C n2", @labels,0,1,1); print '$question: ', join(" ", unpack("(H2)*", $question) ), "\n"; # +hexdump # --> $question: 03 66 6f 6f 03 62 61 72 00 00 01 00 01 # f o o b a r