Пример #1
0
sock_t sock_accept(sock_t serversock, char *ip, size_t len)
{
#ifdef HAVE_GETNAMEINFO
    struct sockaddr_storage sa;
#else
    struct sockaddr_in sa;
#endif
    sock_t ret;
    socklen_t slen;

    slen = sizeof(sa);
    ret = accept(serversock, (struct sockaddr *)&sa, &slen);

    if (ret != SOCK_ERROR)
    {
        sock_set_cloexec (ret);
        if (ip)
        {
#ifdef HAVE_GETNAMEINFO
            if (getnameinfo ((struct sockaddr *)&sa, slen, ip, len, NULL, 0, NI_NUMERICHOST))
                snprintf (ip, len, "unknown");
#else
            /* inet_ntoa is not reentrant, we should protect this */
            strncpy(ip, inet_ntoa(sa.sin_addr), len);
#endif
        }
        sock_set_nolinger(ret);
        sock_set_keepalive(ret);
    }

    return ret;
}
Пример #2
0
int sock_accept(sock_t serversock, char *ip, int len)
{
#ifdef HAVE_INET_PTON
    struct sockaddr_storage sa;
#else    
    struct sockaddr_in sa;
#endif
    int ret;
    socklen_t slen;

    if (!sock_valid_socket(serversock))
        return SOCK_ERROR;

    slen = sizeof(sa);
    ret = accept(serversock, (struct sockaddr *)&sa, &slen);

    if (ret >= 0 && ip != NULL) {
#ifdef HAVE_INET_PTON
        if(((struct sockaddr_in *)&sa)->sin_family == AF_INET) 
            inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr,
                    ip, len);
        else if(((struct sockaddr_in6 *)&sa)->sin6_family == AF_INET6) 
            inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, 
                    ip, len);
        else {
            strncpy(ip, "ERROR", len-1);
            ip[len-1] = 0;
        }
#else
        /* inet_ntoa is not reentrant, we should protect this */
        strncpy(ip, inet_ntoa(sa.sin_addr), len);
#endif
        sock_set_nolinger(ret);
        sock_set_keepalive(ret);
    }

    return ret;
}