Sample code for network interface settings

//--------------------------------------------------------------------------------------------------
/**
* Set the network configuration.
*
*/
//--------------------------------------------------------------------------------------------------
static void SetNetworkConfiguration
(
le_mdc_ProfileRef_t profileRef
)
{
char ipAddr[100] = {0};
char gatewayAddr[100] = {0};
char dns1Addr[100] = {0};
char dns2Addr[100] = {0};
char systemCmd[200] = {0};
FILE* resolvFilePtr;
le_mdc_ConState_t state = LE_MDC_DISCONNECTED;
mode_t oldMask;
 
// Check the state
LE_ASSERT( le_mdc_GetSessionState(profileRef, &state) == LE_OK );
LE_ASSERT( state == LE_MDC_CONNECTED );
 
// Get IP, gateway and DNS addresses for IPv4 or IPv6 connectivity
if ( le_mdc_IsIPv4(profileRef) )
{
LE_ASSERT(le_mdc_GetIPv4Address(profileRef, ipAddr, sizeof(ipAddr)) == LE_OK);
LE_PRINT_VALUE("%s", ipAddr);
 
LE_ASSERT(le_mdc_GetIPv4GatewayAddress(profileRef, gatewayAddr, sizeof(gatewayAddr))
== LE_OK);
LE_PRINT_VALUE("%s", gatewayAddr);
 
dns1Addr, sizeof(dns1Addr),
dns2Addr, sizeof(dns2Addr)) == LE_OK );
LE_PRINT_VALUE("%s", dns1Addr);
LE_PRINT_VALUE("%s", dns2Addr);
 
snprintf(systemCmd, sizeof(systemCmd), "/sbin/route add default gw %s", gatewayAddr);
}
else if ( le_mdc_IsIPv6(profileRef) )
{
LE_ASSERT(le_mdc_GetIPv6Address(profileRef, ipAddr, sizeof(ipAddr)) == LE_OK);
LE_PRINT_VALUE("%s", ipAddr);
 
LE_ASSERT(le_mdc_GetIPv6GatewayAddress(profileRef, gatewayAddr, sizeof(gatewayAddr))
== LE_OK);
LE_PRINT_VALUE("%s", gatewayAddr);
 
dns1Addr, sizeof(dns1Addr),
dns2Addr, sizeof(dns2Addr)) == LE_OK );
LE_PRINT_VALUE("%s", dns1Addr);
LE_PRINT_VALUE("%s", dns2Addr);
 
snprintf(systemCmd, sizeof(systemCmd), "/sbin/route -A inet6 add default gw %s",
gatewayAddr);
}
 
le_thread_Sleep(5);
 
LE_DEBUG("%s", systemCmd);
LE_ASSERT( system(systemCmd) == 0 );
 
// allow fopen to create file with mode=644
oldMask = umask(022);
 
// open the resolver configuration
resolvFilePtr = fopen("/etc/resolv.conf", "w+");
 
if (resolvFilePtr == NULL)
{
LE_ERROR("Unable to open resolv.conf: %m");
}
LE_ASSERT( resolvFilePtr != NULL );
 
LE_ASSERT( fprintf(resolvFilePtr, "nameserver %s\n", dns1Addr) > 0 );
 
if (dns2Addr[0] != '\0')
{
LE_ASSERT( fprintf(resolvFilePtr, "nameserver %s\n", dns2Addr) > 0 );
}
 
LE_ASSERT( fclose(resolvFilePtr) == 0 );
 
// restore old mask
umask(oldMask);
}