コード例 #1
0
ファイル: feed.c プロジェクト: PatrickHildreth-NOAA/LDM
/*
 * Returns:
 *   0             Success.
 *   ENOENT        gethostbyname() failure.
 *   EAFNOSUPPORT  AF_INET address-family not supported.
 *   EMFILE        No more file descriptors available for this process.
 *   ENFILE        No more file descriptors available for the system.
 *   EACCES        The process does not have appropriate privileges.
 *   ENOBUFS       Insufficient resources were available in the system to
 *                 perform the operation.
 *   ENOMEM        Insufficient memory was available to fulfill the request.
 *   ENOSR         There were insufficient STREAMS resources available for the 
 *                 operation to complete.
 *   EADDRNOTAVAIL The specified address is not available from the local 
 *                 machine.
 *   ECONNREFUSED  The target address was not listening for connections or 
 *                 refused the connection request.
 *   EINTR         The attempt to establish a connection was interrupted by
 *                 delivery of a signal that was caught; the connection will be
 *                 established asynchronously.
 *   ENETUNREACH   No route to the network is present.
 *   EPROTOTYPE    The specified address has a different type than the socket 
 *                 bound to the specified peer address.
 *   ETIMEDOUT     The attempt to connect timed out before a connection was 
 *                 made.
 *   ECONNRESET    Remote host reset the connection request.
 *   EHOSTUNREACH  The destination host cannot be reached (probably because the
 *                 host is down or a remote router cannot reach it).
 *   ENETDOWN      The local interface used to reach the destination is down.
 */   
int
port_open(const char *remote, unsigned short port, int *const fdp)
{
        int status = ENOERR;
        int sock;
        struct sockaddr_in addr;

        if (addrbyhost(remote, &addr) != 0)
        {
                status = ENOENT;
                serror("gethostbyname(%s)", remote);
                return status;
        }

        sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock < 0)
        {
                status = errno;
                serror("socket");
                return status;
        }
#if 0 /* doesnt seem to help. See VOODOO in pqing.c */
        {
                const int optval = 1;
                if(setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
                                (char *) &optval, (int) sizeof(optval)) < 0)
                {
                        status = errno;
                        serror("setsockopt SO_KEEPALIVE");
                        return status;
                }
        }
#endif

        addr.sin_port = htons(port);
        if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
        {
                status = errno;
                serror("connect");
                (void) close(sock);
                return status;
        }
        
        unotice("NET \"%s\" %hu", remote, port);
        *fdp = sock;
        return status;
}
コード例 #2
0
/*
 * Potentially lengthy operation.
 */
ErrorObj*
ldm_clnt_addr(const char* const name, struct sockaddr_in* addr)
{
    struct sockaddr_in  ad;
    ErrorObj*            error;

    log_assert(NULL != name);
    log_assert(NULL != addr);

    if (addrbyhost(name, &ad)) {
        const char* msg;

        if (HOST_NOT_FOUND == h_errno) {
            msg = "no such host is known";
        }
        else if (NO_DATA == h_errno) {
            msg = "no address for name";
        }
        else if (NO_RECOVERY == h_errno) {
            msg = "unexpected server failure";
        }
        else if (TRY_AGAIN == h_errno) {
            msg = "try again later";
        }
        else {
            msg = "unknown error";
        }

        error = ERR_NEW(h_errno, NULL, msg);
    }
    else {
        *addr = ad;
        error = NULL;
    }

    return error;
}