private byte[] CreateRequest() { var request = new byte[24]; request[0] = 1; // packet type - request request[1] = 1; // version number - v1 // bytes [2,3] are left empty // bytes [4,5] are address family, since we want the whole routing table, leave empty // bytes [6,7] are left empty OwnIP.GetAddressBytes().CopyTo(request, 8); // so routers can identify each other by ip, since //we are running on loopback //next eight bytes are left empty //following 4 bytes are metric, since max hop count is 15, set it to infinity(16) BitConverter.GetBytes(MaxHopCount + 1).CopyTo(request, 20); return(request); }
private byte[] CreateResponse() { var responseHeader = new byte[24]; responseHeader[0] = 2; // packet type - response responseHeader[1] = 1; // version number - v1 // next 2 bytes are left empty // bytes [4,5] are address family responseHeader[5] = 2; //ip address family is represented by 2 // next 2 bytes are left empty OwnIP.GetAddressBytes().CopyTo(responseHeader, 8); // so routers can identify each other by ip, since //we are running on loopback //next eight bytes are left empty //following 4 bytes are metric, since the router is a neighbor, cost is 0 //BitConverter.GetBytes(0).CopyTo(responseHeader, 20); //every entry in routing table takes up 20 bytes var response = new byte[responseHeader.Length + (RouteTable.Count * 20)]; responseHeader.CopyTo(response, 0); var currentIndex = responseHeader.Length; RouteTable.mutex.WaitOne(); foreach (var entry in RouteTable) { //first 2 bytes are address family //ip address family is represented by 2 response[currentIndex + 1] = 2; // next 2 bytes are left empty // then 4 bytes are ip address of entry BitConverter.GetBytes(ConvertToNetworkOrder(entry.Ip)).CopyTo(response, currentIndex + 4); //next 8 bytes are left empty //then 4 bytes are metric BitConverter.GetBytes(ConvertToNetworkOrder(entry.Cost)).CopyTo(response, currentIndex + 16); currentIndex += 20; } RouteTable.mutex.ReleaseMutex(); return(response); }