![]() They are called Format Characters You can check them from the official docs using this link here. There are many other symbols(letters) that we can use. “IIII” four I means, we are going to packet 4 fields, and “I” itself means, unsigned integer with a standardized size of 4. On, the first parameter of the struct.pack() we are telling the UDP header what type of data it is and also how many fields we are using. On line number 5, we are creating the UDP header with the struct module. In the above 6 lines of codes, we are trying to created a UDP header with the 4 fields required. ![]() source_port = 1111 destination_port = 1112 data_length = len(packet) checksum = checksum_calculator(packet) udp_header = struct.pack(“!IIII”, source_port, destination_port, data_length, checksum) But if you just want to deal with data corruption, you can ignore(fill with arbitrary data) the other stuff except for the checksum. For now, we are going to fill them with arbitrary data. The source port number, the destination port number, the length of the data, and the checksum. But before we do that, we said that the UDP header has 4 fields. data = “Hello world” packet = data.encode() checksum = checksum_calculator(packet)īooyah! We have the checksum calculated now. We usually calculate checksum for bytes data so, we are going to use the encoded data. So, let’s calculate the checksum for the previous data. ![]() Great! We have a checksum calculator function now. import zlib def checksum_calculator(data): checksum = zlib.crc32(data) return checksum First, let’s create a checksum calculator function. In this tutorial, we will use the Zlib library for calculating checksum and the struct library for making the UDP header. But wait, how are we going to do that? Well, we have so many modules to work with. Like we said earlier, we have to make a UDP header, put the checksum in the UDP header and finally attach it to the data. There are no UDP headers therefore we didn’t calculate for the checksum. In the above simple example, we are just sending the data. After that on the last line, we are finally sending the packet to a receiver. We are encoding(changing it to bytes) the data because we can only send bytes over a socket. As you see it there the data is “Hello world”. On the above 4 lines of code, we are trying to send data(packet) to a receiver(client). data = “Hello world” packet = data.encode() receiver_addr = (“127.0.0.1”, 1112) # For now, it is just an arbitary address socket_ndto(packet, receiver_addr) So to make things clear, SOCK_DGRAM is for UDP protocol and SOCK_STREAM is for TCP protocol. If we were to use a socket.SOCK_STREAM, it would have been a TCP protocol socket. The second parameter(socket.SOCK_DGRAM) is the only one making the socket a UDP protocol socket. It is used to designate the type of addresses that our socket can communicate with (in this case, Internet Protocol v4 addresses). The first argument(socket.AF_INET) is the address family. The thing that makes the socket_object a UDP protocol socket is the argument of the socket.socket() object. We bind on a specific address means, senders can send data to us by targeting the specified address. Well, on the above 3 lines of code, we imported the socket module, created the socket object(UDP socket object) that we are going to use for sending and receiving data, and bonded with a local host and port number. import socket socket_object = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socket_object.bind(("127.0.0.1", 1111)) First, let’s create the socket object for the UDP protocol. You don’t have to worry if you never worked with this module. In this article, we are going to use python’s built-in module “socket”. So, in this article like it says from its title, we are going to experiment with UDP protocol, headers including checksum calculation.įirst of all, let’s create a simple script with UDP protocol excluding UDP headers and checksum calculation. And we said that the solution for data corruption is calculating the checksum of packets on both sender and receiver sides. In the previous article(UDP protocol and header explanation), we said that there might be data loss or data corruption while communicating through the UDP protocol. If you don’t fulfill this requirement, I will suggest you read my article on the explanation of UDP protocol and headers. Prerequisites- This article assumes a basic understanding of networking, UDP protocol, and python programming language.Īttention: To continue with this article(tutorial), you have to know how UDP protocol works and how we use UDP headers in order to calculate checksum and do some other stuff. UDP protocol with a header implementation in python
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |