Browse Source

lots of fixes and cleanups that make mii-lite work 100%

r15016
master
Joshua Kwan 18 years ago
parent
commit
7897182e80
  1. 21
      mii-lite.c
  2. 82
      netcfg-common.c
  3. 80
      netcfg.c
  4. 4
      netcfg.h

21
mii-lite.c

@ -41,14 +41,12 @@ method_t
mii_diag_status_lite (char *ifname)
{
u16 *data = NULL;
di_debug("poking at interface %s", ifname);
/* Open a basic socket. */
if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0)
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
di_debug("couldn't open skfd");
return STATIC;
di_error("Couldn't open skfd!");
return DUNNO;
}
/* Verify that the interface supports the ioctl(), and if
@ -60,25 +58,24 @@ mii_diag_status_lite (char *ifname)
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
data[0] = 0;
if (ioctl(skfd, 0x8947, &ifr) >= 0) {
if (ioctl(skfd, 0x8947, &ifr) >= 0)
new_ioctl_nums = 1;
} else if (ioctl(skfd, SIOCDEVPRIVATE, &ifr) >= 0) {
else if (ioctl(skfd, SIOCDEVPRIVATE, &ifr) >= 0)
new_ioctl_nums = 0;
} else {
else {
close(skfd);
di_log(DI_LOG_LEVEL_DEBUG, "none of the ioctls work!");
return DUNNO;
}
if ((mdio_read(skfd, (unsigned)data[0], 1) & 0x0004) == 0)
{
di_debug("ioctl: media state is: disconnected");
di_info("ioctl: media state for %s is: disconnected", ifname);
return STATIC;
}
else
{
close(skfd);
di_debug("ioctl: media state is: connected");
di_info("ioctl: media state for %s is: connected", ifname);
return DHCP;
}
}

82
netcfg-common.c

@ -75,6 +75,7 @@ char* essid = NULL;
/* IW socket for global use - init in main */
int wfd = 0;
int input_result = 0;
static int have_domain = 0;
@ -82,8 +83,7 @@ int my_debconf_input(struct debconfclient *client, char *priority,
char *template, char **p)
{
int ret = 0;
debconf_fset(client, template, "seen", "false");
debconf_input(client, priority, template);
input_result = debconf_input(client, priority, template);
ret = debconf_go(client);
debconf_get(client, template);
*p = client->value;
@ -219,6 +219,11 @@ char *find_in_devnames(const char* iface)
fclose(dn);
len = strlen(result);
if (result[len - 1] == '\n')
result[len - 1] = '\0';
return result;
}
@ -339,16 +344,7 @@ int netcfg_get_interface(struct debconfclient *client, char **interface,
size_t newchars;
char *downbuf;
/* Idempotency hack */
if (asprintf(&downbuf, "/sbin/ifconfig %s down", inter) != -1)
{
di_exec_shell_log(downbuf);
free(downbuf);
}
else
di_log(DI_LOG_LEVEL_ERROR, "asprintf failed trying to take interface %s down!",
inter);
ifconfig_down(inter);
ifdsc = get_ifdsc(client, inter);
newchars = strlen(inter) + strlen(ifdsc) + 5;
if (len < (strlen(ptr) + newchars)) {
@ -873,9 +869,9 @@ int netcfg_activate_static(struct debconfclient *client)
deconfigure_network();
/* configure loopback */
di_exec_shell_log("/sbin/ifconfig lo 127.0.0.1");
di_exec_shell_log("ifconfig lo 127.0.0.1");
snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s",
snprintf(buf, sizeof(buf), "ifconfig %s %s",
interface, inet_ntop (AF_INET, &ipaddress, ptr1, sizeof (ptr1)));
di_snprintfcat(buf, sizeof(buf), " netmask %s", inet_ntop (AF_INET, &netmask, ptr1, sizeof (ptr1)));
di_snprintfcat(buf, sizeof(buf), " broadcast %s",
@ -890,7 +886,7 @@ int netcfg_activate_static(struct debconfclient *client)
if (gateway.s_addr) {
snprintf(buf, sizeof(buf),
"/sbin/route add default gateway %s",
"route add default gateway %s",
inet_ntop (AF_INET, &gateway, ptr1, sizeof (ptr1)));
rv |= di_exec_shell_log(buf);
printf("rv = %i, buf = %s\n", rv, buf);
@ -1097,7 +1093,7 @@ int netcfg_activate_dhcp(struct debconfclient *client)
deconfigure_network();
/* setup loopback */
di_exec_shell_log("/sbin/ifconfig lo 127.0.0.1");
di_exec_shell_log("ifconfig lo 127.0.0.1");
/* load kernel module for network sockets silently */
di_exec_shell("modprobe af_packet");
@ -1105,18 +1101,18 @@ int netcfg_activate_dhcp(struct debconfclient *client)
/* get dhcp lease */
switch (dhcp_client) {
case PUMP:
snprintf(buf, sizeof(buf), "/sbin/pump -i %s", interface);
snprintf(buf, sizeof(buf), "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 -e %s", interface);
snprintf(buf, sizeof(buf), "dhclient -e %s", interface);
break;
case UDHCPC:
snprintf(buf, sizeof(buf), "/sbin/udhcpc -i %s -n",
snprintf(buf, sizeof(buf), "udhcpc -i %s -n",
interface);
if (dhcp_hostname)
di_snprintfcat(buf, sizeof(buf), " -H %s",
@ -1254,9 +1250,6 @@ automatic:
if (!empty_str(wconf.essid) || empty_str(client->value))
{
int i, success = 0;
/* Calculate here, we don't want to do it every time */
int len = strlen(iface) + sizeof("/sbin/ifconfig down") + 1;
char *cmd = malloc(len);
/* Default to any AP */
wconf.essid[0] = '\0';
@ -1272,11 +1265,8 @@ automatic:
for (i = 0; i <= MAX_SECS; i++)
{
snprintf(cmd, len, "/sbin/ifconfig %s up", iface);
di_exec_shell_log(cmd);
ifconfig_up(iface);
sleep (1);
iw_get_basic_config (wfd, iface, &wconf);
if (!empty_str(wconf.essid))
@ -1289,8 +1279,7 @@ automatic:
}
debconf_progress_step(client, 1);
snprintf(cmd, len, "/sbin/ifconfig %s down", iface);
di_exec_shell_log(cmd);
ifconfig_down(iface);
}
debconf_progress_stop(client);
@ -1423,3 +1412,40 @@ int netcfg_wireless_set_wep (struct debconfclient * client, char* iface)
return 0;
}
/* Utility functions. */
int ifconfig_up (char* iface)
{
char * cmd;
size_t len = sizeof("ifconfig up") + strlen(iface) + 1;
int ret;
if (!(cmd = malloc(len)))
return 1;
snprintf(cmd, len, "ifconfig %s up", iface);
ret = di_exec_shell(cmd);
free(cmd);
return ret;
}
int ifconfig_down (char* iface)
{
char * cmd;
size_t len = sizeof("ifconfig down") + strlen(iface) + 1;
int ret;
if (!(cmd = malloc(len)))
return 1;
snprintf(cmd, len, "ifconfig %s down", iface);
ret = di_exec_shell(cmd);
free(cmd);
return ret;
}

80
netcfg.c

@ -36,37 +36,28 @@
static method_t netcfg_method = DHCP;
int netcfg_get_method(struct debconfclient *client)
response_t netcfg_get_method(struct debconfclient *client)
{
int ret, asked = 1;
int iret, ret;
ret = debconf_input(client, "medium", "netcfg/use_dhcp");
di_debug("debconf_input returned %d", ret);
if (ret == 30)
asked = 0;
iret = debconf_input(client, "medium", "netcfg/use_dhcp");
ret = debconf_go(client);
di_debug("debconf_go returned %d", ret);
if (ret == 30)
return GO_BACK;
debconf_get(client, "netcfg/use_dhcp");
di_debug("hum, looks like client->value is %p", (void*)client->value);
if (ret == 30)
return GO_BACK;
if (strcmp(client->value, "true") == 0)
netcfg_method = DHCP;
if (strcmp(client->value, "true") == 0)
netcfg_method = DHCP;
else
netcfg_method = STATIC;
netcfg_method = STATIC;
if (asked == 0)
if (iret == 30)
return NOT_ASKED;
else
return 0;
}
return 0;
}
int main(void)
{
@ -74,7 +65,7 @@ int main(void)
enum { BACKUP, GET_INTERFACE, GET_METHOD, GET_DHCP, GET_STATIC, WCONFIG, QUIT } state = GET_INTERFACE;
static struct debconfclient *client;
static int requested_wireless_tools = 0;
response_t response;
response_t res;
/* initialize libd-i */
di_system_init("netcfg");
@ -99,29 +90,39 @@ int main(void)
}
break;
case GET_METHOD:
if ((response = netcfg_get_method(client)) == GO_BACK)
{
di_debug("wtf get_method said GO_BACK??");
if ((res = netcfg_get_method(client)) == GO_BACK)
state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
}
else
{
char * bah;
/* See if link is established? */
method_t tmp = mii_diag_status_lite(interface);
switch(tmp)
{
case DUNNO: bah = "dunno"; break;
case DHCP: bah = "connected"; break;
case STATIC: bah = "disconnected"; break;
default: bah = "completely fucked up!"; break;
}
di_log(DI_LOG_LEVEL_DEBUG, "mii proposed: %s", bah);
method_t mii_result;
ifconfig_up(interface);
mii_result = mii_diag_status_lite(interface);
ifconfig_down(interface);
di_debug("mii_result = %s",
mii_result == DHCP ? "DHCP" :
( mii_result == STATIC ? "static" :
( mii_result == DUNNO ? "dunno!" : "REALLY dunno")));
di_debug("res = %s",
res == NOT_ASKED ? "not asked" :
( res == GO_BACK ? "go back" : "unknown" ));
/* Don't override the user's choice. */
if (tmp != DUNNO && response == NOT_ASKED)
netcfg_method = tmp;
di_debug("netcfg_method = %s",
netcfg_method == DHCP ? "DHCP" :
( netcfg_method == STATIC ? "static" :
( netcfg_method == DUNNO ? "dunno!" : "REALLY dunno")));
if (mii_result != DUNNO && res == NOT_ASKED)
netcfg_method = mii_result;
di_debug("netcfg_method = %s",
netcfg_method == DHCP ? "DHCP" :
( netcfg_method == STATIC ? "static" :
( netcfg_method == DUNNO ? "dunno!" : "REALLY dunno")));
if (netcfg_method == DHCP)
state = GET_DHCP;
@ -129,6 +130,7 @@ int main(void)
state = GET_STATIC;
}
break;
case GET_DHCP:
if (netcfg_get_dhcp(client))
state = GET_METHOD;

4
netcfg.h

@ -18,6 +18,7 @@ typedef enum { DHCP, STATIC, DUNNO } method_t;
extern int netcfg_progress_displayed;
extern int wfd;
extern int input_result;
/* network config */
extern char *interface;
@ -78,4 +79,7 @@ extern int netcfg_wireless_set_wep (struct debconfclient *client, char* iface);
extern method_t mii_diag_status_lite (char *ifname);
extern int ifconfig_up (char*);
extern int ifconfig_down (char*);
#endif /* _NETCFG_H_ */

Loading…
Cancel
Save