Browse Source

finish up the code I did kinda halfway

r24673
tags/1.08
Joshua Kwan 17 years ago
parent
commit
c2fa8d7a0b
4 changed files with 94 additions and 61 deletions
  1. +31
    -2
      dhcp.c
  2. +61
    -1
      netcfg-common.c
  3. +2
    -0
      netcfg.h
  4. +0
    -58
      static.c

+ 31
- 2
dhcp.c View File

@@ -418,13 +418,13 @@ int netcfg_activate_dhcp (struct debconfclient *client)
}

/* Make sure we have NS going if the DHCP server didn't serve it up */
if (resolv_conf_entries() == 0)
if (resolv_conf_entries() <= 0)
{
char *nameservers = NULL;
if (netcfg_get_nameservers (client, &nameservers) == GO_BACK)
{
state = DHCP_OPTIONS;
state = ASK_OPTIONS;
break;
}

@@ -557,3 +557,32 @@ int netcfg_activate_dhcp (struct debconfclient *client)
}
}

/* returns number of 'nameserver' entries in resolv.conf */
int resolv_conf_entries (void)
{
FILE *f;
int count = 0;

if ((f = fopen("/etc/resolv.conf", "r")) != NULL)
{
char buf[256];

while (fgets(buf, 256, f) != NULL)
{
char *ptr;
if ((ptr = strchr(buf, ' ')) != NULL)
{
*ptr = '\0';
if (strcmp(buf, "nameserver") == 0)
count++;
}
}

fclose(f);
}
else
count = -1;

return count;
}

+ 61
- 1
netcfg-common.c View File

@@ -42,11 +42,15 @@
/* Set if there is currently a progress bar displayed. */
int netcfg_progress_displayed = 0;

/* IP address vars */
struct in_addr ipaddress = { 0 };
struct in_addr gateway = { 0 };
struct in_addr nameserver_array[4] = { { 0 }, };

/* network config */
char *interface = NULL;
char *hostname = NULL;
char *domain = NULL;
struct in_addr ipaddress = { 0 };
int have_domain = 0;

/* File descriptors for ioctls and such */
@@ -769,3 +773,59 @@ void reap_old_files (void)
while (*ptr)
unlink(*ptr++);
}

void netcfg_nameservers_to_array(char *nameservers, struct in_addr array[])
{
char *save, *ptr, *ns;
int i;

if (nameservers) {
save = ptr = strdup(nameservers);

for (i = 0; i < 3; i++)
{
ns = strtok_r(ptr, " \n\t", &ptr);
if (ns)
inet_pton (AF_INET, ns, &array[i]);
else
array[i].s_addr = 0;
}

array[3].s_addr = 0;
free(save);
} else
array[0].s_addr = 0;
}

int netcfg_get_nameservers (struct debconfclient *client, char **nameservers)
{
char *ptr, ptr1[INET_ADDRSTRLEN];
int ret;
if (*nameservers)
ptr = *nameservers;
else if (gateway.s_addr)
{
inet_ntop (AF_INET, &gateway, ptr1, sizeof (ptr1));
ptr = ptr1;
}
else
ptr = "";
debconf_set(client, "netcfg/get_nameservers", ptr);
debconf_input(client, "critical", "netcfg/get_nameservers");
ret = debconf_go(client);

if (ret)
return ret;

debconf_get(client, "netcfg/get_nameservers");
ptr = client->value;

if (*nameservers)
free(*nameservers);
*nameservers = NULL;
if (ptr)
*nameservers = strdup(ptr);
return ret;
}

+ 2
- 0
netcfg.h View File

@@ -95,6 +95,8 @@ extern int netcfg_get_static(struct debconfclient *client);

extern int netcfg_activate_dhcp(struct debconfclient *client);

extern int resolv_conf_entries (void);

extern int ask_dhcp_options (struct debconfclient *client);
extern int netcfg_activate_static(struct debconfclient *client);



+ 0
- 58
static.c View File

@@ -11,11 +11,9 @@
#include <assert.h>

struct in_addr old_ipaddress = { 0 };
struct in_addr nameserver_array[4] = { { 0 }, };
struct in_addr network = { 0 };
struct in_addr broadcast = { 0 };
struct in_addr netmask = { 0 };
struct in_addr gateway = { 0 };
struct in_addr pointopoint = { 0 };

int netcfg_get_ipaddress(struct debconfclient *client)
@@ -176,62 +174,6 @@ int netcfg_get_gateway(struct debconfclient *client)
return 0;
}

int netcfg_get_nameservers (struct debconfclient *client, char **nameservers)
{
char *ptr, ptr1[INET_ADDRSTRLEN];
int ret;
if (*nameservers)
ptr = *nameservers;
else if (gateway.s_addr)
{
inet_ntop (AF_INET, &gateway, ptr1, sizeof (ptr1));
ptr = ptr1;
}
else
ptr = "";
debconf_set(client, "netcfg/get_nameservers", ptr);
debconf_input(client, "critical", "netcfg/get_nameservers");
ret = debconf_go(client);

if (ret)
return ret;

debconf_get(client, "netcfg/get_nameservers");
ptr = client->value;

if (*nameservers)
free(*nameservers);
*nameservers = NULL;
if (ptr)
*nameservers = strdup(ptr);
return ret;
}

void netcfg_nameservers_to_array(char *nameservers, struct in_addr array[])
{
char *save, *ptr, *ns;
int i;

if (nameservers) {
save = ptr = strdup(nameservers);

for (i = 0; i < 3; i++)
{
ns = strtok_r(ptr, " \n\t", &ptr);
if (ns)
inet_pton (AF_INET, ns, &array[i]);
else
array[i].s_addr = 0;
}

array[3].s_addr = 0;
free(save);
} else
array[0].s_addr = 0;
}

static int netcfg_write_static(char *domain, struct in_addr nameservers[])
{
char ptr1[INET_ADDRSTRLEN];


Loading…
Cancel
Save