int ifc_configure(const char *ifname, in_addr_t address, in_addr_t gateway) { in_addr_t netmask = ~0; (void) gateway; ifc_init(); if (ifc_up(ifname)) { LOGE("%s() Failed to turn on interface %s: %s", __func__, ifname, strerror(errno)); ifc_close(); return -1; } if (ifc_set_addr(ifname, address)) { LOGE("%s() Failed to set ipaddr %s: %s", __func__, ipaddr_to_string(address), strerror(errno)); ifc_down(ifname); ifc_close(); return -1; } if (ifc_set_mask(ifname, netmask)) { LOGE("%s() failed to set netmask %s: %s", __func__, ipaddr_to_string(netmask), strerror(errno)); ifc_down(ifname); ifc_close(); return -1; } ifc_close(); return 0; }
int ifc_configure(const char *ifname, in_addr_t address, in_addr_t netmask, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2) { #if 0 char dns_prop_name[PROPERTY_KEY_MAX]; #endif ifc_init(); if (ifc_up(ifname)) { printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno)); ifc_close(); return -1; } if (ifc_set_addr(ifname, address)) { printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno)); ifc_close(); return -1; } if (ifc_set_mask(ifname, netmask)) { printerr("failed to set netmask %s: %s\n", ipaddr_to_string(netmask), strerror(errno)); ifc_close(); return -1; } if (ifc_create_default_route(ifname, gateway)) { printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno)); ifc_close(); return -1; } ifc_close(); #if 0 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname); property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname); property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); #endif return 0; }
int ifc_configure(const char *ifname, in_addr_t address, uint32_t prefixLength, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2) { char dns_prop_name[PROPERTY_KEY_MAX]; ifc_init(); if (ifc_up(ifname)) { printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno)); ifc_close(); return -1; } if (ifc_set_addr(ifname, address)) { printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno)); ifc_close(); return -1; } if (ifc_set_prefixLength(ifname, prefixLength)) { printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno)); ifc_close(); return -1; } if (ifc_create_default_route(ifname, gateway)) { printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno)); ifc_close(); return -1; } ifc_close(); snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname); property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname); property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.gw", ifname); property_set(dns_prop_name, gateway ? ipaddr_to_string(gateway) : ""); return 0; }
static void InitParameters(void) { char cfg[128]; char ip[4][16]; char wifiippass[128]; int i; unsigned int addr = 0; unsigned int mask = 0; unsigned int gateway = 0; memset(cfg, 0x00, sizeof(cfg)); if (my_ssid[0]) { SetWindowText(WifiWnd[0], my_ssid); } else if (LoadStr("SSID", cfg)) { SetWindowText(WifiWnd[0], cfg); } LoadStr("wifipasswd", wifiippass); SetWindowText(WifiWnd[1], wifiippass); memset(oldip, 0, sizeof(oldip)); memset(newip, 0, sizeof(newip)); if (gOptions.wifidhcpfunon) { dhcpswitch(1); if (wireless_is_online()) { //printf("_____%s%d\n", __FILE__, __LINE__); unsigned char ip[4]; memset(ip, 0, sizeof(ip)); wireless_get_ipaddr(&addr); str2ip((char*)ipaddr_to_string(addr), (BYTE*)ip); sprintf((char*)oldip[0], "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); printf("dynaic wifi: %d:%d:%d:%d\n", ip[0], ip[1], ip[2], ip[3]); memset(ip, 0, sizeof(ip)); wireless_get_mask(&mask); str2ip(ipaddr_to_string(mask), (BYTE*)ip); sprintf((char*)oldip[1], "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); memset(ip, 0, sizeof(ip)); wireless_get_default_route(&gateway); str2ip(ipaddr_to_string(gateway), (BYTE*)ip); sprintf((char*)oldip[2], "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); } } else { LoadStr("wifiip", (char*)oldip[0]); LoadStr("wifimask", (char*)oldip[1]); LoadStr("wifigateway", (char*)oldip[2]); } memcpy(newip, oldip, sizeof(oldip)); //set ip if(ParseIP(newip[0],ip[0],ip[1],ip[2],ip[3])){ for(i=0;i<4;i++){ if (strlen(ip[i])>0) { SetWindowText(WifiWnd[3+i],ip[i]); } else { SetWindowText(WifiWnd[3+i],"0"); } } } //set sub mask if(ParseIP(newip[1],ip[0],ip[1],ip[2],ip[3])){ for(i=0;i<4;i++){ SetWindowText(WifiWnd[7+i],ip[i]); } } //set Gate Way if(ParseIP(newip[2],ip[0],ip[1],ip[2],ip[3])){ for(i=0;i<4;i++){ SetWindowText(WifiWnd[11+i],ip[i]); } } }
/* * Start the dhcp client daemon, and wait for it to finish * configuring the interface. */ int dhcp_do_request(const char *interface, char *ipaddr, char *gateway, uint32_t *prefixLength, char *dns1, char *dns2, char *server, uint32_t *lease) { char result_prop_name[PROPERTY_KEY_MAX]; char daemon_prop_name[PROPERTY_KEY_MAX]; char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; char daemon_cmd[PROPERTY_VALUE_MAX * 2]; const char *ctrl_prop = "ctl.start"; const char *desired_status = "running"; char daemon_suffix[MAX_DAEMON_SUFFIX]; get_daemon_suffix(interface, daemon_suffix); snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", DHCP_PROP_NAME_PREFIX, interface); snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", DAEMON_PROP_NAME, daemon_suffix); /* Erase any previous setting of the dhcp result property */ property_set(result_prop_name, ""); /* Start the daemon and wait until it's ready */ if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix, prop_value, interface); else snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface); memset(prop_value, '\0', PROPERTY_VALUE_MAX); property_set(ctrl_prop, daemon_cmd); if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) { snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); return -1; } /* Wait for the daemon to return a result */ if (wait_for_property(result_prop_name, NULL, 30) < 0) { snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); return -1; } if (!property_get(result_prop_name, prop_value, NULL)) { /* shouldn't ever happen, given the success of wait_for_property() */ snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set"); return -1; } if (strcmp(prop_value, "ok") == 0) { char dns_prop_name[PROPERTY_KEY_MAX]; if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease) == -1) { return -1; } /* copy dns data to system properties - TODO - remove this after we have async * notification of renewal's */ snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface); property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : ""); snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface); property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); return 0; } else { snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); return -1; } }