Tcpv4 Checksum

  • * Write a function to calculate the ipv4 packet checksum containing TCP packets and return the ipv4 packet containing the correct checksum
  • * Only the checksum of ipv4 and TCP packets needs to be calculated. Ipv4 fragments are not used. The checksum of packets of other types is not required, for example, ipv6 packets, UDP packets, and ICMP packets. * The checksum in the incoming TCP ipv4 packet may be correct or incorrect. The caller simply returns an otherwise identical, but checksum, TCP ipv4 packet * the function input is an array of bytes with length, and the function output is an array of bytes with length. * Optional function to check the correctness of input packets. * If the writer chooses not to implement this feature, the caller will ensure that the input binary meets the binary array of valid packets when ipv4 and TCP are combined, and that the ipv4 fragments feature will not be used. * If the writer chooses to implement this feature, the caller may enter arbitrary binary content. At this point the examinee should print all errors to standard output. The program should not run out of memory, subscript out of bounds and other unexpected situations. * Use any programming language that meets the above requirements. * You need to write automatic unit tests of the function to prove that your function is correct. Automatic unit tests must include the following “I/O Example 1” for a total of 1 examples. The input and output of automated tests should be in your code and judged automatically. * This function should be done by yourself, and no part of the function is allowed to copy other people’s code. * This function contains each step of the checksum calculation internally. The checksum calculation itself does not allow direct calls to the library implementation. * Function implementations may or may not modify the passed byte array. * This function is called RecomputeChecksum. * The code should be readable, and it should be easy for humans to find all the actual code execution places. Inheritance is recommended to be avoided.
  • Written test results check: * Examiner runs all automated unit tests to see if they compile. Run the results after compiling, check whether the results are fully in line with expectations, this step has a problem written test failed. * The checker randomly modifies an input parameter of an automatic unit test to check whether the result fails after compiling and running. The written test fails in this step. * The examiner adds an example of a test that meets the requirements and is unknown to the examinee, and checks whether the result meets the expectations. If there are problems in this section, the examinee fails. * This written test fails if the code does not compile and run. * If the automated test does not run as expected, the written test will fail. * If the automatic test does not include the following I/O examples 1 This written test fails. * The written test fails if the input and output of the automatic test are not included in the code. * If the requirements for independent completion are not met, the written test will fail. * This written test will fail if the implementation details for each input byte are not met. * If the function named RecomputeChecksum cannot be found, the written test will fail. * If the number or types of input and output parameters of the RecomputeChecksum function do not meet the requirements, the written test will fail. * If the code is so poorly readable that it is difficult to read the code to see where the actual code is being executed, then the written test fails and the checker decides whether to pass or not. * Manual reading of automated unit test code and implemented code to check that goals and requirements are met. * The automatic test covers a wide range of details and scores for written tests are significantly enhanced. * Check whether the input package is correct, any binary input can be correct error processing, and the program will not overflow memory, subscript out of bounds and other unexpected circumstances, the written test scores have a lot of extra points. * Low code complexity, easy to understand code, small bonus for written test scores. * Golang language is used, with small bonus points for written test scores. * Code files use UTF8 without BOM encoding, with a small bonus for written test scores.
  • Reference: * ipv4 checksum https://tools.ietf.org/html/rfc1071 * tcp https://tools.ietf.org/html/rfc793 * tcp checksum https://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_checksum_for_IPv4
  • Func RecomputeChecksum(in []byte)(out []byte){return in}

Typef struct Slice{int len; unsigned char* data; } Slice; Slice RecomputeChecksum(Slice in){ Slice output = {}; output.len = in.len; output.data = in.data; return output; }

* C ++ should use exactly the same input/output interfaces as C. (Still uses structs on input/output interfaces, not classes)

