Beispiel #1
0
/**
 * 劈开ndnb名字,并把各个部分(n个)放到 components 中.
 * 貌似c会被修改. components 可以直接传null,即无所谓.
 * 参数:
 * c => 输入的 ndnb 名字
 * components => 输出
 * n => 需要留下的组件的数量. 负数代表需要remove的数量
 *
 * Chop the name down to n components.
 * @param c contains a ndnb-encoded Name
 * @param components may be NULL; if provided it must be consistent with
 *        some prefix of the name, and is updated accordingly.
 * @param n is the number or components to leave, or, if negative, specifies
 *        how many components to remove,
          e.g. -1 will remove just the last component.
 * @returns -1 for error, otherwise the new number of Components
 */
int
ndn_name_chop(struct ndn_charbuf *c, struct ndn_indexbuf *components, int n)
{
    if (components == NULL) {
        int res;
        components = ndn_indexbuf_create();
        if (components == NULL)
            return(-1);
        res = ndn_name_split(c, components);
        if (res >= 0)
            res = ndn_name_chop(c, components, n);
        ndn_indexbuf_destroy(&components);
        return(res);
    }
    /* Fix up components if needed. We could be a little smarter about this. */
    if (components->n == 0 || components->buf[components->n-1] + 1 != c->length)
        if (ndn_name_split(c, components) < 0)
            return(-1);
    if (n < 0)
        n += (components->n - 1); /* APL-style indexing */
    if (n < 0)
        return(-1);
    if (n < components->n) {
        c->length = components->buf[n];
        ndn_charbuf_append_value(c, NDN_CLOSE, 1);
        components->n = n + 1;
        return(n);
    }
    return(-1);
}
Beispiel #2
0
/**
 * should probably return a new cob, rather than reusing one.
 * should publish link as:
 *    NDNRID_POLICY_URI("ndn:/%C1.M.S.localhost/%C1.M.SRV/repository/POLICY)/%C1.M.K--pubid--/--version--/%00
 * should have key locator which is the key name of the repository
 */
PUBLIC struct ndn_charbuf *
ndnr_init_policy_link_cob(struct ndnr_handle *ndnr, struct ndn *h,
                          struct ndn_charbuf *targetname)
{
    struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT;
    struct ndn_charbuf *name = ndn_charbuf_create();
    struct ndn_charbuf *pubid = ndn_charbuf_create();
    struct ndn_charbuf *pubkey = ndn_charbuf_create();
    struct ndn_charbuf *keyid = ndn_charbuf_create();
    struct ndn_charbuf *content = ndn_charbuf_create();
    struct ndn_charbuf *cob = ndn_charbuf_create();
    struct ndn_charbuf *answer = NULL;
    int res;
    
    res = ndn_get_public_key(h, NULL, pubid, pubkey);
    if (res < 0)
        goto Bail;
    if (ndn_name_from_uri(name, NDNRID_POLICY_URI) < 0)
        goto Bail;
    res |= ndn_charbuf_append_value(keyid, NDN_MARKER_CONTROL, 1);
    res |= ndn_charbuf_append_string(keyid, ".M.K");
    res |= ndn_charbuf_append_value(keyid, 0, 1);
    res |= ndn_charbuf_append_charbuf(keyid, pubid);
    res |= ndn_name_append(name, keyid->buf, keyid->length);
    res |= ndn_create_version(h, name, NDN_V_NOW, 0, 0);
    if (ndn_name_from_uri(name, "%00") < 0)
        goto Bail;
    sp.sp_flags |= NDN_SP_FINAL_BLOCK;
    sp.type = NDN_CONTENT_LINK;
    res |= ndnb_append_Link(content, targetname, "Repository Policy", NULL);
    if (res != 0)
        goto Bail;
    res |= ndn_sign_content(h, cob, name, &sp, content->buf, content->length);
    if (res != 0)
        goto Bail;
    answer = cob;
    cob = NULL;
    
Bail:
    ndn_charbuf_destroy(&name);
    ndn_charbuf_destroy(&pubid);
    ndn_charbuf_destroy(&pubkey);
    ndn_charbuf_destroy(&keyid);
    ndn_charbuf_destroy(&content);
    ndn_charbuf_destroy(&cob);
    return (answer);
}
Beispiel #3
0
/**
 * Advance the last Component of a Name to the next possible value.
 * @param c contains a ndnb-encoded Name to be updated.
 * @returns -1 for error, otherwise the number of Components
 */
int
ndn_name_next_sibling(struct ndn_charbuf *c)
{
    int res = -1;
    struct ndn_indexbuf *ndx;
    unsigned char *lastcomp = NULL;
    size_t lastcompsize = 0;
    size_t i;
    int carry;
    struct ndn_charbuf *newcomp;

    ndx = ndn_indexbuf_create();
    if (ndx == NULL) goto Finish;
    res = ndn_name_split(c, ndx);
    if (res <= 0) {
        res = -1;
        goto Finish;
    }
    res = ndn_ref_tagged_BLOB(NDN_DTAG_Component, c->buf,
        ndx->buf[res-1], ndx->buf[res],
        (const unsigned char **)&lastcomp,
        &lastcompsize);
    if (res < 0) goto Finish;
    for (carry = 1, i = lastcompsize; carry && i > 0; i--) {
        carry = (((++lastcomp[i-1]) & 0xFF) == 0x00);
    }
    if (carry) {
        newcomp = ndn_charbuf_create();
        res |= ndn_charbuf_append_value(newcomp, 0, 1);
        res |= ndn_charbuf_append(newcomp, lastcomp, lastcompsize);
        res |= ndn_name_chop(c, ndx, ndx->n - 2);
        res |= ndn_name_append(c, newcomp->buf, newcomp->length);
        ndn_charbuf_destroy(&newcomp);
        if (res < 0) goto Finish;
    }
    res = ndx->n - 1;
Finish:
    ndn_indexbuf_destroy(&ndx);
    return(res);
}
Beispiel #4
0
/**
 * Parse the buffered configuration found in config
 *
 * The pass argument controls what is done with the result:
 *   0 - silent check for syntax errors;
 *   1 - check for syntax errors and warnings, logging the results,
 *   2 - incorporate settings into environ.
 *
 * @returns -1 if an error is found, otherwise the count of warnings.
 */
int
r_init_parse_config(struct ndnr_handle *h, struct ndn_charbuf *config, int pass)
{
    struct ndn_charbuf *key = NULL;
    struct ndn_charbuf *value = NULL;
    const unsigned char *b;
    int line;
    size_t i;
    size_t sol; /* start of line */
    size_t len; /* config->len */
    size_t ndx; /* temp for column report*/
    int ch;
    int warns = 0;
    int errors = 0;
    int use_it = 0;
    static const char pclegal[] = 
        "~@%-+=:,./[]"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789"
        "_"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const char *klegal = strchr(pclegal, 'a');
    int flags; /* for reporting */
    
    b = config->buf;
    len = config->length;
    if (len == 0)
        return(0);
    ndn_charbuf_as_string(config);
    key = ndn_charbuf_create();
    value = ndn_charbuf_create();
    if (key == NULL || value == NULL)
        return(-1);
    /* Ensure there is null termination in the buffered config */
    if (ndn_charbuf_as_string(config) == NULL)
        return(-1);
    for (line = 1, i = 0, ch = b[0], sol = 0; i < len;) {
        flags = pass;
        use_it = 0;
        if (ch > ' ' && ch != '#') {
            key->length = value->length = 0;
            /* parse key */
            while (i < len && ch != '\n' && ch != '=') {
                ndn_charbuf_append_value(key, ch, 1);
                ch = b[++i];
            }
            if (ch == '=')
                ch = b[++i];
            else {
                r_init_config_msg(h, flags, line, key->length, "missing '='");
                flags |= NDNR_CONFIG_IGNORELINE;
                warns++;
                ch = '\n';
            }
            /* parse value */
            while (i < len && ch > ' ') {
                ndn_charbuf_append_value(value, ch, 1);
                ch = b[++i];
            }
            /* See if it might be one of ours */
            if (key->length < 5 || (memcmp(key->buf, "NDNR_", 5) != 0 &&
                                    memcmp(key->buf, "NDNS_", 5) != 0)) {
                r_init_config_msg(h, flags, line, 0,
                                  "ignoring unrecognized key");
                flags |= NDNR_CONFIG_IGNORELINE;
                warns++;
                use_it = 0;
            }
            else
                use_it = 1;

            /* Check charset of key */
            ndx = strspn(ndn_charbuf_as_string(key), klegal);
            if (ndx != key->length) {
                errors += use_it;
                r_init_config_msg(h, (flags | NDNR_CONFIG_ERR), line, ndx,
                                  "unexpected character in key");
                flags |= NDNR_CONFIG_IGNORELINE;
                warns++;
            }
            /* Check charset of value */
            ndx = strspn(ndn_charbuf_as_string(value), pclegal);
            if (ndx != value->length) {
                errors += use_it;
                r_init_config_msg(h, (flags | NDNR_CONFIG_ERR),
                                  line, key->length + 1 + ndx,
                                  "unexpected character in value");
                flags |= NDNR_CONFIG_IGNORELINE;
                warns++;
            }
        }
        if (ch == '#') {
            /* a comment line or error recovery. */
            while (i < len && ch != '\n')
                ch = b[++i];
        }
        while (i < len && ch <= ' ') {
            if (ch == '\n') {
                line++;
                sol = i;
                break;
            }
            if (memchr("\r\t ", ch, 3) == NULL) {
                r_init_config_msg(h, pass, line, i - sol,
                                  "non-whitespace control char at end of line");
                warns++;
            } 
            ch = b[++i];
        }
        if (i == len) {
            r_init_config_msg(h, flags, line, i - sol,
                              "missing newline at end of file");
            warns++;
            ch = '\n';
        }
        else if (ch == '\n')
            ch = b[++i];
        else {
            r_init_config_msg(h, flags, line, i - sol, "junk at end of line");
            flags |= NDNR_CONFIG_IGNORELINE;
            warns++;
            ch = '#';
        }
        if (flags == 0 && strcmp(ndn_charbuf_as_string(key), "NDNR_DEBUG") == 0) {
            /* Set this on pass 0 so that it takes effect sooner. */
            h->debug = 1;
            setenv("NDNR_DEBUG", ndn_charbuf_as_string(value), 1);
            h->debug = r_init_debug_getenv(h, "NDNR_DEBUG");
        }
        if (pass == 2 && use_it) {
            if (NDNSHOULDLOG(h, mmm, NDNL_FINEST))
                ndnr_msg(h, "config: %s=%s",
                        ndn_charbuf_as_string(key),
                        ndn_charbuf_as_string(value));
            setenv(ndn_charbuf_as_string(key), ndn_charbuf_as_string(value), 1);
        }
    }
    ndn_charbuf_destroy(&key);
    ndn_charbuf_destroy(&value);
    return(errors ? -1 : warns);
}