======================= Midterm Test #1 Answers ======================= - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Test #1 Written Answer Section - Points: 75 (15 of 20%) 1. [Points: 1] Give the dotted-quad network mask for this subnet: 1.1.1.0/26 255.255.255.192 2. [Points: 1] Correct this non-portable line of code: saddr.sin_port=portno; =htons(portno) 3. [Points: 1] Correct this non-portable line of code: serv_addr.sin_addr.s_addr=INADDR_ANY; =htonl(INADDR_ANY) 4. [Points: 2] What is the next sub-net address numerically greater than: 2.2.2.0/25? 2.2.2.128/25 5. [Points: 2] What is the next sub-net address numerically greater than: 9.9.9.0/24? 9.9.10.0/24 6. [Points: 4] Give any two of the three RFC1918 private address space blocks and masks. 10/8 172.16/12 192.168/16 7. [Points: 3] List in execution order the two major Unix/Linux network system call function names used in a TCP client (e.g. your client.c): socket,connect 8. [Points: 5] List in execution order the four major Unix/Linux network system call function names used in a TCP server (e.g. your server2.c): socket,bind,listen,accept 9. [Points: 6] TCP/UDP port numbers are divided into named ranges by ICANN/IANA. Give the IANA name for each range and give the port start and end values of the range. see http://www.iana.org/assignments/port-numbers 10. [Points: 1] Give a C library function call that will send a signal to terminate the program after 30 minutes (e.g. as used in server2.c): alarm(30*60) 11. [Points: 2] Give an example of a possible error message you might see after creating a socket and trying to associate the socket with a particular IP address and port on the local machine (e.g. as done in server2.c): port in use, no permission 12. [Points: 1] 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" ? Little-Endian 13. [Points: 2] Give the dotted-quad form for the IP address represented by the bit pattern -1. 255.255.255.255 14. [Points: 1] What Unix command shows the MAC, IP address, and network mask of each network interface? ifconfig 15. [Points: 3] I want my computer to talk to another computer on a different network from mine. What minimum network configuration do I need? IPaddr,NetMask,GatewayIP 16. [Points: 20] In your exam booklet, write detailed PDL for a forking two-process client (e.g. same as your client.c) that opens a connection to a server and sends standard input (e.g. keyboard) to a remote TCP/IP server and receives the echo of the input back and displays it on standard output (e.g. screen). The PDL should show where one process needs to kill the other process. The PDL should show where one process needs to shut down the writing half of the socket before exiting. ------------------------------------------------------------------ s = create socket connect socket s to remote server IP and port cpid = fork child (cpid == 0): loop: nr = read from stdin (e.g. from keyboard) into buffer on error print message and break on EOF print message and break write nr bytes from buffer to socket s on error print message and break end shut down writing half of socket s exit parent (cpid > 0): loop: nr = read from socket s into buffer on error print message and break on EOF print message and break write nr bytes from buffer to stdout (e.g. to screen) on error print message and break end kill child process via cpid exit ------------------------------------------------------------------ 17. [Points: 20] In your exam booklet, write the C code that would implement the PDL from either one of the two read/write loops in the above client program (client.c). The loop continuously reads bytes from the open file descriptor named in and writes the bytes to the open file descriptor named out until EOF or any errors occur. Write only the loop code; no headers; no comments; no main(); no prompts. If any system call errors occur, print the reason for the error (using the standard C library function to display system call errors), and stop looping. On EOF, print the string "EOF" on standard error and stop looping. Keep the messages simple; no program name needs to be added. Do not use myerror(). As you did in your own client.c, you should read bytes using the system calls read(fd,buf,len) and write the bytes using the cover function sendall(fd,buf,&len). Do not use myerror(). ------------------------------------------------------------------ char buf[256]; int nr; int nw; while(1){ nr = read(in,buf,sizeof(buf)); if ( nr < 0 ) { perror("read"); break; } if( nr == 0 ){ fprintf(stderr,"EOF\n"); break; ) nw = sendall(out,buf,&nr); if ( nw < 0 ) { perror("sendall"); break; } } ------------------------------------------------------------------