Second winter vacation assignment

Posted by zwiebelspaetzle on Mon, 31 Jan 2022 20:21:14 +0100

Which course does this assignment belong to 2020 object oriented programming
What are the requirements for this assignment Second winter vacation assignment
The goal of this assignment Understand computer network related knowledge, learn C language, read and write files, modular implementation functions
Job text as follows

preface

In fact, I don't have any preface, but seeing that everyone writes like this, I'll write one too

It's so hard. I didn't even understand what I was talking about at the beginning. It's so painful and desperate

After that, I consulted various materials, learned from others' homework, watched a lot of online classes and drew a lot of water hhh

The program has not been implemented, but my idea has been clearly presented below 🧐🧐

What to learn in this assignment

1. Learn the relevant knowledge of computer network about rule set, data set and rule matching, and fully understand what rule matching is and how to achieve rule matching.

2. Learn to read and write files in C + + language.

3. Think about the steps required to realize rule matching, and plan in modules. First create each block function group, and then implement it. It's best to clarify ideas and do a good job in division of labor by means of pseudo code and flow chart.

4. Test and optimize according to the functions of each function, and then integrate the code and test process into GitHub.

5. During this period, we should constantly consolidate git knowledge and master common usage.

learning process

1. Clarify the concept of rule set and data set

Rule set Original IP address Destination IP address Source port Destination port Transport layer protocol
(range, dotted decimal) 178.139.217.251/32 126.0.44.183/32 0: 65535 1526: 1526 0x06/0xFF
data set 2995509645 269990131 0 0 255
(decimal)

Since the representation methods of rule set and data set are different, there must be a function to convert hexadecimal.

2. How to judge rule matching

IP address matching is the hardest thing for me to understand

Then I found an introduction to the IP address range from other people's homework, which made it more painful

https://blog.csdn.net/dan15188387481/article/details/49873923

[this part collapsed]

Firstly, the dotted decimal of the rule set is rewritten into binary

Eg: 128.14.35.7/20 -- > 10000000 00001110 00100011 00000111 (the first twenty digits are network prefixes)

Next, make the host numbers all 0 and 1 respectively to get the address range of the rule set

eg: minimum address: 10000000 00001110 00100000 000000 -- > 128.14.32.0

Maximum address: 10000000 00001110 00101111 -- > 128.14.47.255

[later, after consulting my sister who is a computer graduate student, I found that IP range is not important...]

Then convert the IP address in the data set to dotted decimal (or dotted decimal to integer form)

Dotted decimal is to use four numbers from 0 to 255 to represent an IP address (binary is eight bits)

Here I choose


actually, I can't understand this thing. I'm going to learn it slowly later

maybe I'll make a bad copy

Finally, compare the original / destination IP address and the original / destination port range

Here, I intend to use two loops, one to read the packet data by line, and the other to match the data in the rule set

3. Learning C language to read and write files

read file

First learn to open files, read files and other basic function instructions

After that, we need to know how to read and how to read the data in the file into the array

Finally, set the condition (newline character) to read by line

Here I take this opportunity to study systematically

To read the file, of course, opening is the first step


The next step is to read by line

Here I only know the usage of fgets and the end of reading flag

But I still don't know how to read by line

Then I searched again

It turns out that fgets will read data from the next line the second time it encounters a newline character!!

At this time, the row data is stored in the one-dimensional array s

Writing documents

Similar to reading files, the fputs() function is used here

First, set a file pointer FILE*p

Make the pointer point to the file to be written, p=fopen("res.txt", "a")

Here, use "a" to write in order to fill in the position. First, create a "res" text file, otherwise the writing will fail

Then use fputs (ch,p) to write the answer to the file (with line feed)

Finally, fclose (p) closes the file

Set code function by function block

1. Read and close files

 void read()
{
    char file;//file name
    FILE *fp;//Definition file pointer
    printf("Enter the name of the file to be processed:");//Enter the name of the file to open
    scanf("%s",file);
    fp=fopen(file,"r");                          //Open the file, r means read-only, w only write
    if (fp==NULL)
    {
       printf("File does not exist%s\n",file);
    }                                           //This file does not exist
    char prime[500];
    while((fgets(prime,500,fp))!=NULL)//Cycle of reading files by line
    {
        char IP1[100],IP2[100],former[100],goal[100],duc[100];
        int a,b,c,d,e;
        for(a=0;prime[a]!=' ';a++) //a indicates the number of bits of the IP address - 1
        {
            IP1[a]=prime[a];
        }                        //Use a space to mark that the original IP address has been read
        int A=a;
        a-=1;
        for(;prime[A]==' ';A++)//Find the flag at the beginning of the next destination IP address
        for(b=0;prime[A]!=' ';A++,b++) 
        //b represents the number of bits of the IP address - 1, while A enables the prime array to continue reading
        {
            IP2[b]=prime[A];
        }
        b-=1;
        for(;prime[A]==' ';A++)//Find the flag for the beginning of the next source port
        for(c=0;prime[A]!=' ';A++,c++) //Similarly, c represents the number of bits of the source port - 1
        {
            former[c]=prime[A];
        }
        c-=1;
        //Convert source port array to integer Former
        int C,Former;
        for(C=0,Former=0;c>=0;c--,C++)
        {
            Former+=former[C]*pow(10,c);
        }
        for(;prime[A]==' ';A++)//Find the flag to start the next destination port
        for(d=0;prime[A]!=' ';A++,d++) //d represents the number of bits of the destination port
        {
            goal[d]=prime[A];
        }
        d-=1;
        //Convert destination port array to integer Goal
        int D,Goal;
        for(D=0,Goal=0;d>=0;d--,D++)
        {
            Goal+=former[D]*pow(10,d);
        }
        
        
        for(;prime[A]==' ';A++)//Find the flag at the beginning of the next agreement number
        for(e=0;prime[A]!='\n';A++,e++) //e represents the number of digits of the agreement number
        {
            duc[e]=prime[A];
        }
    }
 }

