Ejemplo n.º 1
0
// write Content payload *before* buf[offs], adjust offs and return bytes used
int
ccnl_ccntlv_prependContent(struct ccnl_prefix_s *name,
                           unsigned char *payload, int paylen,
                           unsigned int *lastchunknum, int *contentpos,
                           int *offset, unsigned char *buf)
{
    int tloffset = *offset;

    if (contentpos)
        *contentpos = *offset - paylen;

    // fill in backwards
    if (ccnl_ccntlv_prependBlob(CCNX_TLV_M_Payload, payload, paylen,
                                                        offset, buf) < 0)
        return -1;

    if (ccnl_ccntlv_prependName(name, offset, buf, lastchunknum))
        return -1;

    if (ccnl_ccntlv_prependTL(CCNX_TLV_TL_Object,
                                        tloffset - *offset, offset, buf) < 0)
        return -1;

    return tloffset - *offset;
}
Ejemplo n.º 2
0
// write Content payload *before* buf[offs], adjust offs and return bytes used
int8_t
ccnl_ccntlv_prependContent(struct ccnl_prefix_s *name,
                           uint8_t *payload, size_t paylen,
                           uint32_t *lastchunknum, size_t *contentpos,
                           size_t *offset, uint8_t *buf, size_t *reslen)
{
    size_t tloffset = *offset;

    if (contentpos) {
        *contentpos = *offset - paylen;
    }

    // fill in backwards
    if (ccnl_ccntlv_prependBlob(CCNX_TLV_M_Payload, payload, paylen, offset, buf)) {
        return -1;
    }

    if (ccnl_ccntlv_prependName(name, offset, buf, lastchunknum)) {
        return -1;
    }

    if (ccnl_ccntlv_prependTL(CCNX_TLV_TL_Object, tloffset - *offset, offset, buf)) {
        return -1;
    }

    *reslen = tloffset - *offset;
    return 0;
}
Ejemplo n.º 3
0
// write len bytes *before* position buf[offset], adjust offset
int
ccnl_ccntlv_prependBlob(unsigned short type, unsigned char *blob,
                        unsigned short len, int *offset, unsigned char *buf)
{
    if (*offset < (len + 4))
        return -1;
    *offset -= len;
    memcpy(buf + *offset, blob, len);

    if (ccnl_ccntlv_prependTL(type, len, offset, buf) < 0)
        return -1;
    return len + 4;
}
Ejemplo n.º 4
0
// write Interest payload *before* buf[offs], adjust offs and return bytes used
int
ccnl_ccntlv_prependInterest(struct ccnl_prefix_s *name,
                            int *offset, unsigned char *buf)
{
    int oldoffset = *offset;

    if (ccnl_ccntlv_prependName(name, offset, buf, NULL))
        return -1;
    if (ccnl_ccntlv_prependTL(CCNX_TLV_TL_Interest,
                                        oldoffset - *offset, offset, buf) < 0)
        return -1;

    return oldoffset - *offset;
}
Ejemplo n.º 5
0
// write len bytes *before* position buf[offset], adjust offset
int8_t
ccnl_ccntlv_prependBlob(uint16_t type, uint8_t *blob,
                        size_t len, size_t *offset, uint8_t *buf)
{
    if (*offset < (len + 4)) {
        return -1;
    }
    *offset -= len;
    memcpy(buf + *offset, blob, len);

    if (ccnl_ccntlv_prependTL(type, len, offset, buf)) {
        return -1;
    }
    return 0;
}
Ejemplo n.º 6
0
// write Interest payload *before* buf[offs], adjust offs and return bytes used
int8_t
ccnl_ccntlv_prependInterest(struct ccnl_prefix_s *name,
                            size_t *offset, uint8_t *buf, size_t *len)
{
    size_t oldoffset = *offset;

    if (ccnl_ccntlv_prependName(name, offset, buf, NULL)) {
        return -1;
    }
    if (ccnl_ccntlv_prependTL(CCNX_TLV_TL_Interest,
                                        oldoffset - *offset, offset, buf)) {
        return -1;
    }

    *len = oldoffset - *offset;
    return 0;
}
// write given prefix and chunknum *before* buf[offs], adjust offset
// and return bytes used
int
ccnl_ccntlv_prependName(struct ccnl_prefix_s *name,
                        int *offset, unsigned char *buf,
                        unsigned int *lastchunknum) {

    int nameend, cnt;

    if (lastchunknum) {
        if (ccnl_ccntlv_prependNetworkVarUInt(CCNX_TLV_M_ENDChunk,
                                              *lastchunknum, offset, buf) < 0)
            return -1;
    }

    nameend = *offset;

    if (name->chunknum &&
        ccnl_ccntlv_prependNetworkVarUInt(CCNX_TLV_N_Chunk,
                                          *name->chunknum, offset, buf) < 0)
            return -1;

    // optional: (not used)
    // CCNX_TLV_N_Meta

#ifdef USE_NFN
    if (name->nfnflags & CCNL_PREFIX_NFN)
        if (ccnl_pkt_prependComponent(name->suite, "NFN", offset, buf) < 0)
            return -1;
    if (name->nfnflags & CCNL_PREFIX_THUNK)
        if (ccnl_pkt_prependComponent(name->suite, "THUNK", offset, buf) < 0)
            return -1;
#endif
    for (cnt = name->compcnt - 1; cnt >= 0; cnt--) {
        if (*offset < name->complen[cnt])
            return -1;
        *offset -= name->complen[cnt];
        memcpy(buf + *offset, name->comp[cnt], name->complen[cnt]);
    }
    if (ccnl_ccntlv_prependTL(CCNX_TLV_M_Name, nameend - *offset,
                              offset, buf) < 0)
        return -1;

    return 0;
}
Ejemplo n.º 8
0
// write (in network order and with the minimal number of bytes)
// the given unsigned integer val *before* position buf[offset], adjust offset
// and return number of bytes prepended. 0 is represented as %x00
int
ccnl_ccntlv_prependNetworkVarUInt(unsigned short type, unsigned int intval,
                                  int *offset, unsigned char *buf)
{
    int offs = *offset;
    int len = 0;

    while (offs > 0) {
        buf[--offs] = intval & 0xff;
        len++;
        intval = intval >> 8;
        if (!intval)
            break;
    }
    *offset = offs;

    if (ccnl_ccntlv_prependTL(type, len, offset, buf) < 0)
        return -1;
    return len + 4;
}
Ejemplo n.º 9
0
// write given prefix and chunknum *before* buf[offs], adjust offset.
// Returns 0 on success, non-zero on failure.
int8_t
ccnl_ccntlv_prependName(struct ccnl_prefix_s *name,
                        size_t *offset, uint8_t *buf,
                        const uint32_t *lastchunknum) {

    size_t cnt;
    size_t nameend;

    if (lastchunknum) {
        if (ccnl_ccntlv_prependNetworkVarUInt(CCNX_TLV_M_ENDChunk,
                                              *lastchunknum, offset, buf)) {
            return -1;
        }
    }

    nameend = *offset;

    if (name->chunknum &&
        ccnl_ccntlv_prependNetworkVarUInt(CCNX_TLV_N_Chunk,
                                          *name->chunknum, offset, buf)) {
        return -1;
    }

    // optional: (not used)
    // CCNX_TLV_N_Meta

    for (cnt = name->compcnt; cnt > 0; cnt--) {
        if (*offset < name->complen[cnt-1]) {
            return -1;
        }
        *offset -= name->complen[cnt-1];
        memcpy(buf + *offset, name->comp[cnt-1], name->complen[cnt-1]);
    }
    if (ccnl_ccntlv_prependTL(CCNX_TLV_M_Name, nameend - *offset,
                              offset, buf)) {
        return -1;
    }

    return 0;
}
Ejemplo n.º 10
0
// write (in network order and with the minimal number of bytes)
// the given unsigned integer val *before* position buf[offset], adjust offset
// and return number of bytes prepended. 0 is represented as %x00
int8_t
ccnl_ccntlv_prependNetworkVarUInt(uint16_t type, uint32_t intval,
                                  size_t *offset, uint8_t *buf)
{
    size_t offs = *offset;
    size_t len = 0;

    while (offs > 0) {
        buf[--offs] = (uint8_t) (intval & 0xffU);
        len++;
        intval = intval >> 8U;
        if (!intval) {
            break;
        }
    }
    *offset = offs;

    if (ccnl_ccntlv_prependTL(type, len, offset, buf)) {
        return -1;
    }
    return 0;
}