Skip to content

Commit

Permalink
CCNx' implicit digest name component now supported, added also a test…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
tschudin authored and blacksheeep committed Jul 16, 2013
1 parent 6c0d63d commit a84a5e5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -2,8 +2,8 @@

CC=gcc
#MYCFLAGS=-Wall -pedantic -std=c99 -g
MYCFLAGS=-Wall -g -O0
EXTLIBS=
MYCFLAGS= -Wall -g -O0
EXTLIBS= -lcrypto
EXTMAKE=
EXTMAKECLEAN=

Expand Down Expand Up @@ -37,7 +37,7 @@ ccn-lite-relay: ccn-lite-relay.c \
ccnl-ext-debug.c ccnl-ext.h ccnl-platform.c ccnl-core.c \
ccnl-ext-http.c \
ccnl-ext-sched.c ccnl-pdu.c ccnl-ext-encaps.c ccnl-ext-mgmt.c
${CC} -o $@ ${MYCFLAGS} $<
${CC} -o $@ ${MYCFLAGS} $< ${EXTLIBS}

ccn-lite-simu: ccn-lite-simu.c \
Makefile ccnl-includes.h ccnl.h ccnl-core.h \
Expand Down
2 changes: 2 additions & 0 deletions ccn-lite-minimalrelay.c
Expand Up @@ -84,6 +84,8 @@
#include "ccnl.h"
#include "ccnl-core.h"

#define compute_ccnx_digest(b) NULL

// ----------------------------------------------------------------------

int debug_level;
Expand Down
1 change: 1 addition & 0 deletions ccn-lite-relay.c
Expand Up @@ -29,6 +29,7 @@

#define CCNL_UNIX

#define USE_CCNxDIGEST
#define USE_DEBUG
#define USE_DEBUG_MALLOC
#define USE_ENCAPS
Expand Down
40 changes: 20 additions & 20 deletions ccnl-core.c
Expand Up @@ -20,7 +20,7 @@
* 2011-04-09 created
*/

#define CCNL_VERSION "2013-05-06"
#define CCNL_VERSION "2013-06-27"

static struct ccnl_interest_s* ccnl_interest_remove(struct ccnl_relay_s *ccnl,
struct ccnl_interest_s *i);
Expand All @@ -47,28 +47,30 @@ ccnl_buf_new(void *data, int len)
!memcmp(X->data,Y->data,X->datalen))

