void setIpAddress (const char *interface, const char *address, const char *gateway, const char *mask, const char *DNS1, const char *DNS2, int use_Dhcp){
NaIamStaticParams_t staticParams = {0};
NaIamDhcpParams_t dhcpParams = {0};
int method;
/*
* Get current configuration settings.
*/
if (customizeIamGetStaticConfig(interface, &staticParams) != BP_SUCCESS){
printf("[twutils.c] Error Reading Static Configuration From NVRAM" );
return -1;
}
// DHCP configuration
if (customizeIamGetDhcpConfig(interface, &dhcpParams) != BP_SUCCESS){
printf("[twutils.c] Error Reading DHCP Configuration " );
return -1;
}
else {
if(use_Dhcp == 1){
staticParams.isEnabled = FALSE;
dhcpParams.isEnabled = TRUE;
}
else if(use_Dhcp == 0){
staticParams.isEnabled = TRUE;
dhcpParams.isEnabled = FALSE;
}
}
/*
* Set IP Address. The inet_pton function converts a dotted IP address
* string like "1.2.3.4" into a binary value.
*/
if (inet_pton(AF_INET, address, &staticParams.ipAddress.addr.ipv4.sin_addr) != 1){
printf("[twutils.c] %s Is Not a Valid IP Address.\n", address);
return -1;
}
/*
* Set gateway.
*/
if (inet_pton(AF_INET, gateway, &staticParams.gateway.addr.ipv4.sin_addr) != 1){
printf("[twutils.c] %s Is Not a Valid Gateway Address.\n", gateway);
return -1;
}
/*
* Set subnet mask.
*/
if (inet_pton(AF_INET, mask, &staticParams.subnetMask) != 1){
printf("[twutils.c] %s Is Not a Valid Subnet Mask.\n", mask);
return -1;
}
/*
* Set Primary DNS.
*/
if (inet_pton(AF_INET, DNS1, &staticParams.primaryDns.addr.ipv4.sin_addr) != 1){
printf("[twutils.c] %s Is Not a Valid Primary DNS.\n", mask);
return -1;
}
/*
* Set Secondary DNS.
*/
if (inet_pton(AF_INET, DNS2, &staticParams.secondaryDns.addr.ipv4.sin_addr) != 1){
printf("[twutils.c] %s Is Not a Valid Secondary DNS.\n", mask);
return -1;
}
/*
* Get the method used to set the current IP address. This
* information is needed for the call to naIamRelease.
*/
if (naIamGetCurrentMethod(interface, &method) != BP_SUCCESS)){
printf("[twutils.c] Error Setting IP Address\n" );
return -1;
}
/*
* Write the new configuration to NVRAM.
*/
if (customizeIamSetStaticConfig(interface, &staticParams) != BP_SUCCESS){
printf("[twutils.c] Error Writing the new STATIC IP configuration to NVRAM\n" );
}
if (customizeIamSetDhcpConfig(interface, &dhcpParams) != BP_SUCCESS)){
printf("[twutils.c] Error Writing the new DHCP configuration to NVRAM\n" );
}
//Release current address
if(naIamRelease(interface, method) != NA_IAM_STATUS_SUCCESS){
printf("[twutils.c] Error Releasing IP Address\n" );
}
//Disable current method
if(naIamDisable(interface, myMethod) != NA_IAM_STATUS_SUCCESS){
printf("[twutils.c] Error Disabling Current Method\n" );
}
if(use_Dhcp == 1){
// Use DHCP
if(naIamEnable(interface, NA_IAM_METHOD_DHCP) != NA_IAM_STATUS_SUCCESS){
printf("[twutils.c] Error Enabling IP Acquisition Method\n" );
}
}
else if(use_Dhcp == 0){
// Use static
if(naIamEnable(interface, NA_IAM_METHOD_STATIC) != NA_IAM_STATUS_SUCCESS){
printf("[twutils.c] Error Enabling IP Acquisition Method\n" );
}
}
tx_thread_sleep(10);
}