forked from cn-uofbasel/ccn-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccnl-pdu.c
200 lines (166 loc) · 4.9 KB
/
ccnl-pdu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* @f ccnl-pdu.c
* @b CCN lite - create and manipulate CCN protocol data units
*
* Copyright (C) 2011-13, Christian Tschudin, University of Basel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* File history:
* 2011-03-13 created (cft): orig name ccnl-parse-ccnb.c
* 2013-04-04 modified (ms): #if defined(CCNL_SIMULATION) || defined(CCNL_OMNET)
*/
#ifndef CCNL_PDU
#define CCNL_PDU
int
mkHeader(unsigned char *buf, unsigned int num, unsigned int tt)
{
unsigned char tmp[100];
int len = 0, i;
*tmp = 0x80 | ((num & 0x0f) << 3) | tt;
len = 1;
num = num >> 4;
while (num > 0) {
tmp[len++] = num & 0x7f;
num = num >> 7;
}
for (i = len-1; i >= 0; i--)
*buf++ = tmp[i];
return len;
}
int
addBlob(unsigned char *out, char *cp, int cnt)
{
int len;
len = mkHeader(out, cnt, CCN_TT_BLOB);
memcpy(out+len, cp, cnt);
len += cnt;
return len;
}
int
mkBlob(unsigned char *out, unsigned int num, unsigned int tt,
char *cp, int cnt)
{
int len;
len = mkHeader(out, num, tt);
len += addBlob(out+len, cp, cnt);
out[len++] = 0;
return len;
}
int
mkStrBlob(unsigned char *out, unsigned int num, unsigned int tt,
char *str)
{
return mkBlob(out, num, tt, str, strlen(str));
}
int
mkBinaryInt(unsigned char *out, unsigned int num, unsigned int tt,
unsigned int val, int bytes)
{
int len = mkHeader(out, num, tt);
if (!bytes) {
for (bytes = sizeof(val) - 1; bytes > 0; bytes--)
if (val >> (8*bytes))
break;
bytes++;
}
len += mkHeader(out+len, bytes, CCN_TT_BLOB);
while (bytes > 0) { // big endian
bytes--;
out[len++] = 0x0ff & (val >> (8*bytes));
}
out[len++] = 0; // end-of-entry
return len;
}
int
unmkBinaryInt(unsigned char **data, int *datalen,
unsigned int *result, unsigned char *width)
{
unsigned char *cp = *data;
int len = *datalen, typ, num;
unsigned int val = 0;
if (dehead(&cp, &len, &num, &typ) != 0 || typ != CCN_TT_BLOB)
return -1;
if (width) {
if (*width < num)
num = *width;
else
*width = num;
}
// big endian (network order):
while (num-- > 0 && len > 0) {
val = (val << 8) | *cp++;
len--;
}
*result = val;
if (len < 1 || *cp != '\0') // no end-of-entry
return -1;
*data = cp+1;
*datalen = len-1;
return 0;
}
// ----------------------------------------------------------------------
// (ms): Brought here the following two. I noticed also that some
// of them are replicated elsewhere in the util/ dir. Should we put them
// in one place only ?
int
mkInterest(char **namecomp, unsigned int *nonce, unsigned char *out)
{
int len = 0, k;
len = mkHeader(out, CCN_DTAG_INTEREST, CCN_TT_DTAG); // interest
len += mkHeader(out+len, CCN_DTAG_NAME, CCN_TT_DTAG); // name
while (*namecomp) {
len += mkHeader(out+len, CCN_DTAG_COMPONENT, CCN_TT_DTAG); // comp
k = strlen(*namecomp);
len += mkHeader(out+len, k, CCN_TT_BLOB);
memcpy(out+len, *namecomp++, k);
len += k;
out[len++] = 0; // end-of-component
}
out[len++] = 0; // end-of-name
if (nonce) {
len += mkHeader(out+len, CCN_DTAG_NONCE, CCN_TT_DTAG);
len += mkHeader(out+len, sizeof(unsigned int), CCN_TT_BLOB);
memcpy(out+len, (void*)nonce, sizeof(unsigned int));
len += sizeof(unsigned int);
}
out[len++] = 0; // end-of-interest
return len;
}
#if defined(CCNL_SIMULATION) || defined(CCNL_OMNET)
static int
mkContent(char **namecomp, char *data, int datalen, unsigned char *out)
{
int len = 0, k;
len = mkHeader(out, CCN_DTAG_CONTENTOBJ, CCN_TT_DTAG); // content
len += mkHeader(out+len, CCN_DTAG_NAME, CCN_TT_DTAG); // name
while (*namecomp) {
len += mkHeader(out+len, CCN_DTAG_COMPONENT, CCN_TT_DTAG); // comp
k = strlen(*namecomp);
len += mkHeader(out+len, k, CCN_TT_BLOB);
memcpy(out+len, *namecomp++, k);
len += k;
out[len++] = 0; // end-of-component
}
out[len++] = 0; // end-of-name
len += mkHeader(out+len, CCN_DTAG_CONTENT, CCN_TT_DTAG); // content obj
len += mkHeader(out+len, datalen, CCN_TT_BLOB);
memcpy(out+len, data, datalen);
len += datalen;
out[len++] = 0; // end-of-content obj
out[len++] = 0; // end-of-content
return len;
}
#endif // CCNL_SIMULATION || CCNL_OMNET
#endif /*CCNL_PDU*/
// eof