1

I have a small app that temporary adds several (3-4) IP addresses using AddIPAddress function from IP Helper. After some seconds (2-3), it deletes these addresses using DeleteIPAddress function.

The function DeleteIPAddress works and returns no error, but sometimes (once every 3 or 4 rounds of adding and deleting) it triggers a disconnection event on the interface (exactly like disconnecting and connecting the cable).

I want to avoid these disconnections as they cut any communication being done in the same interface, but I cannot see anything in the documentation regarding this behavior.

This is how I add each IP address:

auto dwRetVal = AddIPAddress(iaIPAddress, iaIPMask, _idx, &NTEContext, &NTEInstance);
    if (dwRetVal == ERROR)
    {
      std::cout << "Error on AddIPAddress" << std::endl;
    }

And this is how I delete them:

auto dwRetVal = DeleteIPAddress(ipContext);
    if (dwRetVal != NO_ERROR)
    {
      std::cout << "Error on DeleteIPAddress" << std::endl;
    }

Am I missing something?

Daniel
  • 11
  • 1

1 Answers1

0

You are not checking the return value from AddIPAddress correctly, you should test != ERROR_SUCCESS.

If AddIPAddress fails you should not call delete.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Thank you. But I don´t think this is the case, as it also happens if I get the context from _GetAdaptersInfo_. Also, after using _AddIPAddress_ I can see that the interface has the address correctly added. And also, if I use an invalid context (if _AddIPAddress_ fails) I get an error from _DeleteIPAddress_. In any case, thank you for the tip, I will change that too. – Daniel May 22 '19 at 12:38
  • I don't know if this will fix the problem either but since the code you posted is buggy I had to add this answer. ipContext is an offset into the table or something like that and a random value from the stack could in theory delete something by accident. – Anders May 22 '19 at 12:46