getsockopt.c 1.2 KB

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