Browse Source

indent -kr -i8 -nut *.c

r929
master
David Whedon 22 years ago
parent
commit
f6dceb6c66
  1. 229
      netcfg-dhcp.c
  2. 299
      netcfg-static.c
  3. 627
      netcfg.c

229
netcfg-dhcp.c

@ -40,157 +40,152 @@ static char *domain = NULL;
static u_int32_t ipaddress = 0;
static u_int32_t nameserver_array[4] = { 0 };
static struct debconfclient *client;
enum
{
PUMP,
DHCLIENT,
UDHCPC
}
dhcp_client_choices;
enum {
PUMP,
DHCLIENT,
UDHCPC
} dhcp_client_choices;
int dhcp_client = PUMP;
static char *dhcp_hostname = NULL;
char *
debconf_input (char *priority, char *template)
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;
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 ()
static void netcfg_get_dhcp()
{
if (dhcp_hostname)
{
free (dhcp_hostname);
dhcp_hostname = NULL;
}
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);
client->command (client, "subst", "netcfg/confirm_dhcp",
"dhcp_hostname",
(dhcp_hostname ? dhcp_hostname : "<none>"), NULL);
if (dhcp_hostname) {
free(dhcp_hostname);
dhcp_hostname = NULL;
}
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);
client->command(client, "subst", "netcfg/confirm_dhcp",
"dhcp_hostname",
(dhcp_hostname ? dhcp_hostname : "<none>"), NULL);
}
static void
netcfg_write_dhcp ()
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);
if (dhcp_hostname)
fprintf (fp, "\thostname %s\n", dhcp_hostname);
fclose (fp);
}
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);
if (dhcp_hostname)
fprintf(fp, "\thostname %s\n", dhcp_hostname);
fclose(fp);
}
}
static void
netcfg_activate_dhcp ()
static void netcfg_activate_dhcp()
{
char buf[128];
di_execlog ("/sbin/ifconfig lo 127.0.0.1");
switch (dhcp_client)
{
case PUMP:
snprintf (buf, sizeof (buf), "/sbin/pump -i %s", interface);
if (dhcp_hostname)
di_snprintfcat (buf, sizeof (buf), " -h %s", dhcp_hostname);
break;
case DHCLIENT:
snprintf (buf, sizeof (buf), "/sbin/dhclient %s", interface);
break;
case UDHCPC:
snprintf (buf, sizeof (buf), "/sbin/udhcpc -i %s -n", interface);
if (dhcp_hostname)
di_snprintfcat (buf, sizeof (buf), " -H %s", dhcp_hostname);
break;
}
if (di_execlog (buf))
netcfg_die (client);
char buf[128];
di_execlog("/sbin/ifconfig lo 127.0.0.1");
switch (dhcp_client) {
case PUMP:
snprintf(buf, sizeof(buf), "/sbin/pump -i %s", interface);
if (dhcp_hostname)
di_snprintfcat(buf, sizeof(buf), " -h %s",
dhcp_hostname);
break;
case DHCLIENT:
snprintf(buf, sizeof(buf), "/sbin/dhclient %s", interface);
break;
case UDHCPC:
snprintf(buf, sizeof(buf), "/sbin/udhcpc -i %s -n",
interface);
if (dhcp_hostname)
di_snprintfcat(buf, sizeof(buf), " -H %s",
dhcp_hostname);
break;
}
if (di_execlog(buf))
netcfg_die(client);
}
int
main (int argc, char *argv[])
int main(int argc, char *argv[])
{
char *ptr;
char *nameservers = NULL;
int finished = 0;
struct stat buf;
client = debconfclient_new ();
client->command (client, "title", "DHCP Network Configuration", NULL);
char *ptr;
char *nameservers = NULL;
int finished = 0;
struct stat buf;
client = debconfclient_new();
client->command(client, "title", "DHCP Network Configuration",
NULL);
if (stat ("/sbin/dhclient", &buf) == 0)
dhcp_client = DHCLIENT;
else if (stat ("/sbin/pump", &buf) == 0)
dhcp_client = PUMP;
else if (stat ("/sbin/udhcpc", &buf) == 0)
dhcp_client = UDHCPC;
else
{
client->command (client, "input", "critical", "netcfg/no_dhcp_client",
NULL);
client->command (client, "go", NULL);
exit (1);
}
if (stat("/sbin/dhclient", &buf) == 0)
dhcp_client = DHCLIENT;
else if (stat("/sbin/pump", &buf) == 0)
dhcp_client = PUMP;
else if (stat("/sbin/udhcpc", &buf) == 0)
dhcp_client = UDHCPC;
else {
client->command(client, "input", "critical",
"netcfg/no_dhcp_client", NULL);
client->command(client, "go", NULL);
exit(1);
}
do
{
netcfg_get_common (client, &interface, &hostname, &domain,
&nameservers);
do {
netcfg_get_common(client, &interface, &hostname, &domain,
&nameservers);
client->command (client, "subst", "netcfg/confirm_dhcp", "interface",
interface, NULL);
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",
"hostname", hostname, NULL);
client->command (client, "subst", "netcfg/confirm_dhcp", "domain",
(domain ? domain : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_dhcp",
"domain", (domain ? domain : "<none>"),
NULL);
netcfg_nameservers_to_array (nameservers, nameserver_array);
netcfg_nameservers_to_array(nameservers, nameserver_array);
client->command (client, "subst", "netcfg/confirm_dhcp",
"nameservers", (nameservers ? nameservers : "<none>"),
NULL);
netcfg_get_dhcp ();
client->command(client, "subst", "netcfg/confirm_dhcp",
"nameservers",
(nameservers ? nameservers : "<none>"),
NULL);
netcfg_get_dhcp();
ptr = debconf_input ("medium", "netcfg/confirm_dhcp");
ptr = debconf_input("medium", "netcfg/confirm_dhcp");
if (strstr (ptr, "true"))
finished = 1;
}
while (!finished);
if (strstr(ptr, "true"))
finished = 1;
}
while (!finished);
netcfg_write_dhcp ();
netcfg_write_common (ipaddress, domain, hostname, nameserver_array);
netcfg_write_dhcp();
netcfg_write_common(ipaddress, domain, hostname, nameserver_array);
debconf_input ("medium", "netcfg/do_dhcp");
netcfg_activate_dhcp ();
debconf_input("medium", "netcfg/do_dhcp");
netcfg_activate_dhcp();
return 0;
return 0;
}

