1. Structure of the packet class in NS2:
The access function in the packet class is as follows:
Inline unsigned char * access (INT off) const {
If (Off <0)
Abort ();
Return (& bits _ [off]);
}
The access function in struct hdr_cen (shared common header) is as follows:
Inline static hdr_cen * access (const packet * P ){
Return (hdr_cen *) P-> access (offset _);
}
From the above function, we can see that as long as the offset _ of a specific header in packet is given, the pointer to this header can be obtained immediately (through forced type conversion, the principle is similar to converting a subclass to its parent class) to access or set the header content.
Common Tcl scripts include the following statements:
Remove-all-packet-headers
Add-packet-header, fig
...
Set NS [New simulator]
The involved functions are as follows:
Proc add-packet-header ARGs {
Foreach Cl $ ARGs {
Packetheadermanager Set Tab _ (packetheader/$ Cl) 1
}
} # Add-packet-header function sets the tab _ array element corresponding to the header to be activated to 1
One of the tasks of the new simulator in the script is to create the package structure:
Simulator instproc create_packetformat {}{
Packetheadermanager instvar tab _
Set PM [New packetheadermanager]
Foreach cl [packetheader info subclass] {
If [info exists tab _ ($ Cl)] {
Set off [$ PM allochdr $ Cl] # Get the offset of the current sub-header in the entire packet
$ Cl offset $ off # assign this variable to the offset variable in the current Subheader
}
}
$ Self set packetmanager _ $ pm
}
# The following function obtains the offset of the current Subheader in the entire packet.
Packetheadermanager instproc allochdr Cl {
Set size [$ Cl set hdrlen _] # obtain the length of the Subheader $ Cl hdrlen _
$ Self instvar hdrlen _ # note that the hdrlen _ variable is of packetheadermanager.
Set ns_align 8
Set incr [expr ($ size + ($ NS_ALIGN-1 ))&~ ($ NS_ALIGN-1)]
# Calculates the offset, which is ensured by the carry-up mode. The length of incr is a multiple of 8 bits.
Set base $ hdrlen _
Incr hdrlen _ $ incr # hdrlen _ plus offset
Return $ base # Return the current offset
}
The statement for calculating the offset NS manual is as follows: The procedure keeps a running variable hdrlen _ with the current length of Bob as new packet headers are enabled. it also arranges for 8-byte alignment for any newly-enabled packet header. this is needed to ensure that when double-world length quantities are used in packet headers on machines where double-word alignment is required, access faults are not produced.
In summary, the basic idea of getting the packet Subheader is: first, set the number of subheaders contained in the packet structure (packet) header, and then initialize the packet structure, set the offsize field of each sub-header to the correct offset. Then, you can use the access function to easily access each sub-header.
2. Communication Process of a mobile node:
Structure of two peer-to-peer wireless nodes
Communication Process between two mobile nodes
3. Packet settings for each layer:
Conclusion: In NS2. The packet structure remains unchanged in a simulation. When packet flows at various layers of the network, the only change is that the content in each sub-header changes. That is, the transport layer sets the IP header, the TCP protocol sets the TCP header, and the network layer Routing Protocol sets the corresponding routing protocol header, the ll layer correctly sets the MAC address of the Mac packet header Based on the IP address. There is no header splitting process.
Appendix: actual data packet structure (the following data sources are from the Internet)
1. IP Header Format:
1) version field flag: version number of the IP packet structure used by the group. For common IPv4, the content of this field is 4; for IPv6, the content of this field is 6.
2) IHL field: the header length, in the unit of 4 bits. The minimum value is 5 and the maximum value is L5, that is, the maximum length of the header is 60 bits.
3) service type field: the host can tell the subnet the services it needs.
4) total length: the length of the packet (including the header and packet). The maximum length is 65 535bit.
5) flag field: the host determines which group the new segment belongs to. All segments belonging to the same group should have the same flag value.
6) dF MF and segment offset fields: Indicate whether the group needs to be segmented, whether all the segments are reached, and whether the segments are in the group position.
7) lifecycle field: A counter used to limit the lifecycle of a group. The maximum lifecycle is 255 S. This field will be incrementally processed in each node. When it is reduced to 0, the group will be discarded.
8) Protocol: Which TCP/IP application protocol is used for this data packet. For TCP, this field is 6; for UDP, this field is ll.
9) header verification: This check header is used to detect errors caused by bad memory characters in the vro.
10) Source Address and Destination Address: the source and destination of the data packet.
For TCP/IP data packets, only the format of IP data packets can be mastered. The data body cannot be completely separated from the data packet, and the true meaning of the data packet cannot be fully understood. On the transport layer above the network layer, TCP/IP defines two data transmission protocols: TCP and UDP, which also define different data structures.
2. TCP Header structure:
1) Source Port and destination port field: indicates the local and remote connection ports.
2) sequence number and validation number field: unique to the TCP stream. TCP numbers each Data byte. The sequence number indicates the number of the Group. The confirmation number indicates the number of the next group to be received.
3) TCP Header Length indicates the length of the TCP Header, in the unit of 4 bits. This information is very important for locating the exact data information, because the length of the option field is not fixed. Only the content with the TCP Header length can confirm the length of the option field.
4) fields such as URG, ack, PSH rst, Syn, and fin indicate the control function of the group.
5) window size field: Indicates TCP sliding window control content.
6) checksum: to ensure high reliability, only the data of the header information is verified.
3. The UDP packet header is very simple:
The fixed value is 8 bits. The content is the source port, destination port, UDP length, and UDP checksum of 2 bits, respectively. The UDP Length indicates the length of the packet including the 8-bit header and the subsequent packets. The UDP checksum and verification are the content of all UDP packets.
4. 802.3 MAC frame structure:
1) authorization code: 7 bytes (10101010), used for clock synchronization between the sending and receiving sides.
2) the start time of a frame. It must be 1 byte (10101011), indicating the start of the frame.
3) Destination Address and Source Address: 2 or 6 bytes, but 10 Mbps baseband system only uses 6 bytes. The destination address can be divided into: single address: the highest bit is "0", used for Unicast (unicast ). Group address: the highest bit is "1", used for multicast (Multicast ). Broadcast address: All "1", used for broadcast (broadcast ). The global address and local address are distinguished by different sub-high positions: Local Address: the sub-high is "1", which is specified by the network administrator and only used in the local network. Global Address: The number of times high is "0", which is uniformly allocated by IEEE to ensure uniqueness in the world. A total of 248-2 ≈ 7? 1013.
4) length field: 2 bytes (0 ~ 1500) indicates the number of bytes in the data field.
5) data domain: 0 ~ 1500 bytes, used to store transmitted data (llc pdu ).
6) fill field: 0 ~ The minimum length of a valid frame from the destination address to the checksum field is 64 bytes (the fixed part is 18 bytes). When the data domain length is less than 46 bytes, this field is used to fill up to meet the requirements of the shortest frame.
7) checksum: 4 bytes, 32-bit CRC, g (x) = x32 + X26 + x23 + x22 + x16 + X12 + X11 + x10 + X8 + X5 + X4 + X2 + x + 1
5. 802.11mac frame format
Address: http://happywh583.blog.sohu.com/68718667.html