retrive ip address list change event by EventSelect and Overlapped

上周遇到一个问题,需要检测FD_ADDRESS_LIST_CHANGE_BIT,是用来检测可绑定地址列表变化的事件。结果在网上搜来搜去只看到一些文字描述(好像都是来自MSDN),没有看到可用的代码。最后没办法自己写了一个,贴出来供以后参考。

  I got two ipsec QFE last week for thread leak issues. An orphan thread will be left every L2TP connect/disconnect. 32kb memory will be lost because of this thread leak. The customer found the system crashed hours later due to out of memory issue.

  The root cause of this bug was using overlapped mechanism unsafely on WinCE. IPsec module used overlapped structure to retrive FD_ADDRESS_LIST_CHANGE. It works ok except in these cases:

1) The last overpad request will never return after the monitoring socket was deleted. Because there never will be any address change again on that socket.

2) If failed to create L2TP connection. No address change happen at all in this case. So the overlapped thread will be waiting for the event forever.

  We can fix the first bug by not setting overlap request without valid L2TP line. It works well according to our test.

  But the second bug is not as easy as the first one. The thread leak was cause by the first overlap request, which waited for the FD_ADDRESS_LIST_CHANGE. But L2TP connection failed, so no L2TP adapter was created at all, and no address list change happened. So the overlap thread waited and waited and waited... How to deal with it then?

1) We can't avoid setting the overlap request, otherwise, IPsec won't get any FD_ADDRESS_LIST_CHANGE any more, even the general ones.

2) We have to terminate the orphan thread before IPsec shutdown. But I didn't found any method to do this in WinCE. The overlap mechanism seems not as strong as desktop version. There is not any method to terminate the waiting thread.

3) So, my last idea was to replace overlap with some other mechanism, EventSelect was a good choice.

  I tried to search example codes for detecting adapter connect/disconnect notifications. But got only descriptions on how to XXX, no usful codes was gotten. At last, I wrote my demo codes like this.

 

For EventSelect mechanism:

1) calling WSAStartup() to initiate ws2.dll;

2) calling WSASocket() to create socket, which will be used to monitor FD_ADDRESS_LIST_CHANGE;

3) calling WSACreateEvent() to creat an event;

4) calling WSAEventSelect() to associate the event with FD_ADDRESS_LIST_CHANGE network event;

5) calling WSAIoctl() to register SIO_ADDRESS_LIST_CHANGE event;

6) calling WaitForMultipleObjects() to wait for the event be signaled.

 

For Overlapped mechanism:

1) calling WSAStartup() to initiate ws2.dll;

2) calling WSASocket() to create socket with WSA_FLAG_OVERLAPPED flag;

3) calling WSACreateEvent() to creat an event;

4) setting wsaOverlapped.hEvent with the just created event;

5) calling WSAIoctl() to register SIO_ADDRESS_LIST_CHANGE event, with overlapped parameter;

6) calling WaitForMultipleObjects() to wait for the event be signaled.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值