You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

274 lines
9.3 KiB

  1. #ifndef _NETCFG_H_
  2. #define _NETCFG_H_
  3. #define INTERFACES_FILE "/etc/network/interfaces"
  4. #define HOSTS_FILE "/etc/hosts"
  5. #define HOSTNAME_FILE "/etc/hostname"
  6. #define NETWORKS_FILE "/etc/networks"
  7. #define RESOLV_FILE "/etc/resolv.conf"
  8. #define DHCLIENT_CONF "/etc/dhclient.conf"
  9. #define DOMAIN_FILE "/tmp/domain_name"
  10. #define NTP_SERVER_FILE "/tmp/dhcp-ntp-servers"
  11. #define WPASUPP_CTRL "/var/run/wpa_supplicant"
  12. #define WPAPID "/var/run/wpa_supplicant.pid"
  13. #define DHCLIENT6_FILE "/var/lib/netcfg/dhclient6.conf"
  14. #define DHCP6C_FILE "/var/lib/netcfg/dhcp6c.conf"
  15. #define DEVNAMES "/etc/network/devnames"
  16. #define DEVHOTPLUG "/etc/network/devhotplug"
  17. #ifdef __linux__
  18. #define STAB "/var/run/stab"
  19. #endif
  20. #define WPA_MIN 8 /* minimum passphrase length */
  21. #define WPA_MAX 64 /* maximum passphrase length */
  22. #define _GNU_SOURCE
  23. #include <sys/types.h>
  24. #include <arpa/inet.h>
  25. #include <stdio.h>
  26. #include <cdebconf/debconfclient.h>
  27. #ifndef ARRAY_SIZE
  28. # define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  29. #endif
  30. #define empty_str(s) (s != NULL && *s == '\0')
  31. #define IPV6_HOSTS \
  32. "# The following lines are desirable for IPv6 capable hosts\n" \
  33. "::1 localhost ip6-localhost ip6-loopback\n" \
  34. "ff02::1 ip6-allnodes\n" \
  35. "ff02::2 ip6-allrouters\n"
  36. /* The number of times to attempt to verify gateway reachability.
  37. * Each try invokes arping with a one second timeout.
  38. */
  39. #define NETCFG_GATEWAY_REACHABILITY_TRIES 50
  40. #ifndef MAXHOSTNAMELEN
  41. #define MAXHOSTNAMELEN 63
  42. #endif
  43. #define RETURN_TO_MAIN 10
  44. #define CONFIGURE_MANUALLY 15
  45. typedef enum { NOT_ASKED = 30, GO_BACK, REPLY_WEP, REPLY_WPA } response_t;
  46. typedef enum { DHCP, STATIC, DUNNO } method_t;
  47. typedef enum { ADHOC = 1, MANAGED = 2 } wifimode_t;
  48. typedef enum { WPA_OK, WPA_QUEUED, WPA_UNAVAIL } wpa_t;
  49. extern int wfd, skfd;
  50. extern int input_result;
  51. extern int have_domain;
  52. /* network config */
  53. extern char hostname[MAXHOSTNAMELEN + 1];
  54. extern char domain[MAXHOSTNAMELEN + 1];
  55. /* wireless */
  56. extern char *essid, *wepkey, *passphrase;
  57. extern wifimode_t mode;
  58. /* Determine which of INET_ADDRSTRLEN or INET6_ADDRSTRLEN is longer
  59. * (yeah, take a wild guess who'll win *that* one) and make that
  60. * the string length. This makes sure that any string defined to
  61. * be NETCFG_ADDRSTRLEN bytes long is guaranteed to be able to
  62. * accomodate either an IPv4 or IPv6 address.
  63. */
  64. #define NETCFG_ADDRSTRLEN ((INET_ADDRSTRLEN < INET6_ADDRSTRLEN) ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN)
  65. /* The maximum number of nameservers and NTP servers we'll store in the
  66. * interface.
  67. */
  68. #define NETCFG_NAMESERVERS_MAX 4
  69. #define NETCFG_NTPSERVERS_MAX 4
  70. /* The information required to configure a network interface. */
  71. struct netcfg_interface {
  72. char *name;
  73. /* Is this a loopback interface?
  74. * -1 if unknown, 0 if no, 1 if yes */
  75. int loopback;
  76. /* Was this interface configured with DHCP?
  77. * -1 if unknown, 0 if no, 1 if yes */
  78. int dhcp;
  79. /* Was this interface configured using stateful DHCPv6?
  80. */
  81. int dhcpv6;
  82. /* Address family of the address we're configuring; AF_INET or AF_INET6 */
  83. int address_family;
  84. /* Did the interface get an IPv6 address/gateway via SLAAC?
  85. * T (1) / F (0) / unknown (-1) */
  86. int slaac;
  87. /* Did the RA indicate that we should use stateful address
  88. * configuration? T/F/?
  89. */
  90. int v6_stateful_config;
  91. /* Did the RA indicate that we should use stateless auxiliary
  92. * config (DNS, NTP, etc)? T/F/?
  93. */
  94. int v6_stateless_config;
  95. /* The list of nameservers this interface has asked us to
  96. * use.
  97. */
  98. char nameservers[NETCFG_NAMESERVERS_MAX][NETCFG_ADDRSTRLEN];
  99. /* The list of NTP servers this interface has asked us to
  100. * use. Interestingly, the DHCP specs only allow NTP servers by
  101. * IP address, not hostname, hence the use of NETCFG_ADDRSTRLEN.
  102. */
  103. char ntp_servers[NETCFG_NTPSERVERS_MAX][NETCFG_ADDRSTRLEN];
  104. /* The 'hostname' we want to send to the DHCP server so it'll give
  105. * us a/the right lease.
  106. */
  107. char dhcp_hostname[MAXHOSTNAMELEN];
  108. char ipaddress[NETCFG_ADDRSTRLEN];
  109. unsigned int masklen;
  110. char gateway[NETCFG_ADDRSTRLEN];
  111. char pointopoint[INET_ADDRSTRLEN];
  112. /* Wireless mode */
  113. wifimode_t mode;
  114. /* Wireless configuration */
  115. response_t wifi_security;
  116. char *wepkey;
  117. char *essid;
  118. /* WPA */
  119. wpa_t wpa_supplicant_status;
  120. char *passphrase;
  121. };
  122. /* Somewhere we can store both in_addr and in6_addr; convenient for all those
  123. * places you couldn't be bothered to deal with it yourself manually.
  124. */
  125. union inX_addr {
  126. struct in_addr in4;
  127. struct in6_addr in6;
  128. };
  129. /* Set default values for all netcfg_interface parameters */
  130. extern void netcfg_interface_init(struct netcfg_interface *iface);
  131. /* common functions */
  132. extern int check_kill_switch (const char *if_name);
  133. extern int is_interface_up (const char *if_name);
  134. extern int get_all_ifs (int all, char ***ptr);
  135. extern char *get_ifdsc (struct debconfclient *client, const char *if_name);
  136. extern FILE *file_open (char *path, const char *opentype);
  137. extern void netcfg_die (struct debconfclient *client);
  138. extern int netcfg_get_interface(struct debconfclient *client, char **if_name, int *num_interfaces, const char *defif);
  139. extern short valid_hostname (const char *hname);
  140. extern short valid_domain (const char *dname);
  141. extern int netcfg_get_hostname(struct debconfclient *client, char *template, char *hostname, short hdset);
  142. extern int netcfg_get_nameservers (struct debconfclient *client, char **nameservers, char *default_nameservers);
  143. extern int netcfg_get_domain(struct debconfclient *client, char domain[]);
  144. extern int netcfg_get_static(struct debconfclient *client, struct netcfg_interface *interface);
  145. extern int netcfg_activate_dhcp(struct debconfclient *client, struct netcfg_interface *interface);
  146. extern int nameserver_count (const struct netcfg_interface *interface);
  147. extern int read_resolv_conf_nameservers (char *resolv_conf_file, struct netcfg_interface *interface);
  148. extern void sigchld_handler (int sig __attribute__ ((unused)));
  149. extern int ask_dhcp_options (struct debconfclient *client, const char *if_name);
  150. extern void netcfg_write_loopback (void);
  151. extern void netcfg_write_common (const char *ipaddress, const char *hostname, const char *domain);
  152. void netcfg_nameservers_to_array(const char *nameservers, struct netcfg_interface *interface);
  153. extern int is_wireless_iface (const char *if_name);
  154. extern int netcfg_wireless_set_essid (struct debconfclient *client, struct netcfg_interface *interface);
  155. extern int netcfg_wireless_set_wep (struct debconfclient *client, struct netcfg_interface *interface);
  156. extern int wireless_security_type (struct debconfclient *client, const char *if_name);
  157. extern int netcfg_set_passphrase (struct debconfclient *client, struct netcfg_interface *interface);
  158. extern int init_wpa_supplicant_support (struct netcfg_interface *interface);
  159. extern int kill_wpa_supplicant (void);
  160. extern int wpa_supplicant_start (struct debconfclient *client, const struct netcfg_interface *interface);
  161. extern int iface_is_hotpluggable(const char *if_name);
  162. extern short find_in_stab (const char *if_name);
  163. extern void deconfigure_network(struct netcfg_interface *iface);
  164. extern void interface_up (const char *if_name);
  165. extern void interface_down (const char *if_name);
  166. extern void loop_setup(void);
  167. extern int get_hostname_from_dns(const struct netcfg_interface *interface, char *hostname, const size_t max_hostname_len);
  168. extern int inet_ptom (int af, const char *src, unsigned int *dst);
  169. extern const char *inet_mtop (int af, unsigned int src, char *dst, socklen_t dst_len);
  170. extern void inet_mton (int af, unsigned int src, void *dst);
  171. extern void parse_args (int argc, char** argv);
  172. extern void open_sockets (void);
  173. extern void reap_old_files (void);
  174. extern void netcfg_update_entropy (void);
  175. extern int netcfg_write_resolv (const char *domain, const struct netcfg_interface *interface);
  176. extern int ethtool_lite (const char *if_name);
  177. extern int netcfg_detect_link(struct debconfclient *client, const struct netcfg_interface *interface);
  178. extern int netcfg_parse_cidr_address(const char *address, struct netcfg_interface *interface);
  179. extern void netcfg_network_address(const struct netcfg_interface *interface, char *network);
  180. extern void netcfg_broadcast_address(const struct netcfg_interface *interface, char *broadcast);
  181. extern int netcfg_gateway_reachable(const struct netcfg_interface *interface);
  182. extern void preseed_hostname_from_fqdn(struct debconfclient *client, char *fqdn);
  183. extern int netcfg_dhcp(struct debconfclient *client, struct netcfg_interface *interface);
  184. extern void rtrim(char *);
  185. extern char *strtrim(char *s);
  186. /* ipv6.c */
  187. extern void nc_v6_wait_for_complete_configuration(const struct netcfg_interface *interface);
  188. extern int nc_v6_interface_configured(const struct netcfg_interface *interface, const int link_local);
  189. extern int nc_v6_get_config_flags(struct debconfclient *client, struct netcfg_interface *interface);
  190. /* write_interfaces.c */
  191. extern int netcfg_write_interface(const struct netcfg_interface *interface);
  192. /* rdnssd.c */
  193. extern int start_rdnssd(struct debconfclient *client);
  194. extern void cleanup_rdnssd(void);
  195. extern void stop_rdnssd(void);
  196. extern void read_rdnssd_nameservers(struct netcfg_interface *interface);
  197. /* autoconfig.c */
  198. extern void cleanup_dhcpv6_client(void);
  199. extern int start_dhcpv6_client(struct debconfclient *client, const struct netcfg_interface *interface);
  200. extern int netcfg_autoconfig(struct debconfclient *client, struct netcfg_interface *interface);
  201. #endif /* _NETCFG_H_ */