libcutils/mq.c
static PeerProxy* peerProxyGetOrCreate(Peer* peer, pid_t pid, 
        bool requestConnection) {
    if (pid == peer->pid) {
        errno = EINVAL;
        return NULL;
    }
    
    if (peerIsDead(peer, pid)) {
        errno = EHOSTDOWN;
        return NULL;
    }
    
    PeerProxy* peerProxy = hashmapGet(peer->peerProxies, &pid);
    if (peerProxy != NULL) {
        return peerProxy;
    }

    // If this is the master peer, we already know about all peers.
    if (peer->master) {
        errno = EHOSTDOWN;
        return NULL;
    }

    // Try to create a peer proxy.
    Credentials credentials;
    credentials.pid = pid;

    // Fake gid and uid until we have the real thing. The real creds are
    // filled in by masterProxyExpectConnection(). These fake creds will
    // never be exposed to the user.
    credentials.uid = 0;
    credentials.gid = 0;

    // Make sure we can allocate the connection request packet.
    OutgoingPacket* packet = NULL;
    if (requestConnection) {
        packet = calloc(1, sizeof(OutgoingPacket));
        if (packet == NULL) {
            errno = ENOMEM;
            return NULL;
        }

        packet->header.type = CONNECTION_REQUEST;
        packet->header.credentials = credentials;
        packet->free = &outgoingPacketFree;
    }
    
    peerProxy = peerProxyCreate(peer, credentials);
    if (peerProxy == NULL) {
        free(packet);
        errno = ENOMEM;
        return NULL;
    } else {
        // Send a connection request to the master.
        if (requestConnection) {
            PeerProxy* masterProxy = peer->masterProxy;
            peerProxyEnqueueOutgoingPacket(masterProxy, packet);
        }
        
        return peerProxy;
    }
}
/**
 * Gets a peer proxy for the given ID. Creates a peer proxy if necessary.
 * Sends a connection request to the master if desired.
 *
 * Returns NULL if an error occurs. Sets errno to EHOSTDOWN if the peer died
 * or ENOMEM if memory couldn't be allocated.
 */
Gets a peer proxy for the given ID. Creates a peer proxy if necessary.
Sends a connection request to the master if desired.

Returns NULL if an error occurs. Sets errno to EHOSTDOWN if the peer died
or ENOMEM if memory couldn't be allocated.
Gets a peer proxy for the given ID . Creates a peer proxy if necessary . Sends a connection request to the master if desired . Returns NULL if an error occurs . Sets errno to EHOSTDOWN if the peer died or ENOMEM if memory couldn ' t be allocated .
static PeerProxy* peerProxyGetOrCreate(Peer* peer, pid_t pid, 
        bool requestConnection) {
    if (pid == peer->pid) {
        errno = EINVAL;
        return NULL;
    }
    if (peerIsDead(peer, pid)) {
        errno = EHOSTDOWN;
        return NULL;
    }
    PeerProxy* peerProxy = hashmapGet(peer->peerProxies, &pid);
    if (peerProxy != NULL) {
        return peerProxy;
    }
    if (peer->master) {
        errno = EHOSTDOWN;
        return NULL;
    }
    Credentials credentials;
    credentials.pid = pid;
    credentials.uid = 0;
    credentials.gid = 0;
    OutgoingPacket* packet = NULL;
    if (requestConnection) {
        packet = calloc(1, sizeof(OutgoingPacket));
        if (packet == NULL) {
            errno = ENOMEM;
            return NULL;
        }
        packet->header.type = CONNECTION_REQUEST;
        packet->header.credentials = credentials;
        packet->free = &outgoingPacketFree;
    }
    peerProxy = peerProxyCreate(peer, credentials);
    if (peerProxy == NULL) {
        free(packet);
        errno = ENOMEM;
        return NULL;
    } else {
        if (requestConnection) {
            PeerProxy* masterProxy = peer->masterProxy;
            peerProxyEnqueueOutgoingPacket(masterProxy, packet);
        }
        return peerProxy;
    }
}
static PeerProxy * peerProxyGetOrCreate ( Peer * peer , pid_t pid , bool requestConnection ) { if ( pid == peer -> pid ) { errno = EINVAL ; return NULL ; } if ( peerIsDead ( peer , pid ) ) { errno = EHOSTDOWN ; return NULL ; } PeerProxy * peerProxy = hashmapGet ( peer -> peerProxies , & pid ) ; if ( peerProxy != NULL ) { return peerProxy ; } if ( peer -> master ) { errno = EHOSTDOWN ; return NULL ; } Credentials credentials ; credentials . pid = pid ; credentials . uid = 0 ; credentials . gid = 0 ; OutgoingPacket * packet = NULL ; if ( requestConnection ) { packet = calloc ( 1 , sizeof ( OutgoingPacket ) ) ; if ( packet == NULL ) { errno = ENOMEM ; return NULL ; } packet -> header . type = CONNECTION_REQUEST ; packet -> header . credentials = credentials ; packet -> free = & outgoingPacketFree ; } peerProxy = peerProxyCreate ( peer , credentials ) ; if ( peerProxy == NULL ) { free ( packet ) ; errno = ENOMEM ; return NULL ; } else { if ( requestConnection ) { PeerProxy * masterProxy = peer -> masterProxy ; peerProxyEnqueueOutgoingPacket ( masterProxy , packet ) ; } return peerProxy ; } }
Gets a peer proxy for the given ID.
Gets a peer proxy for the given ID .
// If this is the master peer, we already know about all peers. // Try to create a peer proxy. // Fake gid and uid until we have the real thing. The real creds are // filled in by masterProxyExpectConnection(). These fake creds will // never be exposed to the user. // Make sure we can allocate the connection request packet. // Send a connection request to the master.