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