299
netcfg-static.c

@ -46,201 +46,202 @@ static u_int32_t gateway = 0;
static u_int32_t pointopoint = 0;
static struct debconfclient *client;
char *
debconf_input (char *priority, char *template)
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;
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 ()
static void netcfg_get_static()
{
char *ptr;
char *ptr;
ipaddress = network = broadcast = netmask = gateway = pointopoint = 0;
ipaddress = network = broadcast = netmask = gateway = pointopoint =
0;
ptr = debconf_input ("critical", "netcfg/get_ipaddress");
dot2num (&ipaddress, ptr);
ptr = debconf_input("critical", "netcfg/get_ipaddress");
dot2num(&ipaddress, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"ipaddress",
(ipaddress ? num2dot (ipaddress) : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_static",
"ipaddress",
(ipaddress ? num2dot(ipaddress) : "<none>"), NULL);
if (strncmp (interface, "plip", 4) == 0
|| strncmp (interface, "slip", 4) == 0)
{
ptr = debconf_input ("critical", "netcfg/get_pointopoint");
dot2num (&pointopoint, ptr);
if (strncmp(interface, "plip", 4) == 0
|| strncmp(interface, "slip", 4) == 0) {
ptr = debconf_input("critical", "netcfg/get_pointopoint");
dot2num(&pointopoint, ptr);
dot2num (&netmask, "255.255.255.255");
network = ipaddress;
gateway = pointopoint;
}
else
{
ptr = debconf_input ("critical", "netcfg/get_netmask");
dot2num (&netmask, ptr);
dot2num(&netmask, "255.255.255.255");
network = ipaddress;
gateway = pointopoint;
} else {
ptr = debconf_input("critical", "netcfg/get_netmask");
dot2num(&netmask, ptr);
ptr = debconf_input ("critical", "netcfg/get_gateway");
dot2num (&gateway, ptr);
ptr = debconf_input("critical", "netcfg/get_gateway");
dot2num(&gateway, ptr);
network = ipaddress & netmask;
network = ipaddress & netmask;
if (gateway && ((gateway & netmask) != network))
{
client->command (client, "input", "high",
"netcfg/gateway_unreachable", NULL);
client->command (client, "go", NULL);
}
}
if (gateway && ((gateway & netmask) != network)) {
client->command(client, "input", "high",
"netcfg/gateway_unreachable",
NULL);
client->command(client, "go", NULL);
}
}
client->command (client, "subst", "netcfg/confirm_static", "netmask",
(netmask ? num2dot (netmask) : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_static",
"netmask", (netmask ? num2dot(netmask) : "<none>"),
NULL);
client->command (client, "subst", "netcfg/confirm_static", "gateway",
(gateway ? num2dot (gateway) : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_static",
"gateway", (gateway ? num2dot(gateway) : "<none>"),
NULL);
client->command (client, "subst", "netcfg/confirm_static", "pointopoint",
(pointopoint ? num2dot (pointopoint) : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_static",
"pointopoint",
(pointopoint ? num2dot(pointopoint) : "<none>"),
NULL);
broadcast = (network | ~netmask);
broadcast = (network | ~netmask);
}
static int
netcfg_write_static ()
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));
if (gateway)
fprintf (fp, "\tgateway %s\n", num2dot (gateway));
if (pointopoint)
fprintf (fp, "\tpointopoint %s\n", num2dot (pointopoint));
fclose (fp);
}
else
goto error;
return 0;
error:
return -1;
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));
if (gateway)
fprintf(fp, "\tgateway %s\n", num2dot(gateway));
if (pointopoint)
fprintf(fp, "\tpointopoint %s\n",
num2dot(pointopoint));
fclose(fp);
} else
goto error;
return 0;
error:
return -1;
}
static int
netcfg_activate_static ()
static int netcfg_activate_static()
{
int rv = 0;
char buf[256];
int rv = 0;
char buf[256];
#ifdef __GNU__
/* I had to do something like this ? */
/* di_execlog ("settrans /servers/socket/2 -fg"); */
di_execlog ("settrans /servers/socket/2 --goaway");
snprintf (buf, sizeof (buf),
"settrans -fg /servers/socket/2 /hurd/pfinet --interface=%s --address=%s",
interface, num2dot (ipaddress));
di_snprintfcat (buf, sizeof (buf), " --netmask=%s", num2dot (netmask));
buf[sizeof (buf) - 1] = '\0';
di_execlog("settrans /servers/socket/2 --goaway");
snprintf(buf, sizeof(buf),
"settrans -fg /servers/socket/2 /hurd/pfinet --interface=%s --address=%s",
interface, num2dot(ipaddress));
di_snprintfcat(buf, sizeof(buf), " --netmask=%s",
num2dot(netmask));
buf[sizeof(buf) - 1] = '\0';
if (gateway)
snprintf (buf, sizeof (buf), " --gateway=%s", num2dot (gateway));
if (gateway)
snprintf(buf, sizeof(buf), " --gateway=%s",
num2dot(gateway));
rv |= di_execlog (buf);
rv |= di_execlog(buf);
#else
di_execlog ("/sbin/ifconfig lo 127.0.0.1");
snprintf (buf, sizeof (buf), "/sbin/ifconfig %s %s",
interface, num2dot (ipaddress));
di_snprintfcat (buf, sizeof (buf), " netmask %s", num2dot (netmask));
di_snprintfcat (buf, sizeof (buf), " broadcast %s", num2dot (broadcast));
buf[sizeof (buf) - 1] = '\0';
if (pointopoint)
di_snprintfcat (buf, sizeof (buf), " pointopoint %s", num2dot (pointopoint));
rv |= di_execlog (buf);
if (gateway)
{
snprintf (buf, sizeof (buf), "/sbin/route add default gateway %s",
num2dot (gateway));
rv |= di_execlog (buf);
}
di_execlog("/sbin/ifconfig lo 127.0.0.1");
snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s",
interface, num2dot(ipaddress));
di_snprintfcat(buf, sizeof(buf), " netmask %s", num2dot(netmask));
di_snprintfcat(buf, sizeof(buf), " broadcast %s",
num2dot(broadcast));
buf[sizeof(buf) - 1] = '\0';
if (pointopoint)
di_snprintfcat(buf, sizeof(buf), " pointopoint %s",
num2dot(pointopoint));
rv |= di_execlog(buf);
if (gateway) {
snprintf(buf, sizeof(buf),
"/sbin/route add default gateway %s",
num2dot(gateway));
rv |= di_execlog(buf);
}
#endif
if (rv != 0)
{
client->command (client, "input", "critical", "netcfg/error_cfg", NULL);
client->command (client, "go", NULL);
}
return 0;
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 main(int argc, char *argv[])
{
int finished = 0;
char *ptr;
char *nameservers = NULL;
client = debconfclient_new ();
client->command (client, "title", "Static Network Configuration", NULL);
int finished = 0;
char *ptr;
char *nameservers = NULL;
client = debconfclient_new();
client->command(client, "title", "Static Network Configuration",
NULL);
do
{
netcfg_get_common (client, &interface, &hostname, &domain,
&nameservers);
do {
netcfg_get_common(client, &interface, &hostname, &domain,
&nameservers);
client->command (client, "subst", "netcfg/confirm_static", "interface",
interface, NULL);
client->command(client, "subst", "netcfg/confirm_static",
"interface", interface, NULL);
client->command (client, "subst", "netcfg/confirm_static",
"hostname", hostname, NULL);
client->command(client, "subst", "netcfg/confirm_static",
"hostname", hostname, NULL);
client->command (client, "subst", "netcfg/confirm_static", "domain",
(domain ? domain : "<none>"), NULL);
client->command(client, "subst", "netcfg/confirm_static",
"domain", (domain ? domain : "<none>"),
NULL);
client->command (client, "subst", "netcfg/confirm_static",
"nameservers", (nameservers ? nameservers : "<none>"),
NULL);
netcfg_nameservers_to_array (nameservers, nameserver_array);
client->command(client, "subst", "netcfg/confirm_static",
"nameservers",
(nameservers ? nameservers : "<none>"),
NULL);
netcfg_nameservers_to_array(nameservers, nameserver_array);
netcfg_get_static ();
netcfg_get_static();
ptr = debconf_input ("medium", "netcfg/confirm_static");
ptr = debconf_input("medium", "netcfg/confirm_static");
if (strstr (ptr, "true"))
finished = 1;
if (strstr(ptr, "true"))
finished = 1;
}
while (!finished);
}
while (!finished);
netcfg_write_common (ipaddress, domain, hostname, nameserver_array);
netcfg_write_static ();
netcfg_activate_static ();
netcfg_write_common(ipaddress, domain, hostname, nameserver_array);
netcfg_write_static();
netcfg_activate_static();
return 0;
return 0;
}

