Homework 1 solution and some questions Ying Zhang Homework 1 total score: 90 Max score: 90 Mean: 85.8858 Std: 6.17 tin_setup_conn() struct sockaddr_in server_addr; int sock, sockopt_on=1; struct hostent * he; //get a socket if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { perror("socket acquisition failed"); exit(1); } // make socket reusable if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, & sockopt_on, sizeof (int)) == -1) { perror ("socket reusable option failed"); exit (1); } // look up host he = gethostbyname (servname); if (he == NULL) { perror ("name lookup failed "); exit (1); } // prepare server address datastructure memset (( char *) &server_addr, 0, sizeof (struct sockaddr_in)); server_addr.sin_family = PF_INET; server_addr.sin_port = htons (port); server_addr.sin_addr = *((struct in_addr *)he->h_addr); // connect to server if (connect (sock, (struct sockaddr *) &server_addr, sizeof (struct sockaddr_in)) == -1) { perror ("connection failed"); exit(1); } return sock; tin_send_hdr() struct _tin_head header; int bytes_to_send= sizeof (struct _tin_head); int bytes_sent=0; //make sure asgn has TIN_ALEN-1 chars. if (strlen(asgn) != (TIN_ALEN-1)) { perror ("assignment name length"); exit(1); } header.tih_vers = htons(TIN_VERS); header.tih_op = htons(op); memcpy(&header.tih_asgn,asgn,TIN_ALEN-1); header.tih_asgn[TIN_ALEN-1]='\0'; // 0 for TIN_LIST taken care in parse header.tih_nfiles=htons((unsigned short)nfiles); // now need to send the header while (bytes_sent < bytes_to_send) { int sent=send (sd, ((char *)(& header)+bytes_sent), bytes_to_send-bytes_sent,0); if (sent==-1) { perror ("tin_send_hdr"); exit(1); } bytes_sent+=sent; } tin_send_fhdr() struct _tif_head header; int bytes_to_send=sizeof (struct _tif_head); int bytes_sent=0; int file; // open the file for reading file=open(fname,O_RDONLY); if (file==-1) { perror ("cannot open file"); exit(1); } // get file size and rewind *fsize=(int)lseek (file, 0, SEEK_END); lseek (file, 0, SEEK_SET); // prepare packet memset(&header,0,bytes_to_send); memcpy(&header.tif_fname,fname,strlen(fname)); header.tif_fsize=htonl(*fsize); // send header while (bytes_sent < bytes_to_send) { int sent=send (sd, ((char *)(&header)+bytes_sent), bytes_to_send-bytes_sent,0); if (sent==-1) { perror ("tin_send_fhdr"); exit(1); } bytes_sent+=sent; } return file; tin_send_file() int total_to_send = (int)lseek (fd, 0, SEEK_END); int bytes_to_send; int bytes_sent=0; char buff [TIN_DLEN]; lseek (fd, 0, SEEK_SET); // rewind while(read(fd, buff, TIN_DLEN)!=0) { bytes_sent=0; bytes_to_send = total_to_send < TIN_DLEN ? total_to_send : TIN_DLEN; // send chunk while (bytes_sent < bytes_to_send) { int sent=send (sd, ((char *)(& buff)+bytes_sent), bytes_to_send-bytes_sent,0); if (sent==-1) { perror ("tin_send_file"); exit(1); } bytes_sent+=sent; } total_to_send -= bytes_sent; } tin_recv_ack() int bytes_received=0; char buff_curr, buff_prev=0; int recvd; do { recvd=recv (sd, & (buff_curr), 1,0); if (recvd==-1) { perror ("tin_recv_ack"); exit(1); } bytes_received +=recvd; fprintf(stderr,"%c",buff_curr); buff_prev=buff_curr; } while (recvd!=0); return bytes_received; tind_recv() while (try < TIND_MAXTRIES) { FD_ZERO (&rfds); //init FD_ZERO (&efds); //init FD_SET(td,&rfds); //add td to listening set FD_SET(td,&efds); //add td to error set to.tv_sec = TIND_SLEEPUSEC / 1000000; to.tv_usec = TIND_SLEEPUSEC % 1000000; // blocking call to select rval=select(td+1,&rfds,NULL,&efds,&to); if (rval<0) { perror ("select"); exit(1); } if (FD_ISSET (td, &efds)) //some error with client connection { //client closes connection too early close (td); return tind_errmsg ("Connection closed\n",0); } if (FD_ISSET (td, &rfds)) //check if it is there { received=recv(td, buf + total_received, *blen - total_received,0 if (received==-1) { //client closes connection too early close (td); return tind_errmsg ("Connection closed\n",0); } else if (received != 0) { total_received += received; if (total_received == * blen) {return NULL;} // correct termination } else { try++; //increment try only if nothing is received } } else { try++; //increment try only if nothing is received } } // if you are here then a client error had occured *blen=total_received; close(td); return tind_errmsg("Connection time-out",0); Questions Q1 What are the two types of transport services that the Internet provides to its applications? What are some characteristics of each of these services? A1: The Internet provides its applications a connection-oriented service (TCP) and a connectionless service (UDP). Each Internet application makes use of one these two services. A1 (cont.) Some of the principle characteristics of the connection-oriented service are: Two end-systems first “handshake” before either starts to send application data to the other. Provides reliable data transfer, i.e., all application data sent by one side of the connection arrives at the other side of the connection in order and without any gaps. Provides flow control, i.e., it makes sure that neither end of a connection overwhelms the buffers in the other end of the connection by sending to many packets to fast. Provides congestion control, i.e., regulates the amount of data that an application can send into the network, helping to prevent the Internet from entering a state of grid lock. A1 (cont.) The principle characteristics of connectionless service are: No handshaking No guarantees of reliable data transfer No flow control or congestion control Q2: Flow control and congestion control are equivalent. Is it true for the Internet’s connectionoriented service? Are the objectives of flow control and congestion control the same? A2: Flow control and congestion control are two distinct control mechanisms with distinct objectives. Flow control makes sure that neither end of a connection overwhelms the buffers in the other end of the connection by sending to many packets to fast. Congestion control regulates the amount of data that an application can send into the network, helping to prevent congestion in the network core (i.e., in the buffers in the network routers). Q3: What is the key distinguishing difference between a tier-1 ISP and a tier-2 ISP? A3 A tier-1 ISP connects to all other tier-1 ISPs; a tier-2 ISP connects to only a few of the tier-1 ISPs. Also, a tier-2 ISP is a customer of one or more tier-1 Q4: Design and describe an application-level protocol to be used between an automatic teller machine and a bank’s centralized computer. Your protocol should allow a user’s card and password to be verified, the account balance (which is maintained at the centralized computer) to be queried, and an account withdrawal to be made( that is, money disbursed to the user). Your protocol entities should be able to handle the all-too-common cases in which there is not enough money in the account to cover the withdrawal. Specify your protocol by listing the messages exchanged and the action taken by the automatic teller machine or the bank’s centralized computer on transmission and receipt of messages. A4: Messages from ATM machine to Server Msg name purpose -------------HELO <userid> Let server know that there is a card in the ATM machine ATM card transmits user ID to Server PASSWD <passwd> User enters PIN, which is sent to server BALANCE User requests balance WITHDRAWL <amount> User asks to withdraw money BYE user all done A4 (Cont.) Messages from Server to ATM machine (display) Msg name -------PASSWD OK ERR AMOUNT <amt> BYE purpose ------Ask user for PIN (password) last requested operation (PASSWD, WITHDRAWL) OK last requested operation (PASSWD, WITHDRAWL) in ERROR sent in response to BALANCE request user done, display welcome screen at ATM Correct operation: client HELO (userid) server --------------> (check if valid userid) <------------- PASSWD PASSWD <passwd> --------------> (check password) <------------- OK (password is OK) BALANCE --------------> <------------- AMOUNT <amt> WITHDRAWL <amt> --------------> check if enough $ to cover withdrawl <------------OK ATM dispenses $ BYE --------------> <------------BYE In situation when there's not enough money client server HELO (userid) --------------> (check if valid userid) <------------- PASSWD PASSWD <passwd> --------------> (check password) <------------- OK (password is OK) BALANCE --------------> <------------- AMOUNT <amt> WITHDRAWL <amt> --------------> check if enough $ to cover withdrawl <------------- ERR (not enough funds) error msg displayed no $ given out BYE --------------> <------------- BYE