1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2020 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Written by Meno Abels, June MMIV
24  */
25 /**
26  * @file
27  * address configuration
28  */
29 
30 #include "lib/dlist.h"
31 
32 class OutputFormatterResource;
33 
34 /* clang-format off */
35 class IPADDR {
36  public:
37   typedef enum
38   {
39     R_SINGLE,
40     R_SINGLE_PORT,
41     R_SINGLE_ADDR,
42     R_MULTIPLE,
43     R_DEFAULT,
44     R_EMPTY,
45     R_UNDEFINED
46   } i_type;
47   IPADDR(int af);
48   IPADDR(const IPADDR& src);
49 
50  private:
51   IPADDR();
52   i_type type = R_UNDEFINED;
53   union {
54     struct sockaddr dontuse;
55     struct sockaddr_in dontuse4;
56 #ifdef HAVE_IPV6
57     struct sockaddr_in6 dontuse6;
58 #endif
59   } saddrbuf;
60   struct sockaddr* saddr = nullptr;
61   struct sockaddr_in* saddr4 = nullptr;
62 #ifdef HAVE_IPV6
63   struct sockaddr_in6* saddr6 = nullptr;
64 #endif
65  public:
66   void SetType(i_type o);
67   i_type GetType() const;
68   unsigned short GetPortNetOrder() const;
GetPortHostOrder()69   unsigned short GetPortHostOrder() const { return ntohs(GetPortNetOrder()); }
70   void SetPortNet(unsigned short port);
71   int GetFamily() const;
72   struct sockaddr* get_sockaddr();
73   int GetSockaddrLen();
74   void CopyAddr(IPADDR* src);
75   void SetAddrAny();
76   void SetAddr4(struct in_addr* ip4);
77 #ifdef HAVE_IPV6
78   void SetAddr6(struct in6_addr* ip6);
79 #endif
80   const char* GetAddress(char* outputbuf, int outlen);
81   void BuildConfigString(OutputFormatterResource& send,
82                                       bool inherited);
83   const char* build_address_str(char* buf, int blen, bool print_port = true);
84 
85   /* private */
86   dlink link;
87 };
88 /* clang-format on */
89 
90 void InitDefaultAddresses(dlist** addr, const char* port);
91 void FreeAddresses(dlist* addrs);
92 
93 const char* GetFirstAddress(dlist* addrs, char* outputbuf, int outlen);
94 int GetFirstPortNetOrder(dlist* addrs);
95 int GetFirstPortHostOrder(dlist* addrs);
96 
97 int AddAddress(dlist** out,
98                IPADDR::i_type type,
99                unsigned short defaultport,
100                int family,
101                const char* hostname_str,
102                const char* port_str,
103                char* buf,
104                int buflen);
105 const char* BuildAddressesString(dlist* addrs,
106                                  char* buf,
107                                  int blen,
108                                  bool print_port = true);
109 
110 int SockaddrGetPortNetOrder(const struct sockaddr* sa);
111 int SockaddrGetPort(const struct sockaddr* sa);
112 char* SockaddrToAscii(const struct sockaddr* sa, char* buf, int len);
113 #ifdef WIN32
114 #  undef HAVE_OLD_SOCKOPT
115 #endif
116 #ifdef HAVE_OLD_SOCKOPT
117 int inet_aton(const char* cp, struct in_addr* inp);
118 #endif
119