1 /** @file
2 
3   Machine
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22 
23   @section details Details
24 
25   Part of the utils library which contains classes that use multiple
26   components of the IO-Core to implement some useful functionality. The
27   classes also serve as good examples of how to use the IO-Core.
28 
29  */
30 
31 #pragma once
32 
33 #include "tscore/ink_inet.h"
34 #include "tscore/ink_uuid.h"
35 
36 #include <unordered_map>
37 #include <unordered_set>
38 #include <memory>
39 
40 /**
41   The Machine is a simple place holder for the hostname and the ip
42   address of an internet host.
43 
44   If a hostname or an IP address is not provided in the constructor,
45   the hostname defaults to the name of the current processor and the
46   IP address is the address of the current host.  If the host has
47   multiple IP addresses, the numerically lowest IP address is used.
48   The IP address is stored in the network byte order.
49 
50   @internal This does not handle multi-homed systems. That should be
51   fixed.
52 
53  */
54 struct Machine {
55   typedef Machine self; ///< Self reference type.
56 
57   char *hostname;   // name of the internet host
58   int hostname_len; // size of the string pointed to by hostname
59 
60   IpEndpoint ip;  ///< Preferred IP address of the host (network order)
61   IpEndpoint ip4; ///< IPv4 address if present.
62   IpEndpoint ip6; ///< IPv6 address if present.
63 
64   ip_text_buffer ip_string; // IP address of the host as a string.
65   int ip_string_len;
66 
67   char ip_hex_string[TS_IP6_SIZE * 2 + 1]; ///< IP address as hex string
68   int ip_hex_string_len;
69 
70   ATSUuid uuid;
71 
72   ~Machine();
73 
74   /** Initialize the singleton.
75       If @a hostname or @a ip are @c nullptr then system defaults are used.
76 
77       @note This must be called before called @c instance so that the
78       singleton is not @em inadvertently default initialized.
79   */
80   static self *init(char const *name     = nullptr, ///< Host name of the machine.
81                     sockaddr const *addr = nullptr  ///< Primary IP address of the machine.
82   );
83   /// @return The global instance of this class.
84   static self *instance();
85   bool is_self(const char *name);
86   bool is_self(const IpAddr *ipaddr);
87   bool is_self(struct sockaddr const *addr);
88   void insert_id(char *id);
89   void insert_id(IpAddr *ipaddr);
90 
91 protected:
92   Machine(char const *hostname, sockaddr const *addr);
93 
94   static self *_instance; ///< Singleton for the class.
95   std::unordered_set<std::string> machine_id_strings;
96   std::unordered_map<std::string, IpAddr *> machine_id_ipaddrs;
97 };
98