------------------------- Week 02 Notes for CST8165 ------------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Midterm test dates are posted on the Course Home Page. Makefiles - blanks and blank lines affect Makefile operation - target command lines must start with a TAB, not blanks - don't have lines with trailing blanks below a target - separate targets by empty lines - in VIM ":set list" will show tabs and trailing blanks - ":set nolist" to turn it off The Internet - who owns it? who controls it? ------------ - the Internet is not just the WWW (HTTP) - but Algonquin College blocks most non-HTTP traffic - in particular, the SMTP port (25) is blocked to external sites - blocks are "drop packet", not "refuse packet" types; they time out - Internet not developed as a proprietary system - standards-based vs. product-based - nobody pays license fees to use TCP/IP, SMTP, HTTP, etc. - Tim Berners-Lee doesn't get royalties for your web site - why do companies still write web pages that only work in one browser? - e.g. Algonquin Blackboard - http://www.anybrowser.org/campaign/ - Like the downtown streets at rush hour, the Internet doesn't (yet) pass traffic based on how much money you have. You can't get higher priority by paying more; though, this may change (on the Internet) in the next year or two if the backbone carries have their way. - http://www.digital-copyright.ca/node/2894 "Net Neutrality: A Public Discussion on the Future of the Internet in Canada" Feb 6 2007 (free) - The Internet is dumb. Some say you could implement it using two cans and a string. (Or carrier pigeons.) The intelligence is "at the edges". - pigeons: http://tools.ietf.org/html/1149 - pigeons: http://www.blug.linux.no/rfc1149/ - WWW slashes are "forward" slashes because the WWW grew up on Unix machines. DOS/Windows came much later. - text-based Internet protocols pre-date XML (everything is text in Unix) - Unix was full of tools to deal with text - an "ethereal" dump of most Internet protocols is often very readable - Internet Engineering Task Force (IETF) - Motto: "Rough consensus and running code." "When I was studying Physics the quickest way to end an argument was to show the explanation in mathematics (albeit a lot of handwaving mathematics!). Most software developers on the otherhand do not grok math, however they surely do grok code. Therefore if you could explain your arguments through code then you would have improved your odds of getting your message through." http://www.manageability.org/blog/stuff/rest-explained-in-code/view "Be liberal in what you accept, and conservative in what you send" (Jon Postel, TCP/IP developer) "If we were all conservative in what we do, then we wouldn't do much that is new, or different. This would seem to retard progress. Of course, the same would be true in protocols so perhaps we need a "where possible" qualifier." http://www.aaronsw.com/weblog/000776 - FLOSS (Free/Libre Open Source Software) - open-source discussions occur with source code samples - Internet standards: ARPAnet Request for Comment - RFC http://tools.ietf.org/html/ IP: http://tools.ietf.org/html/791 (45 pages) SMTP: http://tools.ietf.org/html/2821 (79 pages) * Who controls handing out the IP numbers and port numbers? - the Internet Corporation for Assigned Names and Numbers (ICANN) through its operating unit the Internet Assigned Numbers Authority (IANA) "Dedicated to preserving the central coordinating functions of the global Internet for the public good." ICANN: http://www.icann.org/ IANA: http://www.iana.org/ - IANA delegates to a few Regional Internet Registries (RIRs) to distribute the large blocks of IP addresses http://www.iana.org/ipaddress/ip-addresses.htm http://www.iana.org/assignments/ipv4-address-space - e.g. ARIN IP address list http://www.arin.net/ - special addresses (historical and current) http://www.rfc-editor.org/rfc/rfc3330.txt - note: RFC1918 private address space 10.0.0.0 - 10.255.255.255 (10/8 prefix) 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) "the Internet does not inherently protect against abuse of these addresses; if you expect (for instance) that all packets from the 10.0.0.0/8 block originate within your subnet, all border routers should filter such packets that originate from elsewhere. Attacks have been mounted that depend on the unexpected use of some of these addresses." - IANA TCP/UDP port list (see RFC4340 for the three big divisions) http://www.iana.org/assignments/port-numbers - Well Known Ports are those from 0 through 1023 - Registered Ports are those from 1024 through 49151 - Dynamic and/or Private Ports are those from 49152 through 65535 - a shorter Unix/Linux specific copy of this file is kept in /etc/services Q: Who is the ultimate authority on all IP addresses and ports? Give the full name. Q: Which organization is delegated to manage IP addresses in North America? Give the full name. Q: Give any two of the three RFC1918 private address space blocks and masks Q: Is 172.15.0.0 a RFC1918 private address? Q: Is 172.17.0.0 a RFC1918 private address? Q: What is the last (highest) private address in the RFC1918 10.0.0.0 block? Q: What is the last (highest) private address in the RFC1918 172.16.0.0 block? Q: What is the last (highest) private address in the RFC1918 192.168.0.0 block? Q: T/F Special address block 0.0.0.0 is reserved for hosts on your local network. [RFC3330] Q: T/F IP address 0.0.0.0 is not a valid address. Q: Name and give the port ranges of the three RFC4340 divisions of ports Q: What Unix/Linux file is used to turn "smtp" into "25" when you do $ nc -v localhost smtp Client/Server programming ------------------------ Background: Know the low-level Unix system calls: - man 2 open (returns a small integer file descriptor) - unit 0 is already open in your program as standard input - unit 1 is already open in your program as standard output - unit 2 is already open in your program as standard error - unit 3 is usually the next integer returned by open() in your program - man 2 read - man 2 write - man 2 close The low level Unix system calls "open()", "read()", "write()", and "close()": - have no buffering (are not like stdio fopen/fgets/fread/fwrite/fclose) - return -1 on error and set errno (which can be used by perror()) - errno is only set after a system call *fails*, not when it succeeds You may (should) use perror() to print errno after a system call fails - man 3 perror - errno is only set after a system call *fails*, not when it succeeds Note that a successful system call does *NOT* clear or set errno to zero! - you cannot test errno to know if a system call failed - errno is only set after a system call *fails*, not when it succeeds Socket programming is similar to low-level Unix file I/O - the Unix socket() and accept() system calls return small integer file descriptors, just as open() does - socket descriptors are just like file descriptors - you can use them with read() and write() (many socket programs use the equivalent recv() and send()) - see server examples: http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html http://www.cs.rpi.edu/courses/sysprog/sockets/server.c - read the explanation of the code in the above socket tutorial - note that you should replace the deprecated bzero() with memset() - see "man bstring" http://www.cs.rpi.edu/courses/sysprog/sockets/server2.c - a fork()ing server that handles multiple connections - how to make server2.c read and return more than one line? - add a loop in the child funcion dostuff() The usual order of system calls for a TCP/IP server: - socket(), bind(), listen(), accept() - most servers loop calling accept() to receive multiple connections - server may fork() separate child processes to deal with each connection - each connection may loop reading/writing the accepted socket, to read/write multiple lines from/to the incoming connection - "server.c" only accepts one connection, reads one line, and exits - "server2.c" loops accepting many connections - each connection reads one line, and exits - how would you modify server2.c to create "server3.c" to read/write multiple lines for each connection? Sending data on the network: Big Endian / Little Endian: - what does the function call htons(portno) do? - it puts the short integer "portno" into network byte order - http://www.unixpapa.com/incnote/byteorder.html - http://www.cs.rpi.edu/courses/sysprog/sockets/byteorder.html - http://www.netrino.com/Publications/Glossary/Endianness.php - http://www.rdrop.com/~cary/html/endian_faq.html - "network byte order" is Big Endian - Intel hardware is little-endian - Sun Sparc hardware is big-endian - little-endian hardware incurs a byte-swap penalty handling network traffic Q: What is the meaning of the small integer second parameter to listen()? Q: What does htons() do and why is it necessary? Q: What is the purpose of the bind() syscall? Q: What is the purpose of the listen() syscall? Q: What is the purpose of the accept() syscall? Q: T/F the socket() and accept() syscalls return file descriptors that can be used with standard I/O fread/fwrite/fclose Q: T/F the successful accept() system call returns a socket file descriptor that is a copy of the socket file descriptor that is its first argument Q: T/F the output of perror() appears on standard error, not standard output Q: T/F after a successful system call, perror() prints nothing Q: T/F when most Unix syscalls fail, the return value is zero Q: T/F when most Unix syscalls fail, the external global errno is set to -1 Q: T/F after a successful fork() system call, the parent process receives a non-zero child pid Q: T/F on error, the open() syscall returns zero Q: T/F on error, the read() syscall returns zero Q: T/F on error, the write() syscall returns zero Q: T/F usually the fd to be returned by the first call to socket() or open() in your program will be fd 3 (why or why not?) Q: why doesn't the first call to socket() or open() in a Unix program return file descriptor 1? Q: what is the small integer value usually returned by the first successful call to accept() in a TCP/IP server program? (Hint: accept() is called *after* socket()) Q: T/F "network byte order" is Big Endian Q: T/F a Big Endian processor stores the Big End (most significant byte) of a number in the first (lowest) memory location Q: T/F a Little Endian processor sends the Little End (least significant byte) of a number first over a byte-stream communications channel Q: in a memory dump that shows bytes numbered in ascending order from left-to-right on the page, which Endian order shows multi-byte quantities as written "backwards" ?