If we want to simulate a protocol or multiple protocols for related operations, first we need to understand the data transmission in this Protocol and how to define the package type and format. Usually, few people use C # to do these things. C # Can't do anything at the underlying layer, and I don't plan to use C # for communication in UDP or Radius of the underlying protocol. But if you use it to test a protocol, from the bottom layer to the upper layer, I think C # can still be used for communication between two terminals.
Introduction: If you do a Protocol packet sending and receiving test, for the Definition Format of the general package, usually, a byte [] array is used to store and send an IP address. Sometimes, for example, an ipv4 address, a simple function call Split (. ") 192.168.1.1 to Split. In the radius protocol, if ipv6 is used for the communication address format, how should we handle the address to some character arrays. Let's take a look at the definition of the address format in RFC:
A summary of the NAS-IPv6-Address Attribute format is shown below.
The fields are transmitted from left to right.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+- +-+
| Type | Length | Address
+- +-+
Address
+- +-+
Address
+- +-+
Address
+- +-+
Address |
+-+
No matter how we get to the byte [] array, after all, its storage range is limited. For now, we use the string array for storage in this article. Split the IPv6 address. Speaking of the IPV6 address format, I will briefly describe the children's shoes that are not very clear.
Estimate the output result of the algorithm. First, you can better understand the purpose of the algorithm.
Input: n.
OutPut: new string [8] {"n",... "n"} direct OutPut
Input: 3FFE: FFFF: 8: 800: 20C4: 0: 1
OutPut: {"3FFE", "FFFF", "0", "0 ",.... "20C4", "0"} completion 0 output {"0", "0 ",...... "1 "}
Input: n: d. d. d
OutPut: {"n", "n "..... "d. d-> 16-digit hexadecimal "," d. d-> 16-bit hexadecimal "}
Input: n: d.
Output: {"n", "n", "0", "0",... "d. d-> 16-bit hexadecimal "}
Algorithm part: For this ipv6 address, even if it is the most standard from start to end: split, then directly use the split function to: split, obtain an array of 8 characters.
For those with (: :) In the address, we first use: to divide the IP address into two parts, then calculate the length of the two parts, and fill in the number of omitted 0 in the middle, returns the standard IPV6 array in hexadecimal format.
Hybrid IPv6 addresses are complicated, because if we simply add the first 96 bits: the IPv6 address followed by the 32-bit IPv4 address, we can also process the following parts. after splitting, you can convert it into a 16-bit ipv6. However, considering that there will be an omitted part of 0, that is, the format IPv6 address, we need to refer to the compressed processing method to split: first, and then exist in the pair..
The code is attached, so it is okay to test the code before optimization. It's messy. There are a few things to be improved.
Public static string [] IPAddressToArray (string address)
{
String [] result = new string [8];
// Address = string. Empty;
String [] ip = null;
String [] iptemp = null;
String [] iptempv4 = null;
String [] ipresult = new string [8];
// Address = "5: 2: 10: 20 ";
String [] a = {"::"};