예제 #1
0
파일: ccnl-core.c 프로젝트: AnonMall/RIOT
int consume(int typ, int num, unsigned char **buf, int *len,
            unsigned char **valptr, int *vallen)
{
    if (typ == CCN_TT_BLOB || typ == CCN_TT_UDATA) {
        if (valptr) {
            *valptr = *buf;
        }

        if (vallen) {
            *vallen = num;
        }

        *buf += num, *len -= num;
        return 0;
    }

    if (typ == CCN_TT_DTAG || typ == CCN_TT_DATTR) {
        return hunt_for_end(buf, len, valptr, vallen);
    }

    //  case CCN_TT_TAG, CCN_TT_ATTR:
    //  case DTAG, DATTR:
    return -1;
}
예제 #2
0
파일: ccnl-core.c 프로젝트: AnonMall/RIOT
struct ccnl_buf_s *
ccnl_extract_prefix_nonce_ppkd(unsigned char **data, int *datalen, int *scope,
                               int *aok, int *min, int *max, struct ccnl_prefix_s **prefix,
                               struct ccnl_buf_s **nonce, struct ccnl_buf_s **ppkd,
                               unsigned char **content, int *contlen)
{
    unsigned char *start = *data - 2, *cp;
    int num, typ, len;
    struct ccnl_prefix_s *p;
    struct ccnl_buf_s *buf, *n = 0, *pub = 0;
    DEBUGMSG(99, "ccnl_extract_prefix\n");

    p = (struct ccnl_prefix_s *) ccnl_calloc(1, sizeof(struct ccnl_prefix_s));

    if (!p) {
        puts("can't get more memory from malloc, dropping ccn msg...");
        return NULL;
    }

    p->comp = (unsigned char **) ccnl_malloc(
                  CCNL_MAX_NAME_COMP * sizeof(unsigned char **));
    p->complen = (int *) ccnl_malloc(CCNL_MAX_NAME_COMP * sizeof(int));

    if (!p->comp || !p->complen) {
        puts("can't get more memory from malloc, dropping ccn msg...");
        goto Bail;
    }

    while (dehead(data, datalen, &num, &typ) == 0) {
        if (num == 0 && typ == 0) {
            break;    // end
        }

        if (typ == CCN_TT_DTAG) {
            if (num == CCN_DTAG_NAME) {
                while (1) {
                    if (dehead(data, datalen, &num, &typ) != 0) {
                        goto Bail;
                    }

                    if (num == 0 && typ == 0) {
                        break;
                    }

                    if (typ == CCN_TT_DTAG && num == CCN_DTAG_COMPONENT
                        && p->compcnt < CCNL_MAX_NAME_COMP) {
                        if (hunt_for_end(data, datalen, p->comp + p->compcnt,
                                         p->complen + p->compcnt) < 0) {
                            goto Bail;
                        }

                        p->compcnt++;
                    }
                    else {
                        if (consume(typ, num, data, datalen, 0, 0) < 0) {
                            goto Bail;
                        }
                    }
                }

                continue;
            }

            if (num == CCN_DTAG_SCOPE || num == CCN_DTAG_NONCE
                || num == CCN_DTAG_MINSUFFCOMP
                || num == CCN_DTAG_MAXSUFFCOMP
                || num == CCN_DTAG_PUBPUBKDIGEST) {
                if (hunt_for_end(data, datalen, &cp, &len) < 0) {
                    goto Bail;
                }

                if (num == CCN_DTAG_SCOPE && len == 1 && scope) {
                    *scope = isdigit(*cp) && (*cp < '3') ? *cp - '0' : -1;
                }

                if (num == CCN_DTAG_ANSWERORIGKIND && aok) {
                    *aok = data2uint(cp, len);
                }

                if (num == CCN_DTAG_MINSUFFCOMP && min) {
                    *min = data2uint(cp, len);
                }

                if (num == CCN_DTAG_MAXSUFFCOMP && max) {
                    *max = data2uint(cp, len);
                }

                if (num == CCN_DTAG_NONCE && !n) {
                    n = ccnl_buf_new(cp, len);
                }

                if (num == CCN_DTAG_PUBPUBKDIGEST && !pub) {
                    pub = ccnl_buf_new(cp, len);
                }

                if (num == CCN_DTAG_EXCLUDE) {
                    DEBUGMSG(49, "warning: 'exclude' field ignored\n");
                }
                else {
                    continue;
                }
            }

            if (num == CCN_DTAG_CONTENT || num == CCN_DTAG_CONTENTOBJ) {
                if (consume(typ, num, data, datalen, content, contlen) < 0) {
                    goto Bail;
                }

                continue;
            }
        }

        if (consume(typ, num, data, datalen, 0, 0) < 0) {
            goto Bail;
        }
    }

    if (prefix) {
        p->comp[p->compcnt] = NULL;
        *prefix = p;
    }
    else {
        free_prefix(p);
    }

    if (nonce) {
        *nonce = n;
    }
    else {
        ccnl_free(n);
    }

    if (ppkd) {
        *ppkd = pub;
    }
    else {
        ccnl_free(pub);
    }

    buf = ccnl_buf_new(start, *data - start);

    if (!buf) {
        puts("can't get more memory from malloc, dropping ccn msg...");
        goto Bail;
    }

    // carefully rebase ptrs to new buf because of 64bit pointers:
    if (content) {
        *content = buf->data + (*content - start);
    }

    for (num = 0; num < p->compcnt; num++) {
        p->comp[num] = buf->data + (p->comp[num] - start);
    }

    return buf;
Bail:
    free_prefix(p);
    free_2ptr_list(n, pub);
    return NULL;
}
예제 #3
0
int ccnl_frag_RX_CCNx2013(RX_datagram callback, struct ccnl_relay_s *relay,
                          struct ccnl_face_s *from, unsigned char **data, int *datalen)
{
    int rc, num, typ, pdutypelen;
    unsigned char *pdutype = 0;
    struct serialFragPDU_s s;

    DEBUGMSG(99, "ccnl_frag_RX_CCNx2013 (%d bytes)\n", *datalen);
    serialFragPDU_init(&s);

    while (dehead(data, datalen, &num, &typ) == 0) {
        if (num == 0 && typ == 0) {
            break;    /* end */
        }

        if (typ == CCN_TT_DTAG) {
            switch (num) {
                case CCNL_DTAG_FRAG_TYPE:
                    if (hunt_for_end(data, datalen, &pdutype, &pdutypelen)
                        || pdutypelen != 3) {
                        goto Bail;
                    }

                    continue;

                case CCNL_DTAG_FRAG_SEQNR:
                    getNumField(s.ourseq, s.ourseqwidth, HAS_OSEQ, "ourseq")
                    ;
                    continue;

                case CCNL_DTAG_FRAG_FLAGS:
                    getNumField(s.flags, s.flagwidth, HAS_FLAGS, "flags")
                    ;
                    continue;

                case CCN_DTAG_CONTENT:

                    //		if (frag) /* error: more than one content entry */
                    if (consume(typ, num, data, datalen, &s.content, &s.contlen)
                        < 0) {
                        goto Bail;
                    }

                    continue;

                    /* CCNL extensions: */
                case CCN_DTAG_INTEREST:
                case CCN_DTAG_CONTENTOBJ:
                    rc = ccnl_core_RX_i_or_c(relay, from, data, datalen);

                    if (rc < 0) {
                        return rc;
                    }

                    continue;

                case CCNL_DTAG_FRAG_OLOSS:
                    getNumField(s.ourloss, s.ourlosswidth, HAS_OLOS, "ourloss")
                    ;
                    continue;

                case CCNL_DTAG_FRAG_YSEQN:
                    getNumField(s.yourseq, s.yourseqwidth, HAS_YSEQ, "yourseq")
                    ;
                    continue;

                default:
                    break;
            }
        }

        if (consume(typ, num, data, datalen, 0, 0) < 0) {
            goto Bail;
        }
    }

    if (!pdutype || !s.content) {
        /* ||
         (s.HAS&(HAS_FLAGS|HAS_OSEQ)) != (HAS_FLAGS|HAS_OSEQ) ) {
         */
        DEBUGMSG(1, "* incomplete frag\n");
        return 0;
    }

    DEBUGMSG(1, "hop-by-hop\n");

    if (memcmp(pdutype, CCNL_FRAG_TYPE_CCNx2013_VAL, 3) == 0) { /* hop-by-hop */
        if (from) {
            if (!from->frag)
                from->frag = ccnl_frag_new(CCNL_FRAG_CCNx2013,
                                           relay->ifs[from->ifndx].mtu);

            if (from->frag && from->frag->protocol == CCNL_FRAG_CCNx2013) {
                ccnl_frag_RX_serialfragment(callback, relay, from, &s);
            }
            else {
                DEBUGMSG(1, "WRONG FRAG PROTOCOL\n");
            }
        }
        else {
            ccnl_frag_RX_serialfragment(callback, relay, from, &s);
        }
    }

    /*
     * echo "FMTE" | base64 -d | hexdump -v -e '/1 "@x%02x"'| tr @ '\\'; echo
     */
    if (memcmp(pdutype, "\x14\xc4\xc4", 3) == 0) { /* mid-to-end fragment */
        /* not implemented yet */
    }

    DEBUGMSG(1, "mid-to-end fragment\n");

    return 0;
Bail:
    DEBUGMSG(1, "* frag bailing\n");
    return -1;
}