CSE 220: Systems Programming
1 Getting Started
You should have received a GitHub Classroom invitation for this project. Follow it and check out the resulting
As always, read over the entirety of this handout before starting the assignment. If you have not yet read
Chapter 5 of The C Programming Language by Kernighan & Ritchie, you should also read that before starting.
You will specifically find Sections 5.3, 5.4, and 5.5 to be very helpful.
You should read and understand all of the code in the src directory. You are not expected to read or understand any code in the client directory, although you may look at it if you wish.
Man page sections are noted as a number in square brackets after a keyword in this document. For example,
memset  indicates that the manual page for the memset() function is found in section 3 of the Unix manual,
and you can view it with the command man 3 memset.
In this assignment, you must implement encoding and decoding of packets of data according to the formats
specified below. Each packet is stored internally as a void *, according to a standard format as specified in
Note that the formats in this assignment are very specific, and must be implemented precisely. This includes details such as white space and the value of padding bytes. Be sure not to include any extra characters
in quoted strings, insert extra newlines or other formatting, or deviate from this specification in any way!
The standard defines several packet types:
This packet type sets your status on the server. It should be sent when the user input starts with “/me”
followed by a space. Any input starting with “/me” followed by any other character is invalid.
This packet type tags another user. It is sent when the user input starts with “@” followed by at least one
and no more than NAME_SIZE non-space characters. NAME_SIZE is defined in src/serialize.h.
This packet type is used to request and receive a set of basic statistics from the server. It is sent when
the user input starts with “/stats” followed by the end of the input. Any input beginning with “/stats”
followed by any other character is invalid.
This packet type is the most basic, it is just a normal plaintext message. It should be sent if the user input
does not fall into any of the above categories.
The standard also defines a special packet:
This packet is sent to the server by the client to request new messages to decode. It is sent every second
and has no relation to user input.
Through this assignment, you will implement four functions:
• int pack(void *packed, char *input):
Parse input to determine the packet type and fill packed with the encoded input.
• int pack_refresh(void *packed, int message_id):
Fill packed with an encoded refresh packet according to the specification in Section 3.5.
• int unpack(char *message, void *packed):
Decode packed based on the packet type and fill message with the decoded string.
• int unpack_statistics(struct statistics *statistics, void *packed):
Decode packed according to the STATISTICS format specified in Section 3.4, filling statistics with the data.
Any input beginning with “/” that does not include one of the commands defined above is invalid, but will
never be tested. You may use this to implement commands starting with slash for debugging if it is useful to
For all functions, the return value should be the integer value of the packet type as defined in serialize.h, or
-1 for invalid inputs. You should assume that any non-NULL pointer passed to a function is correctly allocated
and of an adequate size. Other than that, you should not make any assumptions about the validity of inputs; you
are responsible for validating them. This includes unreasonable or meaningless user input as well as malformed
data to be unpacked. Some examples of invalid values will be described in Section 3.