getsockopt.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Unit test for issues #92 and #644 */
  2. #define _GNU_SOURCE
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <netinet/tcp.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/socket.h>
  9. int main(int argc, char** argv) {
  10. int ret;
  11. socklen_t optlen; /* Option length */
  12. int fd = socket(PF_INET, SOCK_STREAM, 0);
  13. if (fd < 0) {
  14. perror("socket failed");
  15. return 1;
  16. }
  17. int so_type;
  18. optlen = sizeof(so_type);
  19. ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &optlen);
  20. if (ret < 0) {
  21. perror("getsockopt(SOL_SOCKET, SO_TYPE) failed");
  22. return 1;
  23. }
  24. if (optlen != sizeof(so_type) || so_type != SOCK_STREAM) {
  25. fprintf(stderr, "getsockopt(SOL_SOCKET, SO_TYPE) failed\n");
  26. return 1;
  27. }
  28. printf("getsockopt: Got socket type OK\n");
  29. int so_flags = 1;
  30. optlen = sizeof(so_flags);
  31. ret = getsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&so_flags, &optlen);
  32. if (ret < 0) {
  33. perror("getsockopt(SOL_TCP, TCP_NODELAY) failed");
  34. return 1;
  35. }
  36. if (optlen != sizeof(so_flags) || (so_flags != 0 && so_flags != 1)) {
  37. fprintf(stderr, "getsockopt(SOL_TCP, TCP_NODELAY) failed\n");
  38. return 1;
  39. }
  40. printf("getsockopt: Got TCP_NODELAY flag OK\n");
  41. return 0;
  42. }