Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COSC264 Assignment

Andreas Willig

2022


1 Administrivia

This assignment is part of the COSC264 assessment process. It is worth 10% of the nal marks. It centers around socket programming using the Python programming language. Your program should be a text mode program that can run from the command line.

Please use the Question and Answer Forum” on the Learn platform for raising and discussing any unclear technical issues. Please do not send emails with technical questions directly to the lecturers or tutors, instead use the learn forum. This way other people can benefit from the question (and the answer).

2 Plagiarism Warning

Your submissions are logged and originality detection software will be used to compare your solution with other solutions. Dishonest practice, which includes

❼ letting someone else create all or part of an item of work,

❼ copying all or part of an item of work from another person with or without modification, and ❼ allowing someone else to copy all or part of an item of work,

may lead to partial or total loss of marks, no grade being awarded, the failing grade X’ (dishonesty) being awarded, or other serious consequences including notification of the University Proctor.

You are encouraged to discuss the general aspects of a problem with others.  However, anything you submit for credit must be entirely your own work and not copied, with or without modification, from any other person.  If you need help with specific details relating to your work, or are not sure what you are allowed to do, contact your tutors or lecturer for advice.  If you copy someone else’s work or share details of your work with anybody else, you are likely to be in breach of university regulations and the Computer Science and Software Engineering department’s policy. For further information please see

❼ Academic Integrity Guidance for Staff and Students

https://www.canterbury.ac.nz/about/governance/ucpolicy/general/academic-integrity-guidance-for-staff-and-students/

❼ Academic Misconduct Regulations

https://www.canterbury.ac.nz/regulations/general-regulations/academic-misconduct-regulations/

You will have to sign a plagiarism declaration upon submission of your assignment report.

3 Problem Description

Your task is to write two programs in Python.  The rst one, called server will allow the other program, called client, to ask the server for the current date or time of day.  The server offers to deliver these in three different languages.


3.1 Packet Types and Packet Processing

The programs will require two different types of packets to be used: DT-Request and DT-Response packets (where ’DT’ stands for DateTime”).  With a DT-Request a client requests either the date or the current time of day from the server. With a DT-Response packet the server returns both the date and current time of day in a binary representation,  followed by either the date or the time of day  (depending on the client’s choice) in a textual representation.

These packets contain a number of 16- or 32-bit wide elds. All these elds shall use the big-endian format.

3.1.1 DT-Request packet


1                                          16                                         32

MagicNo (16 bit)

PacketType (16 bit)

LanguageCode (16 bit)

Year (16 bit)

Month (8 bit)

Day (8 bit)

Hour (8 bit)

Minute (8 bit)

Length (8 bit)

Text (variable length)

Text

Text

The first 13 bytes (shown in green) are the xed header elds. The DT-Response packet consists of the following fields:

❼ The rst 16 bit eld MagicNo’ is taken up by a magic number, which again needs to have the value 0x497E.

❼ The 16 bit eld PacketType’ indicates the packet type within our DateTime protocol.  For a DT-Response

packet this eld needs to have the value 0x0002.

❼ The 16 bit field LanguageCode’ indicates the language used for the textual representation. The value 0x0001

indicates English, the value 0x0002 indicates Te reo Maori, and the value 0x0003 indicates German.

❼ The 16 bit eld Year’ contains the value for the year as a non-negative integer.  As an example, for the

current year 2020 this eld would contain the value 2020.

❼ The eight bit eld Month’ contains the value for the month as a non-negative integer:  1 for January, 2 for

February, and so on.

❼ The eight bit eld Day’ contains the value for the day of month as a non-negative integer. This number is

allowed to range from 1 to 31.

❼ The eight bit eld Hour’ contains the hour of the day. This number is allowed to range from 0 to 23.

❼ The eight bit eld Minute’ contains the minute within the hour. This number is allowed to range from 0 to

59.

❼ The eight bit eld Length’ indicates the length of the following textual representation in bytes. For example

if the text is hello” (without the speech marks) then the Length’ field wound contain the value 5.

❼ The Text’ field is of variable length and contains the actual text being returned (a textual representation of

either the date or the current time of day, see Section 3.4).

The DT-Response packet is being sent from the server to the client.  Upon receiving such a packet the client will have to check:

❼ Whether the packet contains at least 13 bytes of data (i.e. all xed header elds are present).

❼ If it does, check whether the MagicNo’ field contains the value 0x497E.

❼ If it does, check whether the PacketType’ field contains the value 0x0002.

❼ If it does, check whether the LanguageCode’ field contains either of the values 0x0001, 0x0002, or 0x0003.

