/*
 * Function: copy_interface
 * Purpose:
 *   Get a reference to an SCNetworkInterfaceRef for the specified
 *   interface name.   First try to get an interface configured in the
 *   current set.  If that fails, copy a service configured over the
 *   specified interface, and add it to the current set.
 *   
 *   Return the interface, service, and set for the caller to release.
 */
STATIC SCNetworkInterfaceRef
copy_interface(SCPreferencesRef prefs, CFStringRef if_name,
	       SCNetworkSetRef * ret_set_p,
	       SCNetworkServiceRef * ret_service_p)
{
    SCNetworkSetRef		current_set = NULL;
    SCNetworkServiceRef		service = NULL;
    SCNetworkInterfaceRef	net_if;
    SCNetworkInterfaceRef	ret = NULL;

    /* if the interface is part of a service/set, we're done */
    net_if = copy_configured_interface(prefs, if_name);
    if (net_if != NULL) {
	ret = net_if;
	CFRetain(ret);
	goto done;
    }

    /* interface isn't part of a service/set, make it so */
    net_if = copy_present_interface(if_name);
    if (net_if == NULL) {
	goto done;
    }

    /* find the service in any set */
    service = copy_service(prefs, net_if);
    if (service == NULL) {
	EAPLOG(LOG_ERR, 
	       "EAPOLClientConfiguration: can't get service");
	goto done;
    }
    /* add the service to the current set */
    current_set = SCNetworkSetCopyCurrent(prefs);
    if (current_set == NULL) {
	EAPLOG(LOG_ERR,
	       "EAPOLClientConfiguration: can't get current set");
	goto done;
    }
    if (SCNetworkSetAddService(current_set, service) == FALSE) {
	EAPLOG(LOG_ERR,
	       "EAPOLClientConfiguration: failed to add dummy service");
	goto done;
    }
    /* return this SCNetworkInterfaceRef since it's bound to the prefs */
    ret = SCNetworkServiceGetInterface(service);
    CFRetain(ret);

 done:
    my_CFRelease(&net_if);
    if (ret == NULL) {
	my_CFRelease(&service);
	my_CFRelease(&current_set);
    }
    *ret_service_p = service;
    *ret_set_p = current_set;
    return (ret);
}
Beispiel #2
0
LIBCOUCHBASE_API
int
lcbvb_genconfig_ex(lcbvb_CONFIG *vb,
    const char *name, const char *uuid,
    const lcbvb_SERVER *servers,
    unsigned nservers, unsigned nreplica, unsigned nvbuckets)
{
    unsigned ii, jj;
    int srvix = 0, in_nondata = 0;

    assert(nservers);

    if (!name) {
        name = "default";
    }

    memset(vb, 0, sizeof(*vb));
    vb->dtype = LCBVB_DIST_VBUCKET;
    vb->nvb = nvbuckets;
    vb->nrepl = nreplica;
    vb->nsrv = nservers;
    vb->bname = strdup(name);
    if (uuid) {
        vb->buuid = strdup(uuid);
    }

    if (nreplica >= nservers) {
        vb->errstr = "nservers must be > nreplicas";
        return -1;
    }

    if (nreplica > 4) {
        vb->errstr = "Replicas must be <= 4";
        return -1;
    }

    /* Count the number of data servers.. */
    for (ii = 0; ii < nservers; ii++) {
        const lcbvb_SERVER *server = servers + ii;
        if (server->svc.data) {
            if (in_nondata) {
                vb->errstr = "All data servers must be specified before non-data servers";
                return -1;
            }
            vb->ndatasrv++;
        } else {
            in_nondata = 1;
        }
    }

    if (!vb->ndatasrv) {
        vb->errstr = "No data servers in list";
        return -1;
    }

    vb->vbuckets = malloc(vb->nvb * sizeof(*vb->vbuckets));
    if (!vb->vbuckets) {
        vb->errstr = "Couldn't allocate vbucket array";
        return -1;
    }

    for (ii = 0; ii < vb->nvb; ii++) {
        lcbvb_VBUCKET *cur = vb->vbuckets + ii;
        cur->servers[0] = srvix;
        for (jj = 1; jj < vb->nrepl+1; jj++) {
            cur->servers[jj] = (srvix + jj) % vb->ndatasrv;
        }
        srvix = (srvix + 1) % vb->ndatasrv;
    }

    vb->servers = calloc(vb->nsrv, sizeof(*vb->servers));
    vb->randbuf = calloc(vb->nsrv, sizeof(*vb->randbuf));

    for (ii = 0; ii < vb->nsrv; ii++) {
        lcbvb_SERVER *dst = vb->servers + ii;
        const lcbvb_SERVER *src = servers + ii;

        *dst = *src;
        dst->hostname = strdup(src->hostname);
        if (src->viewpath) {
            dst->viewpath = strdup(src->viewpath);
        }
        if (src->querypath) {
            dst->querypath = strdup(src->querypath);
        }

        copy_service(src->hostname, &src->svc, &dst->svc);
        copy_service(src->hostname, &src->svc_ssl, &dst->svc_ssl);
        dst->authority = dst->svc.hoststrs[LCBVB_SVCTYPE_DATA];
    }

    for (ii = 0; ii < vb->nvb; ii++) {
        for (jj = 0; jj < vb->nrepl+1; jj++) {
            int ix = vb->vbuckets[ii].servers[jj];
            if (ix >= 0) {
                vb->servers[ix].nvbs++;
            }
        }
    }
    return 0;
}