//adapter 网卡
namespace inet.adapter

var IPHlpApi = ..raw.loadDll("IPHlpApi.dll")
GetAdaptersInfo = IPHlpApi.api("GetAdaptersInfo","int(pointer info,int& size)")

var IP_ADDR_STRING = class{
	pointer next;
	byte ipAddress[16];
	byte ipMask[16];
	int context;
	getNextIpAddrString = function(){
		if( owner.next ){
			var addr = IP_ADDR_STRING();
			 ..raw.convert(owner.next,addr );
			addr._string_str();
			return addr;
		}
	} 
	_string_str = function(){
		owner.ipAddress = ..string.str(owner.ipAddress )
		owner.ipMask = ..string.str(owner.ipMask )
	} 
}

IP_ADAPTER_INFO = class{
	pointer next;
	int comboIndex;
	byte adapterName[260];
	byte description[132];
	INT addressLength;
	byte address[8];
	INT index;
	INT type;
	INT dhcpEnabled; 
	pointer currentIpAddress;
	struct ipAddressList = IP_ADDR_STRING();
	struct gatewayList = IP_ADDR_STRING();
	struct dhcpServer = IP_ADDR_STRING();
	int haveWins;
	struct primaryWinsServer = IP_ADDR_STRING();
	struct secondaryWinsServer = IP_ADDR_STRING();
	int leaseObtained;
	int leaseExpires;
}

//迭代遍历所有网卡
each =function(){
	var ret,size = GetAdaptersInfo( ,0 );
	if( ret != 111/*ERROR_BUFFER_OVERFLOW*/ )
		error("GetAdaptersInfo get buffer size faild!",2)

	var pBuf = ..raw.malloc( size );
	ret = GetAdaptersInfo(pBuf,size)
	if( ret  )
		error(..string.format("GetAdaptersInfo Faild! errCode:%0x",ret) ,2  )
 
	 
	var ptr =  pBuf; 
	return function(){ 
		if( !ptr){
			pBuf = null;
			return null;
		}
		
		var info = IP_ADAPTER_INFO();
		..raw.convert(ptr,info);
		info.mac =  ..string.format("%02x-%02x-%02x-%02x-%02x-%02x",
			info.address[1],
			info.address[2],
			info.address[3],
			info.address[4],
			info.address[5],
			info.address[6]
		)
		info.adapterName = ..string.str(info.adapterName)
		info.description = ..string.str(info.description)
		info.ipAddressList._string_str()
		info.gatewayList._string_str()
		info.dhcpServer._string_str()
		if(info.haveWins){
			info.primaryWinsServer._string_str()
			info.secondaryWinsServer._string_str()
		}
		
		ptr = pBuf;//for gc;
		ptr = info.next;
		return info; 
	};
}
/**intellisense()

inet.adapter.each() =  @//遍历所有网卡\nfor adptInfo in inet.adapter.each() { \n	__\n} 
?inet.adapter.each = !net_adptInfo. 
!net_adptInfo.mac = MAC地址,文本格式
!net_adptInfo.address  = MAC地址,数值格式
 
!net_adptInfo.adapterName = 网卡名称
!net_adptInfo.description = 网卡描述
!net_adptInfo.addressLength = 网卡地址长度
!net_adptInfo.index = 网络接口索引号
!net_adptInfo.type = 网络接口类型\n例如 _IF_SLIP_ADAPTERTYPE
!net_adptInfo.dhcpEnabled = 是否启用DHCP
!net_adptInfo.currentIpAddress = 当前IP地址
!net_adptInfo.haveWins = 是否启动了Wins服务
!net_adptInfo.leaseObtained = DHCP租赁时间
!net_adptInfo.leaseExpires = DHCP租赁失效时间

!net_adptInfo.ipAddressList = !ipaddr_string.
!net_adptInfo.gatewayList = !ipaddr_string.
!net_adptInfo.dhcpServer = !ipaddr_string.
!net_adptInfo.primaryWinsServer = !ipaddr_string.
!net_adptInfo.secondaryWinsServer = !ipaddr_string.

?.getNextIpAddrString = !ipaddr_string.
!ipaddr_string.getNextIpAddrString() = 获取下一个IP地址\n!ipaddr_string.
!ipaddr_string.ipAddress = IP地址
!ipaddr_string.ipMask = 掩码
!ipaddr_string.context =  
 
_IF_OTHER_ADAPTERTYPE=@0/*_IF_OTHER_ADAPTERTYPE*/
_IF_ETHERNET_ADAPTERTYPE=@1/*_IF_ETHERNET_ADAPTERTYPE*/
_IF_TOKEN_RING_ADAPTERTYPE=@2/*_IF_TOKEN_RING_ADAPTERTYPE*/
_IF_FDDI_ADAPTERTYPE=@3/*_IF_FDDI_ADAPTERTYPE*/
_IF_PPP_ADAPTERTYPE=@4/*_IF_PPP_ADAPTERTYPE*/
_IF_LOOPBACK_ADAPTERTYPE=@5/*_IF_LOOPBACK_ADAPTERTYPE*/
_IF_SLIP_ADAPTERTYPE=@6/*_IF_SLIP_ADAPTERTYPE*/
end intellisense**/ 