akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <netinet/in.h> |
| 4 | #include <netdb.h> |
| 5 | #include <sys/time.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include <ccn/ccn.h> |
| 12 | #include <ccn/uri.h> |
| 13 | #include <ccn/face_mgmt.h> |
| 14 | #include <ccn/reg_mgmt.h> |
| 15 | #include <ccn/charbuf.h> |
| 16 | |
| 17 | #include "nlsr_fib.h" |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 18 | #include "nlsr.h" |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 19 | |
| 20 | |
| 21 | static void |
| 22 | ccn_fib_warn(int lineno, const char *format, ...) |
| 23 | { |
| 24 | struct timeval t; |
| 25 | va_list ap; |
| 26 | va_start(ap, format); |
| 27 | gettimeofday(&t, NULL); |
| 28 | fprintf(stderr, "%d.%06d ccn_fib[%d]:%d: ", (int)t.tv_sec, (unsigned)t.tv_usec, (int)getpid(), lineno); |
| 29 | vfprintf(stderr, format, ap); |
| 30 | va_end(ap); |
| 31 | } |
| 32 | |
| 33 | #define ON_ERROR_CLEANUP(resval) \ |
| 34 | { \ |
| 35 | if ((resval) < 0) { \ |
| 36 | ccn_fib_warn (__LINE__, "OnError cleanup\n"); \ |
| 37 | goto cleanup; \ |
| 38 | } \ |
| 39 | } |
| 40 | |
| 41 | #define ON_NULL_CLEANUP(resval) \ |
| 42 | { \ |
| 43 | if ((resval) == NULL) { \ |
| 44 | ccn_fib_warn(__LINE__, "OnNull cleanup\n"); \ |
| 45 | goto cleanup; \ |
| 46 | } \ |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | static int |
| 52 | register_unregister_prefix(struct ccn *h, struct ccn_charbuf *local_scope_template, |
akmhoque | 483c1eb | 2013-03-08 00:51:09 -0600 | [diff] [blame] | 53 | struct ccn_charbuf *no_name, struct ccn_charbuf *name_prefix, |
| 54 | const unsigned char *ccndid, size_t ccnd_id_size, int faceid, |
| 55 | int operation, long int lifetime) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 56 | { |
| 57 | struct ccn_charbuf *temp = NULL; |
| 58 | struct ccn_charbuf *resultbuf = NULL; |
| 59 | struct ccn_charbuf *signed_info = NULL; |
| 60 | struct ccn_charbuf *name = NULL; |
| 61 | struct ccn_charbuf *prefixreg = NULL; |
| 62 | struct ccn_parsed_ContentObject pcobuf = {0}; |
| 63 | struct ccn_forwarding_entry forwarding_entry_storage = {0}; |
| 64 | struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage; |
| 65 | struct ccn_forwarding_entry *new_forwarding_entry; |
| 66 | const unsigned char *ptr = NULL; |
| 67 | size_t length = 0; |
| 68 | int res; |
| 69 | |
| 70 | /* Register or unregister the prefix */ |
| 71 | forwarding_entry->action = (operation == OP_REG) ? "prefixreg" : "unreg"; |
| 72 | forwarding_entry->name_prefix = name_prefix; |
| 73 | forwarding_entry->ccnd_id = ccndid; |
| 74 | forwarding_entry->ccnd_id_size =ccnd_id_size; |
| 75 | forwarding_entry->faceid = faceid; |
| 76 | forwarding_entry->flags = -1; |
akmhoque | 483c1eb | 2013-03-08 00:51:09 -0600 | [diff] [blame] | 77 | forwarding_entry->lifetime = lifetime; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 78 | |
| 79 | prefixreg = ccn_charbuf_create(); |
| 80 | ccnb_append_forwarding_entry(prefixreg, forwarding_entry); |
| 81 | temp = ccn_charbuf_create(); |
| 82 | res = ccn_sign_content(h, temp, no_name, NULL, prefixreg->buf, prefixreg->length); |
| 83 | resultbuf = ccn_charbuf_create(); |
| 84 | |
| 85 | /* construct Interest containing prefixreg request */ |
| 86 | name = ccn_charbuf_create(); |
| 87 | ccn_name_init(name); |
| 88 | ccn_name_append_str(name, "ccnx"); |
| 89 | ccn_name_append(name, ccndid, ccnd_id_size); |
| 90 | ccn_name_append_str(name, (operation == OP_REG) ? "prefixreg" : "unreg"); |
| 91 | ccn_name_append(name, temp->buf, temp->length); |
| 92 | |
| 93 | /* send Interest, get Data */ |
| 94 | res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0); |
| 95 | ON_ERROR_CLEANUP(res); |
| 96 | |
| 97 | res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length); |
| 98 | ON_ERROR_CLEANUP(res); |
| 99 | |
| 100 | /* extract new forwarding entry from Data */ |
| 101 | new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length); |
| 102 | ON_NULL_CLEANUP(new_forwarding_entry); |
| 103 | |
| 104 | res = new_forwarding_entry->faceid; |
| 105 | |
| 106 | ccn_forwarding_entry_destroy(&new_forwarding_entry); |
| 107 | ccn_charbuf_destroy(&signed_info); |
| 108 | ccn_charbuf_destroy(&temp); |
| 109 | ccn_charbuf_destroy(&resultbuf); |
| 110 | ccn_charbuf_destroy(&name); |
| 111 | ccn_charbuf_destroy(&prefixreg); |
| 112 | |
| 113 | return res; |
| 114 | |
| 115 | cleanup: |
akmhoque | ad6d569 | 2013-03-11 19:43:33 -0500 | [diff] [blame] | 116 | if ( signed_info ) |
| 117 | ccn_charbuf_destroy(&signed_info); |
| 118 | if ( temp ) |
| 119 | ccn_charbuf_destroy(&temp); |
| 120 | if ( resultbuf ) |
| 121 | ccn_charbuf_destroy(&resultbuf); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 122 | ccn_charbuf_destroy(&name); |
akmhoque | ad6d569 | 2013-03-11 19:43:33 -0500 | [diff] [blame] | 123 | if ( prefixreg ) |
| 124 | ccn_charbuf_destroy(&prefixreg); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 125 | |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static int |
| 131 | get_ccndid(struct ccn *h, struct ccn_charbuf *local_scope_template, |
| 132 | unsigned char *ccndid) |
| 133 | { |
| 134 | struct ccn_charbuf *name = NULL; |
| 135 | struct ccn_charbuf *resultbuf = NULL; |
| 136 | struct ccn_parsed_ContentObject pcobuf = {0}; |
| 137 | char ccndid_uri[] = "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY"; |
| 138 | const unsigned char *ccndid_result; |
| 139 | static size_t ccndid_result_size; |
| 140 | int res; |
| 141 | |
| 142 | name = ccn_charbuf_create(); |
| 143 | resultbuf = ccn_charbuf_create(); |
| 144 | |
| 145 | res = ccn_name_from_uri(name, ccndid_uri); |
| 146 | ON_ERROR_CLEANUP(res); |
| 147 | |
| 148 | /* get Data */ |
| 149 | res = ccn_get(h, name, local_scope_template, 4500, resultbuf, &pcobuf, NULL, 0); |
| 150 | ON_ERROR_CLEANUP(res); |
| 151 | |
| 152 | /* extract from Data */ |
| 153 | res = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest, |
| 154 | resultbuf->buf, |
| 155 | pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest], |
| 156 | pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest], |
| 157 | &ccndid_result, &ccndid_result_size); |
| 158 | ON_ERROR_CLEANUP(res); |
| 159 | |
| 160 | memcpy((void *)ccndid, ccndid_result, ccndid_result_size); |
| 161 | |
| 162 | ccn_charbuf_destroy(&name); |
| 163 | ccn_charbuf_destroy(&resultbuf); |
| 164 | |
| 165 | return (ccndid_result_size); |
| 166 | |
| 167 | cleanup: |
| 168 | ccn_charbuf_destroy(&name); |
| 169 | ccn_charbuf_destroy(&resultbuf); |
| 170 | |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | init_data(struct ccn_charbuf *local_scope_template, |
| 176 | struct ccn_charbuf *no_name) |
| 177 | { |
| 178 | ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Interest, CCN_DTAG); |
| 179 | ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Name, CCN_DTAG); |
| 180 | ccn_charbuf_append_closer(local_scope_template); /* </Name> */ |
| 181 | ccnb_tagged_putf(local_scope_template, CCN_DTAG_Scope, "1"); |
| 182 | ccn_charbuf_append_closer(local_scope_template); /* </Interest> */ |
| 183 | |
| 184 | ccn_name_init(no_name); |
| 185 | } |
| 186 | |
| 187 | int |
akmhoque | 483c1eb | 2013-03-08 00:51:09 -0600 | [diff] [blame] | 188 | add_delete_ccn_face_by_face_id(struct ccn *h, const char *uri, int operation, int faceid, long int lifetime) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 189 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 190 | if ( nlsr->debugging ) |
| 191 | { |
| 192 | printf("add_delete_ccn_face_by_face_id called\n"); |
| 193 | printf("Uri: %s Face: %d Operation: %s \n",(char *)uri , faceid, operation == OP_REG ? "Registration" : "Unregistration"); |
| 194 | } |
| 195 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 196 | struct ccn_charbuf *prefix; |
| 197 | struct ccn_charbuf *local_scope_template = ccn_charbuf_create(); |
| 198 | struct ccn_charbuf *no_name = ccn_charbuf_create(); |
| 199 | unsigned char ccndid_storage[32] = {0}; |
| 200 | unsigned char *ccndid = ccndid_storage; |
| 201 | size_t ccndid_size = 0; |
| 202 | int res; |
| 203 | |
| 204 | prefix = ccn_charbuf_create(); |
| 205 | res = ccn_name_from_uri(prefix, uri); |
| 206 | ON_ERROR_CLEANUP(res); |
| 207 | |
| 208 | |
| 209 | init_data(local_scope_template, no_name); |
| 210 | |
| 211 | ccndid_size = get_ccndid(h, local_scope_template, ccndid); |
| 212 | if (ccndid_size != sizeof(ccndid_storage)) |
| 213 | { |
| 214 | fprintf(stderr, "Incorrect size for ccnd id in response\n"); |
| 215 | ON_ERROR_CLEANUP(-1); |
| 216 | } |
| 217 | |
akmhoque | 483c1eb | 2013-03-08 00:51:09 -0600 | [diff] [blame] | 218 | res = register_unregister_prefix(h, local_scope_template, no_name, prefix,ccndid, ccndid_size,faceid, operation,lifetime); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 219 | |
| 220 | ON_ERROR_CLEANUP(res); |
| 221 | |
| 222 | ccn_charbuf_destroy(&local_scope_template); |
| 223 | ccn_charbuf_destroy(&no_name); |
| 224 | ccn_charbuf_destroy(&prefix); |
| 225 | |
| 226 | return 0; |
| 227 | |
| 228 | cleanup: |
| 229 | ccn_charbuf_destroy(&prefix); |
| 230 | ccn_charbuf_destroy(&local_scope_template); |
| 231 | ccn_charbuf_destroy(&no_name); |
| 232 | |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | |