Browse Source
common code between them. In theory it would save about 3k, I think, though my initialy efforts didn't save any space, so we still aren't sharing. r335master

5 changed files with 410 additions and 301 deletions
@ -0,0 +1,135 @@ |
|||
/*
|
|||
netcfg-dhcp.c - Configure a network via dhcp for the debian-installer |
|||
Author - David Whedon |
|||
|
|||
|
|||
*/ |
|||
#include <ctype.h> |
|||
#include <net/if.h> |
|||
#include <sys/socket.h> |
|||
#include <sys/ioctl.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <sys/types.h> |
|||
#include <sys/stat.h> |
|||
#include <debconfclient.h> |
|||
#include "utils.h" |
|||
#include "netcfg.h" |
|||
|
|||
static char *interface = NULL; |
|||
static char *hostname = NULL; |
|||
static char *domain = NULL; |
|||
static u_int32_t ipaddress = 0; |
|||
static u_int32_t nameservers[4] = { 0 }; |
|||
static struct debconfclient *client; |
|||
|
|||
|
|||
static char *dhcp_hostname = NULL; |
|||
|
|||
char * |
|||
debconf_input (char *priority, char *template) |
|||
{ |
|||
client->command (client, "fset", template, "seen", "false", NULL); |
|||
client->command (client, "input", priority, template, NULL); |
|||
client->command (client, "go", NULL); |
|||
client->command (client, "get", template, NULL); |
|||
return client->value; |
|||
} |
|||
|
|||
static void |
|||
netcfg_get_dhcp () |
|||
{ |
|||
if (dhcp_hostname) |
|||
free (dhcp_hostname); |
|||
|
|||
client->command (client, "input", "high", "netcfg/dhcp_hostname", NULL); |
|||
client->command (client, "go", NULL); |
|||
client->command (client, "get", "netcfg/dhcp_hostname", NULL); |
|||
|
|||
if (client->value) |
|||
dhcp_hostname = strdup (client->value); |
|||
} |
|||
|
|||
|
|||
static void |
|||
netcfg_write_dhcp () |
|||
{ |
|||
|
|||
FILE *fp; |
|||
|
|||
if ((fp = file_open (INTERFACES_FILE))) |
|||
{ |
|||
fprintf (fp, |
|||
"\n# This entry was created during the Debian installation\n"); |
|||
fprintf (fp, "iface %s inet dhcp\n", interface); |
|||
} |
|||
|
|||
netcfg_mkdir (DHCPCD_DIR); |
|||
if ((fp = file_open (DHCPCD_FILE))) |
|||
{ |
|||
fprintf (fp, |
|||
"\n# dhcpcd configuration: created during the Debian installation\n"); |
|||
fprintf (fp, "IFACE=%s\n", interface); |
|||
if (dhcp_hostname) |
|||
fprintf (fp, "OPTIONS='-h %s'\n", dhcp_hostname); |
|||
} |
|||
} |
|||
|
|||
|
|||
static void |
|||
netcfg_activate_dhcp () |
|||
{ |
|||
char buf[128]; |
|||
char *ptr; |
|||
execlog ("/sbin/ifconfig lo 127.0.0.1"); |
|||
|
|||
ptr = buf; |
|||
ptr += snprintf (buf, sizeof (buf), "/sbin/dhcpcd-2.2.x"); |
|||
if (dhcp_hostname) |
|||
ptr += |
|||
snprintf (ptr, sizeof (buf) - (ptr - buf), " -h %s", dhcp_hostname); |
|||
|
|||
ptr += snprintf (ptr, sizeof (buf) - (ptr - buf), " %s", interface); |
|||
|
|||
if (execlog (buf)) |
|||
netcfg_die (client); |
|||
} |
|||
|
|||
int |
|||
main (int argc, char *argv[]) |
|||
{ |
|||
char *ptr; |
|||
int finished = 1; |
|||
client = debconfclient_new (); |
|||
client->command (client, "title", "DHCP Network Configuration", NULL); |
|||
|
|||
do |
|||
{ |
|||
netcfg_get_common (client, interface, hostname, domain, nameservers); |
|||
netcfg_get_dhcp (); |
|||
client->command (client, "subst", "netcfg/confirm_dhcp", "interface", |
|||
interface, NULL); |
|||
client->command (client, "subst", "netcfg/confirm_dhcp", "hostname", |
|||
hostname, NULL); |
|||
client->command (client, "subst", "netcfg/confirm_dhcp", "domain", |
|||
(domain ? domain : "<none>"), NULL); |
|||
client->command (client, "subst", "netcfg/confirm_dhcp", |
|||
"dhcp_hostname", |
|||
(dhcp_hostname ? dhcp_hostname : "<none>"), NULL); |
|||
ptr = debconf_input ("medium", "netcfg/confirm_dhcp"); |
|||
|
|||
if (strstr (ptr, "true")) |
|||
finished = 1; |
|||
} |
|||
while (!finished); |
|||
|
|||
netcfg_write_dhcp (); |
|||
netcfg_write_common (ipaddress, domain, hostname, nameservers); |
|||
|
|||
debconf_input ("medium", "netcfg/do_dhcp"); |
|||
netcfg_activate_dhcp (); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,177 @@ |
|||
/*
|
|||
netcfg-static.c - Configure a static network for the debian-installer |
|||
Author - David Whedon |
|||
|
|||
|
|||
*/ |
|||
#include <ctype.h> |
|||
#include <net/if.h> |
|||
#include <sys/socket.h> |
|||
#include <sys/ioctl.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <sys/types.h> |
|||
#include <sys/stat.h> |
|||
#include <debconfclient.h> |
|||
#include "utils.h" |
|||
#include "netcfg.h" |
|||
|
|||
|
|||
static char *interface = NULL; |
|||
static char *hostname = NULL; |
|||
static char *domain = NULL; |
|||
static u_int32_t ipaddress = 0; |
|||
static u_int32_t nameservers[4] = { 0 }; |
|||
static u_int32_t network = 0; |
|||
static u_int32_t broadcast = 0; |
|||
static u_int32_t netmask = 0; |
|||
static u_int32_t gateway = 0; |
|||
static struct debconfclient *client; |
|||
|
|||
char * |
|||
debconf_input (char *priority, char *template) |
|||
{ |
|||
client->command (client, "fset", template, "seen", "false", NULL); |
|||
client->command (client, "input", priority, template, NULL); |
|||
client->command (client, "go", NULL); |
|||
client->command (client, "get", template, NULL); |
|||
return client->value; |
|||
} |
|||
|
|||
|
|||
static void |
|||
netcfg_get_static () |
|||
{ |
|||
char *ptr; |
|||
|
|||
ipaddress = network = broadcast = netmask = gateway = 0; |
|||
|
|||
ptr = debconf_input ("critical", "netcfg/get_ipaddress"); |
|||
dot2num (&ipaddress, ptr); |
|||
|
|||
client->command (client, "subst", "netcfg/confirm_static", |
|||
"ipaddress", |
|||
(ipaddress ? num2dot (ipaddress) : "<none>"), NULL); |
|||
|
|||
ptr = debconf_input ("critical", "netcfg/get_netmask"); |
|||
dot2num (&netmask, ptr); |
|||
client->command (client, "subst", "netcfg/confirm_static", |
|||
"netmask", (netmask ? num2dot (netmask) : "<none>"), NULL); |
|||
|
|||
network = ipaddress & netmask; |
|||
|
|||
ptr = debconf_input ("critical", "netcfg/get_gateway"); |
|||
dot2num (&gateway, ptr); |
|||
|
|||
client->command (client, "subst", "netcfg/confirm_static", |
|||
"gateway", (gateway ? num2dot (gateway) : "<none>"), NULL); |
|||
|
|||
if (gateway && ((gateway & netmask) != network)) |
|||
{ |
|||
client->command (client, "input", "high", |
|||
"netcfg/gateway_unreachable", NULL); |
|||
client->command (client, "go", NULL); |
|||
} |
|||
|
|||
broadcast = (network | ~netmask); |
|||
|
|||
|
|||
} |
|||
|
|||
|
|||
static int |
|||
netcfg_write_static () |
|||
{ |
|||
FILE *fp; |
|||
|
|||
if ((fp = file_open (NETWORKS_FILE))) |
|||
{ |
|||
fprintf (fp, "localnet %s\n", num2dot (network)); |
|||
fclose (fp); |
|||
} |
|||
else |
|||
goto error; |
|||
|
|||
if ((fp = file_open (INTERFACES_FILE))) |
|||
{ |
|||
fprintf (fp, |
|||
"\n# This entry was created during the Debian installation\n"); |
|||
fprintf (fp, "# (network, broadcast and gateway are optional)\n"); |
|||
fprintf (fp, "iface %s inet static\n", interface); |
|||
fprintf (fp, "\taddress %s\n", num2dot (ipaddress)); |
|||
fprintf (fp, "\tnetmask %s\n", num2dot (netmask)); |
|||
fprintf (fp, "\tnetwork %s\n", num2dot (network)); |
|||
fprintf (fp, "\tbroadcast %s\n", num2dot (broadcast)); |
|||
fprintf (fp, "\tgateway %s\n", num2dot (gateway)); |
|||
fclose (fp); |
|||
} |
|||
else |
|||
goto error; |
|||
|
|||
return 0; |
|||
error: |
|||
return -1; |
|||
} |
|||
|
|||
static int |
|||
netcfg_activate_static () |
|||
{ |
|||
int rv; |
|||
char *ptr; |
|||
char buf[128]; |
|||
execlog ("/sbin/ifconfig lo 127.0.0.1"); |
|||
|
|||
ptr = buf; |
|||
ptr += |
|||
snprintf (buf, sizeof (buf), "/sbin/ifconfig %s %s", interface, |
|||
num2dot (ipaddress)); |
|||
ptr += |
|||
snprintf (ptr, sizeof (buf) - (ptr - buf), " netmask %s", |
|||
num2dot (netmask)); |
|||
ptr += |
|||
snprintf (ptr, sizeof (buf) - (ptr - buf), " broadcast %s", |
|||
num2dot (broadcast)); |
|||
|
|||
rv = execlog (buf); |
|||
if (rv != 0) |
|||
{ |
|||
client->command (client, "input", "critical", "netcfg/error_cfg", NULL); |
|||
client->command (client, "go", NULL); |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
int |
|||
main (int argc, char *argv[]) |
|||
{ |
|||
int finished = 0; |
|||
char *ptr; |
|||
client = debconfclient_new (); |
|||
|
|||
client->command (client, "title", "Static Network Configuration", NULL); |
|||
|
|||
|
|||
do |
|||
{ |
|||
netcfg_get_common (client, interface, hostname, domain, nameservers); |
|||
client->command (client, "subst", "netcfg/confirm_static", |
|||
"hostname", hostname, NULL); |
|||
client->command (client, "subst", "netcfg/confirm_static", "domain", |
|||
(domain ? domain : "<none>"), NULL); |
|||
netcfg_get_static (); |
|||
ptr = debconf_input ("medium", "netcfg/confirm_static"); |
|||
|
|||
if (strstr (ptr, "true")) |
|||
finished = 1; |
|||
|
|||
} |
|||
while (!finished); |
|||
|
|||
netcfg_write_common (ipaddress, domain, hostname, nameservers); |
|||
netcfg_write_static (); |
|||
netcfg_activate_static (); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,42 @@ |
|||
#ifndef _NETCFG_H_ |
|||
#define _NETCFG_H_ |
|||
#include <sys/types.h> |
|||
|
|||
#define ETC_DIR "/etc" |
|||
#define NETWORK_DIR "/etc/network" |
|||
#define DHCPCD_DIR "/etc/dhcpc" |
|||
#define INTERFACES_FILE "/etc/network/interfaces" |
|||
#define HOSTS_FILE "/etc/hosts" |
|||
#define NETWORKS_FILE "/etc/networks" |
|||
#define RESOLV_FILE "/etc/resolv.conf" |
|||
#define DHCPCD_FILE "/etc/dhcpc/config" |
|||
|
|||
|
|||
extern int netcfg_mkdir (char *path); |
|||
|
|||
extern int is_interface_up (char *inter); |
|||
|
|||
extern void get_name (char *name, char *p); |
|||
|
|||
extern void getif_start (); |
|||
|
|||
extern void getif_end (); |
|||
|
|||
extern char *get_ifdsc (const char *ifp); |
|||
|
|||
extern FILE *file_open (char *path); |
|||
|
|||
extern void dot2num (u_int32_t * num, char *dot); |
|||
|
|||
extern char *num2dot (u_int32_t num); |
|||
|
|||
extern void netcfg_die (struct debconfclient *client); |
|||
|
|||
extern void netcfg_get_common (struct debconfclient *client, char *interface, |
|||
char *hostname, char *domain, |
|||
u_int32_t nameservers[]); |
|||
|
|||
extern void netcfg_write_common (u_int32_t ipaddress, char *domain, |
|||
char *hostname, u_int32_t nameservers[]); |
|||
|
|||
#endif /* _NETCFG_H_ */ |
Loading…
Reference in new issue