Debian Codegraph @

cassandane/Cassandane/Util/Socket.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Util::Socket;
41 use strict;
42 use warnings;
43 use base qw(Exporter);
44 use IO::Socket::INET;
45 use IO::Socket::INET6;
46 use IO::Socket::UNIX;
47
48 use lib '.';
49 use Cassandane::Util::Log;
50
51 our @EXPORT = qw(create_client_socket);
52
53 sub create_client_socket
54 {
55 my ($af, $host, $port) = @_;
56 my $sock;
57
58 if ($af eq 'inet')
59 {
60 $host ||= '127.0.0.1';
61 xlog "create_client_socket INET host=$host port=$port";
62 return IO::Socket::INET->new(
63 Type => SOCK_STREAM,
64 PeerHost => $host,
65 PeerPort => $port);
66 }
67 elsif ($af eq 'inet6')
68 {
69 # IO::Socket::INET6 doesn't have an option to pass
70 # an address which is explicitly *without* the optional
71 # :port part, but it does allow us to use the same []
72 # syntax for surrounding IPv6 address literals as Cyrus
73 $host ||= '::1';
74 xlog "create_client_socket INET6 addr=[$host]:$port";
75 return IO::Socket::INET6->new(
76 Domain => AF_INET6,
77 Type => SOCK_STREAM,
78 PeerAddr => "[$host]:$port");
79 }
80 elsif ($af eq 'unix')
81 {
82 xlog "create_client_socket UNIX peer=$port";
83 return IO::Socket::UNIX->new(
84 Type => SOCK_STREAM,
85 Peer => $port);
86 }
87 die "Cannot create sock for address family \"$af\"";
88 }
89
90
91 1;