void Usage(void) {
printf("Usage: Iprenew [ -l ] [ -r<index id> ] [ -n<index id>]\n\n"
"\t -l List adapters with corresponding index ID information\n"
"\t -r Release IP address for adapter index ID\n"
"\t -n Renew IP address for adapter index ID\n");
}
if (argc < 2)
{
Usage();
return;
}
for (i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch(tolower(argv[i][1]))
{
case 'l':
OptList = TRUE;
break;
case 'r':
OptRelease = TRUE;
if (strlen(argv[i]) > 2)
Index = atoi(&argv[i][2]);
break;
case 'n':
OptRenew = TRUE;
if (strlen(argv[i]) > 2)
Index = atoi(&argv[i][2]);
break;
default:
Usage();
return;
}
}
else
{
Usage();
return;
}
}
// Check options
if ((OptRelease && Index == -1) || (OptRenew && Index == -1))
{
Usage();
return;
}
if (OptList)
{
if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
printf("GetAdaptersInfo sizing failed with error %d\n", Err);
return;
}
}
// Allocate memory from sizing information
if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}
// Get actual adapter information
if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
{
printf("GetAdaptersInfo failed with error %d\n", Err);
return;
}
if ((Err = GetInterfaceInfo(NULL, &InterfaceInfoSize)) != 0)
{
if (Err != ERROR_INSUFFICIENT_BUFFER)
{
printf("GetInterfaceInfo sizing failed with error %d\n", Err);
return;
}
}
// Allocate memory from sizing information
if ((pInterfaceInfo = (PIP_INTERFACE_INFO) GlobalAlloc(GPTR, InterfaceInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}
// Get actual adapter information
if ((Err = GetInterfaceInfo(pInterfaceInfo, &InterfaceInfoSize)) != 0)
{
printf("GetInterfaceInfo failed with error %d\n", Err);
return;
}
if (OptRelease)
{
for (i = 0; i < pInterfaceInfo->NumAdapters; i++)
if (Index == pInterfaceInfo->Adapter[i].Index)
{
if ((Err = IpReleaseAddress(&pInterfaceInfo->Adapter[i])) != 0)
{
printf("IpReleaseAddress failed with error %d\n", Err);
return;
}
break;
}
}
if (OptRenew)
{
for (i = 0; i < pInterfaceInfo->NumAdapters; i++)
if (Index == pInterfaceInfo->Adapter[i].Index)
{
if ((Err = IpRenewAddress(&pInterfaceInfo->Adapter[i])) != 0)
{
printf("IpRenewAddress failed with error %d\n", Err);
return;
}
break;
}
}
}