Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / contrib / socketmapClient.pl
blob28fe603980abf987c16b0058167973cd9148658e
1 #!/usr/bin/perl -w
3 # Contributed by Bastiaan Bakker for SOCKETMAP
4 # $Id: socketmapClient.pl,v 1.1 2003/05/21 15:36:33 ca Exp $
6 use strict;
7 use IO::Socket;
9 die "usage: $0 <connection> <mapname> <key> [<key2> ...]" if (@ARGV < 3);
11 my $connection = shift @ARGV;
12 my $mapname = shift @ARGV;
14 my $sock;
16 if ($connection =~ /tcp:(.+):([0-9]*)/) {
17 $sock = new IO::Socket::INET (
18 PeerAddr => $1,
19 PeerPort => $2,
20 Proto => 'tcp',
22 } elsif ($connection =~ /((unix)|(local)):(.+)/) {
23 $sock = new IO::Socket::UNIX (
24 Type => SOCK_STREAM,
25 Peer => $4
27 } else {
28 die "unrecognized connection specification $connection";
31 die "Could not create socket: $!\n" unless $sock;
33 while(my $key = shift @ARGV) {
34 my $request = "$mapname $key";
35 netstringWrite($sock, $request);
36 $sock->flush();
37 my $response = netstringRead($sock);
39 print "$key => $response\n";
42 $sock->close();
44 sub netstringWrite {
45 my $sock = shift;
46 my $data = shift;
48 print $sock length($data).':'.$data.',';
51 sub netstringRead {
52 my $sock = shift;
53 my $saveSeparator = $/;
54 $/ = ':';
55 my $dataLength = <$sock>;
56 die "cannot read netstring length" unless defined($dataLength);
57 chomp $dataLength;
58 my $data;
59 if ($sock->read($data, $dataLength) == $dataLength) {
60 ($sock->getc() eq ',') or die "data misses closing ,";
61 } else {
62 die "received only ".length($data)." of $dataLength bytes";
65 $/ = $saveSeparator;
66 return $data;