+ wxGethostBuf()
+ {
+ memset(this, 0, sizeof(hostent_data));
+ }
+ };
+#else
+ typedef char wxGethostBuf[1024];
+#endif
+
+#ifdef HAVE_FUNC_GETSERVBYNAME_R_3
+ struct wxGetservBuf : servent_data
+ {
+ wxGethostBuf()
+ {
+ memset(this, 0, sizeof(servent_data));
+ }
+ };
+#else
+ typedef char wxGetservBuf[1024];
+#endif
+
+#ifdef wxHAS_MT_SAFE_GETBY_FUNCS
+ #define wxLOCK_GETBY_MUTEX(name)
+#else // may need mutexes to protect getxxxbyxxx() calls
+ #if defined(HAVE_GETHOSTBYNAME) || \
+ defined(HAVE_GETHOSTBYADDR) || \
+ defined(HAVE_GETSERVBYNAME)
+ #include "wx/thread.h"
+
+ namespace
+ {
+ // these mutexes are used to serialize
+ wxMutex nameLock, // gethostbyname()
+ addrLock, // gethostbyaddr()
+ servLock; // getservbyname()
+ }
+
+ #define wxLOCK_GETBY_MUTEX(name) wxMutexLocker locker(name ## Lock)
+ #endif // we don't have _r functions
+#endif // wxUSE_THREADS
+
+namespace
+{
+
+#if defined(HAVE_GETHOSTBYNAME)
+hostent *deepCopyHostent(hostent *h,
+ const hostent *he,
+ char *buffer,
+ int size,
+ int *err)
+{
+ /* copy old structure */
+ memcpy(h, he, sizeof(hostent));
+
+ /* copy name */
+ int len = strlen(h->h_name);
+ if (len > size)
+ {
+ *err = ENOMEM;
+ return NULL;
+ }
+ memcpy(buffer, h->h_name, len);
+ buffer[len] = '\0';
+ h->h_name = buffer;
+
+ /* track position in the buffer */
+ int pos = len + 1;
+
+ /* reuse len to store address length */
+ len = h->h_length;
+
+ /* ensure pointer alignment */
+ unsigned int misalign = sizeof(char *) - pos%sizeof(char *);
+ if(misalign < sizeof(char *))
+ pos += misalign;
+
+ /* leave space for pointer list */
+ char **p = h->h_addr_list, **q;
+ char **h_addr_list = (char **)(buffer + pos);
+ while(*(p++) != 0)
+ pos += sizeof(char *);
+
+ /* copy addresses and fill new pointer list */
+ for (p = h->h_addr_list, q = h_addr_list; *p != 0; p++, q++)
+ {
+ if (size < pos + len)
+ {
+ *err = ENOMEM;
+ return NULL;
+ }
+ memcpy(buffer + pos, *p, len); /* copy content */
+ *q = buffer + pos; /* set copied pointer to copied content */
+ pos += len;
+ }
+ *++q = 0; /* null terminate the pointer list */
+ h->h_addr_list = h_addr_list; /* copy pointer to pointers */
+
+ /* ensure word alignment of pointers */
+ misalign = sizeof(char *) - pos%sizeof(char *);
+ if(misalign < sizeof(char *))
+ pos += misalign;
+
+ /* leave space for pointer list */
+ p = h->h_aliases;
+ char **h_aliases = (char **)(buffer + pos);
+ while(*(p++) != 0)
+ pos += sizeof(char *);
+
+ /* copy aliases and fill new pointer list */
+ for (p = h->h_aliases, q = h_aliases; *p != 0; p++, q++)
+ {
+ len = strlen(*p);
+ if (size <= pos + len)
+ {
+ *err = ENOMEM;
+ return NULL;
+ }
+ memcpy(buffer + pos, *p, len); /* copy content */
+ buffer[pos + len] = '\0';
+ *q = buffer + pos; /* set copied pointer to copied content */
+ pos += len + 1;