============== Administrivia: ============== - Course evaluations are this week. Be sure to show up for them if you want to provide official feedback for the course. - Lab L4 is happening. It is due on Friday at midnight. ================ Text Formatting: ================ - printf() can be very powerful. Take advantage of it. - printf("0x%02X", some_uint & 0xff) will print the low order byte with a leading zero. - printf("%c", isprint(some_char) ? some_char : '.') will print a character if it is printable, and a period otherwise. ================= Berkeley Sockets: ================= - Allows network connections to behave like files. - Based on connections to ports. - Processes create sockets with the socket() syscall call. - Connect your socket to another socket with connect(). - Give the socket a name with bind(). - Tell the socket to allow connections with listen(). - Accept a connection request with accept(). ====================== Servers and Protocols: ====================== SMTP summary from RFC 821 ------------------------- - protocol for transferring mail - any reliable transport will do - communication starts from machine on which mail is sent - message transferred via SMTP to next machine (final or intermediate) - next machine can reject the mail - to transfer, the source machine connects to a server on the next machine (assigned number: port 25) - the protocol is a series of commands issued by the sender, - responses from the receiver with error codes - mention full BNF, error codes to be found in rfc821 - protocol state diagrams also in RFC sender commands: (followed by ) "HELO " (identify source machine, start of connection) "MAIL FROM:" (id of sender, optional reverseroute for errors) "RCPT TO:" (id of recipient, optional forward source route) "DATA" (then enter the text, INCLUDING HEADERS, . to end) "QUIT" (close connection) "NOOP" (nothing) "RSET" (reset buffers - forward route, reverse route, msg buffer) "HELP" (handy-dandy protocol command help) receiver replies: first digit: good, bad, incomplete response 2nd digit: category (syntax, information, etc) 3rd digit: diversity within category 220 (Service ready) 250 (OK) 421 (Connection terminated) 500 (Syntax Error) - Sample session: [Make a slide of this] > telnet coltrane.stampede.cs.cmu.edu smtp Trying 128.2.181.76... Connected to coltrane.stampede.cs.cmu.edu. Escape character is '^]'. 220 coltrane.stampede.cs.cmu.edu SMTP, Problems to: Help@FAC.CS.CMU.EDU helo gs138.sp.cs.cmu.edu 250 coltrane.stampede.cs.cmu.edu mail from: 250 OK rcpt to: 250 Rcpt OK data 354 Enter Mail, end with a '.' This is a sample message to show the class how a session works. . 250 Sub & q (msg.aa02024) quit 221 Goodbye Connection closed by foreign host. - If a period starts a line, a period is prefixed to the line. The receiver strips out leading periods. - Protocol has state: the commands must occur in the order "helo", "mail", "rcpt"*n, "data". - Defined in RFC821 /afs/andrew/common/rfc/rfcXXXX.txt /afs/andrew/common/internet-drafts/... HTTP 1.0 summary from RFC 1945 ------------------------------ - stateless application-level protocol - can be used for name-servers and distributed object management systems - includes notion of data-types --> MIME (rfc1521) - da Web - basic mechanism: naming - URI, transport - lots of nasty terminology (entity, resource, user agent, proxy, etc) - lots of nasty BNF - deals with variety of date formats, character sets, data types, encoding schemes, all of which makes it hairy to learn - request/response mechanism with NO STATE (unlike SMTP) state must accompany the request in the form of headers - basic request types: "GET", "HEAD", "POST" (assigned number: port 80) telnet www.theonion.com http -->"GET / HTTP/1.0" <--"HTTP/1.1 200 OK", headers, HTML of the root page! telnet www.theonion.com http -->"GET http://www.cnn.com/ HTTP/1.0" <--"HTTP/1.0 403 Forbidden", headers, HTML of the error page telnet www.cs.cmu.edu http -->"HEAD /~kwalker/index.html HTTP/1.0" <--"HTTP/1.1 200 OK", headers, including data (Fri, 28 Aug 1998 20:00:46 GMT) telnet www.cs.cmu.edu http -->"GET /~kwalker/index.html HTTP/1.0 If-Modified-Since: " <--"HTTP/1.0 304 Not Modified" - response codes: 2xx good news 3xx redirection (and not modified) 4xx client error 5xx server error ========== fork()ing: ========== - Fork is what is used in Unix to start all processes - Splits a process into two (almost) identical processes - Will return 0 to the child process, and the child's PID to the parent - Correct usage: if(fork() == 0) { /* I am the child. Do stuff. */ } else { /* I am the parent. Do stuff. */ /* Wait for the child to finish: */ wait(); } - Incorrect usage: What does this do? while(1) { fork(); } [Fork bomb -- infinitely forks until the machine can't handle all of the processes. This is *bad*.] - How about this? while(fork() == 0) {} [Morphing program. Constantly changes PID. Very hard to kill.] - When debugging a program that forks, check to see if you have left behind extra processes. System V: ps -eu colohan e = info about every process u = restrict to user "colohan" BSD: ps x x = include processes not bound to a terminal - To kill an errant process: kill - To toast it with predjudice: kill -9 - To kill every process you have authority to kill (DO NOT RUN AS ROOT): kill -9 -1