int
ccnl_prefix_cmp(struct ccnl_prefix_s *name, struct ccnl_prefix_s *p,
int plen, int mode)
ccnl_prefix_cmp(struct ccnl_prefix_s *name, unsigned char *md,
struct ccnl_prefix_s *p, int plen, int mode)
/* returns -1 if no match at all (all modes) or exact match failed
returns 0 if full match (CMP_EXACT and CMP_MATCH)
returns n>0 for matched components+1 (CMP_MATCH, CMP_LONGEST) */
{
int i, rc = -1;
int i, clen, nlen = name->compcnt + (md ? 1 : 0), rc = -1;
unsigned char *comp;

if (mode == CMP_EXACT && name->compcnt != plen)
if (mode == CMP_EXACT && nlen != plen)
goto done;
for (i = 0; i < name->compcnt && i < plen; i++) {
if (name->complen[i] != p->complen[i] ||
memcmp(name->comp[i], p->comp[i], p->complen[i])) {
for (i = 0; i < nlen && i < plen; i++) {
comp = i < name->compcnt ? name->comp[i] : md;
clen = i < name->compcnt ? name->complen[i] : 32; // SHA256_DIGEST_LEN
if (clen != p->complen[i] || memcmp(comp, p->comp[i], p->complen[i])) {
rc = mode == CMP_EXACT ? -1 : i;
goto done;
}
}
rc = (mode == CMP_EXACT) ? 0 : i;
done:
DEBUGMSG(49, "ccnl_prefix_cmp (mode=%d, len=%d, %d/%d), name=%s: %d\n",
mode, plen, name->compcnt, p->compcnt,
ccnl_prefix_to_path(name), rc);
DEBUGMSG(49, "ccnl_prefix_cmp (mode=%d, nlen=%d, plen=%d, %d/%d), name=%s: %d (%p)\n",
mode, nlen, plen, name->compcnt, p->compcnt,
ccnl_prefix_to_path(name), rc, md);
return rc;
}

Expand Down Expand Up @@ -625,7 +627,7 @@ ccnl_interest_propagate(struct ccnl_relay_s *ccnl, struct ccnl_interest_s *i)
// transmit an Interest Message on all listed dest faces in sequence."
// CCNL strategy: we forward on the face(s) with the longest prefix match
for (fwd = ccnl->fib; fwd; fwd = fwd->next) {
int rc = ccnl_prefix_cmp(i->prefix, fwd->prefix,
int rc = ccnl_prefix_cmp(i->prefix, NULL, fwd->prefix,
fwd->prefix->compcnt, CMP_LONGEST);
if (rc > maxmatch) {
maxmatch = rc;
Expand All @@ -637,7 +639,7 @@ ccnl_interest_propagate(struct ccnl_relay_s *ccnl, struct ccnl_interest_s *i)
if (!best)
return;
for (fwd = ccnl->fib; fwd; fwd = fwd->next) {
int rc = ccnl_prefix_cmp(i->prefix, fwd->prefix,
int rc = ccnl_prefix_cmp(i->prefix, NULL, fwd->prefix,
fwd->prefix->compcnt, CMP_LONGEST);
if (rc == maxmatch) {
// suppress forwarding to origin of interest, except wireless
Expand Down Expand Up @@ -675,14 +677,13 @@ ccnl_i_prefixof_c(struct ccnl_prefix_s *prefix, struct ccnl_buf_s *ppkd,
int minsuffix, int maxsuffix, struct ccnl_content_s *c)
{
int cmplen = prefix->compcnt;
unsigned char *md;
DEBUGMSG(99, "ccnl_i_prefixof_c prefix=%s min=%d max=%d\n",
ccnl_prefix_to_path(prefix), minsuffix, maxsuffix);

// CONFORM: we do prefix match, honour min. and maxsuffix,
// and check the PublisherPublicKeyDigest if present

// NON-CONFORM: we do not compare the content's implicit SHA256 component

// NON-CONFORM: "Note that to match a ContentObject must satisfy
// all of the specifications given in the Interest Message."
// >> CCNL does not honour the exclusion filtering
Expand All @@ -691,10 +692,9 @@ ccnl_i_prefixof_c(struct ccnl_prefix_s *prefix, struct ccnl_buf_s *ppkd,
(cmplen + minsuffix) > (c->name->compcnt + 1) ||
(cmplen + maxsuffix) < (c->name->compcnt + 1) )
return 0;
// how to "handle" (=cheat) the implicit checksum field?
if (prefix->compcnt - c->name->compcnt == 1)
cmplen = c->name->compcnt;
return ccnl_prefix_cmp(c->name, prefix, cmplen, CMP_MATCH) == cmplen;

md = prefix->compcnt - c->name->compcnt == 1 ? compute_ccnx_digest(c->pkt) : NULL;
return ccnl_prefix_cmp(c->name, md, prefix, cmplen, CMP_MATCH) == cmplen;
}

struct ccnl_content_s*
Expand Down Expand Up @@ -900,7 +900,7 @@ ccnl_core_RX_i_or_c(struct ccnl_relay_s *relay, struct ccnl_face_s *from,
}
// CONFORM: Step 2: check whether interest is already known
for (i = relay->pit; i; i = i->next) {
if (ccnl_prefix_cmp(i->prefix, p, p->compcnt, CMP_EXACT) == 0 &&
if (!ccnl_prefix_cmp(i->prefix, NULL, p, p->compcnt, CMP_EXACT) &&
i->minsuffix == minsfx && i->maxsuffix == maxsfx &&
((!ppkd && !i->ppkd) || buf_equal(ppkd, i->ppkd)) )
break;
Expand Down
6 changes: 6 additions & 0 deletions ccnl-ext.h
Expand Up @@ -22,6 +22,12 @@

// ----------------------------------------------------------------------

#ifdef USE_CCNxDIGEST
# define compute_ccnx_digest(buf) SHA256(buf->data, buf->datalen, NULL)
#else
# define compute_ccnx_digest(b) NULL
#endif

#ifdef USE_ENCAPS

struct ccnl_encaps_s* ccnl_encaps_new(int protocol, int mtu);
Expand Down
3 changes: 3 additions & 0 deletions ccnl-includes.h
Expand Up @@ -54,6 +54,9 @@
# include <linux/if_packet.h> // sockaddr_ll
#endif

#ifdef USE_CCNxDIGEST
#endif
# include <openssl/sha.h>
#else // else we are compiling for the Linux kernel

#include <stddef.h>
Expand Down
89 changes: 68 additions & 21 deletions util/ccn-lite-peek.c
Expand Up @@ -70,6 +70,34 @@ mkHeader(unsigned char *buf, unsigned int num, unsigned int tt)
return len;
}

int
hex2int(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
c = tolower(c);
if (c >= 'a' && c <= 'f')
return c - 'a' + 0x0a;
return 0;
}

int
unescape_component(unsigned char *comp) // inplace, returns len after shrinking
{
unsigned char *in = comp, *out = comp;
int len;

for (len = 0; *in; len++) {
if (in[0] != '%' || !in[1] || !in[2]) {
*out++ = *in++;
continue;
}
*out++ = hex2int(in[1])*16 + hex2int(in[2]);
in += 3;
}
return len;
}

int
mkInterest(char **namecomp, unsigned int *nonce, unsigned char *out)
{
Expand All @@ -80,7 +108,7 @@ mkInterest(char **namecomp, unsigned int *nonce, unsigned char *out)

while (*namecomp) {
len += mkHeader(out+len, CCN_DTAG_COMPONENT, CCN_TT_DTAG); // comp
k = strlen(*namecomp);
k = unescape_component((unsigned char*) *namecomp);
len += mkHeader(out+len, k, CCN_TT_BLOB);
memcpy(out+len, *namecomp++, k);
len += k;
Expand Down Expand Up @@ -195,16 +223,38 @@ block_on_read(int sock, float wait)
{
fd_set readfs;
struct timeval timeout;
int rc;

FD_ZERO(&readfs);
FD_SET(sock, &readfs);
timeout.tv_sec = wait;
timeout.tv_usec = 1000000.0 * (wait - timeout.tv_sec);
if (select(sock+1, &readfs, NULL, NULL, &timeout) < 0) {
rc = select(sock+1, &readfs, NULL, NULL, &timeout);
if (rc < 0)
perror("select()");
return -1;
return rc;
}

void
request_content(int sock, int (*sendproc)(int,char*,unsigned char*,int),
char *dest, unsigned char *out, int len, float wait)
{
unsigned char buf[8*1024];
int len2 = sendproc(sock, dest, out, len), rc;

if (len2 < 0) {
perror("sendto");
myexit(1);
}

rc = block_on_read(sock, wait);
if (rc == 1) {
len2 = recv(sock, buf, sizeof(buf), 0);
if (len2 > 0) {
write(1, buf, len2);
myexit(0);
}
}
return 0;
}

// ----------------------------------------------------------------------
Expand All @@ -214,8 +264,10 @@ main(int argc, char *argv[])
{
unsigned char out[8*1024];
int i = 0, len, opt, sock = 0;
char *prefix[32], *cp, *udp = "127.0.0.1/9695", *ux = NULL;
char *prefix[CCNL_MAX_NAME_COMP], *cp, *dest;
char *udp = "127.0.0.1/9695", *ux = NULL;
float wait = 3.0;
int (*sendproc)(int,char*,unsigned char*,int);

while ((opt = getopt(argc, argv, "hu:w:x:")) != -1) {
switch (opt) {
Expand Down Expand Up @@ -244,35 +296,30 @@ main(int argc, char *argv[])
if (!argv[optind])
goto Usage;
cp = strtok(argv[optind], "/");
while (i < 31 && cp) {
while (i < (CCNL_MAX_NAME_COMP - 1) && cp) {
prefix[i++] = cp;
cp = strtok(NULL, "/");
}
prefix[i] = NULL;
len = mkInterest(prefix, NULL, out);

if (ux) { // use UNIX socket
dest = ux;
sock = ux_open();
len = ux_sendto(sock, ux, out, len);
sendproc = ux_sendto;
} else { // UDP
dest = udp;
sock = udp_open();
len = udp_sendto(sock, udp, out, len);
}
if (len < 0) {
perror("sendto");
myexit(1);
sendproc = udp_sendto;
}

/*
for(; !block_on_read(sock, wait);) {
*/
len = recv(sock, out, sizeof(out), 0);
if (len > 0)
write(1, out, len);
// }

for (i = 0; i < 3; i++) {
request_content(sock, sendproc, dest, out, len, wait);
// fprintf(stderr, "retry\n");
}
close(sock);
myexit(0);

myexit(-1);
return 0; // avoid a compiler warning
}

Expand Down

0 comments on commit a84a5e5

Please sign in to comment.