627
netcfg.c

@ -35,70 +35,65 @@
#include "netcfg.h"
static char *
debconf_input (struct debconfclient *client, char *priority, char *template)
static char *debconf_input(struct debconfclient *client, 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;
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;
}
int
is_interface_up (char *inter)
int is_interface_up(char *inter)
{
struct ifreq ifr;
int sfd = -1, ret = -1;
struct ifreq ifr;
int sfd = -1, ret = -1;
if ((sfd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
goto int_up_done;
if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
goto int_up_done;
strncpy (ifr.ifr_name, inter, sizeof (ifr.ifr_name));
strncpy(ifr.ifr_name, inter, sizeof(ifr.ifr_name));
if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
goto int_up_done;
if (ioctl(sfd, SIOCGIFFLAGS, &ifr) < 0)
goto int_up_done;
ret = (ifr.ifr_flags & IFF_UP) ? 1 : 0;
ret = (ifr.ifr_flags & IFF_UP) ? 1 : 0;
int_up_done:
if (sfd != -1)
close (sfd);
return ret;
int_up_done:
if (sfd != -1)
close(sfd);
return ret;
}
void
get_name (char *name, char *p)
void get_name(char *name, char *p)
{
while (isspace (*p))
p++;
while (*p)
{
if (isspace (*p))
break;
if (*p == ':')
{ /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit (*p))
*name++ = *p++;
if (*p != ':')
{ /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return;
while (isspace(*p))
p++;
while (*p) {
if (isspace(*p))
break;
if (*p == ':') { /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit(*p))
*name++ = *p++;
if (*p != ':') { /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return;
}
@ -106,345 +101,313 @@ get_name (char *name, char *p)
static FILE *ifs = NULL;
static char ibuf[512];
void
getif_start ()
void getif_start()
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
if ((ifs = fopen ("/proc/net/dev", "r")) != NULL)
{
fgets (ibuf, sizeof (ibuf), ifs); /* eat header */
fgets (ibuf, sizeof (ibuf), ifs); /* ditto */
}
return;
if (ifs != NULL) {
fclose(ifs);
ifs = NULL;
}
if ((ifs = fopen("/proc/net/dev", "r")) != NULL) {
fgets(ibuf, sizeof(ibuf), ifs); /* eat header */
fgets(ibuf, sizeof(ibuf), ifs); /* ditto */
}
return;
}
char *
getif (int all)
char *getif(int all)
{
char rbuf[512];
if (ifs == NULL)
return NULL;
if (fgets (rbuf, sizeof (rbuf), ifs) != NULL)
{
get_name (ibuf, rbuf);
if (!strcmp (ibuf, "lo")) /* ignore the loopback */
return getif (all); /* seriously doubt there is an infinite number of lo devices */
if (all || is_interface_up (ibuf) == 1)
return ibuf;
}
return NULL;
char rbuf[512];
if (ifs == NULL)
return NULL;
if (fgets(rbuf, sizeof(rbuf), ifs) != NULL) {
get_name(ibuf, rbuf);
if (!strcmp(ibuf, "lo")) /* ignore the loopback */
return getif(all); /* seriously doubt there is an infinite number of lo devices */
if (all || is_interface_up(ibuf) == 1)
return ibuf;
}
return NULL;
}
void
getif_end ()
void getif_end()
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
return;
if (ifs != NULL) {
fclose(ifs);
ifs = NULL;
}
return;
}
char *
get_ifdsc (const char *ifp)
char *get_ifdsc(const char *ifp)
{
int i;
struct if_alist_struct
{
char *interface;
char *description;
}
interface_alist[] =
{
{
"eth", "Ethernet or Fast Ethernet"}
,
{
"pcmcia", "PC-Card (PCMCIA) Ethernet or Token Ring"}
,
{
"tr", "Token Ring"}
,
{
"arc", "Arcnet"}
,
{
"slip", "Serial-line IP"}
,
{
"plip", "Parallel-line IP"}
,
{
NULL, NULL}
};
for (i = 0; interface_alist[i].interface != NULL; i++)
if (!strncmp (ifp, interface_alist[i].interface,
strlen (interface_alist[i].interface)))
return interface_alist[i].description;
return "unknown interface";
int i;
struct if_alist_struct {
char *interface;
char *description;
} interface_alist[] = {
{
"eth", "Ethernet or Fast Ethernet"}
, {
"pcmcia", "PC-Card (PCMCIA) Ethernet or Token Ring"}
, {
"tr", "Token Ring"}
, {
"arc", "Arcnet"}
, {
"slip", "Serial-line IP"}
, {
"plip", "Parallel-line IP"}
, {
NULL, NULL}
};
for (i = 0; interface_alist[i].interface != NULL; i++)
if (!strncmp(ifp, interface_alist[i].interface,
strlen(interface_alist[i].interface)))
return interface_alist[i].description;
return "unknown interface";
}
FILE *
file_open (char *path)
FILE *file_open(char *path)
{
FILE *fp;
if ((fp = fopen (path, "w")))
return fp;
else
{
fprintf (stderr, "%s\n", path);
perror ("fopen");
return NULL;
}
FILE *fp;
if ((fp = fopen(path, "w")))
return fp;
else {
fprintf(stderr, "%s\n", path);
perror("fopen");
return NULL;
}
}
void
dot2num (u_int32_t * num, char *dot)
void dot2num(u_int32_t * num, char *dot)
{
char *p = dot - 1;
char *e;
int ix;
unsigned long val;
if (!dot)
goto exit;
*num = 0;
for (ix = 0; ix < 4; ix++)
{
*num <<= 8;
p++;
val = strtoul (p, &e, 10);
if (e == p)
val = 0;
else if (val > 255)
goto exit;
*num += val;
/*printf("%#8x, %#2x\n", *num, val); */
if (ix < 3 && *e != '.')
goto exit;
p = e;
}
return;
exit:
*num = 0;
char *p = dot - 1;
char *e;
int ix;
unsigned long val;
if (!dot)
goto exit;
*num = 0;
for (ix = 0; ix < 4; ix++) {
*num <<= 8;
p++;
val = strtoul(p, &e, 10);
if (e == p)
val = 0;
else if (val > 255)
goto exit;
*num += val;
/*printf("%#8x, %#2x\n", *num, val); */
if (ix < 3 && *e != '.')
goto exit;
p = e;
}
return;
exit:
*num = 0;
}
static char num2dot_buf[16];
char *
num2dot (u_int32_t num)
char *num2dot(u_int32_t num)
{
int byte[4];
int ix;
char *dot = num2dot_buf;
for (ix = 3; ix >= 0; ix--)
{
byte[ix] = num & 0xff;
num >>= 8;
}
sprintf (dot, "%d.%d.%d.%d", byte[0], byte[1], byte[2], byte[3]);
return dot;
int byte[4];
int ix;
char *dot = num2dot_buf;
for (ix = 3; ix >= 0; ix--) {
byte[ix] = num & 0xff;
num >>= 8;
}
sprintf(dot, "%d.%d.%d.%d", byte[0], byte[1], byte[2], byte[3]);
return dot;
}
void
netcfg_die (struct debconfclient *client)
void netcfg_die(struct debconfclient *client)
{
client->command (client, "input", "high", "netcfg/error", NULL);
client->command (client, "go", NULL);
exit (1);
client->command(client, "input", "high", "netcfg/error", NULL);
client->command(client, "go", NULL);
exit(1);
}
static void
netcfg_get_interface (struct debconfclient *client, char **interface)
netcfg_get_interface(struct debconfclient *client, char **interface)
{
char *inter;
int len;
int newchars;
char *ptr;
int num_interfaces = 0;
if (*interface)
{
free (*interface);
*interface = NULL;
}
if (!(ptr = malloc (128)))
netcfg_die (client);
len = 128;
*ptr = '\0';
getif_start ();
while ((inter = getif (1)) != NULL)
{
newchars = strlen (inter) + strlen (get_ifdsc (inter)) + 5;
if (len < (strlen (ptr) + newchars))
{
if (!(ptr = realloc (ptr, len + newchars + 128)))
goto error;
len += newchars + 128;
}
di_snprintfcat (ptr, len, "%s: %s, ", inter, get_ifdsc (inter));
num_interfaces++;
}
getif_end ();
if (num_interfaces == 0)
{
client->command (client, "input", "high", "netcfg/no_interfaces", NULL);
client->command (client, "go", NULL);
free (ptr);
exit (1);
}
else if (num_interfaces > 1)
{
client->command (client, "subst", "netcfg/choose_interface",
"ifchoices", ptr, NULL);
free (ptr);
inter = debconf_input (client, "high", "netcfg/choose_interface");
if (!inter)
netcfg_die (client);
}
else if (num_interfaces == 1)
inter = ptr;
/* grab just the interface name, not the description too */
*interface = inter;
ptr = strchr (inter, ':');
if (ptr == NULL)
goto error;
*ptr = '\0';
*interface = strdup (*interface);
return;
error:
if (ptr)
free (ptr);
netcfg_die (client);
char *inter;
int len;
int newchars;
char *ptr;
int num_interfaces = 0;
if (*interface) {
free(*interface);
*interface = NULL;
}
if (!(ptr = malloc(128)))
netcfg_die(client);
len = 128;
*ptr = '\0';
getif_start();
while ((inter = getif(1)) != NULL) {
newchars = strlen(inter) + strlen(get_ifdsc(inter)) + 5;
if (len < (strlen(ptr) + newchars)) {
if (!(ptr = realloc(ptr, len + newchars + 128)))
goto error;
len += newchars + 128;
}
di_snprintfcat(ptr, len, "%s: %s, ", inter,
get_ifdsc(inter));
num_interfaces++;
}
getif_end();
if (num_interfaces == 0) {
client->command(client, "input", "high",
"netcfg/no_interfaces", NULL);
client->command(client, "go", NULL);
free(ptr);
exit(1);
} else if (num_interfaces > 1) {
client->command(client, "subst", "netcfg/choose_interface",
"ifchoices", ptr, NULL);
free(ptr);
inter =
debconf_input(client, "high",
"netcfg/choose_interface");
if (!inter)
netcfg_die(client);
} else if (num_interfaces == 1)
inter = ptr;
/* grab just the interface name, not the description too */
*interface = inter;
ptr = strchr(inter, ':');
if (ptr == NULL)
goto error;
*ptr = '\0';
*interface = strdup(*interface);
return;
error:
if (ptr)
free(ptr);
netcfg_die(client);
}
void
netcfg_get_common (struct debconfclient *client, char **interface,
char **hostname, char **domain, char **nameservers)
netcfg_get_common(struct debconfclient *client, char **interface,
char **hostname, char **domain, char **nameservers)
{
char *ptr;
netcfg_get_interface (client, interface);
if (*hostname)
{
free (*hostname);
*hostname = NULL;
}
*hostname =
strdup (debconf_input (client, "medium", "netcfg/get_hostname"));
if (*domain)
{
free (*domain);
}
*domain = NULL;
if ((ptr = debconf_input (client, "medium", "netcfg/get_domain")))
*domain = strdup (ptr);
if (*nameservers)
{
free (*nameservers);
}
*nameservers = NULL;
if ( (ptr = debconf_input (client, "medium", "netcfg/get_nameservers")))
*nameservers = strdup (ptr);
char *ptr;
netcfg_get_interface(client, interface);
if (*hostname) {
free(*hostname);
*hostname = NULL;
}
*hostname =
strdup(debconf_input(client, "medium", "netcfg/get_hostname"));
if (*domain) {
free(*domain);
}
*domain = NULL;
if ((ptr = debconf_input(client, "medium", "netcfg/get_domain")))
*domain = strdup(ptr);
if (*nameservers) {
free(*nameservers);
}
*nameservers = NULL;
if ((ptr =
debconf_input(client, "medium", "netcfg/get_nameservers")))
*nameservers = strdup(ptr);
}
void
netcfg_nameservers_to_array (char *nameservers, u_int32_t array[])
void netcfg_nameservers_to_array(char *nameservers, u_int32_t array[])
{
char *save, *ptr, *ns;
char *save, *ptr, *ns;
if (nameservers) {
save = ptr = strdup(nameservers);
if (nameservers)
{
save = ptr = strdup (nameservers);
ns = strtok_r (ptr, " \n\t", &ptr);
dot2num (&array[0], ns);
ns = strtok_r(ptr, " \n\t", &ptr);
dot2num(&array[0], ns);
ns = strtok_r (NULL, " \n\t", &ptr);
dot2num (&array[1], ns);
ns = strtok_r(NULL, " \n\t", &ptr);
dot2num(&array[1], ns);
ns = strtok_r (NULL, " \n\t", &ptr);
dot2num (&array[2], ns);
ns = strtok_r(NULL, " \n\t", &ptr);
dot2num(&array[2], ns);
array[3] = 0;
free (save);
}
else
array[0] = 0;
array[3] = 0;
free(save);
} else
array[0] = 0;
}
void
netcfg_write_common (u_int32_t ipaddress, char *domain, char *hostname,
u_int32_t nameservers[])
netcfg_write_common(u_int32_t ipaddress, char *domain, char *hostname,
u_int32_t nameservers[])
{
FILE *fp;
if ((fp = file_open (HOSTS_FILE)))
{
fprintf (fp, "127.0.0.1\tlocalhost\n");
if (ipaddress)
{
if (domain)
fprintf (fp, "%s\t%s.%s\t%s\n", num2dot (ipaddress),
hostname, domain, hostname);
else
fprintf (fp, "%s\t%s\n", num2dot (ipaddress), hostname);
}
fclose (fp);
}
if ((fp = file_open (RESOLV_FILE)))
{
int i = 0;
if (domain)
fprintf (fp, "search %s\n", domain);
while (nameservers[i])
fprintf (fp, "nameserver %s\n", num2dot (nameservers[i++]));
fclose (fp);
}
FILE *fp;
if ((fp = file_open(HOSTS_FILE))) {
fprintf(fp, "127.0.0.1\tlocalhost\n");
if (ipaddress) {
if (domain)
fprintf(fp, "%s\t%s.%s\t%s\n",
num2dot(ipaddress), hostname,
domain, hostname);
else
fprintf(fp, "%s\t%s\n", num2dot(ipaddress),
hostname);
}
fclose(fp);
}
if ((fp = file_open(RESOLV_FILE))) {
int i = 0;
if (domain)
fprintf(fp, "search %s\n", domain);
while (nameservers[i])
fprintf(fp, "nameserver %s\n",
num2dot(nameservers[i++]));
fclose(fp);
}
}

Loading…
Cancel
Save