| 77 | } |
| 78 | |
| 79 | int |
| 80 | socket_set_options(auto_file& fd, const socket_args& args, std::string& err_r) |
| 81 | { |
| 82 | if (args.timeout != 0 && !args.nonblocking) { |
| 83 | struct timeval tv = { }; |
| 84 | tv.tv_sec = args.timeout; |
| 85 | tv.tv_usec = 0; |
| 86 | if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) { |
| 87 | return errno_string("setsockopt SO_RCVTIMEO", errno, err_r); |
| 88 | } |
| 89 | tv.tv_sec = args.timeout; |
| 90 | tv.tv_usec = 0; |
| 91 | if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) { |
| 92 | return errno_string("setsockopt SO_RCVTIMEO", errno, err_r); |
| 93 | } |
| 94 | } |
| 95 | if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { |
| 96 | return errno_string("fcntl O_NONBLOCK", errno, err_r); |
| 97 | } |
| 98 | if (args.sndbuf != 0) { |
| 99 | const int v = args.sndbuf; |
| 100 | if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) != 0) { |
| 101 | return errno_string("setsockopt SO_SNDBUF", errno, err_r); |
| 102 | } |
| 103 | } |
| 104 | if (args.rcvbuf != 0) { |
| 105 | const int v = args.rcvbuf; |
| 106 | if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) != 0) { |
| 107 | return errno_string("setsockopt SO_RCVBUF", errno, err_r); |
| 108 | } |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | socket_open(auto_file& fd, const socket_args& args, std::string& err_r) |
no test coverage detected