+/*
+ * GSocket_SetTimeout()
+ */
+
+#ifndef LINUX
+# define CAN_USE_TIMEOUT
+#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__)
+# if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 1)
+# define CAN_USE_TIMEOUT
+# endif
+#endif
+
+void GSocket_SetTimeout(GSocket *socket, unsigned long millisec)
+{
+ assert(socket != NULL);
+
+ /* Neither GLIBC 2.0 nor the kernel 2.0.36 define SO_SNDTIMEO or
+ SO_RCVTIMEO. The man pages, that these flags should exist but
+ are read only. RR. */
+ /* OK, restrict this to GLIBC 2.1. GL. */
+#ifdef CAN_USE_TIMEOUT
+ socket->m_timeout = millisec;
+ if (socket->m_fd != -1) {
+ struct timeval tval;
+
+ tval.tv_sec = millisec / 1000;
+ tval.tv_usec = (millisec % 1000) * 1000;
+ setsockopt(socket->m_fd, SOL_SOCKET, SO_SNDTIMEO, &tval, sizeof(tval));
+ setsockopt(socket->m_fd, SOL_SOCKET, SO_RCVTIMEO, &tval, sizeof(tval));
+ }
+#endif
+}
+