Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Raw Socket of C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what is the Raw Socket of C#". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the Raw Socket of C#"?

When it comes to C # Raw Socket programming, you may think of QQ and IE, yes. There are many network tools such as P2P, NetMeeting and other applications implemented in the application layer, which are also implemented in Socket. Socket is a network programming interface, which is implemented in the network application layer. Windows Socket includes a set of system components, which makes full use of the message-driven characteristics of Microsoft Windows. The Socket specification version 1.1 was released in January 1993 and is widely used in Windows9x operating systems that have emerged since then. Version 2.2 of the Socket specification (its version on the Windows platform is Winsock2.2, also known as Winsock2) was released in May 1996. Windows systems of Windows NT 5.0 and later support Winsock2. In Winsock2, it supports raw sockets of multiple transport protocols, overlapping Imax O model, quality of service control, and so on.

This paper introduces some programming of Windows Sockets about the original socket (Raw Socket) implemented in C # and the network packet monitoring technology implemented on this basis. Compared with Winsock1, the most obvious thing about Winsock2 is that it supports the Raw Socket socket type. Using Raw Socket, the network card can be set into a hybrid mode. In this mode, we can receive IP packets on the network, including IP packets that are not intended for the local machine. Through the original socket, we can also control a variety of protocols under Windows more freely, and can control the underlying transmission mechanism of the network.

In the example of this article, I implemented the C # Raw Socket class in the nbyte.BasicClass namespace, which contains our core technology for packet monitoring. Before implementing this class, you need to write an IP header structure to temporarily store some information about network packets:

Public struct IPHeader {[FieldOffset (0)] public byte ip_verlen; / / I4-bit header length + 4-bit IP version number [FieldOffset (1)] public byte ip_tos; / / 8-bit service type TOS [FieldOffset (2)] public ushort ip_totallength; / / 16-bit packet length (bytes) [FieldOffset (4)] public ushort ip_id; / / 16-bit ID [FieldOffset (6)] public ushort ip_offset / / 3-bit marker [FieldOffset (8)] public byte ip_ttl; / / 8-bit lifetime TTL [FieldOffset (9)] public byte ip_protocol; / / 8-bit protocol (TCP, UDP, ICMP, Etc.) [FieldOffset (10)] public ushort ip_checksum; / / 16-bit IP header checksum [FieldOffset (12)] public uint ip_srcaddr; / / 32-bit source IP address [FieldOffset (16)] public uint ip_destaddr; / / 32-bit destination IP address}

In this way, when each packet arrives, you can use forced type conversion to convert the data flow in the package into IPHeader objects.

Let's start by writing the Raw Socket class, starting with defining several parameters, including:

Does the private bool error_occurred; / / socket generate an error when receiving the packet public bool KeepRunning; / / whether to continue with the private static int len_receive_buf; / / the length of the data stream byte [] receive_buf_bytes; / / bytes received private Socket socket = null; / / declare the socket

There is another constant:

Const int SIO_RCVALL = unchecked ((int) 0x98000001); / / listen for all packets

The SIO_RCVALL here instructs the Raw Socket to receive all the packets, which will be used in the later IOContrl function. In the following constructor, some variable parameters are initialized:

Public RawSocket () / / constructor {error_occurred=false; len_receive_buf = 4096; receive_buf_bytes = new byte [len _ receive_buf];}

The following function creates the Raw Socket and binds it to the endpoint (IPEndPoint: native IP and port):

Public void CreateAndBindSocket (string IP) / / establish and bind socket {socket = new Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); socket.Blocking = false; / / set socket non-blocking state socket.Bind (new IPEndPoint (IPAddress.Parse (IP), 0); / / bind socket if (SetSocketOption () = = false) error_occurred=true;}

Where there are three parameters in the sentence Socket = new Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); that creates the socket:

The first parameter is to set the address family, which is described on MSDN as "specifying the addressing scheme used by Socket instances to resolve addresses". When you want to bind a socket to an endpoint (IPEndPoint), you need to use InterNetwork members, that is, the address format of IP version 4, which is also an addressing scheme (AddressFamily) used in most socket programming today.

The socket type set by the second parameter is the Raw type we use. SocketType is an enumerated data type, and the Raw socket type supports access to the underlying transport protocol. By using SocketType.Raw, you can communicate not only using Transmission Control Protocol (Tcp) and user Datagram Protocol (Udp), but also using Internet message Control Protocol (Icmp) and Internet Group Management Protocol (Igmp). When sending, your application must provide a complete IP header. The received Datagram will return with its IP header and options unchanged.

The third parameter sets the protocol type, and the Socket class uses the ProtocolType enumerated data type to notify Windows Socket API of the requested protocol. The IP protocol is used here, so the ProtocolType.IP parameter is used.

At this point, I believe you have a deeper understanding of "what is the Raw Socket of C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report