sinkudp.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
  3. * Permission to use or modify this software and its documentation only for
  4. * educational purposes and without fee is hereby granted, provided that
  5. * the above copyright notice appear in all copies. The author makes no
  6. * representations about the suitability of this software for any purpose.
  7. * It is provided "as is" without express or implied warranty.
  8. */
  9. #include "sock.h"
  10. void
  11. sink_udp(int sockfd) /* TODO: use recvfrom ?? */
  12. {
  13. int n, flags;
  14. if (pauseinit)
  15. sleep_us(pauseinit*1000);
  16. for ( ; ; ) { /* read until peer closes connection; -n opt ignored */
  17. /* msgpeek = 0 or MSG_PEEK */
  18. flags = msgpeek;
  19. oncemore:
  20. if ( (n = recv(sockfd, rbuf, readlen, flags)) < 0) {
  21. err_sys("recv error");
  22. } else if (n == 0) {
  23. if (verbose)
  24. fprintf(stderr, "connection closed by peer\n");
  25. break;
  26. #ifdef notdef /* following not possible with TCP */
  27. } else if (n != readlen)
  28. err_quit("read returned %d, expected %d", n, readlen);
  29. #else
  30. }
  31. #endif
  32. if (verbose) {
  33. fprintf(stderr, "received %d bytes%s\n", n,
  34. (flags == MSG_PEEK) ? " (MSG_PEEK)" : "");
  35. if (verbose > 1) {
  36. fprintf(stderr, "printing %d bytes\n", n);
  37. rbuf[n] = 0; /* make certain it's null terminated */
  38. fprintf(stderr, "SDAP header: %lx\n", *((long *) rbuf));
  39. fprintf(stderr, "next long: %lx\n", *((long *) rbuf+4));
  40. fputs(&rbuf[8], stderr);
  41. }
  42. }
  43. if (pauserw)
  44. sleep_us(pauserw*1000);
  45. if (flags != 0) {
  46. flags = 0; /* avoid infinite loop */
  47. goto oncemore; /* read the message again */
  48. }
  49. }
  50. if (pauseclose) {
  51. if (verbose)
  52. fprintf(stderr, "pausing before close\n");
  53. sleep_us(pauseclose*1000);
  54. }
  55. if (close(sockfd) < 0)
  56. err_sys("close error");
  57. }