Application Layer Client sends a user name to the server. The server responds with a "y" or an "n", depending upon whether the user is logged onto the server machine or not. Presentation Layer All data passed from client to server will be ASCII characters. No integers or machine-specific data will be transmitted. Session Layer The user name will be terminated with a NULL character. The server will respond with a single character, and that character must be a `y' or a `n'. Transport Layer UDP, the Internet User Datagram Protocol. Server Code: 1 #include 2 #include 3 #include 4 #include 5 #define SERV_PORT 5134 6 #define MAXNAME 5124 7 8 main() 9 { 10 int dg_fd; /* file descriptor into UDP */ 11 struct sockaddr_in myaddr; /* the addr of this service */ 12 int len; /* length of address */ 13 char buff[MAXNAME]; /* buffer to hold datagram */ 14 char reply; /* response to the client */ 15 struct sockaddr_in client_addr; /* the address of client */ 16 char cmd[BUFSIZ+MAXNAME]; /* used to figure out if */ 17 char junkbuf[BUFSIZ+MAXNAME]; /* the user is logged onto*/ 18 FILE *fp; /* the system */ 19 20 /* 21 * Open a socket into UDP. 22 */ 23 if ((dg_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 24 perror("socket failed"); 25 exit(1); 26 } 27 /* 28 * Set up our address. 29 */ 30 bzero((char *)&myaddr, sizeof(myaddr)); 31 myaddr.sin_family = AF_INET; 32 myaddr.sin_addr.s_addr = htonl(INADDR_ANY); 33 myaddr.sin_port = htons(SERV_PORT); 34 /* 35 * Bind to the address of this service. 36 */ 37 if (bind(dg_fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { 38 perror("bind failed"); 39 exit(1); 40 } 41 /* 42 * Loop continuously, processing datagrams. 43 */ 44 len = sizeof(struct sockaddr); 45 while (1) { 46 if (recvfrom(dg_fd, buff, MAXNAME, 0, &client_addr, &len) < 0) { 47 perror("could not read datagram!"); 48 continue; 49 } 50 sprintf(cmd, "who | grep `^%s `", buff); 51 if ((fp = popen(cmd, "r")) == NULL 52 || fgets(junkbuf, BUFSIZ, fp) == NULL) { 53 reply = `n'; 54 } else { 55 reply = `y'; 56 } 57 if (sendto(dg_fd, &reply, 1, 0, &client_addr, len) < 0) { 58 perror("could not send datagram!"); 59 } 60 } 61 } 62 Client Code: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #define SERV_PORT 5134 8 #define MAXNAME 5124 9 #define NUM_TRIES 20 10 int timed_out; 11 12 main(argc, argv) 13 int argc; 14 char **argv; 15 { 16 int fd; /* fd into transport provider */ 17 char buf[MAXNAME]; /* holds message from server */ 18 int tries = NUM_TRIES; /* number of tries to send */ 19 int got_it = 0; /* determines datagram receipt */ 20 struct hostent *hp; /* holds address of server */ 21 struct sockaddr_in myaddr; /* holds the local address */ 22 struct sockaddr_in servaddr; /* holds the server addr */ 23 int length; /* length of user name */ 24 int size; /* size of sockaddr structure */ 25 void handler(); /* handles time out signals */ 26 /* 27 * Check for proper usage. 28 */ 29 if (argc != 3) { 30 fprintf(stderr,"Usage: %s user server\n", argv[0]); 31 exit(2); 32 } 33 /* 34 * Open the socket into UDP. 35 */ 36 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 37 perror("socket failed!"); 38 exit(1); 39 } 40 /* 41 * Bind to an arbitrary return address. 42 */ 43 bzero((char *)&myaddr, sizeof(myaddr)); 44 myaddr.sin_family = AF_INET; 45 myaddr.sin_addr.s_addr = htonl(INADDR_ANY); 46 myaddr.sin_port = htons(0); 47 48 if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { 49 perror("bind failed!"); 50 exit(1); 51 } 52 /* 53 * Fill in the server's UDP/IP address. 54 */ 55 bzero((char *)&servaddr, sizeof(servaddr)); 56 servaddr.sin_family = AF_INET; 57 servaddr.sin_port = htons(SERV_PORT); 58 hp = gethostbyname(argv[2]); 59 if (hp == 0) { 60 fprintf(stderr, "could not obtain address of %s\n", argv [2]); 61 return (-1); 62 } 63 64 bcopy(hp->h_addr_list[0], (caddr_t)&servaddr.sin_addr, hp->h_length); 65 length = strlen(argv[1]) + 1; 66 size = sizeof(servaddr); 67 while (tries --) { 68 if (sendto(fd, argv[1], length, 0, (struct sockaddr *)&servaddr, size) != length) { 69 perror("sendto failed!\n"); 70 exit(1); 71 } 72 /* 73 * Allow 15 seconds for the response to arrive. 74 */ 75 timed_out = 0; 76 signal(SIGALRM, handler); 77 alarm(15); 78 if (recvfrom(fd, buf, MAXNAME, 0, (struct sockaddr *)0, (int *)0) >= 0) { 79 alarm(0); 80 got_it = 1; 81 break; 82 } 83 if (timed_out) { 84 printf("Server not responding, retrying...\n"); 85 continue; 86 } 87 fprintf(stderr, "recvfrom failed!\n"); 88 exit(1); 89 } 90 if (!got_it) { 91 printf("failed %d times, exiting\n", NUM_TRIES); 92 exit(1); 93 } 94 95 if (buf[0] == `y') { 96 printf("%s is logged on server\n", argv[1]); 97 } else { 98 printf("%s is not logged on server\n", argv[1]); 99 } 100 exit(0); 101 } 102 103 void 104 handler() 105 { 106 timed_out = 1; 107 } 108 On host2: $ server_proc & On host1: $ client_proc elvis host2 elvis is not logged on server $ client_proc root host2 root is logged on server $ client_proc xxxxxxxxxx...xxxxxxxxx host2 xxxxxxxxx....xxxxxxx is not logged on server Here's what's on the wire 1. ff ff ff ff ff ff 08 00 5a 47 43 58 08 06 00 01 2. 08 00 06 04 00 01 08 00 5a 47 43 58 c6 4b 18 79 3. 00 00 00 00 00 00 c6 4b 18 24 10 02 00 35 92 95 4. 01 04 00 00 00 00 00 00 00 00 20 20 5. 6. 08 00 5a 47 43 58 08 00 20 21 fb 7d 08 06 00 01 7. 08 00 06 04 00 02 08 00 20 21 fb 7d c6 4b 18 24 8. 08 00 5a 47 43 58 c6 4b 18 79 10 02 00 35 92 95 9. 01 04 00 00 00 00 00 00 00 00 20 20 10. 11. 08 00 20 21 fb 7d 08 00 5a 47 43 58 08 00 45 00 12. 00 22 fa ed 00 00 1e 11 e4 a9 c6 4b 18 79 c6 4b 13. 18 24 0f d4 14 0e 00 0e cf e5 65 6c 76 69 73 00 14. 10 02 00 35 92 95 01 04 00 00 00 00 15. 16. 08 00 5a 47 43 58 08 00 20 21 fb 7d 08 00 45 00 17. 00 1d c0 fa 00 00 3c 11 00 a2 c6 4b 18 24 c6 4b 18. 18 79 14 0e 0f d4 00 09 00 00 6e 00 00 00 00 00 19. 00 00 00 00 00 00 00 00 00 00 00 00 20. 21. 08 00 20 21 fb 7d 08 00 5a 47 43 58 08 00 45 00 22. 00 21 ff f0 00 00 1e 11 df a7 c6 4b 18 79 c6 4b 23. 18 24 0f f2 14 0e 00 0d 3c bc 72 6f 6f 74 00 00 24. 00 00 70 01 48 35 68 05 58 30 00 00 25. 26. 08 00 5a 47 43 58 08 00 20 21 fb 7d 08 00 45 00 27. 00 1d f9 84 00 00 3c 11 c8 17 c6 4b 18 24 c6 4b 28. 18 79 14 0e 0f f2 00 09 00 00 79 24 04 f7 00 17 29. 7a 54 68 36 35 bd d1 0d 50 10 10 00 30. 31. 08 00 20 21 fb 7d 08 00 5a 47 43 58 08 00 45 00 32. 05 dc fc 4a 20 00 1e 11 bd 92 c6 4b 18 79 c6 4b 33. 18 24 0f df 14 0e 07 d9 78 84 78 78 78 78 78 78 34. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 35. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 36. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 37. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 38. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 39. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 40. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 41. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 42. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 43. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 44. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 45. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 46. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 47. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 48. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 49. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 50. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 51. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 52. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 53. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 54. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 55. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 56. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 57. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 58. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 59. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 60. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 61. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 62. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 63. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 64. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 65. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 66. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 67. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 68. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 69. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 70. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 71. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 72. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 73. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 74. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 75. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 76. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 77. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 79. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 80. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 81. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 82. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 83. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 84. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 85. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 86. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 87. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 88. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 89. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 90. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 91. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 92. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 93. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 94. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 95. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 96. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 97. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 98. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 99. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 100. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 101. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 102. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 103. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 104. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 105. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 106. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 107. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 108. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 109. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 110. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 111. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 112. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 113. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 114. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 115. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 116. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 117. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 118. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 119. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 120. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 121. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 122. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 123. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 124. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 125. 78 78 78 78 78 78 78 78 78 78 126. 127. 08 00 20 21 fb 7d 08 00 5a 47 43 58 08 00 45 00 128. 02 25 fc 4a 00 b9 1e 11 e0 90 c6 4b 18 79 c6 4b 129. 18 24 78 78 78 78 78 78 78 78 78 78 78 78 78 78 130. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 131. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 132. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 133. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 134. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 135. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 136. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 137. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 138. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 139. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 140. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 141. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 142. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 143. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 144. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 145. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 146. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 147. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 148. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 149. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 150. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 151. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 152. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 153. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 154. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 155. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 156. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 157. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 158. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 159. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 160. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 161. 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 162. 78 78 00 163. 164. 08 00 5a 47 43 58 08 00 20 21 fb 7d 08 00 45 00 165. 00 1d d8 2f 00 00 3c 11 e9 6c c6 4b 18 24 c6 4b 166. 18 79 14 0e 0f df 00 09 00 00 6e 1f 6c 48 00 00 167. 00 01 00 00 00 00 00 00 00 00 00 00 Let's go to another network! $ client_proc elvis 169.124.21.148 elvis is not logged on server 1. ff ff ff ff ff ff 08 00 5a 47 43 58 08 06 00 01 2. 08 00 06 04 00 01 08 00 5a 47 43 58 c6 4b 18 79 3. 00 00 00 00 00 00 c6 4b 18 01 00 00 00 1c 00 1f 4. c9 a0 00 00 00 00 00 00 38 40 00 00 5. 6. 08 00 5a 47 43 58 00 00 0c 19 b5 d1 08 06 00 01 7. 08 00 06 04 00 02 00 00 0c 19 b5 d1 c6 4b 18 01 8. 08 00 5a 47 43 58 c6 4b 18 79 00 00 00 00 00 00 9. 00 00 00 00 00 00 00 00 00 00 00 00 10. 11. 00 00 0c 19 b5 d1 08 00 5a 47 43 58 08 00 45 00 12. 00 22 08 27 00 00 1e 11 f6 cf c6 4b 18 79 a9 7c 13. 15 94 10 0f 14 0e 00 0e ef 09 65 6c 76 69 73 00 14. 00 00 00 72 01 48 35 68 05 58 30 00 15. 16. 08 00 5a 47 43 58 00 00 0c 19 b5 d1 08 00 45 00 17. 00 1d e1 b5 00 00 38 11 03 46 a9 7c 15 94 c6 4b 18. 18 79 14 0e 10 0f 00 09 00 00 6e 00 00 00 00 00 19. 00 00 00 00 00 00 00 01 00 00 00 00