+ struct hostent *he = NULL;
+ *err = 0;
+#if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
+ if (gethostbyname_r(hostname, h, (char*)buffer, size, &he, err))
+ he = NULL;
+#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
+ he = gethostbyname_r(hostname, h, (char*)buffer, size, err);
+#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
+ if (gethostbyname_r(hostname, h, (struct hostent_data*) buffer))
+ {
+ he = NULL;
+ *err = h_errno;
+ }
+ else
+ he = h;
+#elif defined(HAVE_GETHOSTBYNAME)
+#if wxUSE_THREADS
+ static wxMutex nameLock;
+ wxMutexLocker locker(nameLock);
+#endif
+ he = gethostbyname(hostname);
+ if (!he)
+ *err = h_errno;
+ else
+ he = deepCopyHostent(h, he, (char*)buffer, size, err);
+#endif
+ return he;
+}
+
+struct hostent * wxGethostbyaddr_r(const char *addr_buf, int buf_size,
+ int proto, struct hostent *h,
+ void *buffer, int size, int *err)
+{
+ struct hostent *he = NULL;
+ *err = 0;
+#if defined(HAVE_FUNC_GETHOSTBYNAME_R_6)
+ if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
+ (char*)buffer, size, &he, err))
+ he = NULL;
+#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
+ he = gethostbyaddr_r(addr_buf, buf_size, proto, h, (char*)buffer, size, err);
+#elif defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
+ if (gethostbyaddr_r(addr_buf, buf_size, proto, h,
+ (struct hostent_data*) buffer))
+ {
+ he = NULL;
+ *err = h_errno;
+ }
+ else
+ he = h;
+#elif defined(HAVE_GETHOSTBYNAME)
+#if wxUSE_THREADS
+ static wxMutex addrLock;
+ wxMutexLocker locker(addrLock);
+#endif
+ he = gethostbyaddr(addr_buf, buf_size, proto);
+ if (!he)
+ *err = h_errno;
+ else
+ he = deepCopyHostent(h, he, (char*)buffer, size, err);
+#endif
+ return he;
+}
+
+#if defined(HAVE_GETSERVBYNAME)
+static struct servent * deepCopyServent(struct servent *s,
+ const struct servent *se,
+ char *buffer, int size)
+{
+ memcpy(s, se, sizeof(struct servent));
+ int len = strlen(s->s_name);
+ if (len > size)
+ len = size - 1;
+ memcpy(buffer, s->s_name, len);
+ buffer[len] = '\0';
+ s->s_name = buffer;
+ buffer += len + 1;
+ size -= len + 1;
+ len = strlen(s->s_proto);
+ if (len > size)
+ len = size - 1;
+ memcpy(buffer, s->s_proto, len);
+ buffer[len] = '\0';
+ s->s_proto = buffer;
+ buffer += len + 1;
+ size -= len + 1;
+ for (char **q = s->s_aliases; size > 0 && *q != 0; q++){
+ len = strlen(*q);
+ if (len > size)
+ len = size - 1;
+ memcpy(buffer, *q, len);
+ buffer[len] = '\0';
+ *q = buffer;
+ buffer += len + 1;
+ size -= len + 1;
+ }
+ return s;