/src/tests/kits/net/sock/buffers.c

http://github.com/Barrett17/Haiku-services-branch · C · 60 lines · 38 code · 10 blank · 12 comment · 20 complexity · 780927f5de29ba2f7d60b389873b59ce MD5 · raw file

  1. /* -*- c-basic-offset: 8; -*-
  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. void 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("error: requested rcvbuflen = %d, resulting 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("error: requested sndbuflen = %d, resulting SO_SNDBUF = %d", sndbuflen, n);
  47. if (verbose)
  48. fprintf(stderr, "SO_SNDBUF = %d\n", n);
  49. }
  50. }