8 #ifdef HAVE_SYS_TIMEB_H
9 # include <sys/timeb.h>
14 #ifdef HAVE_ARPA_INET_H
15 # include <arpa/inet.h>
18 #if !defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
19 int gettimeofday(struct timeval * tv, struct timezone * tz)
29 ftime(&tb); /* FIXME: some versions are void return others int */
32 tv->tv_usec = ((long)tb.millitm)*1000;
38 #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
39 struct tm *localtime_r(const time_t *timep, struct tm *result)
41 memcpy(result, localtime(timep), sizeof(*result));
47 void * memcpy(void * dest, void const * src, unsigned long n)
49 /* very slow, your fault for not having memcpy()*/
50 unsigned char * td=dest;
51 unsigned char * ts=src;
64 /* very slow, deal with it */
65 void * memset(void * dest, int c, unsigned long n)
67 unsigned char * temp=dest;
74 temp[i] = (unsigned char)c;
80 char * strdup(char const * str)
86 if (!(out = malloc(strlen(str)+1)))
94 char const * strerror(int errornum)
100 return "Operation not permitted";
103 if (errornum==ENOENT)
104 return "No such file or directory";
108 return "No such process";
112 return "Interrupted system call";
120 return "No such device or address";
124 return "Bad file number";
127 if (errornum==EAGAIN)
131 if (errornum==ENOMEM)
132 return "Out of memory";
135 if (errornum==EACCES)
136 return "Permission denied";
139 if (errornum==EFAULT)
140 return "Bad address";
144 return "Device or resource busy";
147 if (errornum==EEXIST)
148 return "File exists";
152 return "Cross-device link";
156 return "Resource deadlock would occur";
159 if (errornum==EDEADLOCK)
160 return "Resource deadlock would occur";
163 if (errornum==ENODEV)
164 return "No such device";
167 if (errornum==ENOTDIR)
168 return "Not a directory";
171 if (errornum==EISDIR)
172 return "Is a directory";
175 if (errornum==EINVAL)
176 return "Invalid argument";
179 if (errornum==ENFILE)
180 return "Too many open files in system";
183 if (errornum==EMFILE)
184 return "Too many open files";
187 if (errornum==ENOTTY)
188 return "Not a typewriter";
191 if (errornum==ETXTBSY)
192 return "Text file busy";
196 return "File too large";
199 if (errornum==ENOSPC)
200 return "No space left on device";
203 if (errornum==ESPIPE)
204 return "Illegal seek";
208 return "Read-only file system";
211 if (errornum==EMLINK)
212 return "Too many links";
216 return "Broken pipe";
220 return "Math argument out of domain of func";
223 if (errornum==ERANGE)
224 return "Math result not representable";
227 if (errornum==ENAMETOOLONG)
228 return "File name too long";
231 if (errornum==ENOLCK)
232 return "No record locks avaliable";
235 if (errornum==ENOSYS)
236 return "Function not implemented";
239 if (errornum==ENOTEMPTY)
240 return "Directory not empty";
244 return "Too many symbolic links encountered";
247 if (errornum==EHOSTDOWN)
248 return "Host is down";
251 if (errornum==EHOSTUNREACH)
252 return "No route to host";
255 if (errornum==EALREADY)
256 return "Operation already in progress";
259 if (errornum==EINPROGRESS)
260 return "Operation now in progress";
263 if (errornum==ESTALE)
264 return "Stale NFS filehandle";
267 if (errornum==EDQUOT)
268 return "Quota exceeded";
271 if (errornum==EWOULDBLOCK)
272 return "Operation would block";
276 return "Communication error on send";
279 if (errornum==EPROTO)
280 return "Protocol error";
282 #ifdef EPROTONOSUPPORT
283 if (errornum==EPROTONOSUPPORT)
284 return "Protocol not supported";
286 #ifdef ESOCKTNOSUPPORT
287 if (errornum==ESOCKTNOSUPPORT)
288 return "Socket type not supported";
290 #ifdef ESOCKTNOSUPPORT
291 if (errornum==EOPNOTSUPP)
292 return "Operation not supported";
295 if (errornum==EPFNOSUPPORT)
296 return "Protocol family not supported";
299 if (errornum==EAFNOSUPPORT)
300 return "Address family not supported by protocol family";
303 if (errornum==EADDRINUSE)
304 return "Address already in use";
307 if (errornum==EADDRNOTAVAIL)
308 return "Cannot assign requested address";
311 if (errornum==ENETDOWN)
312 return "Network is down";
315 if (errornum==ENETUNREACH)
316 return "Network is unreachable";
319 if (errornum==ENETRESET)
320 return "Network dropped connection on reset";
323 if (errornum==ECONNABORTED)
324 return "Software caused connection abort";
327 if (errornum==ECONNRESET)
328 return " Connection reset by peer";
331 if (errornum==ENOBUFS)
332 return "No buffer space available";
335 if (errornum==EISCONN)
336 return "Socket is already connected";
339 if (errornum==ENOTCONN)
340 return "Socket is not connected";
343 if (errornum==ESHUTDOWN)
344 return " Cannot send after socket shutdown";
347 if (errornum==ETIMEDOUT)
348 return "Connection timed out";
351 if (errornum==ECONNREFUSED)
352 return "Connection refused";
354 return "Unknown error";
358 #ifndef HAVE_GETADDRINFO
360 int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
362 /* Only support IPv4 if OS doesn't provide this function. */
363 struct sockaddr_in sin;
365 if (hints && hints->ai_family != AF_INET)
367 memset(&sin, 0, sizeof(sin));
368 sin.sin_family = AF_INET;
371 if (hints && hints->ai_flags & AI_NUMERICHOST) {
373 if (!inet_aton(node, &sin.sin_addr))
376 sin.sin_addr.s_addr = inet_addr(node);
377 if (sin.sin_addr.s_addr == INADDR_NONE)
382 he = gethostbyname(node);
385 memcpy(&sin.sin_addr, he->h_addr, he->h_length);
387 } else if (hints && hints->ai_flags & AI_PASSIVE) {
388 /* leave it unspecifed */
390 inet_aton("127.0.0.1", &sin.sin_addr);
394 sin.sin_port = ntohs(0);
395 else if (!(sin.sin_port = ntohs(atoi(service))))
398 *res = calloc(1, sizeof(**res) + sizeof(sin));
399 (*res)->ai_family = sin.sin_family;
400 (*res)->ai_socktype = hints && hints->ai_socktype ? hints->ai_socktype : SOCK_STREAM;
401 (*res)->ai_protocol = hints && hints->ai_socktype ? hints->ai_socktype : 0;
402 (*res)->ai_addrlen = sizeof(sin);
403 (*res)->ai_addr = (struct sockaddr*)(*res + 1);
404 memcpy((*res)->ai_addr, &sin, (*res)->ai_addrlen);
405 (*res)->ai_canonname = 0;
410 void freeaddrinfo(struct addrinfo *res)
412 struct addrinfo *next;
413 for (; res; res = next) {
420 #ifndef HAVE_GAI_STRERROR
421 const char *gai_strerror(int errcode)
424 #if defined(EAI_ADDRFAMILY)
425 case EAI_ADDRFAMILY: return "Address family not supported.";
427 #if defined(EAI_AGAIN)
428 case EAI_AGAIN: return "A temporary failure occurred during name resolution.";
430 #if defined(EAI_BADFLAGS)
431 case EAI_BADFLAGS: return "Invalid flags hint.";
433 #if defined(EAI_FAIL)
434 case EAI_FAIL: return "An unrecoverable failure occurred during name resolution.";
436 #if defined(EAI_FAMILY)
437 case EAI_FAMILY: return "Address family not supported.";
439 #if defined(EAI_MEMORY)
440 case EAI_MEMORY: return "Not enough memory.";
442 #if defined(EAI_NODATA)
443 case EAI_NODATA: return "The name resolves to an empty record.";
445 #if defined(EAI_NONAME)
446 case EAI_NONAME: return "The name does not resolve.";
448 #if defined(EAI_OVERFLOW)
449 case EAI_OVERFLOW: return "Resolved name was too large for buffer.";
451 #if defined(EAI_SERVICE)
452 case EAI_SERVICE: return "The socket type does not support the requested service.";
454 #if defined(EAI_SOCKTYPE)
455 case EAI_SOCKTYPE: return "Unknown socket type.";
457 #if defined(EAI_SYSTEM)
458 case EAI_SYSTEM: return "A system error occurred during name resolution.";
461 return "Unknown GAI_* error";
465 #ifndef HAVE_GETNAMEINFO
466 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
467 char *host, size_t hostlen,
468 char *serv, size_t servlen, int flags)
470 if (sa->sa_family == AF_INET) {
471 const struct sockaddr_in *sin;
474 /* This comparison is a bit screwy looking, but socklen_t is
475 * signed on some platforms and unsigned on others, so gcc
476 * will complain if we use "salen < 0".
478 if (salen < 1 || (size_t)salen < sizeof(*sin))
480 sin = (const struct sockaddr_in *)sa;
481 tmp = snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
482 if (tmp < 1 || (size_t)tmp >= servlen)
484 if (0 == (flags & NI_NUMERICHOST)) {
487 /* Try to get host entry by address.
488 * The first argument should be void *, but Cygwin is
489 * apparently wandering around the pre-C89 era.
491 he = gethostbyaddr((const char*)&sin->sin_addr, sa->sa_family, SOCK_STREAM);
493 if (servlen <= strlen(he->h_name))
495 safestrncpy(serv, he->h_name, servlen);
499 /* If we couldn't, why did we fail, and what should we do? */
506 /* Fall through and out to inet_ntop() path. */
511 /* Try to get numeric representation of address. */
512 if (inet_ntop(sa->sa_family, &sin->sin_addr, host, hostlen) != NULL)
514 else if (errno == ENOSPC)