File: netcfg.h

package info (click to toggle)
netcfg 1.160
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,532 kB
  • sloc: ansic: 5,361; sh: 183; makefile: 77
file content (273 lines) | stat: -rw-r--r-- 9,548 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef _NETCFG_H_
#define _NETCFG_H_

#define INTERFACES_FILE "/etc/network/interfaces"
#define HOSTS_FILE      "/etc/hosts"
#define HOSTNAME_FILE   "/etc/hostname"
#define NETWORKS_FILE   "/etc/networks"
#define RESOLV_FILE     "/etc/resolv.conf"
#define DHCLIENT_CONF   "/etc/dhclient.conf"
#define DOMAIN_FILE     "/tmp/domain_name"
#define NTP_SERVER_FILE "/tmp/dhcp-ntp-servers"
#define WPASUPP_CTRL    "/var/run/wpa_supplicant"
#define WPAPID          "/var/run/wpa_supplicant.pid"

#define DHCLIENT6_FILE  "/var/lib/netcfg/dhclient6.conf"
#define DHCP6C_FILE     "/var/lib/netcfg/dhcp6c.conf"

#define DEVNAMES	"/etc/network/devnames"
#define DEVHOTPLUG	"/etc/network/devhotplug"
#ifdef __linux__
#define STAB		"/var/run/stab"
#endif

#define WPA_MIN         8    /* minimum passphrase length */
#define WPA_MAX         64   /* maximum passphrase length */

#define _GNU_SOURCE

#include <sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <cdebconf/debconfclient.h>

#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif

#define empty_str(s) (s != NULL && *s == '\0')

#define IPV6_HOSTS \
"# The following lines are desirable for IPv6 capable hosts\n" \
"::1     localhost ip6-localhost ip6-loopback\n" \
"ff02::1 ip6-allnodes\n" \
"ff02::2 ip6-allrouters\n"

/* The number of times to attempt to verify gateway reachability.
 * Each try invokes arping with a one second timeout.
 */
#define NETCFG_GATEWAY_REACHABILITY_TRIES 50

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 63
#endif

#define RETURN_TO_MAIN 10
#define CONFIGURE_MANUALLY 15

typedef enum { NOT_ASKED = 30, GO_BACK, REPLY_WEP, REPLY_WPA } response_t;
typedef enum { DHCP, STATIC, DUNNO } method_t;
typedef enum { ADHOC = 1, MANAGED = 2 } wifimode_t;
typedef enum { WPA_OK, WPA_QUEUED, WPA_UNAVAIL } wpa_t;

extern int wfd, skfd;
extern int input_result;
extern int have_domain;

/* network config */
extern char hostname[MAXHOSTNAMELEN + 1];
extern char domain[MAXHOSTNAMELEN + 1];

/* wireless */
extern char *essid, *wepkey, *passphrase;
extern wifimode_t mode;

/* Determine which of INET_ADDRSTRLEN or INET6_ADDRSTRLEN is longer
 * (yeah, take a wild guess who'll win *that* one) and make that
 * the string length.  This makes sure that any string defined to
 * be NETCFG_ADDRSTRLEN bytes long is guaranteed to be able to
 * accomodate either an IPv4 or IPv6 address.
 */
#define NETCFG_ADDRSTRLEN ((INET_ADDRSTRLEN < INET6_ADDRSTRLEN) ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN)

/* The maximum number of nameservers and NTP servers we'll store in the
 * interface.
 */
#define NETCFG_NAMESERVERS_MAX	4
#define NETCFG_NTPSERVERS_MAX	4

/* The information required to configure a network interface. */
struct netcfg_interface {
	char *name;
	
	/* Is this a loopback interface?
	 * -1 if unknown, 0 if no, 1 if yes */
	int loopback;

	/* Was this interface configured with DHCP?
	 * -1 if unknown, 0 if no, 1 if yes */
	int dhcp;
	
	/* Was this interface configured using stateful DHCPv6?
	 */
	int dhcpv6;

	/* Address family of the address we're configuring; AF_INET or AF_INET6 */
	int address_family;

	/* Did the interface get an IPv6 address/gateway via SLAAC?
	 * T (1) / F (0) / unknown (-1) */
	int slaac;
	
	/* Did the RA indicate that we should use stateful address
	 * configuration?  T/F/?
	 */
	int v6_stateful_config;
	
	/* Did the RA indicate that we should use stateless auxiliary
	 * config (DNS, NTP, etc)?  T/F/?
	 */
	int v6_stateless_config;
	
	/* The list of nameservers this interface has asked us to
	 * use.
	 */
	char nameservers[NETCFG_NAMESERVERS_MAX][NETCFG_ADDRSTRLEN];

	/* The list of NTP servers this interface has asked us to
	 * use.  Interestingly, the DHCP specs only allow NTP servers by
	 * IP address, not hostname, hence the use of NETCFG_ADDRSTRLEN.
	 */
	char ntp_servers[NETCFG_NTPSERVERS_MAX][NETCFG_ADDRSTRLEN];

	/* The 'hostname' we want to send to the DHCP server so it'll give
	 * us a/the right lease.
	 */
	char dhcp_hostname[MAXHOSTNAMELEN];
	
	char ipaddress[NETCFG_ADDRSTRLEN];
	unsigned int masklen;
	char gateway[NETCFG_ADDRSTRLEN];
	char pointopoint[INET_ADDRSTRLEN];

