/UNIX/unp/sock/sourcesink.c

https://gitlab.com/o1s2/selflrrn · C · 92 lines · 63 code · 20 blank · 9 comment · 30 complexity · dcd24619654e8db786d540ec45cac960 MD5 · raw file

  1. /* Authors: W. R. Stevens, B. Fenner, A. M. Rudoff */
  2. /*
  3. * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
  4. * Permission to use or modify this software and its documentation only for
  5. * educational purposes and without fee is hereby granted, provided that
  6. * the above copyright notice appear in all copies. The author makes no
  7. * representations about the suitability of this software for any purpose.
  8. * It is provided "as is" without express or implied warranty.
  9. */
  10. #include "sock.h"
  11. #include <ctype.h>
  12. void pattern(char *ptr, int len);
  13. void
  14. sink(int sockfd)
  15. {
  16. int i, n;
  17. char oob;
  18. if (client) {
  19. pattern(wbuf, writelen); /* fill send buffer with a pattern */
  20. if (pauseinit)
  21. sleep(pauseinit);
  22. for (i = 1; i <= nbuf; i++) {
  23. if (urgwrite == i) {
  24. oob = urgwrite;
  25. if ( (n = send(sockfd, &oob, 1, MSG_OOB)) != 1)
  26. err_sys("send of MSG_OOB returned %d, expected %d",
  27. n, writelen);
  28. if (verbose)
  29. fprintf(stderr, "wrote %d byte of urgent data\n", n);
  30. }
  31. if ( (n = write(sockfd, wbuf, writelen)) != writelen)
  32. err_sys("write returned %d, expected %d", n, writelen);
  33. if (verbose)
  34. fprintf(stderr, "wrote %d bytes\n", n);
  35. if (pauserw)
  36. sleep(pauserw);
  37. }
  38. } else {
  39. if (pauseinit)
  40. sleep(pauseinit);
  41. for ( ; ; ) {
  42. if ( (n = read(sockfd, rbuf, readlen)) < 0) {
  43. err_sys("read error");
  44. } else if (n == 0) {
  45. break; /* connection closed by peer */
  46. } else if (n != readlen)
  47. err_quit("read returned %d, expected %d", n, readlen);
  48. if (verbose)
  49. fprintf(stderr, "received %d bytes\n", n);
  50. if (pauserw)
  51. sleep(pauserw);
  52. }
  53. }
  54. if (pauseclose) {
  55. if (verbose)
  56. fprintf(stderr, "pausing before close\n");
  57. sleep(pauseclose);
  58. }
  59. if (close(sockfd) < 0)
  60. err_sys("close error"); /* since SO_LINGER may be set */
  61. }
  62. void
  63. pattern(char *ptr, int len)
  64. {
  65. char c;
  66. c = 0;
  67. while(len-- > 0) {
  68. while(isprint((c & 0x7F)) == 0)
  69. c++; /* skip over nonprinting characters */
  70. *ptr++ = (c++ & 0x7F);
  71. }
  72. }