├── LICENSE
├── README.md
├── mail-client
├── mail-client.py
└── read-first.md
├── ping
├── ping.py
└── running.md
├── web-proxy
├── testing.md
└── web-proxy.py
└── web-server
├── tips-for-testing.md
└── web-server.py
/LICENSE:
--------------------------------------------------------------------------------
1 | CC0 1.0 Universal
2 |
3 | Statement of Purpose
4 |
5 | The laws of most jurisdictions throughout the world automatically confer
6 | exclusive Copyright and Related Rights (defined below) upon the creator and
7 | subsequent owner(s) (each and all, an "owner") of an original work of
8 | authorship and/or a database (each, a "Work").
9 |
10 | Certain owners wish to permanently relinquish those rights to a Work for the
11 | purpose of contributing to a commons of creative, cultural and scientific
12 | works ("Commons") that the public can reliably and without fear of later
13 | claims of infringement build upon, modify, incorporate in other works, reuse
14 | and redistribute as freely as possible in any form whatsoever and for any
15 | purposes, including without limitation commercial purposes. These owners may
16 | contribute to the Commons to promote the ideal of a free culture and the
17 | further production of creative, cultural and scientific works, or to gain
18 | reputation or greater distribution for their Work in part through the use and
19 | efforts of others.
20 |
21 | For these and/or other purposes and motivations, and without any expectation
22 | of additional consideration or compensation, the person associating CC0 with a
23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25 | and publicly distribute the Work under its terms, with knowledge of his or her
26 | Copyright and Related Rights in the Work and the meaning and intended legal
27 | effect of CC0 on those rights.
28 |
29 | 1. Copyright and Related Rights. A Work made available under CC0 may be
30 | protected by copyright and related or neighboring rights ("Copyright and
31 | Related Rights"). Copyright and Related Rights include, but are not limited
32 | to, the following:
33 |
34 | i. the right to reproduce, adapt, distribute, perform, display, communicate,
35 | and translate a Work;
36 |
37 | ii. moral rights retained by the original author(s) and/or performer(s);
38 |
39 | iii. publicity and privacy rights pertaining to a person's image or likeness
40 | depicted in a Work;
41 |
42 | iv. rights protecting against unfair competition in regards to a Work,
43 | subject to the limitations in paragraph 4(a), below;
44 |
45 | v. rights protecting the extraction, dissemination, use and reuse of data in
46 | a Work;
47 |
48 | vi. database rights (such as those arising under Directive 96/9/EC of the
49 | European Parliament and of the Council of 11 March 1996 on the legal
50 | protection of databases, and under any national implementation thereof,
51 | including any amended or successor version of such directive); and
52 |
53 | vii. other similar, equivalent or corresponding rights throughout the world
54 | based on applicable law or treaty, and any national implementations thereof.
55 |
56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of,
57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59 | and Related Rights and associated claims and causes of action, whether now
60 | known or unknown (including existing as well as future claims and causes of
61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum
62 | duration provided by applicable law or treaty (including future time
63 | extensions), (iii) in any current or future medium and for any number of
64 | copies, and (iv) for any purpose whatsoever, including without limitation
65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66 | the Waiver for the benefit of each member of the public at large and to the
67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver
68 | shall not be subject to revocation, rescission, cancellation, termination, or
69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work
70 | by the public as contemplated by Affirmer's express Statement of Purpose.
71 |
72 | 3. Public License Fallback. Should any part of the Waiver for any reason be
73 | judged legally invalid or ineffective under applicable law, then the Waiver
74 | shall be preserved to the maximum extent permitted taking into account
75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76 | is so judged Affirmer hereby grants to each affected person a royalty-free,
77 | non transferable, non sublicensable, non exclusive, irrevocable and
78 | unconditional license to exercise Affirmer's Copyright and Related Rights in
79 | the Work (i) in all territories worldwide, (ii) for the maximum duration
80 | provided by applicable law or treaty (including future time extensions), (iii)
81 | in any current or future medium and for any number of copies, and (iv) for any
82 | purpose whatsoever, including without limitation commercial, advertising or
83 | promotional purposes (the "License"). The License shall be deemed effective as
84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the
85 | License for any reason be judged legally invalid or ineffective under
86 | applicable law, such partial invalidity or ineffectiveness shall not
87 | invalidate the remainder of the License, and in such case Affirmer hereby
88 | affirms that he or she will not (i) exercise any of his or her remaining
89 | Copyright and Related Rights in the Work or (ii) assert any associated claims
90 | and causes of action with respect to the Work, in either case contrary to
91 | Affirmer's express Statement of Purpose.
92 |
93 | 4. Limitations and Disclaimers.
94 |
95 | a. No trademark or patent rights held by Affirmer are waived, abandoned,
96 | surrendered, licensed or otherwise affected by this document.
97 |
98 | b. Affirmer offers the Work as-is and makes no representations or warranties
99 | of any kind concerning the Work, express, implied, statutory or otherwise,
100 | including without limitation warranties of title, merchantability, fitness
101 | for a particular purpose, non infringement, or the absence of latent or
102 | other defects, accuracy, or the present or absence of errors, whether or not
103 | discoverable, all to the greatest extent permissible under applicable law.
104 |
105 | c. Affirmer disclaims responsibility for clearing rights of other persons
106 | that may apply to the Work or any use thereof, including without limitation
107 | any person's Copyright and Related Rights in the Work. Further, Affirmer
108 | disclaims responsibility for obtaining any necessary consents, permissions
109 | or other rights required for any use of the Work.
110 |
111 | d. Affirmer understands and acknowledges that Creative Commons is not a
112 | party to this document and has no duty or obligation with respect to this
113 | CC0 or use of the Work.
114 |
115 | For more information, please see
116 |
117 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | A cleanroom implementation of the programming exercises from Chapter 2 of Kurose and Ross "Computer Networking: A Top-Down Approach" sixth edition.
4 |
5 | The materials required for the exercises are locked behind a paywall and cannot be accessed if you (like me) are using a library copy of the book.
6 | Hence I decided to implement the exercises myself.
7 |
8 | How to use
9 | ---------
10 |
11 | * Fork and clone the github repository
12 | * Create your own solution branch based on the exercises branch with `git checkout -b ${USER}-solutions exercises`.
13 | * Hack away
14 | * If you need tips look at other peoples solutions with e.g. `git show david-solutions:web-server/web-server.py`
15 |
16 |
17 | A warning
18 | -------
19 |
20 | I'm new to socket programming, so it is unlikely that the solutions given here embody any kind of best-practices. If you are writing a "real" application you need to be much more careful!
21 |
--------------------------------------------------------------------------------
/mail-client/mail-client.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | import argparse
4 |
5 | import sys
6 | import socket
7 | from socket import socket as Socket
8 |
9 |
10 | def main():
11 |
12 | parser = argparse.ArgumentParser()
13 | parser.add_argument('my_address', type=str)
14 | parser.add_argument('mail_server', type=str)
15 | parser.add_argument('their_address', type=str)
16 | parser.add_argument('message', type=str)
17 | args = parser.parse_args()
18 |
19 |
20 | send_mail(args.my_address, args.mail_server, args.their_address, args.message)
21 |
22 |
23 | # success
24 | return 0
25 |
26 |
27 | def send_mail(my_address, mail_server, their_address, message):
28 |
29 | # Fill in the code to talk to the mail server here. Read the file
30 | # read-first.md to find out how to set up a mail server for testing. A log
31 | # of a telnet session with your mail server may be (very) useful here.
32 |
33 | raise NotImplementedError
34 |
35 | return
36 |
37 |
38 |
39 | if __name__ == "__main__":
40 | sys.exit(main())
41 |
--------------------------------------------------------------------------------
/mail-client/read-first.md:
--------------------------------------------------------------------------------
1 | Unfortunately gmail [no longer accepts unencrypted smtp connections](http://stackoverflow.com/questions/1516754/connecting-to-smtp-gmail-com-via-command-line), so we can't use it to test our client.
2 |
3 | The only alternative I've found is to run your own mail server. I've chosen to run it in a local mail only mode, which limits the amount of interesting stuff we can do but at least means we can't be used as a spam relay! I used `postfix`, which can be set up on Ubuntu 13.10 (and probably most other Debian-based Linux distributions) by running
4 |
5 | sudo apt-get install postfix
6 |
7 | and in the curses dialog that appears choose to run in local mode. The server should now be running, you can test it by running
8 |
9 | telnet localhost 25
10 |
11 | and following the instructions on pg. 123 of the textbook. Note that you can only send mail to users that exist, i.e. `$USER@localhost` (where `$USER` is your username). For me this interation looks like this:
12 |
13 | telnet localhost 25
14 | Trying 127.0.0.1...
15 | Connected to localhost.
16 | Escape character is '^]'.
17 | 220 david-simulations ESMTP Postfix (Ubuntu)
18 | helo localhost
19 | 250 david-simulations
20 | mail from: david@localhost
21 | 250 2.1.0 Ok
22 | rcpt to: david@localhost
23 | 250 2.1.5 Ok
24 | data
25 | 354 End data with .
26 | hello
27 | .
28 | 250 2.0.0 Ok: queued as 015D3120C36
29 | quit
30 | 221 2.0.0 Bye
31 | Connection closed by foreign host.
32 |
33 | Then we can check that the mail was delivered with `cat /var/mail/david`:
34 |
35 | From david@localhost Tue Oct 14 11:34:26 2014
36 | Return-Path:
37 | X-Original-To: david@localhost
38 | Delivered-To: david@localhost
39 | Received: from localhost (localhost [127.0.0.1])
40 | by david-simulations (Postfix) with SMTP id 015D3120C36
41 | for ; Tue, 14 Oct 2014 11:34:11 +0100 (BST)
42 | Message-Id: <20141014103421.015D3120C36@david-simulations>
43 | Date: Tue, 14 Oct 2014 11:34:11 +0100 (BST)
44 | From: david@localhost
45 |
46 | hello
47 |
48 |
49 | Great! Now we just need to implement this in python.
50 |
--------------------------------------------------------------------------------
/ping/ping.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | import sys
4 | import argparse
5 |
6 | import time
7 | import socket
8 | from socket import socket as Socket
9 |
10 | def main():
11 |
12 | # Command line arguments. Use a server_port > 1024 by default so that we can run
13 | # server without sudo.
14 | parser = argparse.ArgumentParser()
15 |
16 | parser.add_argument('--server-port', '-p', default=2081, type=int,
17 | help='Server_Port to use')
18 |
19 | parser.add_argument('--run-server', '-s', action='store_true',
20 | help='Run a ping server')
21 |
22 | parser.add_argument('server_address', default='localhost',
23 | help='Server to ping, no effect if running as a server.')
24 |
25 | args = parser.parse_args()
26 |
27 |
28 | if args.run_server:
29 | return run_server(args.server_port)
30 | else:
31 | return run_client(args.server_address, args.server_port,)
32 |
33 |
34 |
35 |
36 | def run_server(server_port):
37 | """Run the UDP pinger server
38 | """
39 |
40 | # Create the server socket (to handle UDP requests using ipv4), make sure
41 | # it is always closed by using with statement.
42 | with Socket(socket.AF_INET, socket.SOCK_DGRAM) as server_socket:
43 |
44 | # The socket stays connected even after this script ends. So in order
45 | # to allow the immediate reuse of the socket (so that we can kill and
46 | # re-run the server while debugging) we set the following option. This
47 | # is potentially dangerous in real code: in rare cases you may get junk
48 | # data arriving at the socket.
49 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
50 |
51 | # Set the server port
52 | server_socket.bind(('', server_port))
53 |
54 | # Start accepting ping requests
55 | print("Ping server ready on port", server_port)
56 | while True:
57 | # Receive message and send one back
58 | _, client_address = server_socket.recvfrom(1024)
59 | server_socket.sendto("".encode(), client_address)
60 |
61 | return 0
62 |
63 |
64 | def run_client(server_address, server_port):
65 | """Ping a UDP pinger server running at the given address
66 | """
67 |
68 | # Fill in the client side code here.
69 |
70 | raise NotImplementedError
71 |
72 | return 0
73 |
74 | if __name__ == "__main__":
75 | sys.exit(main())
76 |
--------------------------------------------------------------------------------
/ping/running.md:
--------------------------------------------------------------------------------
1 | To run the server use the command
2 |
3 | ./ping.py -s localhost
4 |
5 | To ping the server run
6 |
7 | ./ping.py [server-address]
8 |
9 | Unfortunately I've only been able to test this locally so far... Let me
10 | know if you have any issues on remote servers.
11 |
--------------------------------------------------------------------------------
/web-proxy/testing.md:
--------------------------------------------------------------------------------
1 | I tested this code by using `curl` to grab a website via the proxy. If the proxy is running on the same computer the correct command to do this is
2 |
3 | curl --proxy localhost:8080 http://orgmode.org/manual/Introduction.html
4 |
5 | where I've used the url of the introduction to emacs' org-mode because it's short and mostly text, and so won't fill your terminal with junk.
6 |
7 | My implementation is only partially complete:
8 |
9 | * It only forwards the first chunk of the recived data (i.e. `recv(..)` is only called once) because the rules for receiving later chunks are [complex](http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-24.html#message.body.length).
10 | * It isn't multithreaded because this would be more complex to implement in ways that have nothing to do with network protocols.
11 | * It doesn't handle updating the results when they are out of date because it would be tricky to test (I guess we would need to fake the data and the current time, bit more fiddly).
12 |
--------------------------------------------------------------------------------
/web-proxy/web-proxy.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | import argparse
4 |
5 | import sys
6 | import socket
7 | import itertools
8 | from socket import socket as Socket
9 |
10 |
11 | def main():
12 |
13 | # Command line arguments. Use port 8080 by default: widely used for proxys
14 | # and >1024 so we don't need sudo to run.
15 | parser = argparse.ArgumentParser()
16 | parser.add_argument('--port', '-p', default=8080, type=int,
17 | help='Port to use')
18 | args = parser.parse_args()
19 |
20 |
21 | # Create the server socket (to handle tcp requests using ipv4), make sure
22 | # it is always closed by using with statement.
23 | with Socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
24 |
25 | # The socket stays connected even after this script ends. So in order
26 | # to allow the immediate reuse of the socket (so that we can kill and
27 | # re-run the server while debugging) we set the following option. This
28 | # is potentially dangerous in real code: in rare cases you may get junk
29 | # data arriving at the socket.
30 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
31 |
32 | server_socket.bind(('', args.port))
33 | server_socket.listen(1)
34 | # no multithreaded yet, would need to set up atomic updates to dict.
35 | # Might be automatic in python?
36 |
37 | # Create empty dict for cached pages
38 | cache_dict = {}
39 |
40 | print("Proxy server ready")
41 |
42 | while True:
43 | # Accept TCP connection from client
44 | with server_socket.accept()[0] as connection_socket:
45 |
46 |
47 | # Fill in the code to recive the request, check if the url is
48 | # in cache_dict and either serve the cached version or request
49 | # the page from the real server and cache it.
50 |
51 | # You may want to use code from the web server to extract
52 | # information from the request.
53 |
54 | # If you want to do more after that you could try to handle
55 | # updating cached pages, and then try to convert the server to
56 | # a multithreaded version.
57 |
58 | return 0
59 |
60 |
61 | if __name__ == "__main__":
62 | sys.exit(main())
63 |
--------------------------------------------------------------------------------
/web-server/tips-for-testing.md:
--------------------------------------------------------------------------------
1 |
2 | * It's much easier to test parsing functions on a python string, without any involvement of sockets (i.e. unit test them)
3 |
4 | * You should test the socket code is working locally using e.g.
5 |
6 | `telnet localhost 8011`
7 |
8 | and then inputting the request as shown in the text book.
9 |
10 | * Trying it out using a real web browser can be more tricky. First run the server as `sudo ./web-server.py -p 80` (sudo is needed for low number ports). Secondly you will need to make sure that port 80 is accessible from outside your local network, look up information on "port forwarding" for this.
11 |
--------------------------------------------------------------------------------
/web-server/web-server.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | import argparse
4 |
5 | import sys
6 | import itertools
7 | import socket
8 | from socket import socket as Socket
9 |
10 | # A simple web server
11 |
12 | # Issues:
13 | # Ignores CRLF requirement
14 | # Header must be < 1024 bytes
15 | # ...
16 | # probabaly loads more
17 |
18 |
19 | def main():
20 |
21 | # Command line arguments. Use a port > 1024 by default so that we can run
22 | # without sudo, for use as a real server you need to use port 80.
23 | parser = argparse.ArgumentParser()
24 | parser.add_argument('--port', '-p', default=2080, type=int,
25 | help='Port to use')
26 | args = parser.parse_args()
27 |
28 | # Create the server socket (to handle tcp requests using ipv4), make sure
29 | # it is always closed by using with statement.
30 | with Socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
31 |
32 | # The socket stays connected even after this script ends. So in order
33 | # to allow the immediate reuse of the socket (so that we can kill and
34 | # re-run the server while debugging) we set the following option. This
35 | # is potentially dangerous in real code: in rare cases you may get junk
36 | # data arriving at the socket.
37 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
38 |
39 | server_socket.bind(('', args.port))
40 | server_socket.listen(1)
41 |
42 | print("server ready")
43 |
44 | while True:
45 |
46 | with server_socket.accept()[0] as connection_socket:
47 | request = connection_socket.recv(1024).decode('ascii')
48 | reply = http_handle(request)
49 | connection_socket.send(reply.encode('ascii'))
50 |
51 |
52 | print("\n\nReceived request")
53 | print("======================")
54 | print(request.rstrip())
55 | print("======================")
56 |
57 |
58 | print("\n\nReplied with")
59 | print("======================")
60 | print(reply.rstrip())
61 | print("======================")
62 |
63 |
64 | return 0
65 |
66 |
67 | def http_handle(request_string):
68 | """Given a http requst return a response
69 |
70 | Both request and response are unicode strings with platform standard
71 | line endings.
72 | """
73 |
74 | assert not isinstance(request_string, bytes)
75 |
76 |
77 | # Fill in the code to handle the http request here. You will probably want
78 | # to write additional functions to parse the http request into a nicer data
79 | # structure (eg a dict), and to easily create http responses.
80 |
81 | raise NotImplementedError
82 |
83 | pass
84 |
85 |
86 |
87 | if __name__ == "__main__":
88 | sys.exit(main())
89 |
--------------------------------------------------------------------------------