xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/testsock.pl (revision 8aaca124c0ad52af9550477f296b63debc7b4c98)
1#!/usr/bin/perl
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14# Test whether the interfaces on 10.53.0.* are up.
15
16require 5.001;
17
18use Cwd 'abs_path';
19use File::Basename;
20use Socket;
21use Getopt::Long;
22
23my $port = 0;
24my $id = 0;
25GetOptions("p=i" => \$port,
26           "i=i" => \$id);
27
28my @ids;
29if ($id != 0) {
30	@ids = ($id);
31} else {
32	my $dir = dirname(abs_path($0));
33	my $fn = "$dir/ifconfig.sh.in";
34	open FH, "< $fn" or die "open < $fn: $!\n";
35	while (<FH>) {
36		@ids = (1..$1)
37		    if /^max=(\d+)\s*$/;
38	}
39	close FH;
40	die "could not find max IP address in $fn\n"
41	    unless @ids > 1;
42}
43
44foreach $id (@ids) {
45        my $addr = pack("C4", 10, 53, 0, $id);
46	my $sa = pack_sockaddr_in($port, $addr);
47	socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname("tcp"))
48      		or die "$0: socket: $!\n";
49	setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
50
51	bind(SOCK, $sa)
52	    	or die sprintf("$0: bind(%s, %d): $!\n",
53			       inet_ntoa($addr), $port);
54	close(SOCK);
55}
56