	/* Wireless mode */
	wifimode_t mode;

	/* Wireless configuration */
        response_t wifi_security;
	char *wepkey;
	char *essid;

	/* WPA */
	wpa_t wpa_supplicant_status;
	char *passphrase;
};

/* Somewhere we can store both in_addr and in6_addr; convenient for all those
 * places you couldn't be bothered to deal with it yourself manually.
 */
union inX_addr {
	struct in_addr in4;
	struct in6_addr in6;
};

/* Set default values for all netcfg_interface parameters */
extern void netcfg_interface_init(struct netcfg_interface *iface);

/* common functions */
extern int check_kill_switch (const char *if_name);

extern int is_interface_up (const char *if_name);

extern int get_all_ifs (int all, char ***ptr);

extern char *get_ifdsc (struct debconfclient *client, const char *if_name);

extern FILE *file_open (char *path, const char *opentype);

extern void netcfg_die (struct debconfclient *client);

extern int netcfg_get_interface(struct debconfclient *client, char **if_name, int *num_interfaces, const char *defif);

extern short valid_hostname (const char *hname);
extern short valid_domain (const char *dname);

extern int netcfg_get_hostname(struct debconfclient *client, char *template, char *hostname, short hdset);

extern int netcfg_get_nameservers (struct debconfclient *client, char **nameservers, char *default_nameservers);

extern int netcfg_get_domain(struct debconfclient *client,  char domain[]);

extern int netcfg_get_static(struct debconfclient *client, struct netcfg_interface *interface);

extern int netcfg_activate_dhcp(struct debconfclient *client, struct netcfg_interface *interface);

extern int nameserver_count (const struct netcfg_interface *interface);

extern int read_resolv_conf_nameservers (char *resolv_conf_file, struct netcfg_interface *interface);

extern void sigchld_handler (int sig __attribute__ ((unused)));

extern int ask_dhcp_options (struct debconfclient *client, const char *if_name);

extern void netcfg_write_loopback (void);
extern void netcfg_write_common (const char *ipaddress, const char *hostname, const char *domain);

void netcfg_nameservers_to_array(const char *nameservers, struct netcfg_interface *interface);

extern int is_wireless_iface (const char *if_name);
extern int netcfg_wireless_set_essid (struct debconfclient *client, struct netcfg_interface *interface);
extern int netcfg_wireless_set_wep (struct debconfclient *client, struct netcfg_interface *interface);
extern int wireless_security_type (struct debconfclient *client, const char *if_name);
extern int netcfg_set_passphrase (struct debconfclient *client, struct netcfg_interface *interface);
extern int init_wpa_supplicant_support (struct netcfg_interface *interface);
extern int kill_wpa_supplicant (void);

extern int wpa_supplicant_start (struct debconfclient *client, const struct netcfg_interface *interface);
extern int iface_is_hotpluggable(const char *if_name);
extern short find_in_stab (const char *if_name);
extern void deconfigure_network(struct netcfg_interface *iface);

extern void interface_up (const char *if_name);
extern void interface_down (const char *if_name);

extern void loop_setup(void);
extern int get_hostname_from_dns(const struct netcfg_interface *interface, char *hostname, const size_t max_hostname_len);

extern int inet_ptom (int af, const char *src, unsigned int *dst);
extern const char *inet_mtop (int af, unsigned int src, char *dst, socklen_t dst_len);
extern void inet_mton (int af, unsigned int src, void *dst);

extern void parse_args (int argc, char** argv);
extern void open_sockets (void);
extern void reap_old_files (void);

extern void netcfg_update_entropy (void);

extern int netcfg_write_resolv (const char *domain, const struct netcfg_interface *interface);

extern int ethtool_lite (const char *if_name);
extern int netcfg_detect_link(struct debconfclient *client, const struct netcfg_interface *interface);

extern int netcfg_parse_cidr_address(const char *address, struct netcfg_interface *interface);
extern void netcfg_network_address(const struct netcfg_interface *interface, char *network);
extern void netcfg_broadcast_address(const struct netcfg_interface *interface, char *broadcast);
extern int netcfg_gateway_reachable(const struct netcfg_interface *interface);

extern void preseed_hostname_from_fqdn(struct debconfclient *client, char *fqdn);

extern int netcfg_dhcp(struct debconfclient *client, struct netcfg_interface *interface);

extern void rtrim(char *);
extern char *strtrim(char *s);

/* ipv6.c */
extern void nc_v6_wait_for_complete_configuration(const struct netcfg_interface *interface);
extern int nc_v6_interface_configured(const struct netcfg_interface *interface, const int link_local);
extern int nc_v6_get_config_flags(struct debconfclient *client, struct netcfg_interface *interface);

/* write_interfaces.c */
extern int netcfg_write_interface(const struct netcfg_interface *interface);

/* rdnssd.c */
extern int start_rdnssd(struct debconfclient *client);
extern void cleanup_rdnssd(void);
extern void stop_rdnssd(void);
extern void read_rdnssd_nameservers(struct netcfg_interface *interface);

/* autoconfig.c */
extern void cleanup_dhcpv6_client(void);
extern int start_dhcpv6_client(struct debconfclient *client, const struct netcfg_interface *interface);
extern int netcfg_autoconfig(struct debconfclient *client, struct netcfg_interface *interface);

#endif /* _NETCFG_H_ */