X-Git-Url: https://jfr.im/git/irc/evilnet/x3.git/blobdiff_plain/d76ed9a966ee3d955c8ef00ecc02e643c2005e2e..3b7fa78b1de8f9ee8718cba3da3b2db522b70620:/src/compat.c diff --git a/src/compat.c b/src/compat.c index 820771e..61d9f74 100644 --- a/src/compat.c +++ b/src/compat.c @@ -11,6 +11,9 @@ #ifdef HAVE_MEMORY_H # include #endif +#ifdef HAVE_ARPA_INET_H +# include +#endif #if !defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME) extern gettimeofday(struct timeval * tv, struct timezone * tz); @@ -40,10 +43,6 @@ extern gettimeofday(struct timeval * tv, struct timezone * tz); #ifndef HAVE_MEMCPY extern void * memcpy(void * dest, void const * src, unsigned long n) { -#ifdef HAVE_BCOPY - bcopy(src,dest,n); - return dest; -#else /* very slow, your fault for not having memcpy()*/ unsigned char * td=dest; unsigned char * ts=src; @@ -55,7 +54,6 @@ extern void * memcpy(void * dest, void const * src, unsigned long n) for (i=0; iai_family != AF_INET) + return 1; + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + + if (node) { + if (hints && hints->ai_flags & AI_NUMERICHOST) { +#if HAVE_INET_ATON + if (!inet_aton(node, &sin.sin_addr)) + return 2; +#else + sin.sin_addr.s_addr = inet_addr(node); + if (sin.sin_addr.s_addr == INADDR_NONE) + return 2; +#endif + } else { + struct hostent *he; + he = gethostbyname(node); + if (!he) + return 3; + memcpy(&sin.sin_addr, he->h_addr, he->h_length); + } + } else if (hints && hints->ai_flags & AI_PASSIVE) { + /* leave it unspecifed */ + } else { + inet_aton("127.0.0.1", &sin.sin_addr); + } + + if (!service) + sin.sin_port = ntohs(0); + else if (!(sin.sin_port = ntohs(atoi(service)))) + return 4; + + *res = calloc(1, sizeof(**res) + sizeof(sin)); + (*res)->ai_family = sin.sin_family; + (*res)->ai_socktype = hints && hints->ai_socktype ? hints->ai_socktype : SOCK_STREAM; + (*res)->ai_protocol = hints && hints->ai_socktype ? hints->ai_socktype : 0; + (*res)->ai_addrlen = sizeof(sin); + (*res)->ai_addr = (struct sockaddr*)(*res + 1); + memcpy((*res)->ai_addr, &sin, (*res)->ai_addrlen); + (*res)->ai_canonname = 0; + (*res)->ai_next = 0; + return 0; +} + +/* TODO: implement fallback getnameinfo() */ + +void freeaddrinfo(struct addrinfo *res) +{ + struct addrinfo *next; + for (; res; res = next) { + next = res->ai_next; + free(res); + } +} + +#endif +