- if( p_hints == NULL )
- memset( &hints, 0, sizeof( hints ) );
- else
- memcpy( &hints, p_hints, sizeof( hints ) );
+ memset (&hints, 0, sizeof (hints));
+ if (p_hints != NULL)
+ {
+ const int safe_flags =
+ AI_PASSIVE |
+ AI_CANONNAME |
+ AI_NUMERICHOST |
+#ifdef AI_NUMERICSERV
+ AI_NUMERICSERV |
+#endif
+#ifdef AI_ALL
+ AI_ALL |
+#endif
+#ifdef AI_ADDRCONFIG
+ AI_ADDRCONFIG |
+#endif
+#ifdef AI_V4MAPPED
+ AI_V4MAPPED |
+#endif
+ 0;
+
+ hints.ai_family = p_hints->ai_family;
+ hints.ai_socktype = p_hints->ai_socktype;
+ hints.ai_protocol = p_hints->ai_protocol;
+ /* Unfortunately, some flags chang the layout of struct addrinfo, so
+ * they cannot be copied blindly from p_hints to &hints. Therefore, we
+ * only copy flags that we know for sure are "safe".
+ */
+ hints.ai_flags = p_hints->ai_flags & safe_flags;
+ }
+
+#ifdef AI_NUMERICSERV
+ /* We only ever use port *numbers* */
+ hints.ai_flags |= AI_NUMERICSERV;
+#endif