Function RecomputeChecksum(inPara){var outBuffer = new Uint8Array(inpara.length); for (var i=0; i<inPara.length; i++){ outBuffer[i] = inPara[i]; } return outBuffer }

  • Input and output examples: * Input and output Example 1 (following with golang syntax) : * Input: X45 byte [] {0, 0 x00 to 0 x00 to 0 x8c, 0 x28, 0 xd1, 0 x00 to 0 x00 to 0 XFF, 0 x06, 0 x00 to 0 x00 to 0 x73, 0 xef, 0 xd2, 0 x1b, X15 0 xac, 0, 0 x00 to 0 x01, 0 x00 to 0 x50, 0 xe7, 0 xa3, 0 x93, 0 x2d, 0 xac, 0 XDB, 0 x9d, 0 x0e, 0 x0f, 0 x41, X50 0, 0 x10, 0 XFF, 0 XFF, 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x34, 0 x70, 0 x78, 0 x3b, 0 x70, 0 x61, x64 0, 0 x64, X67 x69 0, 0 x6e, 0, 0, x2d x6c 0, 0 x65, 0 x66, 0 x74, 0 x3a, 0 x31, 0 x30, 0 x70, 0 x78, 0 x3b, 0 x70, 0 x61. X69 x64 0, 0 x64, 0, 0, x6e x67 0, 0 x2d, 0 x72, 0 x69, 0 x67, 0 x68, 0 x74, 0 x3a, 0 x31, 0 x30, 0 x70, 0 x78, X75 x3b 0, 0 x63, 0, 0 x72, 0 x73, 0 x6f, 0 x72, 0 x3a, x64, 0 0 x65, 0 x66, 0 x61, 0 x75, 0 x6c, 0 x74, 0 x3b, X65 x6f 0, 0 x76, 0, 0 x72, 0 x66, 0 x6c, 0 x6f, 0 x77, 0 x3a, 0 x68, 0 x69, x64, 0 0 x64, 0 x65, 0 x6e, 0 x3b, X69 x77 0, 0 x68, 0, 0 x74, 0 x65, 0 x2d, 0 x73, 0 x70, 0 x61, 0 x63, 0 x65, 0 x3a, 0 x6e, 0 x6f, 0 x77, 0 x72, X70 0 x61, 0, 0 x7d, 0 x2e, 0 x63, 0 x2d, x64, 0 0 x72, 0 x6f, 0 x70, x64 0, 0 x6f,} * output: X45 byte [] {0, 0 x00 to 0 x00 to 0 x8c, 0 x28, 0 xd1, 0 x00 to 0 x00 to 0 XFF, 0 x06, 0 xa0, 0 x79, 0 x73, 0 xef, 0 xd2, 0 x1b, X15 0 xac, 0, 0 x00 to 0 x01, 0 x00 to 0 x50, 0 xe7, 0 xa3, 0 x93, 0 x2d, 0 xac, 0 XDB, 0 x9d, 0 x0e, 0 x0f, 0 x41, 0 x50, 0 x10, 0 XFF, 0 XFF, 0 XFF, 0 xe6, 0 x00 to 0 x00 to 0 x34, 0 x70, 0 x78, 0 x3b, 0 x70, 0 x61, x64 0, 0 x64, X67 x69 0, 0 x6e, 0, 0, x2d x6c 0, 0 x65, 0 x66, 0 x74, 0 x3a, 0 x31, 0 x30, 0 x70, 0 x78, 0 x3b, 0 x70, 0 x61. X69 x64 0, 0 x64, 0, 0, x6e x67 0, 0 x2d, 0 x72, 0 x69, 0 x67, 0 x68, 0 x74, 0 x3a, 0 x31, 0 x30, 0 x70, 0 x78, X75 x3b 0, 0 x63, 0, 0 x72, 0 x73, 0 x6f, 0 x72, 0 x3a, x64, 0 0 x65, 0 x66, 0 x61, 0 x75, 0 x6c, 0 x74, 0 x3b, X65 x6f 0, 0 x76, 0, 0 x72, 0 x66, 0 x6c, 0 x6f, 0 x77, 0 x3a, 0 x68, 0 x69, x64, 0 0 x64, 0 x65, 0 x6e, 0 x3b, X69 x77 0, 0 x68, 0, 0 x74, 0 x65, 0 x2d, 0 x73, 0 x70, 0 x61, 0 x63, 0 x65, 0 x3a, 0 x6e, 0 x6f, 0 x77, 0 x72, X70 0 x61, 0, 0 x7d, 0 x2e, 0 x63, 0 x2d, x64, 0 0 x72, 0 x6f, 0 x70, x64 0, 0 x6f,}




Copy the code