buffers.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. buffers(int sockfd)
  12. {
  13. int n;
  14. socklen_t optlen;
  15. /* Allocate the read and write buffers. */
  16. if (rbuf == NULL) {
  17. if ( (rbuf = malloc(readlen)) == NULL)
  18. err_sys("malloc error for read buffer");
  19. }
  20. if (wbuf == NULL) {
  21. if ( (wbuf = malloc(writelen)) == NULL)
  22. err_sys("malloc error for write buffer");
  23. }
  24. /* Set the socket send and receive buffer sizes (if specified).
  25. The receive buffer size is tied to TCP's advertised window. */
  26. if (rcvbuflen) {
  27. if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuflen,
  28. sizeof(rcvbuflen)) < 0)
  29. err_sys("SO_RCVBUF setsockopt error");
  30. optlen = sizeof(n);
  31. if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, &optlen) < 0)
  32. err_sys("SO_RCVBUF getsockopt error");
  33. if (n != rcvbuflen)
  34. err_quit("rcvbuflen = %d, SO_RCVBUF = %d", rcvbuflen, n);
  35. if (verbose)
  36. fprintf(stderr, "SO_RCVBUF = %d\n", n);
  37. }
  38. if (sndbuflen) {
  39. if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuflen,
  40. sizeof(sndbuflen)) < 0)
  41. err_sys("SO_SNDBUF setsockopt error");
  42. optlen = sizeof(n);
  43. if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &n, &optlen) < 0)
  44. err_sys("SO_SNDBUF getsockopt error");
  45. if (n != sndbuflen)
  46. err_quit("sndbuflen = %d, SO_SNDBUF = %d", sndbuflen, n);
  47. if (verbose)
  48. fprintf(stderr, "SO_SNDBUF = %d\n", n);
  49. }
  50. }