MySQL 8.0.32
Source Code Documentation
sock_probe_ix.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2022, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#include <net/if.h>
24#include <sys/ioctl.h>
25#ifndef __linux__
26#include <sys/sockio.h>
27#endif
28#include <arpa/inet.h>
29#include <assert.h>
30#include <errno.h>
31#include <ifaddrs.h>
32#include <net/if.h>
33#include <netdb.h>
34#include <stdlib.h>
35#include <string.h>
36
37#define BSD_COMP
38
39#include "xcom/simset.h"
40#include "xcom/sock_probe.h"
41#include "xcom/task.h"
42#include "xcom/task_debug.h"
43#include "xcom/task_net.h"
44#include "xcom/task_os.h"
45#include "xcom/x_platform.h"
46#include "xcom/xcom_base.h"
47#include "xcom/xcom_common.h"
48#include "xcom/xcom_memory.h"
49#include "xcom/xcom_profile.h"
50#include "xcom/xcom_transport.h"
51#include "xdr_gen/xcom_vp.h"
52
53struct sock_probe {
54 int nbr_ifs; /* number of valid pointers in ifrp */
55 struct ifaddrs *interfaces;
56};
57
58static int number_of_interfaces(sock_probe *s);
59
60static void get_sockaddr_address(sock_probe *s, int count,
61 struct sockaddr **out);
62
63static void get_sockaddr_netmask(sock_probe *s, int count,
64 struct sockaddr **out);
65
66typedef enum SockaddrOp {
70
71static void get_sockaddr(sock_probe *s, int count, struct sockaddr **out,
72 SockaddrOp addr_operation);
73static bool_t is_if_running(sock_probe *s, int count);
74static char *get_if_name(sock_probe *s, int count);
75
76/* Initialize socket probe */
78 struct ifaddrs *ifa_tmp;
79
80 if (s == nullptr) {
81 goto err;
82 }
83
84 s->interfaces = nullptr;
85 if (getifaddrs(&s->interfaces) == -1) {
86 goto err;
87 }
88
89 ifa_tmp = s->interfaces;
90
91 while (ifa_tmp) {
92 if ((ifa_tmp->ifa_addr) && ((ifa_tmp->ifa_addr->sa_family == AF_INET) ||
93 (ifa_tmp->ifa_addr->sa_family == AF_INET6))) {
94 s->nbr_ifs++;
95 }
96 ifa_tmp = ifa_tmp->ifa_next;
97 }
98
99 return 0;
100err:
101 return -1;
102}
103
104/* Close socket of sock_probe */
106 if (s->interfaces) freeifaddrs(s->interfaces);
107 free(s);
108}
109
110/* Return the number of IP interfaces on this machine.*/
112 if (s == nullptr) {
113 return 0;
114 }
115
116 return s->nbr_ifs; /* Number of interfaces */
117}
118
119static struct ifaddrs *get_interface(sock_probe *s, int count) {
120 struct ifaddrs *net_if = nullptr;
121 int i = 0;
122
123 if (s == nullptr) {
124 return nullptr;
125 }
126
127 net_if = s->interfaces;
129 while (net_if != nullptr) {
130 if ((net_if->ifa_addr) && ((net_if->ifa_addr->sa_family == AF_INET) ||
131 (net_if->ifa_addr->sa_family == AF_INET6))) {
132 if (i >= count)
133 return net_if;
134 else
135 i++;
136 }
137
138 net_if = net_if->ifa_next;
139 }
140 }
141
142 return nullptr;
143}
144
146 struct ifaddrs *net_if = nullptr;
147
148 if (s == nullptr) {
149 return 0;
150 }
151
152 net_if = get_interface(s, count);
153 return net_if != nullptr && (net_if->ifa_flags & IFF_UP) &&
154 (net_if->ifa_flags & IFF_RUNNING);
155}
156
158 struct sockaddr **out) {
160}
161
163 struct sockaddr **out) {
165}
166
167/* Return the sockaddr of interface #count. */
168static void get_sockaddr(sock_probe *s, int count, struct sockaddr **out,
169 SockaddrOp addr_operation) {
170 struct ifaddrs *net_if = get_interface(s, count);
171
172 if (net_if == nullptr) {
173 *out = nullptr;
174 return;
175 }
176
177 switch (addr_operation) {
179 *out = (struct sockaddr *)net_if->ifa_addr;
180 break;
182 *out = (struct sockaddr *)net_if->ifa_netmask;
183 break;
184 default:
185 break;
186 }
187}
188
189static char *get_if_name(sock_probe *s, int count) {
190 struct ifaddrs *net_if = get_interface(s, count);
191
192 return net_if != nullptr ? net_if->ifa_name : nullptr;
193}
#define free(A)
Definition: lexyy.cc:915
static int count
Definition: myisam_ftdump.cc:42
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:909
SockaddrOp
Definition: sock_probe_ix.h:66
@ kSockaddrOpAddress
Definition: sock_probe_ix.h:67
@ kSockaddrOpNetmask
Definition: sock_probe_ix.h:68
static char * get_if_name(sock_probe *s, int count)
Definition: sock_probe_ix.h:189
static bool_t is_if_running(sock_probe *s, int count)
Definition: sock_probe_ix.h:145
static int init_sock_probe(sock_probe *s)
Definition: sock_probe_ix.h:77
static void close_sock_probe(sock_probe *s)
Definition: sock_probe_ix.h:105
static void get_sockaddr_netmask(sock_probe *s, int count, struct sockaddr **out)
Definition: sock_probe_ix.h:162
static struct ifaddrs * get_interface(sock_probe *s, int count)
Definition: sock_probe_ix.h:119
static void get_sockaddr_address(sock_probe *s, int count, struct sockaddr **out)
Definition: sock_probe_ix.h:157
static void get_sockaddr(sock_probe *s, int count, struct sockaddr **out, SockaddrOp addr_operation)
Definition: sock_probe_ix.h:168
static int number_of_interfaces(sock_probe *s)
Definition: sock_probe_ix.h:111
struct sockaddr sockaddr
Definition: sock_probe_win32.h:62
Definition: sock_probe_ix.h:53
int nbr_ifs
Definition: sock_probe_ix.h:54
struct ifaddrs * interfaces
Definition: sock_probe_ix.h:55
Rudimentary task system in portable C, based on Tom Duff's switch-based coroutine trick and a stack o...
int bool_t
Definition: types.h:34
#define idx_check_ret(x, limit, ret)
Definition: xcom_common.h:64