+static void set_udp_buffer(int nfd, int type, const char *name, int size, bool warnings) {
+ if(!size) {
+ return;
+ }
+
+ if(setsockopt(nfd, SOL_SOCKET, type, (void *)&size, sizeof(size))) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i: %s", name, size, sockstrerror(sockerrno));
+ return;
+ }
+
+ if(!warnings) {
+ return;
+ }
+
+ // The system may cap the requested buffer size.
+ // Read back the value and check if it is now as requested.
+ int actual = -1;
+ socklen_t optlen = sizeof(actual);
+
+ if(getsockopt(nfd, SOL_SOCKET, type, (void *)&actual, &optlen)) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: %s", name, sockstrerror(sockerrno));
+ } else if(optlen != sizeof(actual)) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: unexpected returned optlen %d", name, (int)optlen);
+ } else if(actual < size) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i, the system set it to %i instead", name, size, actual);
+ }
+}
+