In this way, very idiots read a row of data and put them in five arrays to distinguish

(I don't know whether it's right or not, because I only learned so much hhh)

Correspondingly, close the file when all operations are completed

fclose(fp);

2. Conversion base

I won't 👻👻

3. Compare

void compare()
{
    //Suppose this is in the loop of opening the rule set file
    FILE *fp2;//Definition file pointer
    fp2=fopen("rule1","r");//Open rule set file
    int cnt=0;//Count rows
    while((fgets(rule,500,fp2))!=NULL)
    {
    //So here we assume that we have transformed the same integer form into the array, hee hee
    //The converted data in the rule set are t1 [], t2 [], t3 [], t4 [], t5 []
        int i,f=0;
        for(i=0;IP1[i]!='\n';i++)//Compare source IP address
        {
            if(IP1[i]!=t1[i])
            {
                f=1;
                break;
            }
        }
        if(f)
        {
            cnt++;//Mismatch found, number of rows increased
            continue;//Go to the next line
        }
        for(i=0;IP2[i]!='\n';i++)//Compare source IP address
        {
            if(IP2[i]!=t2[i])
            {
                f=1;
                break;
            }
        }
        if(f)
        {
            cnt++;//Mismatch found, number of rows increased
            continue;//Go to the next line
        }
        
        
        //The next step is to extract from the range represented by the array
        int j,formermin=0,formermax=0;
        for(j=0;t3[j]!=' ';j++)//Find the boundary of the maximum value
        int k=j;//Number of digits of minimum value
        for(;k>0;k--)
        {
            formermin+=t3[j-k]*pow(10,k);
        }                          //Get the minimum value
        k=j+4;                 //Array starting point of maximum value
        j+=4;
        for(;t3[k]>=0&&t3[k]<=9;k++)//Find the boundary of the maximum value
        for(;j<k;j++)
        {
            formermax+=t3[j]*pow(10,k-j-1);
        }                          //Get the maximum value
        
        //Compare the ports converted to integers
        if(Former>=formermin&&Former<=formermax)
        f=0;
        else
        f=1;
        if(f)
        {
            cnt++;              //Mismatch found, number of rows increased
            continue;           //Next line
        }

        //Similarly, compare the range of conversion destination ports
        int goalmin=0,goalmax=0;
        for(j=0;t4[j]!=' ';j++)//Find the boundary of the maximum value
        k=j;                 //Number of digits of minimum value
        for(;k>0;k--)
        {
            goalmin+=t4[j-k]*pow(10,k);
        }                          //Get the minimum value
        k=j+4;                 //Array starting point of maximum value
        j+=4;
        for(;t4[k]>=0&&t4[k]<=9;k++)//Find the boundary of the maximum value
        for(;j<k;j++)
        {
            goalmax+=t4[j]*pow(10,k-j-1);
        }                          //Get the maximum value
        
        //Compare the ports converted to integers
        if(Goal>=goalmin&&Goal<=goalmax)
        f=0;
        else
        f=1;
        if(f)
        {
            cnt++;             //Mismatch found, number of rows increased
            continue;         //Go to the next line
        }

        //Last comparison agreement number
        //In fact, there are only two situations given in the title: first, the agreement number is 6, and all match; Second, if the protocol number is not 6, it only matches 0x00
        //Then we only need to see whether the packet protocol number is 6 and whether the last character of the rule set protocol number is F or 0
        if(duc[e-1]==6&&e==1)     //If it is 6, then all matches
        f=0;
        else if(t5[8]==0)        //If it is not 6, but the rule set is 0x00, it also matches
        f=0;
        else                    //Other situations do not match
        f=1;
        if(f)
        {
            cnt++;//Mismatch found, number of rows increased
            continue;//Go to the next line
        }
        if(f==0)
        {
            //Here is the function linked to the next output file, and the final number of output lines
        }
    }
}

4. Output file

void write()
{
    FILE *p;
    p=fopen("res.txt","a");//You need to create a file before you can export it
    char ans[2];//Build an array with the number of answer lines and line breaks
    ans[0]=cnt;
    ans[1]='\n';
    fputs(ans,p);
    fclose(p);
}

summary

I can only barely understand the requirements for this assignment

After knowing a lot of knowledge about computer networks and asking others, I really understand what problems to solve

However, they still can't learn independently and can only complete such a small part of the tasks

My code is fragmented, so it is presented here. There is no way to integrate it into GitHub

But I recorded my learning process one by one in this blog

I can only present ideas within my ability

It seems that there is no content, but I spent a lot of thought. I can only say I tried my best, ha ha ha