uopspop

Untitled

Jul 5th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.52 KB | None | 0 0
  1. /*
  2.  * csapp.c - Functions for the CS:APP3e book
  3.  *
  4.  * Updated 10/2016 reb:
  5.  *   - Fixed bug in sio_ltoa that didn't cover negative numbers
  6.  *
  7.  * Updated 2/2016 droh:
  8.  *   - Updated open_clientfd and open_listenfd to fail more gracefully
  9.  *
  10.  * Updated 8/2014 droh:
  11.  *   - New versions of open_clientfd and open_listenfd are reentrant and
  12.  *     protocol independent.
  13.  *
  14.  *   - Added protocol-independent inet_ntop and inet_pton functions. The
  15.  *     inet_ntoa and inet_aton functions are obsolete.
  16.  *
  17.  * Updated 7/2014 droh:
  18.  *   - Aded reentrant sio (signal-safe I/O) routines
  19.  *
  20.  * Updated 4/2013 droh:
  21.  *   - rio_readlineb: fixed edge case bug
  22.  *   - rio_readnb: removed redundant EINTR check
  23.  */
  24. /* $begin csapp.c */
  25. #include "csapp.h"
  26.  
  27. /**************************
  28.  * Error-handling functions
  29.  **************************/
  30. /* $begin errorfuns */
  31. /* $begin unixerror */
  32. void unix_error(char *msg) /* Unix-style error */
  33. {
  34.     fprintf(stderr, "%s: %s\n", msg, strerror(errno));
  35.     exit(0);
  36. }
  37. /* $end unixerror */
  38.  
  39. void posix_error(int code, char *msg) /* Posix-style error */
  40. {
  41.     fprintf(stderr, "%s: %s\n", msg, strerror(code));
  42.     exit(0);
  43. }
  44.  
  45. void gai_error(int code, char *msg) /* Getaddrinfo-style error */
  46. {
  47.     fprintf(stderr, "%s: %s\n", msg, gai_strerror(code));
  48.     exit(0);
  49. }
  50.  
  51. void app_error(char *msg) /* Application error */
  52. {
  53.     fprintf(stderr, "%s\n", msg);
  54.     exit(0);
  55. }
  56. /* $end errorfuns */
  57.  
  58. void dns_error(char *msg) /* Obsolete gethostbyname error */
  59. {
  60.     fprintf(stderr, "%s\n", msg);
  61.     exit(0);
  62. }
  63.  
  64.  
  65. /*********************************************
  66.  * Wrappers for Unix process control functions
  67.  ********************************************/
  68.  
  69. /* $begin forkwrapper */
  70. pid_t Fork(void)
  71. {
  72.     pid_t pid;
  73.  
  74.     if ((pid = fork()) < 0)
  75.     unix_error("Fork error");
  76.     return pid;
  77. }
  78. /* $end forkwrapper */
  79.  
  80. void Execve(const char *filename, char *const argv[], char *const envp[])
  81. {
  82.     if (execve(filename, argv, envp) < 0)
  83.     unix_error("Execve error");
  84. }
  85.  
  86. /* $begin wait */
  87. pid_t Wait(int *status)
  88. {
  89.     pid_t pid;
  90.  
  91.     if ((pid  = wait(status)) < 0)
  92.     unix_error("Wait error");
  93.     return pid;
  94. }
  95. /* $end wait */
  96.  
  97. pid_t Waitpid(pid_t pid, int *iptr, int options)
  98. {
  99.     pid_t retpid;
  100.  
  101.     if ((retpid  = waitpid(pid, iptr, options)) < 0)
  102.     unix_error("Waitpid error");
  103.     return(retpid);
  104. }
  105.  
  106. /* $begin kill */
  107. void Kill(pid_t pid, int signum)
  108. {
  109.     int rc;
  110.  
  111.     if ((rc = kill(pid, signum)) < 0)
  112.     unix_error("Kill error");
  113. }
  114. /* $end kill */
  115.  
  116. void Pause()
  117. {
  118.     (void)pause();
  119.     return;
  120. }
  121.  
  122. unsigned int Sleep(unsigned int secs)
  123. {
  124.     unsigned int rc;
  125.  
  126.     if ((rc = sleep(secs)) < 0)
  127.     unix_error("Sleep error");
  128.     return rc;
  129. }
  130.  
  131. unsigned int Alarm(unsigned int seconds) {
  132.     return alarm(seconds);
  133. }
  134.  
  135. void Setpgid(pid_t pid, pid_t pgid) {
  136.     int rc;
  137.  
  138.     if ((rc = setpgid(pid, pgid)) < 0)
  139.     unix_error("Setpgid error");
  140.     return;
  141. }
  142.  
  143. pid_t Getpgrp(void) {
  144.     return getpgrp();
  145. }
  146.  
  147. /************************************
  148.  * Wrappers for Unix signal functions
  149.  ***********************************/
  150.  
  151. /* $begin sigaction */
  152. handler_t *Signal(int signum, handler_t *handler)
  153. {
  154.     struct sigaction action, old_action;
  155.  
  156.     action.sa_handler = handler;  
  157.     sigemptyset(&action.sa_mask); /* Block sigs of type being handled */
  158.     action.sa_flags = SA_RESTART; /* Restart syscalls if possible */
  159.  
  160.     if (sigaction(signum, &action, &old_action) < 0)
  161.     unix_error("Signal error");
  162.     return (old_action.sa_handler);
  163. }
  164. /* $end sigaction */
  165.  
  166. void Sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
  167. {
  168.     if (sigprocmask(how, set, oldset) < 0)
  169.     unix_error("Sigprocmask error");
  170.     return;
  171. }
  172.  
  173. void Sigemptyset(sigset_t *set)
  174. {
  175.     if (sigemptyset(set) < 0)
  176.     unix_error("Sigemptyset error");
  177.     return;
  178. }
  179.  
  180. void Sigfillset(sigset_t *set)
  181. {
  182.     if (sigfillset(set) < 0)
  183.     unix_error("Sigfillset error");
  184.     return;
  185. }
  186.  
  187. void Sigaddset(sigset_t *set, int signum)
  188. {
  189.     if (sigaddset(set, signum) < 0)
  190.     unix_error("Sigaddset error");
  191.     return;
  192. }
  193.  
  194. void Sigdelset(sigset_t *set, int signum)
  195. {
  196.     if (sigdelset(set, signum) < 0)
  197.     unix_error("Sigdelset error");
  198.     return;
  199. }
  200.  
  201. int Sigismember(const sigset_t *set, int signum)
  202. {
  203.     int rc;
  204.     if ((rc = sigismember(set, signum)) < 0)
  205.     unix_error("Sigismember error");
  206.     return rc;
  207. }
  208.  
  209. int Sigsuspend(const sigset_t *set)
  210. {
  211.     int rc = sigsuspend(set); /* always returns -1 */
  212.     if (errno != EINTR)
  213.         unix_error("Sigsuspend error");
  214.     return rc;
  215. }
  216.  
  217. /*************************************************************
  218.  * The Sio (Signal-safe I/O) package - simple reentrant output
  219.  * functions that are safe for signal handlers.
  220.  *************************************************************/
  221.  
  222. /* Private sio functions */
  223.  
  224. /* $begin sioprivate */
  225. /* sio_reverse - Reverse a string (from K&R) */
  226. static void sio_reverse(char s[])
  227. {
  228.     int c, i, j;
  229.  
  230.     for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
  231.         c = s[i];
  232.         s[i] = s[j];
  233.         s[j] = c;
  234.     }
  235. }
  236.  
  237. /* sio_ltoa - Convert long to base b string (from K&R) */
  238. static void sio_ltoa(long v, char s[], int b)
  239. {
  240.     int c, i = 0;
  241.     int neg = v < 0;
  242.  
  243.     if (neg)
  244.     v = -v;
  245.  
  246.     do {  
  247.         s[i++] = ((c = (v % b)) < 10)  ?  c + '0' : c - 10 + 'a';
  248.     } while ((v /= b) > 0);
  249.  
  250.     if (neg)
  251.     s[i++] = '-';
  252.  
  253.     s[i] = '\0';
  254.     sio_reverse(s);
  255. }
  256.  
  257. /* sio_strlen - Return length of string (from K&R) */
  258. static size_t sio_strlen(char s[])
  259. {
  260.     int i = 0;
  261.  
  262.     while (s[i] != '\0')
  263.         ++i;
  264.     return i;
  265. }
  266. /* $end sioprivate */
  267.  
  268. /* Public Sio functions */
  269. /* $begin siopublic */
  270.  
  271. ssize_t sio_puts(char s[]) /* Put string */
  272. {
  273.     return write(STDOUT_FILENO, s, sio_strlen(s)); //line:csapp:siostrlen
  274. }
  275.  
  276. ssize_t sio_putl(long v) /* Put long */
  277. {
  278.     char s[128];
  279.    
  280.     sio_ltoa(v, s, 10); /* Based on K&R itoa() */  //line:csapp:sioltoa
  281.     return sio_puts(s);
  282. }
  283.  
  284. void sio_error(char s[]) /* Put error message and exit */
  285. {
  286.     sio_puts(s);
  287.     _exit(1);                                      //line:csapp:sioexit
  288. }
  289. /* $end siopublic */
  290.  
  291. /*******************************
  292.  * Wrappers for the SIO routines
  293.  ******************************/
  294. ssize_t Sio_putl(long v)
  295. {
  296.     ssize_t n;
  297.  
  298.     if ((n = sio_putl(v)) < 0)
  299.     sio_error("Sio_putl error");
  300.     return n;
  301. }
  302.  
  303. ssize_t Sio_puts(char s[])
  304. {
  305.     ssize_t n;
  306.  
  307.     if ((n = sio_puts(s)) < 0)
  308.     sio_error("Sio_puts error");
  309.     return n;
  310. }
  311.  
  312. void Sio_error(char s[])
  313. {
  314.     sio_error(s);
  315. }
  316.  
  317. /********************************
  318.  * Wrappers for Unix I/O routines
  319.  ********************************/
  320.  
  321. int Open(const char *pathname, int flags, mode_t mode)
  322. {
  323.     int rc;
  324.  
  325.     if ((rc = open(pathname, flags, mode))  < 0)
  326.     unix_error("Open error");
  327.     return rc;
  328. }
  329.  
  330. ssize_t Read(int fd, void *buf, size_t count)
  331. {
  332.     ssize_t rc;
  333.  
  334.     if ((rc = read(fd, buf, count)) < 0)
  335.     unix_error("Read error");
  336.     return rc;
  337. }
  338.  
  339. ssize_t Write(int fd, const void *buf, size_t count)
  340. {
  341.     ssize_t rc;
  342.  
  343.     if ((rc = write(fd, buf, count)) < 0)
  344.     unix_error("Write error");
  345.     return rc;
  346. }
  347.  
  348. off_t Lseek(int fildes, off_t offset, int whence)
  349. {
  350.     off_t rc;
  351.  
  352.     if ((rc = lseek(fildes, offset, whence)) < 0)
  353.     unix_error("Lseek error");
  354.     return rc;
  355. }
  356.  
  357. void Close(int fd)
  358. {
  359.     int rc;
  360.  
  361.     if ((rc = close(fd)) < 0)
  362.     unix_error("Close error");
  363. }
  364.  
  365. int Select(int  n, fd_set *readfds, fd_set *writefds,
  366.        fd_set *exceptfds, struct timeval *timeout)
  367. {
  368.     int rc;
  369.  
  370.     if ((rc = select(n, readfds, writefds, exceptfds, timeout)) < 0)
  371.     unix_error("Select error");
  372.     return rc;
  373. }
  374.  
  375. int Dup2(int fd1, int fd2)
  376. {
  377.     int rc;
  378.  
  379.     if ((rc = dup2(fd1, fd2)) < 0)
  380.     unix_error("Dup2 error");
  381.     return rc;
  382. }
  383.  
  384. void Stat(const char *filename, struct stat *buf)
  385. {
  386.     if (stat(filename, buf) < 0)
  387.     unix_error("Stat error");
  388. }
  389.  
  390. void Fstat(int fd, struct stat *buf)
  391. {
  392.     if (fstat(fd, buf) < 0)
  393.     unix_error("Fstat error");
  394. }
  395.  
  396. /*********************************
  397.  * Wrappers for directory function
  398.  *********************************/
  399.  
  400. DIR *Opendir(const char *name)
  401. {
  402.     DIR *dirp = opendir(name);
  403.  
  404.     if (!dirp)
  405.         unix_error("opendir error");
  406.     return dirp;
  407. }
  408.  
  409. struct dirent *Readdir(DIR *dirp)
  410. {
  411.     struct dirent *dep;
  412.    
  413.     errno = 0;
  414.     dep = readdir(dirp);
  415.     if ((dep == NULL) && (errno != 0))
  416.         unix_error("readdir error");
  417.     return dep;
  418. }
  419.  
  420. int Closedir(DIR *dirp)
  421. {
  422.     int rc;
  423.  
  424.     if ((rc = closedir(dirp)) < 0)
  425.         unix_error("closedir error");
  426.     return rc;
  427. }
  428.  
  429. /***************************************
  430.  * Wrappers for memory mapping functions
  431.  ***************************************/
  432. void *Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
  433. {
  434.     void *ptr;
  435.  
  436.     if ((ptr = mmap(addr, len, prot, flags, fd, offset)) == ((void *) -1))
  437.     unix_error("mmap error");
  438.     return(ptr);
  439. }
  440.  
  441. void Munmap(void *start, size_t length)
  442. {
  443.     if (munmap(start, length) < 0)
  444.     unix_error("munmap error");
  445. }
  446.  
  447. /***************************************************
  448.  * Wrappers for dynamic storage allocation functions
  449.  ***************************************************/
  450.  
  451. void *Malloc(size_t size)
  452. {
  453.     void *p;
  454.  
  455.     if ((p  = malloc(size)) == NULL)
  456.     unix_error("Malloc error");
  457.     return p;
  458. }
  459.  
  460. void *Realloc(void *ptr, size_t size)
  461. {
  462.     void *p;
  463.  
  464.     if ((p  = realloc(ptr, size)) == NULL)
  465.     unix_error("Realloc error");
  466.     return p;
  467. }
  468.  
  469. void *Calloc(size_t nmemb, size_t size)
  470. {
  471.     void *p;
  472.  
  473.     if ((p = calloc(nmemb, size)) == NULL)
  474.     unix_error("Calloc error");
  475.     return p;
  476. }
  477.  
  478. void Free(void *ptr)
  479. {
  480.     free(ptr);
  481. }
  482.  
  483. /******************************************
  484.  * Wrappers for the Standard I/O functions.
  485.  ******************************************/
  486. void Fclose(FILE *fp)
  487. {
  488.     if (fclose(fp) != 0)
  489.     unix_error("Fclose error");
  490. }
  491.  
  492. FILE *Fdopen(int fd, const char *type)
  493. {
  494.     FILE *fp;
  495.  
  496.     if ((fp = fdopen(fd, type)) == NULL)
  497.     unix_error("Fdopen error");
  498.  
  499.     return fp;
  500. }
  501.  
  502. char *Fgets(char *ptr, int n, FILE *stream)
  503. {
  504.     char *rptr;
  505.  
  506.     if (((rptr = fgets(ptr, n, stream)) == NULL) && ferror(stream))
  507.     app_error("Fgets error");
  508.  
  509.     return rptr;
  510. }
  511.  
  512. FILE *Fopen(const char *filename, const char *mode)
  513. {
  514.     FILE *fp;
  515.  
  516.     if ((fp = fopen(filename, mode)) == NULL)
  517.     unix_error("Fopen error");
  518.  
  519.     return fp;
  520. }
  521.  
  522. void Fputs(const char *ptr, FILE *stream)
  523. {
  524.     if (fputs(ptr, stream) == EOF)
  525.     unix_error("Fputs error");
  526. }
  527.  
  528. size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  529. {
  530.     size_t n;
  531.  
  532.     if (((n = fread(ptr, size, nmemb, stream)) < nmemb) && ferror(stream))
  533.     unix_error("Fread error");
  534.     return n;
  535. }
  536.  
  537. void Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  538. {
  539.     if (fwrite(ptr, size, nmemb, stream) < nmemb)
  540.     unix_error("Fwrite error");
  541. }
  542.  
  543.  
  544. /****************************
  545.  * Sockets interface wrappers
  546.  ****************************/
  547.  
  548. int Socket(int domain, int type, int protocol)
  549. {
  550.     int rc;
  551.  
  552.     if ((rc = socket(domain, type, protocol)) < 0)
  553.     unix_error("Socket error");
  554.     return rc;
  555. }
  556.  
  557. void Setsockopt(int s, int level, int optname, const void *optval, int optlen)
  558. {
  559.     int rc;
  560.  
  561.     if ((rc = setsockopt(s, level, optname, optval, optlen)) < 0)
  562.     unix_error("Setsockopt error");
  563. }
  564.  
  565. void Bind(int sockfd, struct sockaddr *my_addr, int addrlen)
  566. {
  567.     int rc;
  568.  
  569.     if ((rc = bind(sockfd, my_addr, addrlen)) < 0)
  570.     unix_error("Bind error");
  571. }
  572.  
  573. void Listen(int s, int backlog)
  574. {
  575.     int rc;
  576.  
  577.     if ((rc = listen(s,  backlog)) < 0)
  578.     unix_error("Listen error");
  579. }
  580.  
  581. int Accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  582. {
  583.     int rc;
  584.  
  585.     if ((rc = accept(s, addr, addrlen)) < 0)
  586.     unix_error("Accept error");
  587.     return rc;
  588. }
  589.  
  590. void Connect(int sockfd, struct sockaddr *serv_addr, int addrlen)
  591. {
  592.     int rc;
  593.  
  594.     if ((rc = connect(sockfd, serv_addr, addrlen)) < 0)
  595.     unix_error("Connect error");
  596. }
  597.  
  598. /*******************************
  599.  * Protocol-independent wrappers
  600.  *******************************/
  601. /* $begin getaddrinfo */
  602. void Getaddrinfo(const char *node, const char *service,
  603.                  const struct addrinfo *hints, struct addrinfo **res)
  604. {
  605.     int rc;
  606.  
  607.     if ((rc = getaddrinfo(node, service, hints, res)) != 0)
  608.         gai_error(rc, "Getaddrinfo error");
  609. }
  610. /* $end getaddrinfo */
  611.  
  612. void Getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host,
  613.                  size_t hostlen, char *serv, size_t servlen, int flags)
  614. {
  615.     int rc;
  616.  
  617.     if ((rc = getnameinfo(sa, salen, host, hostlen, serv,
  618.                           servlen, flags)) != 0)
  619.         gai_error(rc, "Getnameinfo error");
  620. }
  621.  
  622. void Freeaddrinfo(struct addrinfo *res)
  623. {
  624.     freeaddrinfo(res);
  625. }
  626.  
  627. void Inet_ntop(int af, const void *src, char *dst, socklen_t size)
  628. {
  629.     if (!inet_ntop(af, src, dst, size))
  630.         unix_error("Inet_ntop error");
  631. }
  632.  
  633. void Inet_pton(int af, const char *src, void *dst)
  634. {
  635.     int rc;
  636.  
  637.     rc = inet_pton(af, src, dst);
  638.     if (rc == 0)
  639.     app_error("inet_pton error: invalid dotted-decimal address");
  640.     else if (rc < 0)
  641.         unix_error("Inet_pton error");
  642. }
  643.  
  644. /*******************************************
  645.  * DNS interface wrappers.
  646.  *
  647.  * NOTE: These are obsolete because they are not thread safe. Use
  648.  * getaddrinfo and getnameinfo instead
  649.  ***********************************/
  650.  
  651. /* $begin gethostbyname */
  652. struct hostent *Gethostbyname(const char *name)
  653. {
  654.     struct hostent *p;
  655.  
  656.     if ((p = gethostbyname(name)) == NULL)
  657.     dns_error("Gethostbyname error");
  658.     return p;
  659. }
  660. /* $end gethostbyname */
  661.  
  662. struct hostent *Gethostbyaddr(const char *addr, int len, int type)
  663. {
  664.     struct hostent *p;
  665.  
  666.     if ((p = gethostbyaddr(addr, len, type)) == NULL)
  667.     dns_error("Gethostbyaddr error");
  668.     return p;
  669. }
  670.  
  671. /************************************************
  672.  * Wrappers for Pthreads thread control functions
  673.  ************************************************/
  674.  
  675. void Pthread_create(pthread_t *tidp, pthread_attr_t *attrp,
  676.             void * (*routine)(void *), void *argp)
  677. {
  678.     int rc;
  679.  
  680.     if ((rc = pthread_create(tidp, attrp, routine, argp)) != 0)
  681.     posix_error(rc, "Pthread_create error");
  682. }
  683.  
  684. void Pthread_cancel(pthread_t tid) {
  685.     int rc;
  686.  
  687.     if ((rc = pthread_cancel(tid)) != 0)
  688.     posix_error(rc, "Pthread_cancel error");
  689. }
  690.  
  691. void Pthread_join(pthread_t tid, void **thread_return) {
  692.     int rc;
  693.  
  694.     if ((rc = pthread_join(tid, thread_return)) != 0)
  695.     posix_error(rc, "Pthread_join error");
  696. }
  697.  
  698. /* $begin detach */
  699. void Pthread_detach(pthread_t tid) {
  700.     int rc;
  701.  
  702.     if ((rc = pthread_detach(tid)) != 0)
  703.     posix_error(rc, "Pthread_detach error");
  704. }
  705. /* $end detach */
  706.  
  707. void Pthread_exit(void *retval) {
  708.     pthread_exit(retval);
  709. }
  710.  
  711. pthread_t Pthread_self(void) {
  712.     return pthread_self();
  713. }
  714.  
  715. void Pthread_once(pthread_once_t *once_control, void (*init_function)()) {
  716.     pthread_once(once_control, init_function);
  717. }
  718.  
  719. /*******************************
  720.  * Wrappers for Posix semaphores
  721.  *******************************/
  722.  
  723. void Sem_init(sem_t *sem, int pshared, unsigned int value)
  724. {
  725.     if (sem_init(sem, pshared, value) < 0)
  726.     unix_error("Sem_init error");
  727. }
  728.  
  729. void P(sem_t *sem)
  730. {
  731.     if (sem_wait(sem) < 0)
  732.     unix_error("P error");
  733. }
  734.  
  735. void V(sem_t *sem)
  736. {
  737.     if (sem_post(sem) < 0)
  738.     unix_error("V error");
  739. }
  740.  
  741. /****************************************
  742.  * The Rio package - Robust I/O functions
  743.  ****************************************/
  744.  
  745. /*
  746.  * rio_readn - Robustly read n bytes (unbuffered)
  747.  */
  748. /* $begin rio_readn */
  749. ssize_t rio_readn(int fd, void *usrbuf, size_t n)
  750. {
  751.     size_t nleft = n;
  752.     ssize_t nread;
  753.     char *bufp = usrbuf;
  754.  
  755.     while (nleft > 0) {
  756.     if ((nread = read(fd, bufp, nleft)) < 0) {
  757.         if (errno == EINTR) /* Interrupted by sig handler return */
  758.         nread = 0;      /* and call read() again */
  759.         else
  760.         return -1;      /* errno set by read() */
  761.     }
  762.     else if (nread == 0)
  763.         break;              /* EOF */
  764.     nleft -= nread;
  765.     bufp += nread;
  766.     }
  767.     return (n - nleft);         /* Return >= 0 */
  768. }
  769. /* $end rio_readn */
  770.  
  771. /*
  772.  * rio_writen - Robustly write n bytes (unbuffered)
  773.  */
  774. /* $begin rio_writen */
  775. ssize_t rio_writen(int fd, void *usrbuf, size_t n)
  776. {
  777.     size_t nleft = n;
  778.     ssize_t nwritten;
  779.     char *bufp = usrbuf;
  780.  
  781.     while (nleft > 0) {
  782.     if ((nwritten = write(fd, bufp, nleft)) <= 0) {
  783.         if (errno == EINTR)  /* Interrupted by sig handler return */
  784.         nwritten = 0;    /* and call write() again */
  785.         else
  786.         return -1;       /* errno set by write() */
  787.     }
  788.     nleft -= nwritten;
  789.     bufp += nwritten;
  790.     }
  791.     return n;
  792. }
  793. /* $end rio_writen */
  794.  
  795.  
  796. /*
  797.  * rio_read - This is a wrapper for the Unix read() function that
  798.  *    transfers min(n, rio_cnt) bytes from an internal buffer to a user
  799.  *    buffer, where n is the number of bytes requested by the user and
  800.  *    rio_cnt is the number of unread bytes in the internal buffer. On
  801.  *    entry, rio_read() refills the internal buffer via a call to
  802.  *    read() if the internal buffer is empty.
  803.  */
  804. /* $begin rio_read */
  805. static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n)
  806. {
  807.     int cnt;
  808.  
  809.     while (rp->rio_cnt <= 0) {  /* Refill if buf is empty */
  810.     rp->rio_cnt = read(rp->rio_fd, rp->rio_buf,
  811.                sizeof(rp->rio_buf));
  812.     if (rp->rio_cnt < 0) {
  813.         if (errno != EINTR) /* Interrupted by sig handler return */
  814.         return -1;
  815.     }
  816.     else if (rp->rio_cnt == 0)  /* EOF */
  817.         return 0;
  818.     else
  819.         rp->rio_bufptr = rp->rio_buf; /* Reset buffer ptr */
  820.     }
  821.  
  822.     /* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */
  823.     cnt = n;          
  824.     if (rp->rio_cnt < n)  
  825.     cnt = rp->rio_cnt;
  826.     memcpy(usrbuf, rp->rio_bufptr, cnt);
  827.     rp->rio_bufptr += cnt;
  828.     rp->rio_cnt -= cnt;
  829.     return cnt;
  830. }
  831. /* $end rio_read */
  832.  
  833. /*
  834.  * rio_readinitb - Associate a descriptor with a read buffer and reset buffer
  835.  */
  836. /* $begin rio_readinitb */
  837. void rio_readinitb(rio_t *rp, int fd)
  838. {
  839.     rp->rio_fd = fd;  
  840.     rp->rio_cnt = 0;  
  841.     rp->rio_bufptr = rp->rio_buf;
  842. }
  843. /* $end rio_readinitb */
  844.  
  845. /*
  846.  * rio_readnb - Robustly read n bytes (buffered)
  847.  */
  848. /* $begin rio_readnb */
  849. ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n)
  850. {
  851.     size_t nleft = n;
  852.     ssize_t nread;
  853.     char *bufp = usrbuf;
  854.    
  855.     while (nleft > 0) {
  856.     if ((nread = rio_read(rp, bufp, nleft)) < 0)
  857.             return -1;          /* errno set by read() */
  858.     else if (nread == 0)
  859.         break;              /* EOF */
  860.     nleft -= nread;
  861.     bufp += nread;
  862.     }
  863.     return (n - nleft);         /* return >= 0 */
  864. }
  865. /* $end rio_readnb */
  866.  
  867. /*
  868.  * rio_readlineb - Robustly read a text line (buffered)
  869.  */
  870. /* $begin rio_readlineb */
  871. ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen)
  872. {
  873.     int n, rc;
  874.     char c, *bufp = usrbuf;
  875.  
  876.     for (n = 1; n < maxlen; n++) {
  877.         if ((rc = rio_read(rp, &c, 1)) == 1) {
  878.         *bufp++ = c;
  879.         if (c == '\n') {
  880.                 n++;
  881.             break;
  882.             }
  883.     } else if (rc == 0) {
  884.         if (n == 1)
  885.         return 0; /* EOF, no data read */
  886.         else
  887.         break;    /* EOF, some data was read */
  888.     } else
  889.         return -1;    /* Error */
  890.     }
  891.     *bufp = 0;
  892.     return n-1;
  893. }
  894. /* $end rio_readlineb */
  895.  
  896. /**********************************
  897.  * Wrappers for robust I/O routines
  898.  **********************************/
  899. ssize_t Rio_readn(int fd, void *ptr, size_t nbytes)
  900. {
  901.     ssize_t n;
  902.  
  903.     if ((n = rio_readn(fd, ptr, nbytes)) < 0)
  904.     unix_error("Rio_readn error");
  905.     return n;
  906. }
  907.  
  908. void Rio_writen(int fd, void *usrbuf, size_t n)
  909. {
  910.     if (rio_writen(fd, usrbuf, n) != n)
  911.     unix_error("Rio_writen error");
  912. }
  913.  
  914. void Rio_readinitb(rio_t *rp, int fd)
  915. {
  916.     rio_readinitb(rp, fd);
  917. }
  918.  
  919. ssize_t Rio_readnb(rio_t *rp, void *usrbuf, size_t n)
  920. {
  921.     ssize_t rc;
  922.  
  923.     if ((rc = rio_readnb(rp, usrbuf, n)) < 0)
  924.     unix_error("Rio_readnb error");
  925.     return rc;
  926. }
  927.  
  928. ssize_t Rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen)
  929. {
  930.     ssize_t rc;
  931.  
  932.     if ((rc = rio_readlineb(rp, usrbuf, maxlen)) < 0)
  933.     unix_error("Rio_readlineb error");
  934.     return rc;
  935. }
  936.  
  937. /********************************
  938.  * Client/server helper functions
  939.  ********************************/
  940. /*
  941.  * open_clientfd - Open connection to server at <hostname, port> and
  942.  *     return a socket descriptor ready for reading and writing. This
  943.  *     function is reentrant and protocol-independent.
  944.  *
  945.  *     On error, returns:
  946.  *       -2 for getaddrinfo error
  947.  *       -1 with errno set for other errors.
  948.  */
  949. /* $begin open_clientfd */
  950. int open_clientfd(char *hostname, char *port) {
  951.     int clientfd, rc;
  952.     struct addrinfo hints, *listp, *p;
  953.  
  954.     /* Get a list of potential server addresses */
  955.     memset(&hints, 0, sizeof(struct addrinfo));
  956.     hints.ai_socktype = SOCK_STREAM;  /* Open a connection */
  957.     hints.ai_flags = AI_NUMERICSERV;  /* ... using a numeric port arg. */
  958.     hints.ai_flags |= AI_ADDRCONFIG;  /* Recommended for connections */
  959.     if ((rc = getaddrinfo(hostname, port, &hints, &listp)) != 0) {
  960.         fprintf(stderr, "getaddrinfo failed (%s:%s): %s\n", hostname, port, gai_strerror(rc));
  961.         return -2;
  962.     }
  963.  
  964.     /* Walk the list for one that we can successfully connect to */
  965.     for (p = listp; p; p = p->ai_next) {
  966.         /* Create a socket descriptor */
  967.         if ((clientfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
  968.             continue; /* Socket failed, try the next */
  969.  
  970.         /* Connect to the server */
  971.         if (connect(clientfd, p->ai_addr, p->ai_addrlen) != -1)
  972.             break; /* Success */
  973.         if (close(clientfd) < 0) { /* Connect failed, try another */  //line:netp:openclientfd:closefd
  974.             fprintf(stderr, "open_clientfd: close failed: %s\n", strerror(errno));
  975.             return -1;
  976.         }
  977.     }
  978.  
  979.     /* Clean up */
  980.     freeaddrinfo(listp);
  981.     if (!p) /* All connects failed */
  982.         return -1;
  983.     else    /* The last connect succeeded */
  984.         return clientfd;
  985. }
  986. /* $end open_clientfd */
  987.  
  988. /*  
  989.  * open_listenfd - Open and return a listening socket on port. This
  990.  *     function is reentrant and protocol-independent.
  991.  *
  992.  *     On error, returns:
  993.  *       -2 for getaddrinfo error
  994.  *       -1 with errno set for other errors.
  995.  */
  996. /* $begin open_listenfd */
  997. int open_listenfd(char *port)
  998. {
  999.     struct addrinfo hints, *listp, *p;
  1000.     int listenfd, rc, optval=1;
  1001.  
  1002.     /* Get a list of potential server addresses */
  1003.     memset(&hints, 0, sizeof(struct addrinfo));
  1004.     hints.ai_socktype = SOCK_STREAM;             /* Accept connections */
  1005.     hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* ... on any IP address */
  1006.     hints.ai_flags |= AI_NUMERICSERV;            /* ... using port number */
  1007.     if ((rc = getaddrinfo(NULL, port, &hints, &listp)) != 0) {
  1008.         fprintf(stderr, "getaddrinfo failed (port %s): %s\n", port, gai_strerror(rc));
  1009.         return -2;
  1010.     }
  1011.  
  1012.     /* Walk the list for one that we can bind to */
  1013.     for (p = listp; p; p = p->ai_next) {
  1014.         /* Create a socket descriptor */
  1015.         if ((listenfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
  1016.             continue;  /* Socket failed, try the next */
  1017.  
  1018.         /* Eliminates "Address already in use" error from bind */
  1019.         setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,    //line:netp:csapp:setsockopt
  1020.                    (const void *)&optval , sizeof(int));
  1021.  
  1022.         /* Bind the descriptor to the address */
  1023.         if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0)
  1024.             break; /* Success */
  1025.         if (close(listenfd) < 0) { /* Bind failed, try the next */
  1026.             fprintf(stderr, "open_listenfd close failed: %s\n", strerror(errno));
  1027.             return -1;
  1028.         }
  1029.     }
  1030.  
  1031.  
  1032.     /* Clean up */
  1033.     freeaddrinfo(listp);
  1034.     if (!p) /* No address worked */
  1035.         return -1;
  1036.  
  1037.     /* Make it a listening socket ready to accept connection requests */
  1038.     if (listen(listenfd, LISTENQ) < 0) {
  1039.         close(listenfd);
  1040.     return -1;
  1041.     }
  1042.     return listenfd;
  1043. }
  1044. /* $end open_listenfd */
  1045.  
  1046. /****************************************************
  1047.  * Wrappers for reentrant protocol-independent helpers
  1048.  ****************************************************/
  1049. int Open_clientfd(char *hostname, char *port)
  1050. {
  1051.     int rc;
  1052.  
  1053.     if ((rc = open_clientfd(hostname, port)) < 0)
  1054.     unix_error("Open_clientfd error");
  1055.     return rc;
  1056. }
  1057.  
  1058. int Open_listenfd(char *port)
  1059. {
  1060.     int rc;
  1061.  
  1062.     if ((rc = open_listenfd(port)) < 0)
  1063.     unix_error("Open_listenfd error");
  1064.     return rc;
  1065. }
  1066.  
  1067. /* $end csapp.c */
Add Comment
Please, Sign In to add comment