#include <stdio.h>
#include "SOSAccountPriv.h"
#include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
#include <Security/SecureObjectSync/SOSTransportMessage.h>
#include <Security/SecureObjectSync/SOSPeerInfoV2.h>
bool SOSAccountIsMyPeerActive(SOSAccountRef account, CFErrorRef* error) {
SOSPeerInfoRef me = SOSFullPeerInfoGetPeerInfo(account->my_identity);
return me ? SOSCircleHasActivePeer(account->trusted_circle, me, error) : false;
}
SOSTransportMessageRef SOSAccountGetMessageTransportFor(SOSAccountRef account, SOSPeerInfoRef peerInfo) {
return (SOSPeerInfoHasDeviceID(peerInfo) && (false)) ? account->ids_message_transport : account->kvs_message_transport;
}
static void sosArrayAppendPeerCopy(CFMutableArrayRef appendPeersTo, SOSPeerInfoRef peer) {
SOSPeerInfoRef peerInfo = SOSPeerInfoCreateCopy(kCFAllocatorDefault, peer, NULL);
CFArrayAppendValue(appendPeersTo, peerInfo);
CFRelease(peerInfo);
}
static CFArrayRef SOSAccountCopySortedPeerArray(SOSAccountRef account,
CFErrorRef *error,
void (^action)(SOSCircleRef circle, CFMutableArrayRef appendPeersTo)) {
if (!SOSAccountHasPublicKey(account, error))
return NULL;
CFMutableArrayRef peers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
action(account->trusted_circle, peers);
CFArrayOfSOSPeerInfosSortByID(peers);
return peers;
}
CFArrayRef SOSAccountCopyNotValidPeers(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
if(!SOSPeerInfoApplicationVerify(peer, account->user_public, NULL)) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
}
});
});
}
CFArrayRef SOSAccountCopyValidPeers(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
if(SOSPeerInfoApplicationVerify(peer, account->user_public, NULL)) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
}
});
});
}
void SOSAccountForEachCirclePeerExceptMe(SOSAccountRef account, void (^action)(SOSPeerInfoRef peer)) {
SOSPeerInfoRef myPi = SOSAccountGetMyPeerInfo(account);
if (account->trusted_circle && myPi) {
CFStringRef myPi_id = SOSPeerInfoGetPeerID(myPi);
SOSCircleForEachPeer(account->trusted_circle, ^(SOSPeerInfoRef peer) {
CFStringRef peerID = SOSPeerInfoGetPeerID(peer);
if (peerID && !CFEqual(peerID, myPi_id)) {
action(peer);
}
});
}
}
CFArrayRef SOSAccountCopyPeersToListenTo(SOSAccountRef account, CFErrorRef *error) {
SOSPeerInfoRef myPeerInfo = SOSFullPeerInfoGetPeerInfo(account->my_identity);
CFStringRef myID = myPeerInfo ? SOSPeerInfoGetPeerID(myPeerInfo) : NULL;
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
if(!CFEqualSafe(myID, SOSPeerInfoGetPeerID(peer)) &&
SOSPeerInfoApplicationVerify(peer, account->user_public, NULL) &&
!SOSPeerInfoIsRetirementTicket(peer)) {
CFArrayAppendValue(appendPeersTo, peer);
}
});
});
}
CFArrayRef SOSAccountCopyRetired(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachRetiredPeer(circle, ^(SOSPeerInfoRef peer) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
});
});
}
CFArrayRef SOSAccountCopyApplicants(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachApplicant(circle, ^(SOSPeerInfoRef peer) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
});
});
}
CFArrayRef SOSAccountCopyPeers(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
});
});
}
CFArrayRef SOSAccountCopyActivePeers(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachActivePeer(circle, ^(SOSPeerInfoRef peer) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
});
});
}
CFArrayRef SOSAccountCopyActiveValidPeers(SOSAccountRef account, CFErrorRef *error) {
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleForEachActiveValidPeer(circle, account->user_public, ^(SOSPeerInfoRef peer) {
sosArrayAppendPeerCopy(appendPeersTo, peer);
});
});
}
CFArrayRef SOSAccountCopyConcurringPeers(SOSAccountRef account, CFErrorRef *error)
{
return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
SOSCircleAppendConcurringPeers(circle, appendPeersTo, NULL);
});
}
SOSPeerInfoRef SOSAccountCopyPeerWithID(SOSAccountRef account, CFStringRef peerid, CFErrorRef *error) {
if(!account->trusted_circle) return NULL;
return SOSCircleCopyPeerWithID(account->trusted_circle, peerid, error);
}