1. ICMP packets are encapsulated in IP packets for transmission.
Understanding this is very important for later programming. Figure 03 shows the structure of IP data packets:
Figure 03: Structure of IP data packets |
Traditionally, IP data packets are divided into three parts:
(1) data of the first 20 bytes in the IP data packet, that is, from [4-bit version] to [32-bit destination IP address] In Figure 03, this is called the IP address header.
(2). Option, that is, option (if any) in Figure 03.
(3). Data, that is, the [data] section in Figure 03.
The latter two parts constitute ICMP packets. The specific composition of ICMP packets 04:
Figure 04: Structure of ICMP packets |
2.
The ICMP protocol does not have a fixed port number.
Setsockopt function Parsing
Int setsockopt (
Socket
S
,
Int
Level
,
Int
Optname
,
Const char far *
Optval
,
Int
Optlen
);
The Windows Sockets
SetsockoptFunction sets a socket
Option.
It seems to be: set socket options.
First look at the following code:
Setsockopt (sockraw, ipproto_ip, ip_hdrincl, (char
*) & Flag, sizeof (INT ))
Here we set ip_hdrincl in the IP Option of the socket sockraw.
Refer to the following materials:
**************************************** **************************************** *******************
.
Socket options
Sometimes we need to control the socket behavior (such as modifying the buffer size). At this time, we need to control the socket options.
Getsockopt and setsockopt
Int getsockopt (INT sockfd, int level, int optname, void * optval, socklen_t
* Optlen)
Int setsockopt (INT sockfd, int level, int optname, const void
* Optval, socklen_t * optlen)
Level specifies the level of the control socket. Three values can be obtained:
1) sol_socket: General socket options.
2) ipproto_ip: IP Option.
3) ipproto_tcp: TCP option.
Optname specifies the control method (option name), which is explained in detail below
Optval gets or sets socket options. Convert the Data Type of the Option name
Option Name Description Data Type
========================================================== ======================================
Sol_socket
------------------------------------------------------------------------
So_broadcast allows sending broadcast data int
So_debug allows int debugging
So_dontroute
So_error get socket error int
So_keepalive
So_linger
Delay disconnect struct linger
So_oobinline put out-of-band data into normal data stream int
So_rcvbuf receive buffer size int
So_sndbuf
Sending buffer size int
So_rcvlowat lower limit int of the receiving buffer
So_sndlowat lower limit int of the sending Buffer
So_rcvtimeo
Receiving timeout struct timeval
So_sndtimeo
Sending timeout struct timeval
So_reuseraddr
Allow reuse of local address and port int
So_type: Obtain the socket type Int.
So_bsdcompat is compatible with the BSD system int
========================================================== ========================================
Ipproto_ip
--------------------------------------------------------------------------
Ip_hdrincl contains the IP header int in the data package.
Ip_optinos IP header option int
Ip_tos service type
Ip_ttl time int
========================================================== ========================================
Ippro_tcp
--------------------------------------------------------------------------
Tcp_maxseg maximum TCP Data Segment Size int
Tcp_nodelay
Do not use the Nagle algorithm int
========================================================== ======================================
For more information about these options, see Linux programmer "s manual
8.2 IOCTL
IOCTL can control all file descriptors. Here we will introduce the options for controlling sockets.
Int IOCTL (int fd, int req ,...)
========================================================== ========================================
IOCTL control options
--------------------------------------------------------------------------
Whether the siocatmark has reached the out-of-band mark int
Fioasync asynchronous input/output flag int
Number of bytes readable in the fionread buffer int
========================================================== ========================================
Reference Program: http://115.com/file/bj3k2wa1