❼ If it does, check whether the year is a number below 2,100.

If it is, check whether the Month is a number from 1 to 12.

❼ If it is, check whether the Day is a number from 1 to 31.

If it is, check whether the Hour is a number from 0 to 23.

❼ If it is, check whether the Minute is a number from 0 to 59.


❼ If it is, check whether the actual length of the entire packet you have received equals the sum of 13 (for the

fixed header) plus the contents of the Length’ field.

If all these conditions are met the client will accept the packet for further processing. If any of these conditions is not met, the client shall simply discard the packet, print an appropriate diagnostic message and exit the program. Note that this in particular implies that the client will only process received packets with PacketType’ being equal to 0x0002, i.e. it processes only DT-Response packets.

3.2 Server

The server program is started on a host and takes three different port numbers as parameters on the command line.  All three port numbers must be different, and they must be numbers between 1,024 and 64,000.  If these conditions are not met then the server should exit with an appropriate error message.  The rst port number is used by clients wanting to get the date/time information in English, the second port number by clients wanting it in Te reo Maori, and the third port number by clients wanting it in German.

Next the server will attempt to open three UDP / datagram sockets and to bind these three sockets to the three given port numbers. If this fails, then the server will exit with an appropriate error message.

After binding the port numbers the server will enter an infinite loop. In this loop:

❼ The server uses the select() system call to wait for a request packet on any of the three sockets. Note that

the server should not use any CPU resources while waiting.

❼ When a packet is received on any of these three sockets, the server:

Retrieves the packet from the socket using the Python equivalent of recvfrom(). The received packet is stored in a bytearray, and furthermore the server stores the IP address and port number of the packet sender and memoizes on which of the three sockets the packet has been received this socket determines the language to be used for the textual representation and is also the socket through which the response packet needs to be sent.

Performs all the checks necessary to see whether the packet is a valid DT-Request packet (see Section

3.1.1).  If it is not, print a diagnostic message on the terminal, discard the packet and continue at the start of the loop.

Determines from the request whether the client wants to know today’s date or the current time of day.

Uses a system call to obtain the current time of day hh : mm and date yyyy : mm : dd.

Prepares a DT-Response packet as follows:

* Prepare the textual representation T of either the date or the time of day in the chosen language as a bytearray (see Section 3.4). Let |T | be the length of the text in bytes (e.g. |hello| = 5). Check whether |T | < 255. If not, print a diagnostic message and return to the start of the loop without any further processing of this request.

* Allocate a sufficiently large buffer for the DT-Response packet which can hold both the xed elds and the text T.

* Fill in all the xed header elds (from MagicNo’ to Minute’) according to the specifications given in Section 3.1.2.

* Fill in the Length’ field with |T |.

* Fill in the textual representation T.

Finally sends this response packet back to the original sender through the socket on which the corre- sponding request packet has been received.  You will have to use the Python equivalent of sendto(), using the client IP address and port number retrieved at the start.

3.3 Client

The client program is started on the same host or another host, and takes three parameters on the command line:

❼ The rst is either the string date” or the string time”, letting the user specify what they wish to see.  If

the rst parameter is not equal to either of these, the client program should print an error message and stop.

❼ The second parameter is either an IP address in dotted-decimal notation (e.g. “130.66.22.212”) or the host-

name of the computer running the server (e.g. “datetime.mydomain.nz”). The client will attempt to convert


this parameter to an IP address using the Python equivalent of the getaddrinfo() function. If this conver- sion fails (e.g. because the hostname does not exist or an IP address given in dotted-decimal notation is not well-formed) then the client should print an error message and stop.

❼ The third parameter is the port number to use on the server. The port number should be between 1,024 and

64,000. If it is not, then the client should print an error message and stop.

After all command line parameters have been checked the client performs the following operations:

❼ It opens a UDP socket and prepares a DT-Request packet according to the format specified in Section 3.1.1.

❼ This packet is then sent to the server using the Python equivalent of the function sendto(), using the IP

address and port number obtained from the parameters as the destination.

❼ After sending the packet the client should wait for a response packet, but only for a limited time of one

second (you may want to look into the Python equivalent of select() to limit your waiting time on a socket to one second). If no response packet arrives within one second, the client should print an error message and exit.

❼ If a response packet arrives within one second, the client should retrieve it from the socket and check whether

it is a proper DT-Response packet (see Section 3.1.1).  If it is not, the client should print an error message and exit.

❼ The client prints the complete contents of the DT-Response packet, all the elds separately, not as a byte

array, and then exits.